MainWindowViewModel.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 ExtendClientAreaChromeHints _chromeHints = ExtendClientAreaChromeHints.PreferSystemChrome;
  19. private bool _extendClientAreaEnabled;
  20. private bool _systemTitleBarEnabled;
  21. private bool _preferSystemChromeEnabled;
  22. private double _titleBarHeight;
  23. public MainWindowViewModel()
  24. {
  25. AboutCommand = MiniCommand.CreateFromTask(async () =>
  26. {
  27. var dialog = new AboutAvaloniaDialog();
  28. if ((App.Current?.ApplicationLifetime as IClassicDesktopStyleApplicationLifetime)?.MainWindow is { } mainWindow)
  29. {
  30. await dialog.ShowDialog(mainWindow);
  31. }
  32. });
  33. ExitCommand = MiniCommand.Create(() =>
  34. {
  35. (App.Current?.ApplicationLifetime as IClassicDesktopStyleApplicationLifetime)?.Shutdown();
  36. });
  37. ToggleMenuItemCheckedCommand = MiniCommand.Create(() =>
  38. {
  39. IsMenuItemChecked = !IsMenuItemChecked;
  40. });
  41. WindowState = WindowState.Normal;
  42. WindowStates = new WindowState[]
  43. {
  44. WindowState.Minimized,
  45. WindowState.Normal,
  46. WindowState.Maximized,
  47. WindowState.FullScreen,
  48. };
  49. this.WhenAnyValue(x => x.SystemTitleBarEnabled, x=>x.PreferSystemChromeEnabled)
  50. .Subscribe(x =>
  51. {
  52. var hints = ExtendClientAreaChromeHints.NoChrome | ExtendClientAreaChromeHints.OSXThickTitleBar;
  53. if(x.Item1)
  54. {
  55. hints |= ExtendClientAreaChromeHints.SystemChrome;
  56. }
  57. if(x.Item2)
  58. {
  59. hints |= ExtendClientAreaChromeHints.PreferSystemChrome;
  60. }
  61. ChromeHints = hints;
  62. });
  63. SystemTitleBarEnabled = true;
  64. TitleBarHeight = -1;
  65. }
  66. public ExtendClientAreaChromeHints ChromeHints
  67. {
  68. get { return _chromeHints; }
  69. set { this.RaiseAndSetIfChanged(ref _chromeHints, value); }
  70. }
  71. public bool ExtendClientAreaEnabled
  72. {
  73. get { return _extendClientAreaEnabled; }
  74. set { this.RaiseAndSetIfChanged(ref _extendClientAreaEnabled, value); }
  75. }
  76. public bool SystemTitleBarEnabled
  77. {
  78. get { return _systemTitleBarEnabled; }
  79. set { this.RaiseAndSetIfChanged(ref _systemTitleBarEnabled, value); }
  80. }
  81. public bool PreferSystemChromeEnabled
  82. {
  83. get { return _preferSystemChromeEnabled; }
  84. set { this.RaiseAndSetIfChanged(ref _preferSystemChromeEnabled, value); }
  85. }
  86. public double TitleBarHeight
  87. {
  88. get { return _titleBarHeight; }
  89. set { this.RaiseAndSetIfChanged(ref _titleBarHeight, value); }
  90. }
  91. public WindowState WindowState
  92. {
  93. get { return _windowState; }
  94. set { this.RaiseAndSetIfChanged(ref _windowState, value); }
  95. }
  96. public WindowState[] WindowStates
  97. {
  98. get { return _windowStates; }
  99. set { this.RaiseAndSetIfChanged(ref _windowStates, value); }
  100. }
  101. public bool IsMenuItemChecked
  102. {
  103. get { return _isMenuItemChecked; }
  104. set { this.RaiseAndSetIfChanged(ref _isMenuItemChecked, value); }
  105. }
  106. public MiniCommand AboutCommand { get; }
  107. public MiniCommand ExitCommand { get; }
  108. public MiniCommand ToggleMenuItemCheckedCommand { get; }
  109. private DateTime? _validatedDateExample;
  110. /// <summary>
  111. /// A required DateTime which should demonstrate validation for the DateTimePicker
  112. /// </summary>
  113. [Required]
  114. public DateTime? ValidatedDateExample
  115. {
  116. get => _validatedDateExample;
  117. set => this.RaiseAndSetIfChanged(ref _validatedDateExample, value);
  118. }
  119. }
  120. }