MainWindowViewModel.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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)
  55. .Subscribe(x =>
  56. {
  57. ChromeHints = ExtendClientAreaChromeHints.NoChrome | ExtendClientAreaChromeHints.OSXThickTitleBar;
  58. if(x.Item1)
  59. {
  60. ChromeHints |= ExtendClientAreaChromeHints.SystemChromeButtons;
  61. }
  62. if(x.Item2)
  63. {
  64. ChromeHints |= ExtendClientAreaChromeHints.ManagedChromeButtons;
  65. }
  66. if(x.Item3)
  67. {
  68. ChromeHints |= ExtendClientAreaChromeHints.SystemTitleBar;
  69. }
  70. });
  71. SystemTitleBarEnabled = true;
  72. SystemChromeButtonsEnabled = true;
  73. TitleBarHeight = -1;
  74. }
  75. private int _transparencyLevel;
  76. public int TransparencyLevel
  77. {
  78. get { return _transparencyLevel; }
  79. set { this.RaiseAndSetIfChanged(ref _transparencyLevel, value); }
  80. }
  81. private ExtendClientAreaChromeHints _chromeHints;
  82. public ExtendClientAreaChromeHints ChromeHints
  83. {
  84. get { return _chromeHints; }
  85. set { this.RaiseAndSetIfChanged(ref _chromeHints, value); }
  86. }
  87. private bool _extendClientAreaEnabled;
  88. public bool ExtendClientAreaEnabled
  89. {
  90. get { return _extendClientAreaEnabled; }
  91. set { this.RaiseAndSetIfChanged(ref _extendClientAreaEnabled, value); }
  92. }
  93. private bool _systemTitleBarEnabled;
  94. public bool SystemTitleBarEnabled
  95. {
  96. get { return _systemTitleBarEnabled; }
  97. set { this.RaiseAndSetIfChanged(ref _systemTitleBarEnabled, value); }
  98. }
  99. private bool _systemChromeButtonsEnabled;
  100. public bool SystemChromeButtonsEnabled
  101. {
  102. get { return _systemChromeButtonsEnabled; }
  103. set { this.RaiseAndSetIfChanged(ref _systemChromeButtonsEnabled, value); }
  104. }
  105. private bool _managedChromeButtonsEnabled;
  106. public bool ManagedChromeButtonsEnabled
  107. {
  108. get { return _managedChromeButtonsEnabled; }
  109. set { this.RaiseAndSetIfChanged(ref _managedChromeButtonsEnabled, value); }
  110. }
  111. private double _titleBarHeight;
  112. public double TitleBarHeight
  113. {
  114. get { return _titleBarHeight; }
  115. set { this.RaiseAndSetIfChanged(ref _titleBarHeight, value); }
  116. }
  117. public WindowState WindowState
  118. {
  119. get { return _windowState; }
  120. set { this.RaiseAndSetIfChanged(ref _windowState, value); }
  121. }
  122. public WindowState[] WindowStates
  123. {
  124. get { return _windowStates; }
  125. set { this.RaiseAndSetIfChanged(ref _windowStates, value); }
  126. }
  127. public IManagedNotificationManager NotificationManager
  128. {
  129. get { return _notificationManager; }
  130. set { this.RaiseAndSetIfChanged(ref _notificationManager, value); }
  131. }
  132. public bool IsMenuItemChecked
  133. {
  134. get { return _isMenuItemChecked; }
  135. set { this.RaiseAndSetIfChanged(ref _isMenuItemChecked, value); }
  136. }
  137. public ReactiveCommand<Unit, Unit> ShowCustomManagedNotificationCommand { get; }
  138. public ReactiveCommand<Unit, Unit> ShowManagedNotificationCommand { get; }
  139. public ReactiveCommand<Unit, Unit> ShowNativeNotificationCommand { get; }
  140. public ReactiveCommand<Unit, Unit> AboutCommand { get; }
  141. public ReactiveCommand<Unit, Unit> ExitCommand { get; }
  142. public ReactiveCommand<Unit, Unit> ToggleMenuItemCheckedCommand { get; }
  143. }
  144. }