mirror of
https://github.com/aaru-dps/Aaru.git
synced 2025-12-16 19:24:25 +00:00
[TUI] Implement hex view window for displaying image sectors
This commit is contained in:
@@ -16,6 +16,9 @@
|
|||||||
<DependentUpon>MainWindow.axaml</DependentUpon>
|
<DependentUpon>MainWindow.axaml</DependentUpon>
|
||||||
<SubType>Code</SubType>
|
<SubType>Code</SubType>
|
||||||
</Compile>
|
</Compile>
|
||||||
|
<Compile Update="Views\Windows\HexViewWindow.axaml.cs">
|
||||||
|
<DependentUpon>HexViewWindow.axaml</DependentUpon>
|
||||||
|
</Compile>
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
|
|||||||
115
Aaru.Tui/ViewModels/Windows/HexViewWindowViewModel.cs
Normal file
115
Aaru.Tui/ViewModels/Windows/HexViewWindowViewModel.cs
Normal file
@@ -0,0 +1,115 @@
|
|||||||
|
using System.Collections.ObjectModel;
|
||||||
|
using System.Windows.Input;
|
||||||
|
using Aaru.CommonTypes.Interfaces;
|
||||||
|
using Avalonia;
|
||||||
|
using Avalonia.Controls;
|
||||||
|
using Avalonia.Controls.ApplicationLifetimes;
|
||||||
|
using CommunityToolkit.Mvvm.ComponentModel;
|
||||||
|
using CommunityToolkit.Mvvm.Input;
|
||||||
|
|
||||||
|
namespace Aaru.Tui.ViewModels.Windows;
|
||||||
|
|
||||||
|
public sealed partial class HexViewWindowViewModel : ViewModelBase
|
||||||
|
{
|
||||||
|
private const int BYTES_PER_LINE = 16;
|
||||||
|
readonly IMediaImage _imageFormat;
|
||||||
|
readonly Window _parent;
|
||||||
|
readonly Window _view;
|
||||||
|
readonly ulong _currentSector;
|
||||||
|
[ObservableProperty]
|
||||||
|
string _filePath;
|
||||||
|
[ObservableProperty]
|
||||||
|
long _fileSize;
|
||||||
|
[ObservableProperty]
|
||||||
|
ObservableCollection<HexViewLine> _lines = [];
|
||||||
|
|
||||||
|
internal HexViewWindowViewModel(Window parent, Window view, IMediaImage imageFormat, string filePath)
|
||||||
|
{
|
||||||
|
ExitCommand = new RelayCommand(Exit);
|
||||||
|
BackCommand = new RelayCommand(Back);
|
||||||
|
|
||||||
|
_parent = parent;
|
||||||
|
_view = view;
|
||||||
|
_imageFormat = imageFormat;
|
||||||
|
_currentSector = 0;
|
||||||
|
FilePath = filePath;
|
||||||
|
}
|
||||||
|
|
||||||
|
public ICommand BackCommand { get; }
|
||||||
|
public ICommand ExitCommand { get; }
|
||||||
|
|
||||||
|
void Back()
|
||||||
|
{
|
||||||
|
_parent.Show();
|
||||||
|
_view.Close();
|
||||||
|
}
|
||||||
|
|
||||||
|
void Exit()
|
||||||
|
{
|
||||||
|
var lifetime = Application.Current!.ApplicationLifetime as IControlledApplicationLifetime;
|
||||||
|
lifetime!.Shutdown();
|
||||||
|
}
|
||||||
|
|
||||||
|
void LoadSector()
|
||||||
|
{
|
||||||
|
_imageFormat.ReadSector(_currentSector, out byte[]? sector);
|
||||||
|
|
||||||
|
using var stream = new MemoryStream(sector);
|
||||||
|
var buffer = new byte[BYTES_PER_LINE];
|
||||||
|
long offset = 0;
|
||||||
|
|
||||||
|
const int maxLines = 1000;
|
||||||
|
|
||||||
|
for(var lineCount = 0; stream.Position < stream.Length && lineCount < maxLines; lineCount++)
|
||||||
|
{
|
||||||
|
int bytesRead = stream.Read(buffer, 0, BYTES_PER_LINE);
|
||||||
|
|
||||||
|
if(bytesRead == 0) break;
|
||||||
|
|
||||||
|
var line = new HexViewLine
|
||||||
|
{
|
||||||
|
Offset = offset,
|
||||||
|
Bytes = buffer.Take(bytesRead).ToArray()
|
||||||
|
};
|
||||||
|
|
||||||
|
Lines.Add(line);
|
||||||
|
offset += bytesRead;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void LoadComplete()
|
||||||
|
{
|
||||||
|
LoadSector();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
sealed class HexViewLine
|
||||||
|
{
|
||||||
|
internal long Offset { get; init; }
|
||||||
|
internal byte[] Bytes { get; init; }
|
||||||
|
|
||||||
|
public string OffsetString => $"{Offset:X8}";
|
||||||
|
|
||||||
|
public string HexString
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
var hex = string.Join(" ", Bytes.Select(b => $"{b:X2}"));
|
||||||
|
|
||||||
|
// Pad to 16 bytes worth of hex (16 * 3 - 1 = 47 chars)
|
||||||
|
return hex.PadRight(47);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public string AsciiString
|
||||||
|
{
|
||||||
|
get
|
||||||
|
{
|
||||||
|
var ascii = new char[Bytes.Length];
|
||||||
|
|
||||||
|
for(var i = 0; i < Bytes.Length; i++) ascii[i] = Bytes[i] >= 32 && Bytes[i] < 127 ? (char)Bytes[i] : '.';
|
||||||
|
|
||||||
|
return new string(ascii).PadRight(16);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -42,6 +42,7 @@ public sealed partial class MainWindowViewModel : ViewModelBase
|
|||||||
{
|
{
|
||||||
_view = view;
|
_view = view;
|
||||||
ExitCommand = new RelayCommand(Exit);
|
ExitCommand = new RelayCommand(Exit);
|
||||||
|
SectorViewCommand = new RelayCommand(SectorView);
|
||||||
OpenSelectedFileCommand = new RelayCommand(OpenSelectedFile, CanOpenSelectedFile);
|
OpenSelectedFileCommand = new RelayCommand(OpenSelectedFile, CanOpenSelectedFile);
|
||||||
|
|
||||||
InformationalVersion =
|
InformationalVersion =
|
||||||
@@ -74,6 +75,7 @@ public sealed partial class MainWindowViewModel : ViewModelBase
|
|||||||
|
|
||||||
public ICommand OpenSelectedFileCommand { get; }
|
public ICommand OpenSelectedFileCommand { get; }
|
||||||
public ICommand ExitCommand { get; }
|
public ICommand ExitCommand { get; }
|
||||||
|
public ICommand SectorViewCommand { get; }
|
||||||
public bool IsFileInfoAvailable => SelectedFile?.FileInfo != null;
|
public bool IsFileInfoAvailable => SelectedFile?.FileInfo != null;
|
||||||
public bool SelectedFileIsNotDirectory => SelectedFile?.IsDirectory == false;
|
public bool SelectedFileIsNotDirectory => SelectedFile?.IsDirectory == false;
|
||||||
public long? SelectedFileLength => SelectedFile?.IsDirectory == false ? SelectedFile?.FileInfo?.Length : 0;
|
public long? SelectedFileLength => SelectedFile?.IsDirectory == false ? SelectedFile?.FileInfo?.Length : 0;
|
||||||
@@ -85,6 +87,18 @@ public sealed partial class MainWindowViewModel : ViewModelBase
|
|||||||
|
|
||||||
public string? SelectedFileInformation => SelectedFile?.Information;
|
public string? SelectedFileInformation => SelectedFile?.Information;
|
||||||
|
|
||||||
|
void SectorView()
|
||||||
|
{
|
||||||
|
if(SelectedFile?.ImageFormat is null) return;
|
||||||
|
|
||||||
|
var view = new HexViewWindow();
|
||||||
|
|
||||||
|
var vm = new HexViewWindowViewModel(_view, view, SelectedFile.ImageFormat, SelectedFile.Path);
|
||||||
|
view.DataContext = vm;
|
||||||
|
view.Show();
|
||||||
|
_view.Hide();
|
||||||
|
}
|
||||||
|
|
||||||
void Exit()
|
void Exit()
|
||||||
{
|
{
|
||||||
var lifetime = Application.Current!.ApplicationLifetime as IControlledApplicationLifetime;
|
var lifetime = Application.Current!.ApplicationLifetime as IControlledApplicationLifetime;
|
||||||
@@ -299,6 +313,8 @@ public sealed partial class MainWindowViewModel : ViewModelBase
|
|||||||
CurrentPath = SelectedFile.Path;
|
CurrentPath = SelectedFile.Path;
|
||||||
Environment.CurrentDirectory = CurrentPath;
|
Environment.CurrentDirectory = CurrentPath;
|
||||||
LoadFiles();
|
LoadFiles();
|
||||||
|
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(SelectedFile.ImageFormat is null) return;
|
if(SelectedFile.ImageFormat is null) return;
|
||||||
|
|||||||
79
Aaru.Tui/Views/Windows/HexViewWindow.axaml
Normal file
79
Aaru.Tui/Views/Windows/HexViewWindow.axaml
Normal file
@@ -0,0 +1,79 @@
|
|||||||
|
<Window xmlns="https://github.com/avaloniaui"
|
||||||
|
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||||
|
xmlns:console="https://github.com/jinek/consolonia"
|
||||||
|
xmlns:windows="clr-namespace:Aaru.Tui.ViewModels.Windows"
|
||||||
|
x:Class="Aaru.Tui.Views.Windows.HexViewWindow"
|
||||||
|
RequestedThemeVariant="Dark">
|
||||||
|
<Design.DataContext>
|
||||||
|
<windows:HexViewWindowViewModel />
|
||||||
|
</Design.DataContext>
|
||||||
|
<DockPanel>
|
||||||
|
<Menu DockPanel.Dock="Bottom">
|
||||||
|
<MenuItem Header="ESC Back"
|
||||||
|
HotKey="Escape"
|
||||||
|
Command="{Binding BackCommand, Mode=OneWay}" />
|
||||||
|
<MenuItem Header="F10 Exit"
|
||||||
|
HotKey="F10"
|
||||||
|
Command="{Binding ExitCommand, Mode=OneWay}" />
|
||||||
|
</Menu>
|
||||||
|
<Border BorderThickness="1">
|
||||||
|
<Border.BorderBrush>
|
||||||
|
<console:LineBrush LineStyle="DoubleLine"
|
||||||
|
Brush="Blue" />
|
||||||
|
</Border.BorderBrush>
|
||||||
|
<Grid RowDefinitions="Auto,Auto,*">
|
||||||
|
<TextBlock Grid.Row="0"
|
||||||
|
Foreground="Green"
|
||||||
|
Text="{Binding FilePath, Mode=OneWay}" />
|
||||||
|
<!-- Header -->
|
||||||
|
<Grid Grid.Row="1"
|
||||||
|
ColumnDefinitions="Auto,*,Auto">
|
||||||
|
<TextBlock Grid.Column="0"
|
||||||
|
Text="Offset"
|
||||||
|
HorizontalAlignment="Left"
|
||||||
|
Foreground="Cyan"
|
||||||
|
FontWeight="Bold" />
|
||||||
|
<TextBlock Grid.Column="1"
|
||||||
|
HorizontalAlignment="Center"
|
||||||
|
Text="00 01 02 03 04 05 06 07 08 09 0A 0B 0C 0D 0E 0F"
|
||||||
|
Foreground="Cyan"
|
||||||
|
FontWeight="Bold" />
|
||||||
|
<TextBlock Grid.Column="2"
|
||||||
|
Text="ASCII "
|
||||||
|
HorizontalAlignment="Left"
|
||||||
|
Foreground="Cyan"
|
||||||
|
FontWeight="Bold" />
|
||||||
|
</Grid>
|
||||||
|
|
||||||
|
<!-- Hex View Content -->
|
||||||
|
<ScrollViewer Grid.Row="2"
|
||||||
|
Background="Transparent"
|
||||||
|
x:Name="HexScrollViewer">
|
||||||
|
<ItemsControl ItemsSource="{Binding Lines, Mode=OneWay}">
|
||||||
|
<ItemsControl.ItemTemplate>
|
||||||
|
<DataTemplate DataType="windows:HexViewLine">
|
||||||
|
<Grid ColumnDefinitions="Auto,*,Auto">
|
||||||
|
<!-- Offset Column -->
|
||||||
|
<TextBlock Grid.Column="0"
|
||||||
|
Text="{Binding OffsetString, Mode=OneWay}"
|
||||||
|
Foreground="Yellow" />
|
||||||
|
|
||||||
|
<!-- Hex Column -->
|
||||||
|
<TextBlock Grid.Column="1"
|
||||||
|
HorizontalAlignment="Center"
|
||||||
|
Text="{Binding HexString, Mode=OneWay}"
|
||||||
|
Foreground="White" />
|
||||||
|
|
||||||
|
<!-- ASCII Column -->
|
||||||
|
<TextBlock Grid.Column="2"
|
||||||
|
Text="{Binding AsciiString, Mode=OneWay}"
|
||||||
|
Foreground="LightGreen" />
|
||||||
|
</Grid>
|
||||||
|
</DataTemplate>
|
||||||
|
</ItemsControl.ItemTemplate>
|
||||||
|
</ItemsControl>
|
||||||
|
</ScrollViewer>
|
||||||
|
</Grid>
|
||||||
|
</Border>
|
||||||
|
</DockPanel>
|
||||||
|
</Window>
|
||||||
22
Aaru.Tui/Views/Windows/HexViewWindow.axaml.cs
Normal file
22
Aaru.Tui/Views/Windows/HexViewWindow.axaml.cs
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
using Aaru.Tui.ViewModels.Windows;
|
||||||
|
using Avalonia.Controls;
|
||||||
|
using Avalonia.Interactivity;
|
||||||
|
|
||||||
|
namespace Aaru.Tui.Views.Windows;
|
||||||
|
|
||||||
|
public partial class HexViewWindow : Window
|
||||||
|
{
|
||||||
|
public HexViewWindow()
|
||||||
|
{
|
||||||
|
InitializeComponent();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// <inheritdoc />
|
||||||
|
protected override void OnLoaded(RoutedEventArgs e)
|
||||||
|
{
|
||||||
|
base.OnLoaded(e);
|
||||||
|
|
||||||
|
(DataContext as HexViewWindowViewModel)?.LoadComplete();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -11,7 +11,10 @@
|
|||||||
</Design.DataContext>
|
</Design.DataContext>
|
||||||
<DockPanel>
|
<DockPanel>
|
||||||
<Menu DockPanel.Dock="Bottom">
|
<Menu DockPanel.Dock="Bottom">
|
||||||
<MenuItem Header="ESC " />
|
<MenuItem Header="ESC " /> <MenuItem Header="F1 Help" />
|
||||||
|
<MenuItem Header="F2 ScVw"
|
||||||
|
Command="{Binding SectorViewCommand, Mode=OneWay}"
|
||||||
|
HotKey="F2" />
|
||||||
<MenuItem Header="F10 Exit"
|
<MenuItem Header="F10 Exit"
|
||||||
Command="{Binding ExitCommand, Mode=OneWay}"
|
Command="{Binding ExitCommand, Mode=OneWay}"
|
||||||
HotKey="F10" />
|
HotKey="F10" />
|
||||||
|
|||||||
Reference in New Issue
Block a user