MainWindowViewModel.cs 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  1. using Avalonia.Controls;
  2. using Avalonia.Controls.ApplicationLifetimes;
  3. using Avalonia.Dialogs;
  4. using Avalonia.Platform;
  5. using System;
  6. using System.ComponentModel.DataAnnotations;
  7. using Avalonia;
  8. using MiniMvvm;
  9. namespace ControlCatalog.ViewModels
  10. {
  11. class MainWindowViewModel : ViewModelBase
  12. {
  13. private WindowState _windowState;
  14. private WindowState[] _windowStates = Array.Empty<WindowState>();
  15. private ExtendClientAreaChromeHints _chromeHints = ExtendClientAreaChromeHints.PreferSystemChrome;
  16. private bool _extendClientAreaEnabled;
  17. private bool _systemTitleBarEnabled;
  18. private bool _preferSystemChromeEnabled;
  19. private double _titleBarHeight;
  20. private bool _isSystemBarVisible;
  21. private bool _displayEdgeToEdge;
  22. private Thickness _safeAreaPadding;
  23. private bool _canResize;
  24. private bool _canMinimize;
  25. private bool _canMaximize;
  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. WindowState = WindowState.Normal;
  41. WindowStates = new WindowState[]
  42. {
  43. WindowState.Minimized,
  44. WindowState.Normal,
  45. WindowState.Maximized,
  46. WindowState.FullScreen,
  47. };
  48. PropertyChanged += (s, e) =>
  49. {
  50. if (e.PropertyName is nameof(SystemTitleBarEnabled) or nameof(PreferSystemChromeEnabled))
  51. {
  52. var hints = ExtendClientAreaChromeHints.NoChrome | ExtendClientAreaChromeHints.OSXThickTitleBar;
  53. if (SystemTitleBarEnabled)
  54. {
  55. hints |= ExtendClientAreaChromeHints.SystemChrome;
  56. }
  57. if (PreferSystemChromeEnabled)
  58. {
  59. hints |= ExtendClientAreaChromeHints.PreferSystemChrome;
  60. }
  61. ChromeHints = hints;
  62. }
  63. };
  64. SystemTitleBarEnabled = true;
  65. TitleBarHeight = -1;
  66. CanResize = true;
  67. CanMinimize = true;
  68. CanMaximize = true;
  69. }
  70. public ExtendClientAreaChromeHints ChromeHints
  71. {
  72. get { return _chromeHints; }
  73. set { RaiseAndSetIfChanged(ref _chromeHints, value); }
  74. }
  75. public bool ExtendClientAreaEnabled
  76. {
  77. get { return _extendClientAreaEnabled; }
  78. set
  79. {
  80. if (RaiseAndSetIfChanged(ref _extendClientAreaEnabled, value) && !value)
  81. {
  82. SystemTitleBarEnabled = true;
  83. }
  84. }
  85. }
  86. public bool SystemTitleBarEnabled
  87. {
  88. get { return _systemTitleBarEnabled; }
  89. set
  90. {
  91. if (RaiseAndSetIfChanged(ref _systemTitleBarEnabled, value) && !value)
  92. {
  93. TitleBarHeight = -1;
  94. }
  95. }
  96. }
  97. public bool PreferSystemChromeEnabled
  98. {
  99. get { return _preferSystemChromeEnabled; }
  100. set { RaiseAndSetIfChanged(ref _preferSystemChromeEnabled, value); }
  101. }
  102. public double TitleBarHeight
  103. {
  104. get { return _titleBarHeight; }
  105. set { RaiseAndSetIfChanged(ref _titleBarHeight, value); }
  106. }
  107. public WindowState WindowState
  108. {
  109. get { return _windowState; }
  110. set { RaiseAndSetIfChanged(ref _windowState, value); }
  111. }
  112. public WindowState[] WindowStates
  113. {
  114. get { return _windowStates; }
  115. set { RaiseAndSetIfChanged(ref _windowStates, value); }
  116. }
  117. public bool IsSystemBarVisible
  118. {
  119. get { return _isSystemBarVisible; }
  120. set { RaiseAndSetIfChanged(ref _isSystemBarVisible, value); }
  121. }
  122. public bool DisplayEdgeToEdge
  123. {
  124. get { return _displayEdgeToEdge; }
  125. set { RaiseAndSetIfChanged(ref _displayEdgeToEdge, value); }
  126. }
  127. public Thickness SafeAreaPadding
  128. {
  129. get { return _safeAreaPadding; }
  130. set { RaiseAndSetIfChanged(ref _safeAreaPadding, value); }
  131. }
  132. public bool CanResize
  133. {
  134. get { return _canResize; }
  135. set { RaiseAndSetIfChanged(ref _canResize, value); }
  136. }
  137. public bool CanMinimize
  138. {
  139. get { return _canMinimize; }
  140. set { RaiseAndSetIfChanged(ref _canMinimize, value); }
  141. }
  142. public bool CanMaximize
  143. {
  144. get { return _canMaximize; }
  145. set { RaiseAndSetIfChanged(ref _canMaximize, value); }
  146. }
  147. public MiniCommand AboutCommand { get; }
  148. public MiniCommand ExitCommand { get; }
  149. private DateTime? _validatedDateExample;
  150. /// <summary>
  151. /// A required DateTime which should demonstrate validation for the DateTimePicker
  152. /// </summary>
  153. [Required]
  154. public DateTime? ValidatedDateExample
  155. {
  156. get => _validatedDateExample;
  157. set => RaiseAndSetIfChanged(ref _validatedDateExample, value);
  158. }
  159. }
  160. }