Skip to content

Commit

Permalink
refactor: Status message view
Browse files Browse the repository at this point in the history
Signed-off-by: Maximilien Noal <noal.maximilien@gmail.com>
  • Loading branch information
maximilien-noal committed Nov 3, 2024
1 parent 3397aec commit 94a3f4f
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 38 deletions.
1 change: 1 addition & 0 deletions src/Spice86/ViewModels/BreakpointViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ namespace Spice86.ViewModels;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;

using Spice86.Core.Emulator.Memory;
using Spice86.Core.Emulator.VM;
using Spice86.Core.Emulator.VM.Breakpoint;
using Spice86.Models.Debugging;
Expand Down
27 changes: 14 additions & 13 deletions src/Spice86/ViewModels/BreakpointsViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ namespace Spice86.ViewModels;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;

using Spice86.Core.Emulator.Memory;
using Spice86.Core.Emulator.VM;
using Spice86.Core.Emulator.VM.Breakpoint;
using Spice86.Models.Debugging;
Expand All @@ -12,13 +13,14 @@ namespace Spice86.ViewModels;

public partial class BreakpointsViewModel : ViewModelBase {
private readonly EmulatorBreakpointsManager _emulatorBreakpointsManager;
private readonly IPauseHandler _pauseHandler;
private readonly IMemory _memory;

public BreakpointsViewModel(EmulatorBreakpointsManager emulatorBreakpointsManager) {
public BreakpointsViewModel(IPauseHandler pauseHandler, IMemory memory, EmulatorBreakpointsManager emulatorBreakpointsManager) {
_emulatorBreakpointsManager = emulatorBreakpointsManager;
_memory = memory;
_pauseHandler = pauseHandler;
}

[ObservableProperty]
private bool _showBreakpointCreationDialog = false;

[ObservableProperty]
private ObservableCollection<BreakpointViewModel> _breakpoints = new();
Expand All @@ -38,23 +40,18 @@ private void ToggleSelectedBreakpoint() {
private BreakpointViewModel? _selectedBreakpoint;

internal void AddAddressBreakpoint(AddressBreakPoint addressBreakPoint) {
var breakpointViewModel = new BreakpointViewModel(_emulatorBreakpointsManager, addressBreakPoint);
var breakpointViewModel = new BreakpointViewModel( _emulatorBreakpointsManager, addressBreakPoint);
Breakpoints.Add(breakpointViewModel);
SelectedBreakpoint = breakpointViewModel;
}

[RelayCommand]
private void Create() {
ShowBreakpointCreationDialog = true;
}

private bool RemoveBreakpointCanExecute() => SelectedBreakpoint is not null;


[RelayCommand(CanExecute = nameof(RemoveBreakpointCanExecute))]
private void RemoveBreakpoint() {
if (SelectedBreakpoint is not null) {
Breakpoints.Remove(SelectedBreakpoint);
DeleteBreakpoint(SelectedBreakpoint);
}
}

Expand All @@ -65,8 +62,12 @@ internal bool HasBreakpoint(CpuInstructionInfo instructionInfo) {
internal void RemoveBreakpoint(CpuInstructionInfo selectedInstruction) {
BreakpointViewModel? breakpoint = Breakpoints.FirstOrDefault(x => x.IsFor(selectedInstruction));
if (breakpoint is not null) {
breakpoint.Disable();
Breakpoints.Remove(breakpoint);
DeleteBreakpoint(breakpoint);
}
}

private void DeleteBreakpoint(BreakpointViewModel breakpoint) {
breakpoint.Disable();
Breakpoints.Remove(breakpoint);
}
}
2 changes: 1 addition & 1 deletion src/Spice86/ViewModels/DebugWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public DebugWindowViewModel(IInstructionExecutor cpu, State cpuState, Stack stac
messenger.Register<RemoveViewModelMessage<DisassemblyViewModel>>(this);
messenger.Register<RemoveViewModelMessage<MemoryViewModel>>(this);
_messenger = messenger;
BreakpointsViewModel = new(emulatorBreakpointsManager);
BreakpointsViewModel = new(pauseHandler, memory, emulatorBreakpointsManager);
StatusMessageViewModel = new(_messenger);
_pauseHandler = pauseHandler;
IsPaused = pauseHandler.IsPaused;
Expand Down
6 changes: 0 additions & 6 deletions src/Spice86/ViewModels/StatusMessageViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,12 @@ namespace Spice86.ViewModels;
using Spice86.Messages;

public partial class StatusMessageViewModel : ViewModelBase, IRecipient<StatusMessage> {
[ObservableProperty]
private AvaloniaList<StatusMessage> _previousMessages = new();

[ObservableProperty]
private StatusMessage? _message;

public StatusMessageViewModel(IMessenger messenger) => messenger.Register(this);

public void Receive(StatusMessage message) {
PreviousMessages.Add(message);
Message = message;
if (PreviousMessages.Count > 50)
PreviousMessages.RemoveAt(0);
}
}
4 changes: 1 addition & 3 deletions src/Spice86/Views/BreakpointsView.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<StackPanel Grid.Row="0" Orientation="Horizontal" HorizontalAlignment="Center">
<Button Content="Create..." Command="{Binding CreateCommand}" Margin="5"/>
<Button Content="Remove" Command="{Binding RemoveBreakpointCommand}" Margin="5"/>
<Button Content="Toggle" Command="{Binding ToggleSelectedBreakpointCommand}" Margin="5"/>
</StackPanel>
Expand All @@ -23,8 +22,7 @@
CanUserReorderColumns="True"
CanUserResizeColumns="True"
CanUserSortColumns="False"
IsReadOnly="True"
SelectionMode="Extended">
IsReadOnly="True">
<DataGrid.Columns>
<DataGridCheckBoxColumn Binding="{Binding IsEnabled}" Header="Is Enabled ?" />
<DataGridCheckBoxColumn Binding="{Binding IsRemovedOnTrigger}" Header="Is removed on trigger ?" />
Expand Down
22 changes: 7 additions & 15 deletions src/Spice86/Views/StatusMessageView.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,13 @@
x:DataType="viewModels:StatusMessageViewModel">
<controls:StatusBar>
<controls:StatusBarItem>
<StackPanel Orientation="Horizontal">
<ComboBox SelectedItem="{Binding Message, Mode=TwoWay}" ItemsSource="{Binding PreviousMessages}">
<ComboBox.ItemTemplate>
<DataTemplate>
<TextBlock>
<Run Text="{Binding Time}" />
<Run Text=": " />
<Run Text="{Binding Origin}" />
<Run Text=" - " />
<Run Text="{Binding Message}" />
</TextBlock>
</DataTemplate>
</ComboBox.ItemTemplate>
</ComboBox>
</StackPanel>
<TextBlock DataContext="{Binding Message}">
<Run Text="{Binding Time}" />
<Run Text=": " />
<Run Text="{Binding Origin}" />
<Run Text=" - " />
<Run Text="{Binding Message}" />
</TextBlock>
</controls:StatusBarItem>
</controls:StatusBar>
</UserControl>

0 comments on commit 94a3f4f

Please sign in to comment.