浏览代码

Refactor: migrate menu and window-related commands to `MainWindowViewModel`. Update bindings across views, remove redundant commands from `MainViewModel`, and streamline logic for better organization and maintainability.

Ruben 3 月之前
父节点
当前提交
1be7e04d50

+ 1 - 1
src/PicView.Avalonia.MacOS/Views/MacMainWindow.axaml

@@ -324,7 +324,7 @@
                 <NativeMenu>
                     <NativeMenuItem Command="{CompiledBinding NewWindowCommand}" Header="{CompiledBinding Translation.NewWindow.Value, Mode=OneWay, Mode=OneWay}" />
                     <NativeMenuItemSeparator />
-                    <NativeMenuItem Command="{CompiledBinding MaximizeCommand}" Header="{CompiledBinding Translation.Maximize.Value, Mode=OneWay}" />
+                    <NativeMenuItem Command="{CompiledBinding MainWindow.MaximizeCommand}" Header="{CompiledBinding Translation.Maximize.Value, Mode=OneWay}" />
                     <NativeMenuItem Command="{CompiledBinding ToggleFullscreenCommand}" Header="{CompiledBinding Translation.Fullscreen.Value, Mode=OneWay}" />
                 </NativeMenu>
             </NativeMenuItem>

+ 2 - 2
src/PicView.Avalonia.Win32/Views/WinTitleBar.axaml

@@ -146,7 +146,7 @@
                 BorderBrush="{DynamicResource MainBorderColor}"
                 Classes="hover"
                 ClickMode="Release"
-                Command="{CompiledBinding ExitCommand}"
+                Command="{CompiledBinding MainWindow.ExitCommand}"
                 Data="{StaticResource CloseGeometry}"
                 DockPanel.Dock="Right"
                 Foreground="{DynamicResource MainTextColor}"
@@ -197,7 +197,7 @@
                 BorderThickness="1,0,1,0"
                 Classes="hover "
                 ClickMode="Release"
-                Command="{CompiledBinding MinimizeCommand}"
+                Command="{CompiledBinding MainWindow.MinimizeCommand}"
                 Data="{StaticResource MinimizeGeometry}"
                 DockPanel.Dock="Right"
                 Foreground="{DynamicResource MainTextColor}"

+ 1 - 1
src/PicView.Avalonia/Navigation/FileListManager.cs

@@ -70,7 +70,7 @@ public static class FileListManager
                 }
 
                 var index = files.FindIndex(info => info.FullName.Equals(vm.PicViewer.FileInfo.CurrentValue.FullName));
-                NavigationManager.UpdateFileListAndIndex(files, index);;
+                NavigationManager.UpdateFileListAndIndex(files, index);
                 TitleManager.SetTitle(vm);
                 return true;
             }

+ 6 - 7
src/PicView.Avalonia/ViewModels/FileSortingViewModel.cs

@@ -16,36 +16,35 @@ public class FileSortingViewModel : IDisposable
         await FunctionsMapper.SortFilesByCreationTime();
     });
     
-    public ReactiveCommand<string> SortFilesByLastAccessTimeCommand { get; } = new(async (_, _) =>
+    public ReactiveCommand SortFilesByLastAccessTimeCommand { get; } = new(async (_, _) =>
     {
         await FunctionsMapper.SortFilesByLastAccessTime();
     });
     
-    public ReactiveCommand<string> SortFilesBySizeCommand { get; } = new(async (_, _) =>
+    public ReactiveCommand SortFilesBySizeCommand { get; } = new(async (_, _) =>
     {
         await FunctionsMapper.SortFilesBySize();
     });
     
-    public ReactiveCommand<string> SortFilesByExtensionCommand { get; } = new(async (_, _) =>
+    public ReactiveCommand SortFilesByExtensionCommand { get; } = new(async (_, _) =>
     {
         await FunctionsMapper.SortFilesByExtension();
     });
     
-    public ReactiveCommand<string> SortFilesRandomlyCommand { get; } = new(async (path, _) =>
+    public ReactiveCommand SortFilesRandomlyCommand { get; } = new(async (_, _) =>
     {
         await FunctionsMapper.SortFilesRandomly();
     });
     
-    public ReactiveCommand<string> SortFilesAscendingCommand { get; } = new(async (path, _) =>
+    public ReactiveCommand SortFilesAscendingCommand { get; } = new(async (_, _) =>
     {
         await FunctionsMapper.SortFilesAscending();
     });
     
-    public ReactiveCommand<string> SortFilesDescendingCommand { get; } = new(async (path, _) =>
+    public ReactiveCommand SortFilesDescendingCommand { get; } = new(async (_, _) =>
     {
         await FunctionsMapper.SortFilesDescending();
     });
-    
 
     public BindableReactiveProperty<SortFilesBy> SortOrder { get; } = new();
     

+ 0 - 25
src/PicView.Avalonia/ViewModels/MainViewModel.cs

@@ -5,7 +5,6 @@ using PicView.Avalonia.Functions;
 using PicView.Avalonia.ImageTransformations.Rotation;
 using PicView.Avalonia.Interfaces;
 using PicView.Avalonia.UI;
-using PicView.Avalonia.WindowBehavior;
 using PicView.Core.ProcessHandling;
 using PicView.Core.ViewModels;
 using ReactiveUI;
@@ -39,9 +38,6 @@ public class MainViewModel : ReactiveObject
 
         #region Window commands
 
-        ExitCommand = FunctionsHelper.CreateReactiveCommand(WindowFunctions.Close);
-        MinimizeCommand = FunctionsHelper.CreateReactiveCommand(WindowFunctions.Minimize);
-        MaximizeCommand = FunctionsHelper.CreateReactiveCommand(FunctionsMapper.Maximize);
         RestoreCommand = FunctionsHelper.CreateReactiveCommand(FunctionsMapper.Restore);
         ToggleFullscreenCommand = FunctionsHelper.CreateReactiveCommand(FunctionsMapper.ToggleFullscreen);
         NewWindowCommand = FunctionsHelper.CreateReactiveCommand(ProcessHelper.StartNewProcess);
@@ -57,19 +53,7 @@ public class MainViewModel : ReactiveObject
 
         #endregion Window commands
 
-        #region Menus
 
-        CloseMenuCommand = FunctionsHelper.CreateReactiveCommand(FunctionsMapper.CloseMenus);
-
-        ToggleFileMenuCommand = FunctionsHelper.CreateReactiveCommand(FunctionsMapper.ToggleFileMenu);
-
-        ToggleImageMenuCommand = FunctionsHelper.CreateReactiveCommand(FunctionsMapper.ToggleImageMenu);
-
-        ToggleSettingsMenuCommand = FunctionsHelper.CreateReactiveCommand(FunctionsMapper.ToggleSettingsMenu);
-
-        ToggleToolsMenuCommand = FunctionsHelper.CreateReactiveCommand(FunctionsMapper.ToggleToolsMenu);
-
-        #endregion Menus
 
         #region Image commands
 
@@ -159,17 +143,8 @@ public class MainViewModel : ReactiveObject
     }
 
     #region Commands
-
-    public ReactiveCommand<Unit, Unit>? ExitCommand { get; }
-    public ReactiveCommand<Unit, Unit>? MinimizeCommand { get; }
-    public ReactiveCommand<Unit, Unit>? MaximizeCommand { get; }
     public ReactiveCommand<Unit, Unit>? RestoreCommand { get; }
     public ReactiveCommand<Unit, Unit>? ToggleFullscreenCommand { get; }
-    public ReactiveCommand<Unit, Unit>? CloseMenuCommand { get; }
-    public ReactiveCommand<Unit, Unit>? ToggleFileMenuCommand { get; }
-    public ReactiveCommand<Unit, Unit>? ToggleImageMenuCommand { get; }
-    public ReactiveCommand<Unit, Unit>? ToggleSettingsMenuCommand { get; }
-    public ReactiveCommand<Unit, Unit>? ToggleToolsMenuCommand { get; }
     public ReactiveCommand<Unit, Unit>? NewWindowCommand { get; }
     public ReactiveCommand<Unit, Unit>? ToggleLoopingCommand { get; }
     public ReactiveCommand<Unit, Unit>? RotateLeftCommand { get; }

+ 36 - 0
src/PicView.Avalonia/ViewModels/MainWindowViewModel.cs

@@ -1,7 +1,9 @@
 using Avalonia;
 using Avalonia.Controls;
 using Avalonia.Media;
+using PicView.Avalonia.Functions;
 using PicView.Avalonia.UI;
+using PicView.Avalonia.WindowBehavior;
 using R3;
 
 namespace PicView.Avalonia.ViewModels;
@@ -68,6 +70,40 @@ public class MainWindowViewModel : IDisposable
         Observable.EveryValueChanged(this, x => x.IsFullscreen.CurrentValue, UIHelper.GetFrameProvider)
             .Subscribe(_ => SetButtonValues());
     }
+    
+    #region Menus
+    
+    public ReactiveCommand CloseMenuCommand { get; } = new(CloseMenus);
+    
+    public ReactiveCommand ToggleFileMenuCommand { get; } = new(ToggleFileMenu);
+    public ReactiveCommand ToggleImageMenuCommand { get; } = new(ToggleImageMenu);
+    public ReactiveCommand ToggleSettingsMenuCommand { get; } = new(ToggleSettingsMenu);
+    public ReactiveCommand ToggleToolsMenuCommand { get; } = new(ToggleToolsMenu);
+
+    private static void CloseMenus(Unit unit) => MenuManager.CloseMenus(UIHelper.GetMainView.DataContext as MainViewModel);
+    
+    private static void ToggleFileMenu(Unit unit) => MenuManager.ToggleFileMenu(UIHelper.GetMainView.DataContext as MainViewModel);
+    private static void ToggleImageMenu(Unit unit) => MenuManager.ToggleImageMenu(UIHelper.GetMainView.DataContext as MainViewModel);
+    private static void ToggleSettingsMenu(Unit unit) => MenuManager.ToggleSettingsMenu(UIHelper.GetMainView.DataContext as MainViewModel);
+    private static void ToggleToolsMenu(Unit unit) => MenuManager.ToggleToolsMenu(UIHelper.GetMainView.DataContext as MainViewModel);
+
+    #endregion Menus
+    
+    public ReactiveCommand ExitCommand { get; } = new(async (_, _) =>
+    {
+        await FunctionsMapper.Close();
+    });
+    
+    public ReactiveCommand MaximizeCommand { get; } = new(async (_, _) =>
+    {
+        await FunctionsMapper.Maximize();
+    });
+    
+    public ReactiveCommand MinimizeCommand { get; } = new(async (_, _) =>
+    {
+        await WindowFunctions.Minimize();
+    });
+    
 
     private void SetButtonValues()
     {

+ 4 - 4
src/PicView.Avalonia/Views/BottomBar.axaml

@@ -207,7 +207,7 @@
                     BorderBrush="{DynamicResource MainBorderColor}"
                     BorderThickness="1,0,1,0"
                     Classes="noBorderHover"
-                    Command="{CompiledBinding ToggleFileMenuCommand}"
+                    Command="{CompiledBinding MainWindow.ToggleFileMenuCommand}"
                     Data="{StaticResource AltFolderGeometry}"
                     Foreground="{DynamicResource MainTextColor}"
                     Height="25"
@@ -222,7 +222,7 @@
                     BorderBrush="{DynamicResource MainBorderColor}"
                     BorderThickness="0,0,1,0"
                     Classes="noBorderHover"
-                    Command="{CompiledBinding ToggleImageMenuCommand}"
+                    Command="{CompiledBinding MainWindow.ToggleImageMenuCommand}"
                     Foreground="{DynamicResource MainTextColor}"
                     Height="25"
                     IsTabStop="False"
@@ -301,7 +301,7 @@
                     BorderBrush="{DynamicResource MainBorderColor}"
                     BorderThickness="0,0,1,0"
                     Classes="noBorderHover"
-                    Command="{CompiledBinding ToggleToolsMenuCommand}"
+                    Command="{CompiledBinding MainWindow.ToggleToolsMenuCommand}"
                     Foreground="{DynamicResource MainTextColor}"
                     Height="25"
                     IsTabStop="False"
@@ -343,7 +343,7 @@
                     BorderBrush="{DynamicResource MainBorderColor}"
                     BorderThickness="0,0,1,0"
                     Classes="noBorderHover NoRepeat"
-                    Command="{CompiledBinding ToggleSettingsMenuCommand}"
+                    Command="{CompiledBinding MainWindow.ToggleSettingsMenuCommand}"
                     Data="{StaticResource CogGeometry}"
                     Foreground="{DynamicResource MainTextColor}"
                     Height="25"

+ 1 - 2
src/PicView.Avalonia/Views/EffectsView.axaml.cs

@@ -148,7 +148,7 @@ public partial class EffectsView : UserControl
 
         sliderValueChanges
             .Debounce(TimeSpan.FromMilliseconds(300))
-            .ObserveOn(UIHelper.GetFrameProvider) // Ensure we're on the UI thread to access UI properties
+            .ObserveOn(UIHelper.GetFrameProvider)
             .Select(_ =>
             {
                 // Update the config with the latest slider values
@@ -180,7 +180,6 @@ public partial class EffectsView : UserControl
 
                 return (magick, vm);
             })
-            .ObserveOn(UIHelper.GetFrameProvider) // Switch back to the UI thread to update the UI
             .Subscribe(result =>
             {
                 var (magick, viewModel) = result;

+ 2 - 2
src/PicView.Avalonia/Views/MainView.axaml

@@ -1034,7 +1034,7 @@
 
             <!--  Maximize  -->
             <MenuItem
-                Command="{CompiledBinding MaximizeCommand}"
+                Command="{CompiledBinding MainWindow.MaximizeCommand}"
                 Header="{CompiledBinding Translation.Maximize.Value,
                                          Mode=OneWay}"
                 IsVisible="{CompiledBinding MainWindow.ShouldMaximizeBeShown.Value,
@@ -1067,7 +1067,7 @@
             </MenuItem>
 
             <!--  Close  -->
-            <MenuItem Command="{CompiledBinding ExitCommand}" Header="{CompiledBinding Translation.Close.Value, Mode=OneWay}">
+            <MenuItem Command="{CompiledBinding MainWindow.ExitCommand}" Header="{CompiledBinding Translation.Close.Value, Mode=OneWay}">
                 <MenuItem.Icon>
                     <Path
                         Data="{StaticResource CloseGeometry}"

+ 1 - 1
src/PicView.Avalonia/Views/UC/Buttons/AltClose.axaml

@@ -15,7 +15,7 @@
         BorderThickness="1,0,0,1"
         Classes="ButtonBorder hover"
         ClickMode="Release"
-        Command="{CompiledBinding ExitCommand}"
+        Command="{CompiledBinding MainWindow.ExitCommand}"
         CornerRadius="0,0,0,0"
         DockPanel.Dock="Right"
         Height="30"

+ 1 - 1
src/PicView.Avalonia/Views/UC/Buttons/AltMinimize.axaml

@@ -16,7 +16,7 @@
         BorderThickness="1,0,0,0"
         Classes="ButtonBorder hover"
         ClickMode="Release"
-        Command="{CompiledBinding MinimizeCommand}"
+        Command="{CompiledBinding MainWindow.MinimizeCommand}"
         CornerRadius="0,0,0,10"
         DockPanel.Dock="Right"
         Height="30"

+ 1 - 1
src/PicView.Avalonia/Views/UC/Menus/FileMenu.axaml

@@ -132,7 +132,7 @@
                     Canvas.Left="270"
                     Classes="hover btn"
                     ClickMode="Release"
-                    Command="{CompiledBinding CloseMenuCommand}"
+                    Command="{CompiledBinding MainWindow.CloseMenuCommand}"
                     CornerRadius="0,8,0,0"
                     Data="{StaticResource CloseGeometry}"
                     IconHeight="10"

+ 1 - 1
src/PicView.Avalonia/Views/UC/Menus/ImageMenu.axaml

@@ -175,7 +175,7 @@
                     Canvas.Left="270"
                     Classes="hover"
                     ClickMode="Release"
-                    Command="{CompiledBinding CloseMenuCommand}"
+                    Command="{CompiledBinding MainWindow.CloseMenuCommand}"
                     CornerRadius="0,8,0,0"
                     Data="{StaticResource CloseGeometry}"
                     Foreground="{DynamicResource MainTextColor}"

+ 1 - 1
src/PicView.Avalonia/Views/UC/Menus/SettingsMenu.axaml

@@ -110,7 +110,7 @@
                     Canvas.Top="-1"
                     Classes="hover"
                     ClickMode="Release"
-                    Command="{CompiledBinding CloseMenuCommand}"
+                    Command="{CompiledBinding MainWindow.CloseMenuCommand}"
                     CornerRadius="0,8,0,0"
                     Data="{StaticResource CloseGeometry}"
                     Foreground="{DynamicResource MainTextColor}"

+ 1 - 1
src/PicView.Avalonia/Views/UC/Menus/ToolsMenu.axaml

@@ -113,7 +113,7 @@
                     Canvas.Top="-1"
                     Classes="hover"
                     ClickMode="Release"
-                    Command="{CompiledBinding CloseMenuCommand}"
+                    Command="{CompiledBinding MainWindow.CloseMenuCommand}"
                     CornerRadius="0,8,0,0"
                     Data="{StaticResource CloseGeometry}"
                     Foreground="{DynamicResource MainTextColor}"