Skip to content

Commit 1246d99

Browse files
committed
feat: Add debug mode
1 parent bda0452 commit 1246d99

9 files changed

Lines changed: 376 additions & 33 deletions

File tree

PostCodeSerialMonitor/Assets/Resources.Designer.cs

Lines changed: 91 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

PostCodeSerialMonitor/Assets/Resources.pt-BR.resx

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,46 @@
381381
<value>Versão do App:</value>
382382
<comment>In Views/MainWindow.axaml</comment>
383383
</data>
384+
<data name="Debug" xml:space="preserve">
385+
<value>Depuração</value>
386+
<comment>In Views/MainWindow.axaml, Views/DebugDialog.axaml</comment>
387+
</data>
388+
<data name="FillDummyData" xml:space="preserve">
389+
<value>Preencher com dados fictícios</value>
390+
<comment>In Views/DebugDialog.axaml</comment>
391+
</data>
392+
<data name="Fill" xml:space="preserve">
393+
<value>Preencher</value>
394+
<comment>In Views/DebugDialog.axaml</comment>
395+
</data>
396+
<data name="DecodeStandaloneCode" xml:space="preserve">
397+
<value>Decodificar código avulso</value>
398+
<comment>In Views/DebugDialog.axaml</comment>
399+
</data>
400+
<data name="CodeFlavorLabel" xml:space="preserve">
401+
<value>Tipo de código</value>
402+
<comment>In Views/DebugDialog.axaml</comment>
403+
</data>
404+
<data name="ConsoleTypeLabel" xml:space="preserve">
405+
<value>Modelo do console</value>
406+
<comment>In Views/DebugDialog.axaml</comment>
407+
</data>
408+
<data name="Decode" xml:space="preserve">
409+
<value>Decodificar</value>
410+
<comment>In Views/DebugDialog.axaml</comment>
411+
</data>
412+
<data name="Result" xml:space="preserve">
413+
<value>Resultado</value>
414+
<comment>In Views/DebugDialog.axaml</comment>
415+
</data>
416+
<data name="Close" xml:space="preserve">
417+
<value>Fechar</value>
418+
<comment>In Views/DebugDialog.axaml</comment>
419+
</data>
420+
<data name="InvalidCodeFormatMessageBoxError" xml:space="preserve">
421+
<value>Formato de código inválido: {0}</value>
422+
<comment>In ViewModels/DebugDialogViewModel.cs</comment>
423+
</data>
384424
<data name="VisitXboxResearch" xml:space="preserve">
385425
<value>Visite XboxResearch.com</value>
386426
<comment>In Views/MainWindow.axaml</comment>

PostCodeSerialMonitor/Assets/Resources.resx

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,46 @@
381381
<value>App Version:</value>
382382
<comment>In Views/MainWindow.axaml</comment>
383383
</data>
384+
<data name="Debug" xml:space="preserve">
385+
<value>Debug</value>
386+
<comment>In Views/MainWindow.axaml, Views/DebugDialog.axaml</comment>
387+
</data>
388+
<data name="FillDummyData" xml:space="preserve">
389+
<value>Fill view with dummy data</value>
390+
<comment>In Views/DebugDialog.axaml</comment>
391+
</data>
392+
<data name="Fill" xml:space="preserve">
393+
<value>Fill</value>
394+
<comment>In Views/DebugDialog.axaml</comment>
395+
</data>
396+
<data name="DecodeStandaloneCode" xml:space="preserve">
397+
<value>Decode standalone code</value>
398+
<comment>In Views/DebugDialog.axaml</comment>
399+
</data>
400+
<data name="CodeFlavorLabel" xml:space="preserve">
401+
<value>Code flavor</value>
402+
<comment>In Views/DebugDialog.axaml</comment>
403+
</data>
404+
<data name="ConsoleTypeLabel" xml:space="preserve">
405+
<value>Console type</value>
406+
<comment>In Views/DebugDialog.axaml</comment>
407+
</data>
408+
<data name="Decode" xml:space="preserve">
409+
<value>Decode</value>
410+
<comment>In Views/DebugDialog.axaml</comment>
411+
</data>
412+
<data name="Result" xml:space="preserve">
413+
<value>Result</value>
414+
<comment>In Views/DebugDialog.axaml</comment>
415+
</data>
416+
<data name="Close" xml:space="preserve">
417+
<value>Close</value>
418+
<comment>In Views/DebugDialog.axaml</comment>
419+
</data>
420+
<data name="InvalidCodeFormatMessageBoxError" xml:space="preserve">
421+
<value>Invalid code format: {0}</value>
422+
<comment>In ViewModels/DebugDialogViewModel.cs</comment>
423+
</data>
384424
<data name="VisitXboxResearch" xml:space="preserve">
385425
<value>Visit XboxResearch.com</value>
386426
<comment>In Views/MainWindow.axaml</comment>
Lines changed: 107 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,107 @@
1+
using System;
2+
using System.Collections.ObjectModel;
3+
using System.Linq;
4+
using System.Threading.Tasks;
5+
using CommunityToolkit.Mvvm.ComponentModel;
6+
using CommunityToolkit.Mvvm.Input;
7+
using MsBox.Avalonia;
8+
using MsBox.Avalonia.Enums;
9+
using PostCodeSerialMonitor.Models;
10+
using PostCodeSerialMonitor.Services;
11+
12+
namespace PostCodeSerialMonitor.ViewModels;
13+
14+
public partial class DebugDialogViewModel : ViewModelBase
15+
{
16+
private readonly ObservableCollection<LogEntry> _logEntries;
17+
private readonly SerialLineDecoder _serialLineDecoder;
18+
19+
public ObservableCollection<ConsoleType> ConsoleTypes { get; }
20+
public ObservableCollection<CodeFlavor> CodeFlavors { get; } = new(new[]
21+
{
22+
CodeFlavor.SMC, CodeFlavor.SP, CodeFlavor.CPU, CodeFlavor.OS
23+
});
24+
25+
[ObservableProperty]
26+
private ConsoleType selectedConsoleType;
27+
28+
[ObservableProperty]
29+
private decimal entryCount = 20;
30+
31+
[ObservableProperty]
32+
private string codeInput = string.Empty;
33+
34+
[ObservableProperty]
35+
private string decodedResultText = string.Empty;
36+
37+
public DebugDialogViewModel(
38+
ObservableCollection<LogEntry> logEntries,
39+
SerialLineDecoder serialLineDecoder,
40+
ObservableCollection<ConsoleType> consoleTypes)
41+
{
42+
_logEntries = logEntries;
43+
_serialLineDecoder = serialLineDecoder;
44+
ConsoleTypes = consoleTypes;
45+
46+
SelectedConsoleType = ConsoleTypes.FirstOrDefault();
47+
}
48+
49+
[RelayCommand]
50+
private void FillDummyData()
51+
{
52+
for (int i = 0; i < (int)EntryCount; i++)
53+
{
54+
_logEntries.Add(new LogEntry
55+
{
56+
DecodedCode = new DecodedCode
57+
{
58+
Flavor = CodeFlavors[i % CodeFlavors.Count],
59+
Index = i,
60+
Code = i * 0x11,
61+
SeverityLevel = (CodeSeverity)(i % 3),
62+
Name = $"DEBUG_CODE_{i}",
63+
Description = i % 4 == 0
64+
? $"This is a long debug description for entry {i}, used to verify that the log window wraps text correctly and fills the full width of the resized main window instead of being clipped at a fixed maximum width."
65+
: $"Debug entry {i}"
66+
}
67+
});
68+
}
69+
}
70+
71+
[RelayCommand]
72+
private async Task DecodeStandaloneAsync()
73+
{
74+
int code;
75+
try
76+
{
77+
var hex = CodeInput.Trim();
78+
if (hex.StartsWith("0x", StringComparison.OrdinalIgnoreCase))
79+
hex = hex[2..];
80+
code = Convert.ToInt32(hex, 16) & 0xFFFF;
81+
}
82+
catch (Exception)
83+
{
84+
await MessageBoxManager
85+
.GetMessageBoxStandard(Assets.Resources.Error, string.Format(Assets.Resources.InvalidCodeFormatMessageBoxError, CodeInput), ButtonEnum.Ok)
86+
.ShowAsPopupAsync(GetParentWindow());
87+
return;
88+
}
89+
90+
DecodedResultText = String.Empty;
91+
foreach (var codeFlavor in CodeFlavors)
92+
{
93+
var line = $"{codeFlavor} (0): 0x{code:X4}";
94+
var decoded = _serialLineDecoder.DecodeLine(line, SelectedConsoleType);
95+
DecodedResultText += decoded != null
96+
? new LogEntry { DecodedCode = decoded }.FormattedText
97+
: $"Decoding '{line}' failed";
98+
DecodedResultText += "\n";
99+
}
100+
}
101+
102+
[RelayCommand]
103+
private void Close(Avalonia.Controls.Window window)
104+
{
105+
window.Close();
106+
}
107+
}

PostCodeSerialMonitor/ViewModels/MainWindowViewModel.cs

Lines changed: 27 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,11 @@ public partial class MainWindowViewModel : ViewModelBase
9595
[ObservableProperty]
9696
private string appVersion;
9797

98+
[ObservableProperty]
99+
private bool debugModeUnlocked;
100+
101+
private int _appVersionClickCount;
102+
98103
public IStorageProvider? StorageProvider
99104
{
100105
get => _storageProvider;
@@ -138,38 +143,7 @@ public MainWindowViewModel(
138143
_serialService.Disconnected += OnDisconnected;
139144
_serialService.DeviceStateChanged += OnDeviceStateChanged;
140145
_serialService.DeviceConfigChanged += OnDeviceConfigChanged;
141-
142-
if (false) {
143-
#pragma warning disable CS0162 // Unreachable code
144-
// For debugging UI layout
145-
PrefillDebugLogEntries();
146-
#pragma warning restore CS0162 // Unreachable code
147-
}
148-
}
149-
150-
#pragma warning disable CS0162 // Unreachable code
151-
private void PrefillDebugLogEntries()
152-
{
153-
for (int i = 0; i < 30; i++)
154-
{
155-
LogEntries.Add(new LogEntry
156-
{
157-
DecodedCode = new DecodedCode
158-
{
159-
Flavor = (CodeFlavor)(i % Enum.GetValues(typeof(CodeFlavor)).Length),
160-
Index = i,
161-
Code = i * 0x11,
162-
SeverityLevel = (CodeSeverity)(i % 3),
163-
Name = $"DEBUG_CODE_{i}",
164-
Description = i % 4 == 0
165-
? $"This is a long debug description for entry {i}, used to verify that the log window wraps text correctly and fills the full width of the resized main window instead of being clipped at a fixed maximum width."
166-
: $"Debug entry {i}"
167-
}
168-
});
169-
}
170146
}
171-
#pragma warning restore CS0162 // Unreachable code
172-
173147

174148
private MessageBoxStandardParams MsgBoxHyperlink(string title, string text, string link)
175149
{
@@ -447,4 +421,26 @@ private async Task ShowConfigurationAsync()
447421

448422
ShowTimestamps = _configurationService.Config.ShowTimestamps;
449423
}
424+
425+
[RelayCommand]
426+
private void AppVersionClicked()
427+
{
428+
if (DebugModeUnlocked)
429+
return;
430+
431+
_appVersionClickCount++;
432+
if (_appVersionClickCount >= 5)
433+
DebugModeUnlocked = true;
434+
}
435+
436+
[RelayCommand]
437+
private async Task ShowDebugMenuAsync()
438+
{
439+
var dialog = new DebugDialog
440+
{
441+
DataContext = new DebugDialogViewModel(LogEntries, _serialLineDecoder, ConsoleModels)
442+
};
443+
444+
await dialog.ShowDialog(GetParentWindow());
445+
}
450446
}

0 commit comments

Comments
 (0)