MainWindow.xaml.cs 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  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 ControlCatalog.ViewModels;
  9. namespace ControlCatalog
  10. {
  11. public class MainWindow : Window
  12. {
  13. private WindowNotificationManager _notificationArea;
  14. private NativeMenu _recentMenu;
  15. public MainWindow()
  16. {
  17. this.InitializeComponent();
  18. this.AttachDevTools();
  19. //Renderer.DrawFps = true;
  20. //Renderer.DrawDirtyRects = Renderer.DrawFps = true;
  21. _notificationArea = new WindowNotificationManager(this)
  22. {
  23. Position = NotificationPosition.TopRight,
  24. MaxItems = 3
  25. };
  26. DataContext = new MainWindowViewModel(_notificationArea);
  27. _recentMenu = ((NativeMenu.GetMenu(this).Items[0] as NativeMenuItem).Menu.Items[2] as NativeMenuItem).Menu;
  28. var mainMenu = this.FindControl<Menu>("MainMenu");
  29. mainMenu.AttachedToVisualTree += MenuAttached;
  30. }
  31. public static string MenuQuitHeader => RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? "Quit Avalonia" : "E_xit";
  32. public static KeyGesture MenuQuitGesture => RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ?
  33. new KeyGesture(Key.Q, KeyModifiers.Meta) :
  34. new KeyGesture(Key.F4, KeyModifiers.Alt);
  35. public void MenuAttached(object sender, VisualTreeAttachmentEventArgs e)
  36. {
  37. if (NativeMenu.GetIsNativeMenuExported(this) && sender is Menu mainMenu)
  38. {
  39. mainMenu.IsVisible = false;
  40. }
  41. }
  42. public void OnOpenClicked(object sender, EventArgs args)
  43. {
  44. _recentMenu.Items.Insert(0, new NativeMenuItem("Item " + (_recentMenu.Items.Count + 1)));
  45. }
  46. public void OnCloseClicked(object sender, EventArgs args)
  47. {
  48. Close();
  49. }
  50. private void InitializeComponent()
  51. {
  52. // TODO: iOS does not support dynamically loading assemblies
  53. // so we must refer to this resource DLL statically. For
  54. // now I am doing that here. But we need a better solution!!
  55. // Note, theme swiching probably will not work in runtime for iOS.
  56. if (Application.Current.Styles.Contains(App.FluentDark)
  57. || Application.Current.Styles.Contains(App.FluentLight))
  58. {
  59. var theme = new Avalonia.Themes.Fluent.FluentTheme();
  60. theme.TryGetResource("Button", out _);
  61. }
  62. else
  63. {
  64. var theme = new Avalonia.Themes.Default.DefaultTheme();
  65. theme.TryGetResource("Button", out _);
  66. }
  67. AvaloniaXamlLoader.Load(this);
  68. }
  69. }
  70. }