MainWindowViewModel.cs 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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 MiniMvvm;
  10. namespace ControlCatalog.ViewModels
  11. {
  12. class MainWindowViewModel : ViewModelBase
  13. {
  14. private bool _isMenuItemChecked = true;
  15. private WindowState _windowState;
  16. private WindowState[] _windowStates = Array.Empty<WindowState>();
  17. private ExtendClientAreaChromeHints _chromeHints = ExtendClientAreaChromeHints.PreferSystemChrome;
  18. private bool _extendClientAreaEnabled;
  19. private bool _systemTitleBarEnabled;
  20. private bool _preferSystemChromeEnabled;
  21. private double _titleBarHeight;
  22. public MainWindowViewModel()
  23. {
  24. AboutCommand = MiniCommand.CreateFromTask(async () =>
  25. {
  26. var dialog = new AboutAvaloniaDialog();
  27. if ((App.Current?.ApplicationLifetime as IClassicDesktopStyleApplicationLifetime)?.MainWindow is { } mainWindow)
  28. {
  29. await dialog.ShowDialog(mainWindow);
  30. }
  31. });
  32. ExitCommand = MiniCommand.Create(() =>
  33. {
  34. (App.Current?.ApplicationLifetime as IClassicDesktopStyleApplicationLifetime)?.Shutdown();
  35. });
  36. ToggleMenuItemCheckedCommand = MiniCommand.Create(() =>
  37. {
  38. IsMenuItemChecked = !IsMenuItemChecked;
  39. });
  40. WindowState = WindowState.Normal;
  41. WindowStates = new WindowState[]
  42. {
  43. WindowState.Minimized,
  44. WindowState.Normal,
  45. WindowState.Maximized,
  46. WindowState.FullScreen,
  47. };
  48. this.WhenAnyValue(x => x.SystemTitleBarEnabled, x=>x.PreferSystemChromeEnabled)
  49. .Subscribe(x =>
  50. {
  51. var hints = ExtendClientAreaChromeHints.NoChrome | ExtendClientAreaChromeHints.OSXThickTitleBar;
  52. if(x.Item1)
  53. {
  54. hints |= ExtendClientAreaChromeHints.SystemChrome;
  55. }
  56. if(x.Item2)
  57. {
  58. hints |= ExtendClientAreaChromeHints.PreferSystemChrome;
  59. }
  60. ChromeHints = hints;
  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 IsMenuItemChecked
  101. {
  102. get { return _isMenuItemChecked; }
  103. set { this.RaiseAndSetIfChanged(ref _isMenuItemChecked, value); }
  104. }
  105. public MiniCommand AboutCommand { get; }
  106. public MiniCommand ExitCommand { get; }
  107. public MiniCommand ToggleMenuItemCheckedCommand { get; }
  108. private DateTime? _validatedDateExample;
  109. /// <summary>
  110. /// A required DateTime which should demonstrate validation for the DateTimePicker
  111. /// </summary>
  112. [Required]
  113. public DateTime? ValidatedDateExample
  114. {
  115. get => _validatedDateExample;
  116. set => this.RaiseAndSetIfChanged(ref _validatedDateExample, value);
  117. }
  118. }
  119. }