MainWindowViewModel.cs 6.2 KB

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