MainWindow.xaml.cs 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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. StartupScreenIndex = 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. ExtendClientAreaChromeHints = Avalonia.Platform.ExtendClientAreaChromeHints.OSXThickTitleBar;
  34. }
  35. public static string MenuQuitHeader => RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? "Quit Avalonia" : "E_xit";
  36. public static KeyGesture MenuQuitGesture => RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ?
  37. new KeyGesture(Key.Q, KeyModifiers.Meta) :
  38. new KeyGesture(Key.F4, KeyModifiers.Alt);
  39. public void MenuAttached(object sender, VisualTreeAttachmentEventArgs e)
  40. {
  41. if (NativeMenu.GetIsNativeMenuExported(this) && sender is Menu mainMenu)
  42. {
  43. mainMenu.IsVisible = false;
  44. }
  45. }
  46. public void OnOpenClicked(object sender, EventArgs args)
  47. {
  48. _recentMenu.Items.Insert(0, new NativeMenuItem("Item " + (_recentMenu.Items.Count + 1)));
  49. }
  50. public void OnCloseClicked(object sender, EventArgs args)
  51. {
  52. Close();
  53. }
  54. private void InitializeComponent()
  55. {
  56. // TODO: iOS does not support dynamically loading assemblies
  57. // so we must refer to this resource DLL statically. For
  58. // now I am doing that here. But we need a better solution!!
  59. // Note, theme swiching probably will not work in runtime for iOS.
  60. if (Application.Current.Styles.Contains(App.FluentDark)
  61. || Application.Current.Styles.Contains(App.FluentLight))
  62. {
  63. var theme = new Avalonia.Themes.Fluent.Controls.FluentControls();
  64. theme.TryGetResource("Button", out _);
  65. }
  66. else
  67. {
  68. var theme = new Avalonia.Themes.Default.DefaultTheme();
  69. theme.TryGetResource("Button", out _);
  70. }
  71. AvaloniaXamlLoader.Load(this);
  72. }
  73. }
  74. }