diff --git a/Aaru.Tui/Aaru.Tui.csproj b/Aaru.Tui/Aaru.Tui.csproj index 608fad404..e3eec0511 100644 --- a/Aaru.Tui/Aaru.Tui.csproj +++ b/Aaru.Tui/Aaru.Tui.csproj @@ -19,6 +19,9 @@ HexViewWindow.axaml + + Code + diff --git a/Aaru.Tui/ViewModels/Dialogs/GoToSectorDialogViewModel.cs b/Aaru.Tui/ViewModels/Dialogs/GoToSectorDialogViewModel.cs new file mode 100644 index 000000000..53442cb04 --- /dev/null +++ b/Aaru.Tui/ViewModels/Dialogs/GoToSectorDialogViewModel.cs @@ -0,0 +1,62 @@ +using System.Windows.Input; +using Avalonia.Controls; +using CommunityToolkit.Mvvm.ComponentModel; +using CommunityToolkit.Mvvm.Input; + +namespace Aaru.Tui.ViewModels.Dialogs; + +public sealed partial class GoToSectorDialogViewModel : ViewModelBase +{ + readonly ulong _maxSector; + internal Window _dialog = null!; + + [ObservableProperty] + string _errorMessage = string.Empty; + + [ObservableProperty] + bool _hasError; + + [ObservableProperty] + string _sectorNumber = string.Empty; + + public GoToSectorDialogViewModel(Window dialog, ulong maxSector) + { + _dialog = dialog; + _maxSector = maxSector; + OkCommand = new RelayCommand(Ok); + CancelCommand = new RelayCommand(Cancel); + } + + public ulong? Result { get; private set; } + + public ICommand OkCommand { get; } + public ICommand CancelCommand { get; } + + void Ok() + { + if(!ulong.TryParse((string?)SectorNumber, out ulong sector)) + { + ErrorMessage = "Please enter a valid number."; + HasError = true; + + return; + } + + if(sector > _maxSector) + { + ErrorMessage = $"Sector number must be less than or equal to {_maxSector}."; + HasError = true; + + return; + } + + Result = sector; + _dialog.Close(true); + } + + void Cancel() + { + Result = null; + _dialog.Close(false); + } +} \ No newline at end of file diff --git a/Aaru.Tui/ViewModels/Windows/HexViewWindowViewModel.cs b/Aaru.Tui/ViewModels/Windows/HexViewWindowViewModel.cs index 3f183ca8c..9b5e10866 100644 --- a/Aaru.Tui/ViewModels/Windows/HexViewWindowViewModel.cs +++ b/Aaru.Tui/ViewModels/Windows/HexViewWindowViewModel.cs @@ -1,11 +1,13 @@ using System.Collections.ObjectModel; using System.Windows.Input; using Aaru.CommonTypes.Interfaces; +using Aaru.Tui.ViewModels.Dialogs; using Avalonia; using Avalonia.Controls; using Avalonia.Controls.ApplicationLifetimes; using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; +using GoToSectorDialog = Aaru.Tui.Views.Dialogs.GoToSectorDialog; namespace Aaru.Tui.ViewModels.Windows; @@ -31,6 +33,7 @@ public sealed partial class HexViewWindowViewModel : ViewModelBase BackCommand = new RelayCommand(Back); NextSectorCommand = new RelayCommand(NextSector); PreviousSectorCommand = new RelayCommand(PreviousSector); + GoToCommand = new AsyncRelayCommand(GoToAsync); _parent = parent; _view = view; @@ -44,6 +47,31 @@ public sealed partial class HexViewWindowViewModel : ViewModelBase public ICommand ExitCommand { get; } public ICommand NextSectorCommand { get; } public ICommand PreviousSectorCommand { get; } + public ICommand GoToCommand { get; } + + async Task GoToAsync() + { + var dialog = new GoToSectorDialog + { + DataContext = new GoToSectorDialogViewModel(null!, _imageFormat.Info.Sectors - 1) + }; + + // Set the dialog reference after creation + ((GoToSectorDialogViewModel)dialog.DataContext!)._dialog = dialog; + + bool? result = await dialog.ShowDialog(_view); + + if(result == true) + { + var viewModel = (GoToSectorDialogViewModel)dialog.DataContext; + + if(viewModel.Result.HasValue) + { + CurrentSector = viewModel.Result.Value; + LoadSector(); + } + } + } void PreviousSector() { diff --git a/Aaru.Tui/Views/Dialogs/GoToSectorDialog.axaml b/Aaru.Tui/Views/Dialogs/GoToSectorDialog.axaml new file mode 100644 index 000000000..033928d86 --- /dev/null +++ b/Aaru.Tui/Views/Dialogs/GoToSectorDialog.axaml @@ -0,0 +1,38 @@ + + + + + + + + + + + + + +