MainWindowViewModel.cs 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. using Avalonia.Controls;
  2. using Avalonia.Controls.ApplicationLifetimes;
  3. using Avalonia.Controls.Notifications;
  4. using Avalonia.Dialogs;
  5. using Avalonia.Platform;
  6. using Avalonia.Reactive;
  7. using System;
  8. using System.ComponentModel.DataAnnotations;
  9. using Avalonia;
  10. using MiniMvvm;
  11. namespace ControlCatalog.ViewModels
  12. {
  13. class MainWindowViewModel : ViewModelBase
  14. {
  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. private bool _isSystemBarVisible;
  24. private bool _displayEdgeToEdge;
  25. private Thickness _safeAreaPadding;
  26. public MainWindowViewModel()
  27. {
  28. AboutCommand = MiniCommand.CreateFromTask(async () =>
  29. {
  30. var dialog = new AboutAvaloniaDialog();
  31. if ((App.Current?.ApplicationLifetime as IClassicDesktopStyleApplicationLifetime)?.MainWindow is { } mainWindow)
  32. {
  33. await dialog.ShowDialog(mainWindow);
  34. }
  35. });
  36. ExitCommand = MiniCommand.Create(() =>
  37. {
  38. (App.Current?.ApplicationLifetime as IClassicDesktopStyleApplicationLifetime)?.Shutdown();
  39. });
  40. ToggleMenuItemCheckedCommand = MiniCommand.Create(() =>
  41. {
  42. IsMenuItemChecked = !IsMenuItemChecked;
  43. });
  44. WindowState = WindowState.Normal;
  45. WindowStates = new WindowState[]
  46. {
  47. WindowState.Minimized,
  48. WindowState.Normal,
  49. WindowState.Maximized,
  50. WindowState.FullScreen,
  51. };
  52. this.WhenAnyValue(x => x.SystemTitleBarEnabled, x=>x.PreferSystemChromeEnabled)
  53. .Subscribe(x =>
  54. {
  55. var hints = ExtendClientAreaChromeHints.NoChrome | ExtendClientAreaChromeHints.OSXThickTitleBar;
  56. if(x.Item1)
  57. {
  58. hints |= ExtendClientAreaChromeHints.SystemChrome;
  59. }
  60. if(x.Item2)
  61. {
  62. hints |= ExtendClientAreaChromeHints.PreferSystemChrome;
  63. }
  64. ChromeHints = hints;
  65. });
  66. SystemTitleBarEnabled = true;
  67. TitleBarHeight = -1;
  68. }
  69. public ExtendClientAreaChromeHints ChromeHints
  70. {
  71. get { return _chromeHints; }
  72. set { this.RaiseAndSetIfChanged(ref _chromeHints, value); }
  73. }
  74. public bool ExtendClientAreaEnabled
  75. {
  76. get { return _extendClientAreaEnabled; }
  77. set { this.RaiseAndSetIfChanged(ref _extendClientAreaEnabled, value); }
  78. }
  79. public bool SystemTitleBarEnabled
  80. {
  81. get { return _systemTitleBarEnabled; }
  82. set { this.RaiseAndSetIfChanged(ref _systemTitleBarEnabled, value); }
  83. }
  84. public bool PreferSystemChromeEnabled
  85. {
  86. get { return _preferSystemChromeEnabled; }
  87. set { this.RaiseAndSetIfChanged(ref _preferSystemChromeEnabled, value); }
  88. }
  89. public double TitleBarHeight
  90. {
  91. get { return _titleBarHeight; }
  92. set { this.RaiseAndSetIfChanged(ref _titleBarHeight, value); }
  93. }
  94. public WindowState WindowState
  95. {
  96. get { return _windowState; }
  97. set { this.RaiseAndSetIfChanged(ref _windowState, value); }
  98. }
  99. public WindowState[] WindowStates
  100. {
  101. get { return _windowStates; }
  102. set { this.RaiseAndSetIfChanged(ref _windowStates, value); }
  103. }
  104. public bool IsMenuItemChecked
  105. {
  106. get { return _isMenuItemChecked; }
  107. set { this.RaiseAndSetIfChanged(ref _isMenuItemChecked, value); }
  108. }
  109. public bool IsSystemBarVisible
  110. {
  111. get { return _isSystemBarVisible; }
  112. set { this.RaiseAndSetIfChanged(ref _isSystemBarVisible, value); }
  113. }
  114. public bool DisplayEdgeToEdge
  115. {
  116. get { return _displayEdgeToEdge; }
  117. set { this.RaiseAndSetIfChanged(ref _displayEdgeToEdge, value); }
  118. }
  119. public Thickness SafeAreaPadding
  120. {
  121. get { return _safeAreaPadding; }
  122. set { this.RaiseAndSetIfChanged(ref _safeAreaPadding, value); }
  123. }
  124. public MiniCommand AboutCommand { get; }
  125. public MiniCommand ExitCommand { get; }
  126. public MiniCommand ToggleMenuItemCheckedCommand { get; }
  127. private DateTime? _validatedDateExample;
  128. /// <summary>
  129. /// A required DateTime which should demonstrate validation for the DateTimePicker
  130. /// </summary>
  131. [Required]
  132. public DateTime? ValidatedDateExample
  133. {
  134. get => _validatedDateExample;
  135. set => this.RaiseAndSetIfChanged(ref _validatedDateExample, value);
  136. }
  137. }
  138. }