|
7 | 7 | using System.Reactive.Linq; |
8 | 8 | using System.Reflection; |
9 | 9 | using System.Threading.Tasks; |
| 10 | +using Avalonia.Controls; |
10 | 11 | using Avalonia.Input; |
11 | 12 | using Highbyte.DotNet6502.App.Avalonia.Core.SystemSetup; |
12 | 13 | using Highbyte.DotNet6502.Impl.Avalonia.Commodore64.Input; |
|
20 | 21 |
|
21 | 22 | namespace Highbyte.DotNet6502.App.Avalonia.Core.ViewModels; |
22 | 23 |
|
23 | | -public class C64MenuViewModel : ViewModelBase |
| 24 | +public class C64MenuViewModel : ViewModelBase, ISystemMenuContributor |
24 | 25 | { |
25 | 26 | private readonly AvaloniaHostApp _avaloniaHostApp; |
26 | 27 | private readonly ILoggerFactory _loggerFactory; |
@@ -57,6 +58,14 @@ public class C64MenuViewModel : ViewModelBase |
57 | 58 | public ReactiveCommand<byte[], Unit> LoadBasicFileCommand { get; } |
58 | 59 | public ReactiveCommand<Unit, byte[]> SaveBasicFileCommand { get; } |
59 | 60 | public ReactiveCommand<byte[], Unit> LoadBinaryFileCommand { get; } |
| 61 | + |
| 62 | + // Section toggle / joystick commands used by both the UI click handlers and the menu/shortcut bridge. |
| 63 | + public ReactiveCommand<Unit, Unit> ToggleDiskSectionCommand { get; } |
| 64 | + public ReactiveCommand<Unit, Unit> ToggleLoadSaveSectionCommand { get; } |
| 65 | + public ReactiveCommand<Unit, Unit> ToggleConfigSectionCommand { get; } |
| 66 | + public ReactiveCommand<int, Unit> SetActiveJoystickCommand { get; } |
| 67 | + public ReactiveCommand<Unit, Unit> ToggleJoystickKeyboardCommand { get; } |
| 68 | + public ReactiveCommand<int, Unit> SetKeyboardJoystickCommand { get; } |
60 | 69 | // --- End ReactiveUI Commands --- |
61 | 70 |
|
62 | 71 | public C64MenuViewModel( |
@@ -133,6 +142,40 @@ public C64MenuViewModel( |
133 | 142 | async (fileBuffer) => await LoadBinaryFile(fileBuffer), |
134 | 143 | this.WhenAnyValue(x => x.IsFileOperationEnabled), |
135 | 144 | RxSchedulers.MainThreadScheduler); |
| 145 | + |
| 146 | + ToggleDiskSectionCommand = ReactiveCommandHelper.CreateSafeCommand( |
| 147 | + () => ToggleSection(C64MenuSection.Disk), |
| 148 | + null, |
| 149 | + RxSchedulers.MainThreadScheduler); |
| 150 | + |
| 151 | + ToggleLoadSaveSectionCommand = ReactiveCommandHelper.CreateSafeCommand( |
| 152 | + () => ToggleSection(C64MenuSection.LoadSave), |
| 153 | + null, |
| 154 | + RxSchedulers.MainThreadScheduler); |
| 155 | + |
| 156 | + ToggleConfigSectionCommand = ReactiveCommandHelper.CreateSafeCommand( |
| 157 | + () => ToggleSection(C64MenuSection.Config), |
| 158 | + null, |
| 159 | + RxSchedulers.MainThreadScheduler); |
| 160 | + |
| 161 | + SetActiveJoystickCommand = ReactiveCommandHelper.CreateSafeCommand<int>( |
| 162 | + port => CurrentJoystick = port, |
| 163 | + null, |
| 164 | + RxSchedulers.MainThreadScheduler); |
| 165 | + |
| 166 | + ToggleJoystickKeyboardCommand = ReactiveCommandHelper.CreateSafeCommand( |
| 167 | + () => JoystickKeyboardEnabled = !JoystickKeyboardEnabled, |
| 168 | + null, |
| 169 | + RxSchedulers.MainThreadScheduler); |
| 170 | + |
| 171 | + SetKeyboardJoystickCommand = ReactiveCommandHelper.CreateSafeCommand<int>( |
| 172 | + port => |
| 173 | + { |
| 174 | + if (IsKeyboardJoystickSelectionEnabled) |
| 175 | + KeyboardJoystick = port; |
| 176 | + }, |
| 177 | + null, |
| 178 | + RxSchedulers.MainThreadScheduler); |
136 | 179 | } |
137 | 180 |
|
138 | 181 | private EmulatorState EmulatorState => _avaloniaHostApp.EmulatorState; |
@@ -332,6 +375,186 @@ public bool HasConfigValidationErrors |
332 | 375 | } |
333 | 376 | } |
334 | 377 |
|
| 378 | + // Section expansion state — bound from XAML so both UI clicks and keyboard shortcuts |
| 379 | + // go through the same ViewModel state. |
| 380 | + private bool _isDiskSectionExpanded = true; |
| 381 | + public bool IsDiskSectionExpanded |
| 382 | + { |
| 383 | + get => _isDiskSectionExpanded; |
| 384 | + private set |
| 385 | + { |
| 386 | + if (_isDiskSectionExpanded == value) |
| 387 | + return; |
| 388 | + _isDiskSectionExpanded = value; |
| 389 | + this.RaisePropertyChanged(nameof(IsDiskSectionExpanded)); |
| 390 | + this.RaisePropertyChanged(nameof(DiskSectionHeaderText)); |
| 391 | + } |
| 392 | + } |
| 393 | + |
| 394 | + private bool _isLoadSaveSectionExpanded = false; |
| 395 | + public bool IsLoadSaveSectionExpanded |
| 396 | + { |
| 397 | + get => _isLoadSaveSectionExpanded; |
| 398 | + private set |
| 399 | + { |
| 400 | + if (_isLoadSaveSectionExpanded == value) |
| 401 | + return; |
| 402 | + _isLoadSaveSectionExpanded = value; |
| 403 | + this.RaisePropertyChanged(nameof(IsLoadSaveSectionExpanded)); |
| 404 | + this.RaisePropertyChanged(nameof(LoadSaveSectionHeaderText)); |
| 405 | + } |
| 406 | + } |
| 407 | + |
| 408 | + private bool _isConfigSectionExpanded = false; |
| 409 | + public bool IsConfigSectionExpanded |
| 410 | + { |
| 411 | + get => _isConfigSectionExpanded; |
| 412 | + private set |
| 413 | + { |
| 414 | + if (_isConfigSectionExpanded == value) |
| 415 | + return; |
| 416 | + _isConfigSectionExpanded = value; |
| 417 | + this.RaisePropertyChanged(nameof(IsConfigSectionExpanded)); |
| 418 | + this.RaisePropertyChanged(nameof(ConfigSectionHeaderText)); |
| 419 | + } |
| 420 | + } |
| 421 | + |
| 422 | + public string DiskSectionHeaderText => (IsDiskSectionExpanded ? "▼ " : "▶ ") + "Disk Drive & .D64 images"; |
| 423 | + public string LoadSaveSectionHeaderText => (IsLoadSaveSectionExpanded ? "▼ " : "▶ ") + "Load/Save"; |
| 424 | + public string ConfigSectionHeaderText => (IsConfigSectionExpanded ? "▼ " : "▶ ") + "Configuration"; |
| 425 | + |
| 426 | + private enum C64MenuSection { Disk, LoadSave, Config } |
| 427 | + |
| 428 | + private void ToggleSection(C64MenuSection section) |
| 429 | + { |
| 430 | + bool newState = section switch |
| 431 | + { |
| 432 | + C64MenuSection.Disk => !IsDiskSectionExpanded, |
| 433 | + C64MenuSection.LoadSave => !IsLoadSaveSectionExpanded, |
| 434 | + C64MenuSection.Config => !IsConfigSectionExpanded, |
| 435 | + _ => false, |
| 436 | + }; |
| 437 | + |
| 438 | + SetSectionExpanded(section, newState, collapseOthers: newState); |
| 439 | + } |
| 440 | + |
| 441 | + private void SetSectionExpanded(C64MenuSection section, bool expanded, bool collapseOthers) |
| 442 | + { |
| 443 | + switch (section) |
| 444 | + { |
| 445 | + case C64MenuSection.Disk: |
| 446 | + IsDiskSectionExpanded = expanded; |
| 447 | + if (collapseOthers && expanded) |
| 448 | + { |
| 449 | + IsLoadSaveSectionExpanded = false; |
| 450 | + IsConfigSectionExpanded = false; |
| 451 | + } |
| 452 | + break; |
| 453 | + case C64MenuSection.LoadSave: |
| 454 | + IsLoadSaveSectionExpanded = expanded; |
| 455 | + if (collapseOthers && expanded) |
| 456 | + { |
| 457 | + IsDiskSectionExpanded = false; |
| 458 | + IsConfigSectionExpanded = false; |
| 459 | + } |
| 460 | + break; |
| 461 | + case C64MenuSection.Config: |
| 462 | + IsConfigSectionExpanded = expanded; |
| 463 | + if (collapseOthers && expanded) |
| 464 | + { |
| 465 | + IsDiskSectionExpanded = false; |
| 466 | + IsLoadSaveSectionExpanded = false; |
| 467 | + } |
| 468 | + break; |
| 469 | + } |
| 470 | + } |
| 471 | + |
| 472 | + /// <summary> |
| 473 | + /// Called by the View when validation errors are present: collapse Disk/LoadSave and expand Config. |
| 474 | + /// </summary> |
| 475 | + public void ExpandConfigSectionOnValidationError() |
| 476 | + { |
| 477 | + IsDiskSectionExpanded = false; |
| 478 | + IsLoadSaveSectionExpanded = false; |
| 479 | + IsConfigSectionExpanded = true; |
| 480 | + } |
| 481 | + |
| 482 | + // --- ISystemMenuContributor --- |
| 483 | + public string MenuLabel => "C64"; |
| 484 | + |
| 485 | + public IReadOnlyList<NativeMenuItemBase> GetNativeMenuItems() |
| 486 | + { |
| 487 | + // On macOS, NativeMenu items appear in the OS-level system menu bar (not the app window), |
| 488 | + // which is the desired UX. The menu bar is also exposed via the macOS Accessibility API, |
| 489 | + // making shortcuts self-describing and discoverable by AI agents at runtime. |
| 490 | + // Use Meta+Alt (⌘⌥) as the primary modifier so hints show as "⌘⌥L" etc. |
| 491 | + const KeyModifiers macBase = KeyModifiers.Meta | KeyModifiers.Alt; |
| 492 | + //const KeyModifiers macBase = KeyModifiers.Alt; |
| 493 | + const KeyModifiers macShift = KeyModifiers.Meta | KeyModifiers.Alt | KeyModifiers.Shift; |
| 494 | + |
| 495 | + return new NativeMenuItemBase[] |
| 496 | + { |
| 497 | + BuildMenuItem("Toggle Disk Drive section", new KeyGesture(Key.D, macShift), ToggleDiskSectionCommand), |
| 498 | + BuildMenuItem("Toggle Load/Save section", new KeyGesture(Key.L, macBase), ToggleLoadSaveSectionCommand), |
| 499 | + BuildMenuItem("Toggle Configuration section", new KeyGesture(Key.C, macBase), ToggleConfigSectionCommand), |
| 500 | + new NativeMenuItemSeparator(), |
| 501 | + BuildMenuItem("Active joystick: Port 1", new KeyGesture(Key.D1, macBase), SetActiveJoystickCommand, 1), |
| 502 | + BuildMenuItem("Active joystick: Port 2", new KeyGesture(Key.D2, macBase), SetActiveJoystickCommand, 2), |
| 503 | + new NativeMenuItemSeparator(), |
| 504 | + BuildMenuItem("Toggle Joystick KB", new KeyGesture(Key.K, macBase), ToggleJoystickKeyboardCommand), |
| 505 | + BuildMenuItem("Keyboard joystick: Port 1", new KeyGesture(Key.D1, macShift), SetKeyboardJoystickCommand, 1), |
| 506 | + BuildMenuItem("Keyboard joystick: Port 2", new KeyGesture(Key.D2, macShift), SetKeyboardJoystickCommand, 2), |
| 507 | + }; |
| 508 | + } |
| 509 | + |
| 510 | + public IReadOnlyList<KeyBinding> GetKeyBindings() |
| 511 | + { |
| 512 | + // On Windows/Linux, NativeMenu would render as in-window chrome, which is not the desired |
| 513 | + // UX (on macOS it goes to the OS system menu bar, which is fine there). KeyBindings are |
| 514 | + // used instead: registered on the main Window, they fire regardless of which child has focus. |
| 515 | + // Ctrl+Alt combos are safe alongside the C64 emulator's own Ctrl+key color combinations |
| 516 | + // (those trigger on plain Ctrl, without Alt). |
| 517 | + const KeyModifiers nonMacBase = KeyModifiers.Control | KeyModifiers.Alt; |
| 518 | + const KeyModifiers nonMacShift = KeyModifiers.Control | KeyModifiers.Alt | KeyModifiers.Shift; |
| 519 | + |
| 520 | + return new[] |
| 521 | + { |
| 522 | + BuildKeyBinding(new KeyGesture(Key.D, nonMacShift), ToggleDiskSectionCommand), |
| 523 | + BuildKeyBinding(new KeyGesture(Key.L, nonMacBase), ToggleLoadSaveSectionCommand), |
| 524 | + BuildKeyBinding(new KeyGesture(Key.C, nonMacBase), ToggleConfigSectionCommand), |
| 525 | + BuildKeyBinding(new KeyGesture(Key.D1, nonMacBase), SetActiveJoystickCommand, 1), |
| 526 | + BuildKeyBinding(new KeyGesture(Key.D2, nonMacBase), SetActiveJoystickCommand, 2), |
| 527 | + BuildKeyBinding(new KeyGesture(Key.K, nonMacBase), ToggleJoystickKeyboardCommand), |
| 528 | + BuildKeyBinding(new KeyGesture(Key.D1, nonMacShift), SetKeyboardJoystickCommand, 1), |
| 529 | + BuildKeyBinding(new KeyGesture(Key.D2, nonMacShift), SetKeyboardJoystickCommand, 2), |
| 530 | + }; |
| 531 | + } |
| 532 | + |
| 533 | + private static NativeMenuItem BuildMenuItem(string header, KeyGesture gesture, System.Windows.Input.ICommand command, object? parameter = null) |
| 534 | + { |
| 535 | + var item = new NativeMenuItem |
| 536 | + { |
| 537 | + Header = header, |
| 538 | + Gesture = gesture, |
| 539 | + Command = command, |
| 540 | + }; |
| 541 | + if (parameter != null) |
| 542 | + item.CommandParameter = parameter; |
| 543 | + return item; |
| 544 | + } |
| 545 | + |
| 546 | + private static KeyBinding BuildKeyBinding(KeyGesture gesture, System.Windows.Input.ICommand command, object? parameter = null) |
| 547 | + { |
| 548 | + var binding = new KeyBinding |
| 549 | + { |
| 550 | + Gesture = gesture, |
| 551 | + Command = command, |
| 552 | + }; |
| 553 | + if (parameter != null) |
| 554 | + binding.CommandParameter = parameter; |
| 555 | + return binding; |
| 556 | + } |
| 557 | + |
335 | 558 | private void InitializeC64Data() |
336 | 559 | { |
337 | 560 | // Initialize joystick options |
|
0 commit comments