MainWindow.xaml.cs 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. using System;
  2. using System.Runtime.InteropServices;
  3. using Avalonia;
  4. using Avalonia.Controls;
  5. using Avalonia.Controls.Notifications;
  6. using Avalonia.Input;
  7. using Avalonia.Markup.Xaml;
  8. using Avalonia.Threading;
  9. using ControlCatalog.ViewModels;
  10. namespace ControlCatalog
  11. {
  12. public class MainWindow : Window
  13. {
  14. private WindowNotificationManager _notificationArea;
  15. private NativeMenu _recentMenu;
  16. private int seconds = 0;
  17. public MainWindow()
  18. {
  19. this.InitializeComponent();
  20. this.AttachDevTools();
  21. //Renderer.DrawFps = true;
  22. //Renderer.DrawDirtyRects = Renderer.DrawFps = true;
  23. _notificationArea = new WindowNotificationManager(this)
  24. {
  25. Position = NotificationPosition.TopRight,
  26. MaxItems = 3
  27. };
  28. DataContext = new MainWindowViewModel(_notificationArea);
  29. _recentMenu = ((NativeMenu.GetMenu(this).Items[0] as NativeMenuItem).Menu.Items[2] as NativeMenuItem).Menu;
  30. var timer = new DispatcherTimer();
  31. timer.Interval = TimeSpan.FromSeconds(1);
  32. timer.Tick += (sender, e) =>
  33. {
  34. ((NativeMenu.GetMenu(this).Items[0] as NativeMenuItem).Menu.Items[2] as NativeMenuItem).Header = $"Recent {seconds++}";
  35. };
  36. timer.Start();
  37. var mainMenu = this.FindControl<Menu>("MainMenu");
  38. mainMenu.AttachedToVisualTree += MenuAttached;
  39. }
  40. public static string MenuQuitHeader => RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? "Quit Avalonia" : "E_xit";
  41. public static KeyGesture MenuQuitGesture => RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ?
  42. new KeyGesture(Key.Q, KeyModifiers.Meta) :
  43. new KeyGesture(Key.F4, KeyModifiers.Alt);
  44. public void MenuAttached(object sender, VisualTreeAttachmentEventArgs e)
  45. {
  46. if (NativeMenu.GetIsNativeMenuExported(this) && sender is Menu mainMenu)
  47. {
  48. mainMenu.IsVisible = false;
  49. }
  50. }
  51. public void OnOpenClicked(object sender, EventArgs args)
  52. {
  53. _recentMenu.Items.Insert(0, new NativeMenuItem("Item " + (_recentMenu.Items.Count + 1)));
  54. }
  55. public void OnCloseClicked(object sender, EventArgs args)
  56. {
  57. Close();
  58. }
  59. private void InitializeComponent()
  60. {
  61. // TODO: iOS does not support dynamically loading assemblies
  62. // so we must refer to this resource DLL statically. For
  63. // now I am doing that here. But we need a better solution!!
  64. var theme = new Avalonia.Themes.Default.DefaultTheme();
  65. theme.TryGetResource("Button", out _);
  66. AvaloniaXamlLoader.Load(this);
  67. }
  68. }
  69. }