MainWindowViewModel.cs 6.1 KB

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