MainWindowViewModel.cs 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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. public MainWindowViewModel(IManagedNotificationManager notificationManager)
  18. {
  19. _notificationManager = notificationManager;
  20. ShowCustomManagedNotificationCommand = ReactiveCommand.Create(() =>
  21. {
  22. NotificationManager.Show(new NotificationViewModel(NotificationManager) { Title = "Hey There!", Message = "Did you know that Avalonia now supports Custom In-Window Notifications?" });
  23. });
  24. ShowManagedNotificationCommand = ReactiveCommand.Create(() =>
  25. {
  26. NotificationManager.Show(new Avalonia.Controls.Notifications.Notification("Welcome", "Avalonia now supports Notifications.", NotificationType.Information));
  27. });
  28. ShowNativeNotificationCommand = ReactiveCommand.Create(() =>
  29. {
  30. NotificationManager.Show(new Avalonia.Controls.Notifications.Notification("Error", "Native Notifications are not quite ready. Coming soon.", NotificationType.Error));
  31. });
  32. AboutCommand = ReactiveCommand.CreateFromTask(async () =>
  33. {
  34. var dialog = new AboutAvaloniaDialog();
  35. var mainWindow = (App.Current.ApplicationLifetime as IClassicDesktopStyleApplicationLifetime)?.MainWindow;
  36. await dialog.ShowDialog(mainWindow);
  37. });
  38. ExitCommand = ReactiveCommand.Create(() =>
  39. {
  40. (App.Current.ApplicationLifetime as IClassicDesktopStyleApplicationLifetime).Shutdown();
  41. });
  42. ToggleMenuItemCheckedCommand = ReactiveCommand.Create(() =>
  43. {
  44. IsMenuItemChecked = !IsMenuItemChecked;
  45. });
  46. WindowState = WindowState.Normal;
  47. WindowStates = new WindowState[]
  48. {
  49. WindowState.Minimized,
  50. WindowState.Normal,
  51. WindowState.Maximized,
  52. WindowState.FullScreen,
  53. };
  54. this.WhenAnyValue(x => x.SystemChromeButtonsEnabled, x=>x.ManagedChromeButtonsEnabled, x => x.SystemTitleBarEnabled, x=>x.PreferSystemChromeButtonsEnabled)
  55. .Subscribe(x =>
  56. {
  57. var hints = ExtendClientAreaChromeHints.NoChrome | ExtendClientAreaChromeHints.OSXThickTitleBar;
  58. if(x.Item1)
  59. {
  60. hints |= ExtendClientAreaChromeHints.SystemChromeButtons;
  61. }
  62. if(x.Item2)
  63. {
  64. hints |= ExtendClientAreaChromeHints.ManagedChromeButtons;
  65. }
  66. if(x.Item3)
  67. {
  68. hints |= ExtendClientAreaChromeHints.SystemTitleBar;
  69. }
  70. if(x.Item4)
  71. {
  72. hints |= ExtendClientAreaChromeHints.PreferSystemChromeButtons;
  73. }
  74. ChromeHints = hints;
  75. });
  76. SystemTitleBarEnabled = true;
  77. SystemChromeButtonsEnabled = true;
  78. TitleBarHeight = -1;
  79. }
  80. private int _transparencyLevel;
  81. public int TransparencyLevel
  82. {
  83. get { return _transparencyLevel; }
  84. set { this.RaiseAndSetIfChanged(ref _transparencyLevel, value); }
  85. }
  86. private ExtendClientAreaChromeHints _chromeHints;
  87. public ExtendClientAreaChromeHints ChromeHints
  88. {
  89. get { return _chromeHints; }
  90. set { this.RaiseAndSetIfChanged(ref _chromeHints, value); }
  91. }
  92. private bool _extendClientAreaEnabled;
  93. public bool ExtendClientAreaEnabled
  94. {
  95. get { return _extendClientAreaEnabled; }
  96. set { this.RaiseAndSetIfChanged(ref _extendClientAreaEnabled, value); }
  97. }
  98. private bool _systemTitleBarEnabled;
  99. public bool SystemTitleBarEnabled
  100. {
  101. get { return _systemTitleBarEnabled; }
  102. set { this.RaiseAndSetIfChanged(ref _systemTitleBarEnabled, value); }
  103. }
  104. private bool _systemChromeButtonsEnabled;
  105. public bool SystemChromeButtonsEnabled
  106. {
  107. get { return _systemChromeButtonsEnabled; }
  108. set { this.RaiseAndSetIfChanged(ref _systemChromeButtonsEnabled, value); }
  109. }
  110. private bool _managedChromeButtonsEnabled;
  111. public bool ManagedChromeButtonsEnabled
  112. {
  113. get { return _managedChromeButtonsEnabled; }
  114. set { this.RaiseAndSetIfChanged(ref _managedChromeButtonsEnabled, value); }
  115. }
  116. private bool _preferSystemChromeButtonsEnabled;
  117. public bool PreferSystemChromeButtonsEnabled
  118. {
  119. get { return _preferSystemChromeButtonsEnabled; }
  120. set { this.RaiseAndSetIfChanged(ref _preferSystemChromeButtonsEnabled, value); }
  121. }
  122. private double _titleBarHeight;
  123. public double TitleBarHeight
  124. {
  125. get { return _titleBarHeight; }
  126. set { this.RaiseAndSetIfChanged(ref _titleBarHeight, value); }
  127. }
  128. public WindowState WindowState
  129. {
  130. get { return _windowState; }
  131. set { this.RaiseAndSetIfChanged(ref _windowState, value); }
  132. }
  133. public WindowState[] WindowStates
  134. {
  135. get { return _windowStates; }
  136. set { this.RaiseAndSetIfChanged(ref _windowStates, value); }
  137. }
  138. public IManagedNotificationManager NotificationManager
  139. {
  140. get { return _notificationManager; }
  141. set { this.RaiseAndSetIfChanged(ref _notificationManager, value); }
  142. }
  143. public bool IsMenuItemChecked
  144. {
  145. get { return _isMenuItemChecked; }
  146. set { this.RaiseAndSetIfChanged(ref _isMenuItemChecked, value); }
  147. }
  148. public ReactiveCommand<Unit, Unit> ShowCustomManagedNotificationCommand { get; }
  149. public ReactiveCommand<Unit, Unit> ShowManagedNotificationCommand { get; }
  150. public ReactiveCommand<Unit, Unit> ShowNativeNotificationCommand { get; }
  151. public ReactiveCommand<Unit, Unit> AboutCommand { get; }
  152. public ReactiveCommand<Unit, Unit> ExitCommand { get; }
  153. public ReactiveCommand<Unit, Unit> ToggleMenuItemCheckedCommand { get; }
  154. }
  155. }