MainWindowViewModel.cs 4.9 KB

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