[TUI] Implement changing path.

This commit is contained in:
2025-10-15 22:54:35 +01:00
parent a0637e5cac
commit 4fd1338065
4 changed files with 60 additions and 20 deletions

View File

@@ -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; }
}

View File

@@ -22,10 +22,13 @@ public sealed partial class MainWindowViewModel : ViewModelBase
ObservableCollection<FileModel> _files = [];
[ObservableProperty]
public string _informationalVersion;
[ObservableProperty]
public FileModel _selectedFile;
public MainWindowViewModel()
{
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<AssemblyCopyrightAttribute>()?.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)
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))
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;
}

View File

@@ -24,10 +24,12 @@
<ListBox Grid.Column="0"
ItemsSource="{Binding Files, Mode=OneWay}"
BorderThickness="1"
Background="Transparent">
Background="Transparent"
SelectedItem="{Binding SelectedFile, Mode=TwoWay}"
KeyDown="ListBox_OnKeyDown">
<ListBox.ItemTemplate>
<DataTemplate DataType="models:FileModel">
<TextBox Text="{Binding Filename}"
<TextBox Text="{Binding Filename, Mode=OneWay}"
Foreground="{Binding ForegroundBrush, Mode=OneWay}"
Background="Transparent" />
</DataTemplate>
@@ -56,10 +58,10 @@
<TextBlock Text="Aaru Data Preservation Suite"
Foreground="Red"
FontWeight="Bold" />
<TextBlock Text="{Binding InformationalVersion}"
<TextBlock Text="{Binding InformationalVersion, Mode=OneWay}"
Foreground="Green"
FontWeight="Bold" />
<TextBlock Text="{Binding Copyright}"
<TextBlock Text="{Binding Copyright, Mode=OneWay}"
Foreground="Blue"
FontWeight="Bold" />
</StackPanel>
@@ -75,7 +77,7 @@
Text="Path: "
Foreground="SlateBlue" />
<TextBlock Grid.Column="1"
Text="{Binding CurrentPath}"
Text="{Binding CurrentPath, Mode=OneWay}"
Foreground="Green" />
</Grid>
</Border>

View File

@@ -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;
}
}
}
/// <inheritdoc />
protected override void OnLoaded(RoutedEventArgs e)
{