MainWindow.xaml.cs 2.9 KB

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