MainWindowViewModel.cs 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. using System.Reactive;
  2. using Avalonia.Controls;
  3. using Avalonia.Controls.ApplicationLifetimes;
  4. using Avalonia.Controls.Notifications;
  5. using Avalonia.Dialogs;
  6. using Avalonia.Platform;
  7. using System;
  8. using ReactiveUI;
  9. namespace ControlCatalog.ViewModels
  10. {
  11. class MainWindowViewModel : ReactiveObject
  12. {
  13. private IManagedNotificationManager _notificationManager;
  14. private bool _isMenuItemChecked = true;
  15. private WindowState _windowState;
  16. private WindowState[] _windowStates;
  17. private int _transparencyLevel;
  18. private ExtendClientAreaChromeHints _chromeHints;
  19. private bool _extendClientAreaEnabled;
  20. private bool _systemTitleBarEnabled;
  21. private bool _preferSystemChromeEnabled;
  22. private double _titleBarHeight;
  23. public MainWindowViewModel(IManagedNotificationManager notificationManager)
  24. {
  25. _notificationManager = notificationManager;
  26. ShowCustomManagedNotificationCommand = ReactiveCommand.Create(() =>
  27. {
  28. NotificationManager.Show(new NotificationViewModel(NotificationManager) { Title = "Hey There!", Message = "Did you know that Avalonia now supports Custom In-Window Notifications?" });
  29. });
  30. ShowManagedNotificationCommand = ReactiveCommand.Create(() =>
  31. {
  32. NotificationManager.Show(new Avalonia.Controls.Notifications.Notification("Welcome", "Avalonia now supports Notifications.", NotificationType.Information));
  33. });
  34. ShowNativeNotificationCommand = ReactiveCommand.Create(() =>
  35. {
  36. NotificationManager.Show(new Avalonia.Controls.Notifications.Notification("Error", "Native Notifications are not quite ready. Coming soon.", NotificationType.Error));
  37. });
  38. AboutCommand = ReactiveCommand.CreateFromTask(async () =>
  39. {
  40. var dialog = new AboutAvaloniaDialog();
  41. var mainWindow = (App.Current.ApplicationLifetime as IClassicDesktopStyleApplicationLifetime)?.MainWindow;
  42. await dialog.ShowDialog(mainWindow);
  43. });
  44. ExitCommand = ReactiveCommand.Create(() =>
  45. {
  46. (App.Current.ApplicationLifetime as IClassicDesktopStyleApplicationLifetime).Shutdown();
  47. });
  48. ToggleMenuItemCheckedCommand = ReactiveCommand.Create(() =>
  49. {
  50. IsMenuItemChecked = !IsMenuItemChecked;
  51. });
  52. WindowState = WindowState.Normal;
  53. WindowStates = new WindowState[]
  54. {
  55. WindowState.Minimized,
  56. WindowState.Normal,
  57. WindowState.Maximized,
  58. WindowState.FullScreen,
  59. };
  60. this.WhenAnyValue(x => x.SystemTitleBarEnabled, x=>x.PreferSystemChromeEnabled)
  61. .Subscribe(x =>
  62. {
  63. var hints = ExtendClientAreaChromeHints.NoChrome | ExtendClientAreaChromeHints.OSXThickTitleBar;
  64. if(x.Item1)
  65. {
  66. hints |= ExtendClientAreaChromeHints.SystemChrome;
  67. }
  68. if(x.Item2)
  69. {
  70. hints |= ExtendClientAreaChromeHints.PreferSystemChrome;
  71. }
  72. ChromeHints = hints;
  73. });
  74. SystemTitleBarEnabled = true;
  75. TitleBarHeight = -1;
  76. }
  77. public int TransparencyLevel
  78. {
  79. get { return _transparencyLevel; }
  80. set { this.RaiseAndSetIfChanged(ref _transparencyLevel, value); }
  81. }
  82. public ExtendClientAreaChromeHints ChromeHints
  83. {
  84. get { return _chromeHints; }
  85. set { this.RaiseAndSetIfChanged(ref _chromeHints, value); }
  86. }
  87. public bool ExtendClientAreaEnabled
  88. {
  89. get { return _extendClientAreaEnabled; }
  90. set { this.RaiseAndSetIfChanged(ref _extendClientAreaEnabled, value); }
  91. }
  92. public bool SystemTitleBarEnabled
  93. {
  94. get { return _systemTitleBarEnabled; }
  95. set { this.RaiseAndSetIfChanged(ref _systemTitleBarEnabled, value); }
  96. }
  97. public bool PreferSystemChromeEnabled
  98. {
  99. get { return _preferSystemChromeEnabled; }
  100. set { this.RaiseAndSetIfChanged(ref _preferSystemChromeEnabled, value); }
  101. }
  102. public double TitleBarHeight
  103. {
  104. get { return _titleBarHeight; }
  105. set { this.RaiseAndSetIfChanged(ref _titleBarHeight, value); }
  106. }
  107. public WindowState WindowState
  108. {
  109. get { return _windowState; }
  110. set { this.RaiseAndSetIfChanged(ref _windowState, value); }
  111. }
  112. public WindowState[] WindowStates
  113. {
  114. get { return _windowStates; }
  115. set { this.RaiseAndSetIfChanged(ref _windowStates, value); }
  116. }
  117. public IManagedNotificationManager NotificationManager
  118. {
  119. get { return _notificationManager; }
  120. set { this.RaiseAndSetIfChanged(ref _notificationManager, value); }
  121. }
  122. public bool IsMenuItemChecked
  123. {
  124. get { return _isMenuItemChecked; }
  125. set { this.RaiseAndSetIfChanged(ref _isMenuItemChecked, value); }
  126. }
  127. public ReactiveCommand<Unit, Unit> ShowCustomManagedNotificationCommand { get; }
  128. public ReactiveCommand<Unit, Unit> ShowManagedNotificationCommand { get; }
  129. public ReactiveCommand<Unit, Unit> ShowNativeNotificationCommand { get; }
  130. public ReactiveCommand<Unit, Unit> AboutCommand { get; }
  131. public ReactiveCommand<Unit, Unit> ExitCommand { get; }
  132. public ReactiveCommand<Unit, Unit> ToggleMenuItemCheckedCommand { get; }
  133. }
  134. }