From 4fd1338065a0fff72c7b624d5bf3081c35a94e0c Mon Sep 17 00:00:00 2001 From: Natalia Portillo Date: Wed, 15 Oct 2025 22:54:35 +0100 Subject: [PATCH] [TUI] Implement changing path. --- Aaru.Tui/Models/FileModel.cs | 3 +- .../ViewModels/Windows/MainWindowViewModel.cs | 52 ++++++++++++++----- Aaru.Tui/Views/Windows/MainWindow.axaml | 12 +++-- Aaru.Tui/Views/Windows/MainWindow.axaml.cs | 13 +++++ 4 files changed, 60 insertions(+), 20 deletions(-) diff --git a/Aaru.Tui/Models/FileModel.cs b/Aaru.Tui/Models/FileModel.cs index b85c8650c..e7309fbb9 100644 --- a/Aaru.Tui/Models/FileModel.cs +++ b/Aaru.Tui/Models/FileModel.cs @@ -6,5 +6,6 @@ public class FileModel { public string Path { get; set; } public string Filename { get; set; } - public IBrush ForegroundBrush { get; set; } // Add this property for ListBox Foreground binding + public IBrush ForegroundBrush { get; set; } + public bool IsDirectory { get; set; } } \ No newline at end of file diff --git a/Aaru.Tui/ViewModels/Windows/MainWindowViewModel.cs b/Aaru.Tui/ViewModels/Windows/MainWindowViewModel.cs index 93af10287..9fbc88f39 100644 --- a/Aaru.Tui/ViewModels/Windows/MainWindowViewModel.cs +++ b/Aaru.Tui/ViewModels/Windows/MainWindowViewModel.cs @@ -22,10 +22,13 @@ public sealed partial class MainWindowViewModel : ViewModelBase ObservableCollection _files = []; [ObservableProperty] public string _informationalVersion; + [ObservableProperty] + public FileModel _selectedFile; public MainWindowViewModel() { - ExitCommand = new RelayCommand(Exit); + ExitCommand = new RelayCommand(Exit); + OpenSelectedFileCommand = new RelayCommand(OpenSelectedFile, CanOpenSelectedFile); InformationalVersion = Assembly.GetExecutingAssembly() @@ -36,6 +39,8 @@ public sealed partial class MainWindowViewModel : ViewModelBase Copyright = Assembly.GetExecutingAssembly().GetCustomAttribute()?.Copyright ?? ""; } + public ICommand OpenSelectedFileCommand { get; } + public ICommand ExitCommand { get; } void Exit() @@ -45,33 +50,41 @@ public sealed partial class MainWindowViewModel : ViewModelBase } public void LoadComplete() + { + LoadFiles(); + } + + public void LoadFiles() { CurrentPath = Directory.GetCurrentDirectory(); + Files.Clear(); var parentDirectory = new FileModel { Filename = "..", - Path = Path.GetRelativePath(Directory.GetCurrentDirectory(), ".."), + Path = Path.GetRelativePath(CurrentPath, ".."), ForegroundBrush = new SolidColorBrush(Color.Parse(DirColorsParser.Instance.DirectoryColor ?? - DirColorsParser.Instance.NormalColor)) + DirColorsParser.Instance.NormalColor)), + IsDirectory = true }; Files.Add(parentDirectory); - foreach(FileModel model in Directory - .GetDirectories(Directory.GetCurrentDirectory(), "*", SearchOption.TopDirectoryOnly) - .Select(directory => new FileModel - { - Path = directory, - Filename = Path.GetFileName(directory), - ForegroundBrush = - new SolidColorBrush(Color.Parse(DirColorsParser.Instance.DirectoryColor ?? - DirColorsParser.Instance.NormalColor)) - })) + foreach(FileModel model in Directory.GetDirectories(CurrentPath, "*", SearchOption.TopDirectoryOnly) + .Select(directory => new FileModel + { + Path = directory, + Filename = Path.GetFileName(directory), + ForegroundBrush = + new SolidColorBrush(Color.Parse(DirColorsParser.Instance + .DirectoryColor ?? + DirColorsParser.Instance.NormalColor)), + IsDirectory = true + })) Files.Add(model); - foreach(string file in Directory.GetFiles(Directory.GetCurrentDirectory(), "*", SearchOption.TopDirectoryOnly)) + foreach(string file in Directory.GetFiles(CurrentPath, "*", SearchOption.TopDirectoryOnly)) { var model = new FileModel { @@ -90,4 +103,15 @@ public sealed partial class MainWindowViewModel : ViewModelBase Files.Add(model); } } + + void OpenSelectedFile() + { + if(!SelectedFile.IsDirectory) return; + + CurrentPath = SelectedFile.Path; + Environment.CurrentDirectory = CurrentPath; + LoadFiles(); + } + + bool CanOpenSelectedFile() => SelectedFile != null; } \ No newline at end of file diff --git a/Aaru.Tui/Views/Windows/MainWindow.axaml b/Aaru.Tui/Views/Windows/MainWindow.axaml index dee99ed1e..cc7d1be4d 100644 --- a/Aaru.Tui/Views/Windows/MainWindow.axaml +++ b/Aaru.Tui/Views/Windows/MainWindow.axaml @@ -24,10 +24,12 @@ + Background="Transparent" + SelectedItem="{Binding SelectedFile, Mode=TwoWay}" + KeyDown="ListBox_OnKeyDown"> - @@ -56,10 +58,10 @@ - - @@ -75,7 +77,7 @@ Text="Path: " Foreground="SlateBlue" /> diff --git a/Aaru.Tui/Views/Windows/MainWindow.axaml.cs b/Aaru.Tui/Views/Windows/MainWindow.axaml.cs index 3b832e871..4382e3b7b 100644 --- a/Aaru.Tui/Views/Windows/MainWindow.axaml.cs +++ b/Aaru.Tui/Views/Windows/MainWindow.axaml.cs @@ -1,5 +1,6 @@ using Aaru.Tui.ViewModels.Windows; using Avalonia.Controls; +using Avalonia.Input; using Avalonia.Interactivity; namespace Aaru.Tui.Views.Windows; @@ -11,6 +12,18 @@ public partial class MainWindow : Window InitializeComponent(); } + private void ListBox_OnKeyDown(object sender, KeyEventArgs e) + { + if(e.Key == Key.Enter) + { + if(DataContext is MainWindowViewModel vm && vm.OpenSelectedFileCommand.CanExecute(null)) + { + vm.OpenSelectedFileCommand.Execute(null); + e.Handled = true; + } + } + } + /// protected override void OnLoaded(RoutedEventArgs e) {