MainWindowViewModel.cs 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  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;
  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. var mainWindow = (App.Current.ApplicationLifetime as IClassicDesktopStyleApplicationLifetime)?.MainWindow;
  43. await dialog.ShowDialog(mainWindow);
  44. });
  45. ExitCommand = MiniCommand.Create(() =>
  46. {
  47. (App.Current.ApplicationLifetime as IClassicDesktopStyleApplicationLifetime).Shutdown();
  48. });
  49. ToggleMenuItemCheckedCommand = MiniCommand.Create(() =>
  50. {
  51. IsMenuItemChecked = !IsMenuItemChecked;
  52. });
  53. WindowState = WindowState.Normal;
  54. WindowStates = new WindowState[]
  55. {
  56. WindowState.Minimized,
  57. WindowState.Normal,
  58. WindowState.Maximized,
  59. WindowState.FullScreen,
  60. };
  61. this.WhenAnyValue(x => x.SystemTitleBarEnabled, x=>x.PreferSystemChromeEnabled)
  62. .Subscribe(x =>
  63. {
  64. var hints = ExtendClientAreaChromeHints.NoChrome | ExtendClientAreaChromeHints.OSXThickTitleBar;
  65. if(x.Item1)
  66. {
  67. hints |= ExtendClientAreaChromeHints.SystemChrome;
  68. }
  69. if(x.Item2)
  70. {
  71. hints |= ExtendClientAreaChromeHints.PreferSystemChrome;
  72. }
  73. ChromeHints = hints;
  74. });
  75. SystemTitleBarEnabled = true;
  76. TitleBarHeight = -1;
  77. }
  78. public int TransparencyLevel
  79. {
  80. get { return _transparencyLevel; }
  81. set { this.RaiseAndSetIfChanged(ref _transparencyLevel, value); }
  82. }
  83. public ExtendClientAreaChromeHints ChromeHints
  84. {
  85. get { return _chromeHints; }
  86. set { this.RaiseAndSetIfChanged(ref _chromeHints, value); }
  87. }
  88. public bool ExtendClientAreaEnabled
  89. {
  90. get { return _extendClientAreaEnabled; }
  91. set { this.RaiseAndSetIfChanged(ref _extendClientAreaEnabled, value); }
  92. }
  93. public bool SystemTitleBarEnabled
  94. {
  95. get { return _systemTitleBarEnabled; }
  96. set { this.RaiseAndSetIfChanged(ref _systemTitleBarEnabled, value); }
  97. }
  98. public bool PreferSystemChromeEnabled
  99. {
  100. get { return _preferSystemChromeEnabled; }
  101. set { this.RaiseAndSetIfChanged(ref _preferSystemChromeEnabled, value); }
  102. }
  103. public double TitleBarHeight
  104. {
  105. get { return _titleBarHeight; }
  106. set { this.RaiseAndSetIfChanged(ref _titleBarHeight, value); }
  107. }
  108. public WindowState WindowState
  109. {
  110. get { return _windowState; }
  111. set { this.RaiseAndSetIfChanged(ref _windowState, value); }
  112. }
  113. public WindowState[] WindowStates
  114. {
  115. get { return _windowStates; }
  116. set { this.RaiseAndSetIfChanged(ref _windowStates, value); }
  117. }
  118. public IManagedNotificationManager NotificationManager
  119. {
  120. get { return _notificationManager; }
  121. set { this.RaiseAndSetIfChanged(ref _notificationManager, value); }
  122. }
  123. public bool IsMenuItemChecked
  124. {
  125. get { return _isMenuItemChecked; }
  126. set { this.RaiseAndSetIfChanged(ref _isMenuItemChecked, value); }
  127. }
  128. public MiniCommand ShowCustomManagedNotificationCommand { get; }
  129. public MiniCommand ShowManagedNotificationCommand { get; }
  130. public MiniCommand ShowNativeNotificationCommand { get; }
  131. public MiniCommand AboutCommand { get; }
  132. public MiniCommand ExitCommand { get; }
  133. public MiniCommand ToggleMenuItemCheckedCommand { get; }
  134. private DateTime? _validatedDateExample;
  135. /// <summary>
  136. /// A required DateTime which should demonstrate validation for the DateTimePicker
  137. /// </summary>
  138. [Required]
  139. public DateTime? ValidatedDateExample
  140. {
  141. get => _validatedDateExample;
  142. set => this.RaiseAndSetIfChanged(ref _validatedDateExample, value);
  143. }
  144. }
  145. }