Files
Aaru/Aaru.Tui/ViewModels/Windows/MainWindowViewModel.cs

145 lines
5.3 KiB
C#
Raw Normal View History

2025-10-15 21:25:05 +01:00
using System.Collections.ObjectModel;
using System.Reflection;
using System.Windows.Input;
2025-10-15 22:05:55 +01:00
using Aaru.Helpers;
using Aaru.Tui.Models;
2025-10-15 21:25:05 +01:00
using Avalonia;
using Avalonia.Controls.ApplicationLifetimes;
2025-10-15 22:05:55 +01:00
using Avalonia.Media;
2025-10-15 21:25:05 +01:00
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
2025-10-15 22:05:55 +01:00
using Color = Avalonia.Media.Color;
2025-10-15 21:25:05 +01:00
namespace Aaru.Tui.ViewModels.Windows;
2025-10-15 22:05:55 +01:00
public sealed partial class MainWindowViewModel : ViewModelBase
2025-10-15 21:25:05 +01:00
{
[ObservableProperty]
2025-10-16 00:13:02 +01:00
string _copyright;
2025-10-15 22:05:55 +01:00
[ObservableProperty]
2025-10-16 00:13:02 +01:00
string _currentPath;
2025-10-15 22:44:09 +01:00
[ObservableProperty]
2025-10-15 22:05:55 +01:00
ObservableCollection<FileModel> _files = [];
2025-10-15 21:25:05 +01:00
[ObservableProperty]
2025-10-16 00:13:02 +01:00
string _informationalVersion;
FileModel? _selectedFile;
2025-10-15 21:25:05 +01:00
public MainWindowViewModel()
{
2025-10-15 22:54:35 +01:00
ExitCommand = new RelayCommand(Exit);
OpenSelectedFileCommand = new RelayCommand(OpenSelectedFile, CanOpenSelectedFile);
2025-10-15 21:25:05 +01:00
InformationalVersion =
Assembly.GetExecutingAssembly()
.GetCustomAttribute<AssemblyInformationalVersionAttribute>()
?.InformationalVersion ??
"?.?.?";
2025-10-15 22:05:55 +01:00
Copyright = Assembly.GetExecutingAssembly().GetCustomAttribute<AssemblyCopyrightAttribute>()?.Copyright ?? "";
2025-10-15 21:25:05 +01:00
}
2025-10-16 00:13:02 +01:00
public FileModel? SelectedFile
{
get => _selectedFile;
set
{
SetProperty(ref _selectedFile, value);
OnPropertyChanged(nameof(IsFileInfoAvailable));
OnPropertyChanged(nameof(SelectedFileIsNotDirectory));
OnPropertyChanged(nameof(SelectedFileLength));
OnPropertyChanged(nameof(SelectedFileCreationTime));
OnPropertyChanged(nameof(SelectedFileLastWriteTime));
OnPropertyChanged(nameof(SelectedFileAttributes));
OnPropertyChanged(nameof(SelectedFileUnixMode));
}
}
2025-10-15 22:54:35 +01:00
2025-10-16 00:13:02 +01:00
public ICommand OpenSelectedFileCommand { get; }
public ICommand ExitCommand { get; }
public bool IsFileInfoAvailable => SelectedFile?.FileInfo != null;
public bool SelectedFileIsNotDirectory => SelectedFile?.IsDirectory == false;
public long? SelectedFileLength => SelectedFile?.IsDirectory == false ? SelectedFile?.FileInfo?.Length : 0;
public DateTime? SelectedFileCreationTime => SelectedFile?.FileInfo?.CreationTime;
public DateTime? SelectedFileLastWriteTime => SelectedFile?.FileInfo?.LastWriteTime;
public string? SelectedFileAttributes => SelectedFile?.FileInfo?.Attributes.ToString();
public string? SelectedFileUnixMode => SelectedFile?.FileInfo?.UnixFileMode.ToString();
2025-10-15 21:25:05 +01:00
void Exit()
{
var lifetime = Application.Current!.ApplicationLifetime as IControlledApplicationLifetime;
lifetime!.Shutdown();
}
public void LoadComplete()
2025-10-15 22:54:35 +01:00
{
LoadFiles();
}
public void LoadFiles()
2025-10-15 21:25:05 +01:00
{
2025-10-15 22:44:09 +01:00
CurrentPath = Directory.GetCurrentDirectory();
2025-10-15 22:54:35 +01:00
Files.Clear();
2025-10-15 22:44:09 +01:00
2025-10-15 22:05:55 +01:00
var parentDirectory = new FileModel
{
Filename = "..",
2025-10-15 22:54:35 +01:00
Path = Path.GetRelativePath(CurrentPath, ".."),
2025-10-15 22:05:55 +01:00
ForegroundBrush =
new SolidColorBrush(Color.Parse(DirColorsParser.Instance.DirectoryColor ??
2025-10-15 22:54:35 +01:00
DirColorsParser.Instance.NormalColor)),
IsDirectory = true
2025-10-15 22:05:55 +01:00
};
Files.Add(parentDirectory);
2025-10-15 22:54:35 +01:00
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
}))
2025-10-15 22:05:55 +01:00
Files.Add(model);
2025-10-15 21:25:05 +01:00
2025-10-15 22:54:35 +01:00
foreach(string file in Directory.GetFiles(CurrentPath, "*", SearchOption.TopDirectoryOnly))
2025-10-15 22:05:55 +01:00
{
var model = new FileModel
{
Path = file,
Filename = Path.GetFileName(file)
};
string extension = Path.GetExtension(file);
model.ForegroundBrush =
new SolidColorBrush(Color.Parse(DirColorsParser.Instance.ExtensionColors.TryGetValue(extension,
out string? hex)
? hex
: DirColorsParser.Instance.NormalColor));
Files.Add(model);
}
2025-10-16 00:13:02 +01:00
_ = Task.Run(Worker);
}
void Worker()
{
foreach(FileModel file in Files) file.FileInfo = new FileInfo(file.Path);
2025-10-15 21:25:05 +01:00
}
2025-10-15 22:54:35 +01:00
void OpenSelectedFile()
{
2025-10-16 00:13:02 +01:00
if(SelectedFile?.IsDirectory != true) return;
2025-10-15 22:54:35 +01:00
CurrentPath = SelectedFile.Path;
Environment.CurrentDirectory = CurrentPath;
LoadFiles();
}
bool CanOpenSelectedFile() => SelectedFile != null;
2025-10-15 21:25:05 +01:00
}