MainWindowViewModel.cs 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. using Avalonia;
  2. using Avalonia.Controls;
  3. using Avalonia.Controls.Primitives;
  4. using Avalonia.Media;
  5. using PicView.Avalonia.Functions;
  6. using PicView.Avalonia.UI;
  7. using PicView.Avalonia.WindowBehavior;
  8. using R3;
  9. namespace PicView.Avalonia.ViewModels;
  10. public class MainWindowViewModel : IDisposable
  11. {
  12. public BindableReactiveProperty<Brush?> ImageBackground { get; } = new();
  13. public BindableReactiveProperty<Brush?> ConstrainedImageBackground { get; } = new();
  14. public BindableReactiveProperty<Thickness> RightControlOffSetMargin { get; } = new();
  15. public BindableReactiveProperty<Thickness> TopScreenMargin { get; } = new();
  16. public BindableReactiveProperty<Thickness> BottomScreenMargin { get; } = new();
  17. public BindableReactiveProperty<CornerRadius> BottomCornerRadius { get; } = new();
  18. public BindableReactiveProperty<int> BackgroundChoice { get; } = new();
  19. public BindableReactiveProperty<double> WindowMinSize { get; } = new();
  20. public BindableReactiveProperty<double> TitlebarHeight { get; } = new();
  21. public BindableReactiveProperty<double> BottombarHeight { get; } = new();
  22. public BindableReactiveProperty<SizeToContent> SizeToContent { get; } = new();
  23. public BindableReactiveProperty<ScrollBarVisibility> ToggleScrollBarVisibility { get; } = new();
  24. public BindableReactiveProperty<bool> CanResize { get; } = new();
  25. public BindableReactiveProperty<UserControl?> CurrentView { get; } = new();
  26. public BindableReactiveProperty<bool> IsFileMenuVisible { get; } = new();
  27. public BindableReactiveProperty<bool> IsImageMenuVisible { get; } = new();
  28. public BindableReactiveProperty<bool> IsSettingsMenuVisible { get; } = new();
  29. public BindableReactiveProperty<bool> IsToolsMenuVisible { get; } = new();
  30. public BindableReactiveProperty<double> TitleMaxWidth { get; } = new();
  31. public BindableReactiveProperty<bool> IsFullscreen { get; } = new();
  32. public BindableReactiveProperty<bool> IsMaximized { get; } = new();
  33. public BindableReactiveProperty<bool> ShouldRestore { get; } = new();
  34. public BindableReactiveProperty<bool> ShouldMaximizeBeShown { get; } = new(true);
  35. public BindableReactiveProperty<bool> IsLoadingIndicatorShown { get; } = new();
  36. public BindableReactiveProperty<bool> IsUIShown { get; } = new();
  37. public BindableReactiveProperty<bool> IsTopToolbarShown { get; } = new();
  38. public BindableReactiveProperty<bool> IsBottomToolbarShown { get; } = new();
  39. public BindableReactiveProperty<bool> IsEditableTitlebarOpen { get; } = new();
  40. public BindableReactiveProperty<IImage?> ChangeCtrlZoomImage { get; } = new();
  41. public void LayoutButtonSubscription()
  42. {
  43. Observable.EveryValueChanged(this, x => x.IsMaximized.CurrentValue, UIHelper.GetFrameProvider)
  44. .Subscribe(_ => SetButtonValues());
  45. Observable.EveryValueChanged(this, x => x.IsFullscreen.CurrentValue, UIHelper.GetFrameProvider)
  46. .Subscribe(_ => SetButtonValues());
  47. }
  48. #region Menus
  49. public ReactiveCommand CloseMenuCommand { get; } = new(CloseMenus);
  50. public ReactiveCommand ToggleFileMenuCommand { get; } = new(ToggleFileMenu);
  51. public ReactiveCommand ToggleImageMenuCommand { get; } = new(ToggleImageMenu);
  52. public ReactiveCommand ToggleSettingsMenuCommand { get; } = new(ToggleSettingsMenu);
  53. public ReactiveCommand ToggleToolsMenuCommand { get; } = new(ToggleToolsMenu);
  54. private static void CloseMenus(Unit unit) => MenuManager.CloseMenus(UIHelper.GetMainView.DataContext as MainViewModel);
  55. private static void ToggleFileMenu(Unit unit) => MenuManager.ToggleFileMenu(UIHelper.GetMainView.DataContext as MainViewModel);
  56. private static void ToggleImageMenu(Unit unit) => MenuManager.ToggleImageMenu(UIHelper.GetMainView.DataContext as MainViewModel);
  57. private static void ToggleSettingsMenu(Unit unit) => MenuManager.ToggleSettingsMenu(UIHelper.GetMainView.DataContext as MainViewModel);
  58. private static void ToggleToolsMenu(Unit unit) => MenuManager.ToggleToolsMenu(UIHelper.GetMainView.DataContext as MainViewModel);
  59. #endregion Menus
  60. public ReactiveCommand ExitCommand { get; } = new(async (_, _) =>
  61. {
  62. await FunctionsMapper.Close();
  63. });
  64. public ReactiveCommand MaximizeCommand { get; } = new(async (_, _) =>
  65. {
  66. await FunctionsMapper.Maximize();
  67. });
  68. public ReactiveCommand MinimizeCommand { get; } = new(async (_, _) =>
  69. {
  70. await WindowFunctions.Minimize();
  71. });
  72. public ReactiveCommand RestoreCommand { get; } = new(async (_, _) =>
  73. {
  74. await FunctionsMapper.Restore();
  75. });
  76. public ReactiveCommand ToggleFullscreenCommand { get; } = new(async (_, _) =>
  77. {
  78. await FunctionsMapper.ToggleFullscreen();
  79. });
  80. private void SetButtonValues()
  81. {
  82. ShouldRestore.Value = IsFullscreen.CurrentValue || IsMaximized.CurrentValue;
  83. ShouldMaximizeBeShown.Value = !IsFullscreen.CurrentValue && !IsMaximized.CurrentValue;
  84. }
  85. public void Dispose()
  86. {
  87. Disposable.Dispose(ImageBackground,
  88. ConstrainedImageBackground,
  89. RightControlOffSetMargin,
  90. TopScreenMargin,
  91. BottomScreenMargin,
  92. BottomCornerRadius,
  93. BackgroundChoice,
  94. WindowMinSize,
  95. TitlebarHeight,
  96. BottombarHeight,
  97. SizeToContent,
  98. CanResize,
  99. CurrentView,
  100. TitleMaxWidth,
  101. IsLoadingIndicatorShown,
  102. IsUIShown,
  103. IsTopToolbarShown,
  104. IsBottomToolbarShown,
  105. IsEditableTitlebarOpen);
  106. }
  107. }