From 9b1c3cec911b171a3f8a5353306e087ea5f8500c Mon Sep 17 00:00:00 2001 From: Natalia Portillo Date: Thu, 16 Oct 2025 20:32:46 +0100 Subject: [PATCH] [TUI] Implement GoToPath dialog for navigating to specified directory --- .../Dialogs/GoToPathDialogViewModel.cs | 80 +++++++++++++++++++ .../ViewModels/Windows/MainWindowViewModel.cs | 28 +++++++ Aaru.Tui/Views/Dialogs/GoToPathDialog.axaml | 40 ++++++++++ .../Views/Dialogs/GoToPathDialog.axaml.cs | 11 +++ Aaru.Tui/Views/Windows/MainWindow.axaml | 4 + 5 files changed, 163 insertions(+) create mode 100644 Aaru.Tui/ViewModels/Dialogs/GoToPathDialogViewModel.cs create mode 100644 Aaru.Tui/Views/Dialogs/GoToPathDialog.axaml create mode 100644 Aaru.Tui/Views/Dialogs/GoToPathDialog.axaml.cs diff --git a/Aaru.Tui/ViewModels/Dialogs/GoToPathDialogViewModel.cs b/Aaru.Tui/ViewModels/Dialogs/GoToPathDialogViewModel.cs new file mode 100644 index 000000000..15e54bf4a --- /dev/null +++ b/Aaru.Tui/ViewModels/Dialogs/GoToPathDialogViewModel.cs @@ -0,0 +1,80 @@ +// /*************************************************************************** +// Aaru Data Preservation Suite +// ---------------------------------------------------------------------------- +// +// Author(s) : Natalia Portillo +// +// Component : Text User Interface. +// +// --[ License ] -------------------------------------------------------------- +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as +// published by the Free Software Foundation, either version 3 of the +// License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . +// +// ---------------------------------------------------------------------------- +// Copyright © 2011-2025 Natalia Portillo +// ****************************************************************************/ + +using System.Windows.Input; +using CommunityToolkit.Mvvm.ComponentModel; +using CommunityToolkit.Mvvm.Input; +using Iciclecreek.Avalonia.WindowManager; + +namespace Aaru.Tui.ViewModels.Dialogs; + +public sealed partial class GoToPathDialogViewModel : ViewModelBase +{ + internal ManagedWindow _dialog = null!; + [ObservableProperty] + string _errorMessage = string.Empty; + [ObservableProperty] + bool _hasError; + [ObservableProperty] + string? _path = string.Empty; + + public GoToPathDialogViewModel(ManagedWindow dialog) + { + _dialog = dialog; + OkCommand = new RelayCommand(Ok); + CancelCommand = new RelayCommand(Cancel); + } + + public ICommand OkCommand { get; } + public ICommand CancelCommand { get; } + + void Ok() + { + if(string.IsNullOrWhiteSpace(Path)) + { + HasError = true; + ErrorMessage = "Path cannot be empty"; + + return; + } + + if(!Directory.Exists(Path)) + { + HasError = true; + ErrorMessage = "Path does not exist"; + + return; + } + + _dialog.Close(true); + } + + void Cancel() + { + _dialog.Close(false); + } +} \ No newline at end of file diff --git a/Aaru.Tui/ViewModels/Windows/MainWindowViewModel.cs b/Aaru.Tui/ViewModels/Windows/MainWindowViewModel.cs index 911b24352..5e396e1ce 100644 --- a/Aaru.Tui/ViewModels/Windows/MainWindowViewModel.cs +++ b/Aaru.Tui/ViewModels/Windows/MainWindowViewModel.cs @@ -35,6 +35,8 @@ using Aaru.CommonTypes.Interfaces; using Aaru.Core; using Aaru.Helpers; using Aaru.Tui.Models; +using Aaru.Tui.ViewModels.Dialogs; +using Aaru.Tui.Views.Dialogs; using Aaru.Tui.Views.Windows; using Avalonia; using Avalonia.Controls; @@ -70,6 +72,7 @@ public sealed partial class MainWindowViewModel : ViewModelBase _view = view; ExitCommand = new RelayCommand(Exit); SectorViewCommand = new RelayCommand(SectorView); + GoToPathCommand = new AsyncRelayCommand(GoToPathAsync); OpenSelectedFileCommand = new RelayCommand(OpenSelectedFile, CanOpenSelectedFile); InformationalVersion = @@ -103,6 +106,7 @@ public sealed partial class MainWindowViewModel : ViewModelBase public ICommand OpenSelectedFileCommand { get; } public ICommand ExitCommand { get; } public ICommand SectorViewCommand { get; } + public ICommand GoToPathCommand { get; } public bool IsFileInfoAvailable => SelectedFile?.FileInfo != null; public bool SelectedFileIsNotDirectory => SelectedFile?.IsDirectory == false; public long? SelectedFileLength => SelectedFile?.IsDirectory == false ? SelectedFile?.FileInfo?.Length : 0; @@ -114,6 +118,30 @@ public sealed partial class MainWindowViewModel : ViewModelBase public string? SelectedFileInformation => SelectedFile?.Information; + async Task GoToPathAsync() + { + var dialog = new GoToPathDialog + { + DataContext = new GoToPathDialogViewModel(null!) + }; + + // Set the dialog reference after creation + ((GoToPathDialogViewModel)dialog.DataContext!)._dialog = dialog; + + bool? result = await dialog.ShowDialog(_view); + + if(result == true) + { + var viewModel = (GoToPathDialogViewModel)dialog.DataContext; + + if(viewModel.Path is not null && Directory.Exists(viewModel.Path)) + { + Environment.CurrentDirectory = viewModel.Path; + LoadFiles(); + } + } + } + void SectorView() { if(SelectedFile?.ImageFormat is null) return; diff --git a/Aaru.Tui/Views/Dialogs/GoToPathDialog.axaml b/Aaru.Tui/Views/Dialogs/GoToPathDialog.axaml new file mode 100644 index 000000000..e31137daa --- /dev/null +++ b/Aaru.Tui/Views/Dialogs/GoToPathDialog.axaml @@ -0,0 +1,40 @@ + + + + + + + + + + + + + +