1
0

MainWindowViewModel.cs 4.9 KB

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