Explorar el Código

Improve rotation and set cursor pos, refactor

Ruben hace 1 año
padre
commit
6c7feac257

+ 118 - 0
src/PicView.Avalonia/ImageTransformations/Rotation.cs

@@ -0,0 +1,118 @@
+using Avalonia;
+using Avalonia.Controls;
+using Avalonia.Threading;
+using PicView.Avalonia.Gallery;
+using PicView.Avalonia.UI;
+using PicView.Avalonia.ViewModels;
+using PicView.Avalonia.Views.UC.Menus;
+using PicView.Core.Config;
+
+namespace PicView.Avalonia.ImageTransformations;
+public static class Rotation
+{
+    public enum RotationButton
+    {
+        WindowBorderButton,
+        RotateRightButton,
+        RotateLeftButton
+    }
+
+    public static async Task RotateRight(MainViewModel? vm)
+    {
+        if (vm is null)
+        {
+            return;
+        }
+
+        if (GalleryFunctions.IsFullGalleryOpen)
+        {
+            return;
+        }
+
+        await Dispatcher.UIThread.InvokeAsync(() => { vm.ImageViewer.Rotate(false); });
+    }
+    
+    public static async Task RotateRight(MainViewModel? vm, RotationButton rotationButton)
+    {
+        await RotateRight(vm);
+        
+        // Check if it should move the cursor
+        if (!SettingsHelper.Settings.WindowProperties.AutoFit)
+        {
+            return;
+        }
+
+        await MoveCursorAfterRotation(vm, rotationButton);
+    }
+
+    private static async Task MoveCursorAfterRotation(MainViewModel? vm, RotationButton rotationButton)
+    {
+        // Move cursor when button is clicked
+        await Dispatcher.UIThread.InvokeAsync(() =>
+        {
+            try
+            {
+                Button? button;
+                ImageMenu? menu;
+                switch (rotationButton)
+                {
+                    case RotationButton.WindowBorderButton:
+                        button = UIHelper.GetTitlebar.GetControl<Button>("RotateRightButton");
+                        break;
+                    case RotationButton.RotateRightButton:
+                        menu = UIHelper.GetMainView.MainGrid.Children.OfType<ImageMenu>().FirstOrDefault();
+                        button = menu?.GetControl<Button>("RotateRightButton");
+                        break;
+                    case RotationButton.RotateLeftButton:
+                        menu = UIHelper.GetMainView.MainGrid.Children.OfType<ImageMenu>().FirstOrDefault();
+                        button = menu?.GetControl<Button>("RotateLeftButton");
+                        break;
+                    default:
+                        return;
+                }
+
+                if (button is null || !button.IsPointerOver)
+                {
+                    return;
+                }
+
+                var p = button.PointToScreen(new Point(10, 15));
+                vm.PlatformService?.SetCursorPos(p.X, p.Y);
+            }
+#if DEBUG
+            catch (Exception e)
+            {
+                Console.WriteLine(e);
+            }
+            #else
+            catch (Exception) { }
+#endif
+        });
+    }
+            
+    public static async Task RotateLeft(MainViewModel? vm)
+    {
+        if (vm is null)
+        {
+            return;
+        }
+
+        if (GalleryFunctions.IsFullGalleryOpen)
+        {
+            return;
+        }
+        await Dispatcher.UIThread.InvokeAsync(() => { vm.ImageViewer.Rotate(true); });
+    }
+
+    public static async Task RotateLeft(MainViewModel vm, RotationButton rotationButton)
+    {
+        await RotateLeft(vm);
+        
+        // Check if it should move the cursor
+        if (!SettingsHelper.Settings.WindowProperties.AutoFit)
+        {
+            return;
+        }
+        await MoveCursorAfterRotation(vm, rotationButton);
+    }
+}

+ 7 - 2
src/PicView.Avalonia/UI/FunctionsHelper.cs

@@ -8,6 +8,7 @@ using PicView.Avalonia.Clipboard;
 using PicView.Avalonia.ColorManagement;
 using PicView.Avalonia.FileSystem;
 using PicView.Avalonia.Gallery;
+using PicView.Avalonia.ImageTransformations;
 using PicView.Avalonia.Navigation;
 using PicView.Avalonia.ViewModels;
 using PicView.Core.Config;
@@ -35,6 +36,10 @@ public static class FunctionsHelper
             "Down" => Down,
             "Last" => Last,
             "First" => First,
+            
+            // Rotate
+            "RotateLeft" => RotateLeft,
+            "RotateRight" => RotateRight,
 
             // Scroll
             "ScrollUp" => ScrollUp,
@@ -218,12 +223,12 @@ public static class FunctionsHelper
 
     public static async Task RotateRight()
     {
-        await UIHelper.RotateRight(Vm);
+        await Rotation.RotateRight(Vm);
     }
 
     public static async Task RotateLeft()
     {
-        await UIHelper.RotateLeft(Vm);
+        await Rotation.RotateLeft(Vm);
     }
 
     public static async Task Down()

+ 0 - 51
src/PicView.Avalonia/UI/UIHelper.cs

@@ -343,58 +343,7 @@ namespace PicView.Avalonia.UI
             }
         }
 
-        public static async Task RotateRight(MainViewModel? vm)
-        {
-            if (vm is null)
-            {
-                return;
-            }
-
-            if (GalleryFunctions.IsFullGalleryOpen)
-            {
-                return;
-            }
-
-            await Dispatcher.UIThread.InvokeAsync(() => { vm.ImageViewer.Rotate(false); });
 
-            // Check if it should move the cursor
-            if (!SettingsHelper.Settings.WindowProperties.AutoFit)
-            {
-                return;
-            }
-
-            if (Application.Current?.ApplicationLifetime is not IClassicDesktopStyleApplicationLifetime desktop)
-            {
-                return;
-            }
-
-            // Move cursor when button is clicked
-            // TODO: Dynamically figure out which button is clicked
-            await Dispatcher.UIThread.InvokeAsync(() =>
-            {
-                var titleBar = desktop.MainWindow.GetControl<Control>("Titlebar");
-                var button = titleBar.GetControl<Button>("RotateRightButton");
-                if (button.IsPointerOver)
-                {
-                    var p = button.PointToScreen(new Point(10, 15));
-                    vm.PlatformService?.SetCursorPos(p.X, p.Y);
-                }
-            });
-        }
-        
-        public static async Task RotateLeft(MainViewModel? vm)
-        {
-            if (vm is null)
-            {
-                return;
-            }
-
-            if (GalleryFunctions.IsFullGalleryOpen)
-            {
-                return;
-            }
-            await Dispatcher.UIThread.InvokeAsync(() => { vm.ImageViewer.Rotate(true); });
-        }
 
         #endregion Navigation
 

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

@@ -9,6 +9,7 @@ using PicView.Avalonia.Clipboard;
 using PicView.Avalonia.Converters;
 using PicView.Avalonia.Gallery;
 using PicView.Avalonia.ImageHandling;
+using PicView.Avalonia.ImageTransformations;
 using PicView.Avalonia.Interfaces;
 using PicView.Avalonia.Navigation;
 using PicView.Avalonia.UI;
@@ -459,7 +460,10 @@ public class MainViewModel : ViewModelBase
     public ReactiveCommand<string, Unit>? DuplicateFileCommand { get; }
     public ReactiveCommand<Unit, Unit>? ToggleLoopingCommand { get; }
     public ReactiveCommand<Unit, Unit>? RotateLeftCommand { get; }
+    public ReactiveCommand<Unit, Unit>? RotateLeftButtonCommand { get; }
     public ReactiveCommand<Unit, Unit>? RotateRightCommand { get; }
+    public ReactiveCommand<Unit, Unit>? RotateRightButtonCommand { get; }
+    public ReactiveCommand<Unit, Unit>? RotateRightWindowBorderButtonCommand { get; }
     public ReactiveCommand<Unit, Unit>? FlipCommand { get; }
     public ReactiveCommand<Unit, Unit>? StretchCommand { get; }
     public ReactiveCommand<Unit, Unit>? CropCommand { get; }
@@ -1733,8 +1737,21 @@ public class MainViewModel : ViewModelBase
         #region Image commands
 
         RotateLeftCommand = ReactiveCommand.CreateFromTask(FunctionsHelper.RotateLeft);
+        RotateLeftButtonCommand = ReactiveCommand.CreateFromTask(async () =>
+        {
+            await Rotation.RotateLeft(this, Rotation.RotationButton.RotateLeftButton);
+        });
 
         RotateRightCommand = ReactiveCommand.CreateFromTask(FunctionsHelper.RotateRight);
+        RotateRightButtonCommand = ReactiveCommand.CreateFromTask(async () =>
+        {
+            await Rotation.RotateRight(this, Rotation.RotationButton.RotateRightButton);
+        });
+        
+        RotateRightWindowBorderButtonCommand = ReactiveCommand.CreateFromTask(async () =>
+        {
+            await Rotation.RotateRight(this, Rotation.RotationButton.WindowBorderButton);
+        });
 
         FlipCommand = ReactiveCommand.CreateFromTask(FunctionsHelper.Flip);
 

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

@@ -38,7 +38,7 @@
                     BorderBrush="{DynamicResource MainBorderColor}"
                     BorderThickness="0,0,1,0"
                     Classes="hover"
-                    Command="{CompiledBinding RotateLeftCommand}"
+                    Command="{CompiledBinding RotateLeftButtonCommand}"
                     CornerRadius="8,0,0,0"
                     Data="{StaticResource RotateLeftGeometry}"
                     Foreground="{DynamicResource MainTextColor}"
@@ -47,7 +47,8 @@
                     IconWidth="15"
                     ToolTip.Tip="{CompiledBinding RotateLeft,
                                                   Mode=OneWay}"
-                    Width="45" />
+                    Width="45"
+                    x:Name="RotateLeftButton" />
 
                 <customControls:IconButton
                     Background="Transparent"
@@ -55,7 +56,7 @@
                     BorderThickness="0,0,1,0"
                     Canvas.Left="45"
                     Classes="hover"
-                    Command="{CompiledBinding RotateRightCommand}"
+                    Command="{CompiledBinding RotateRightButtonCommand}"
                     Data="{StaticResource RotateRightGeometry}"
                     Foreground="{DynamicResource MainTextColor}"
                     Height="45"
@@ -63,7 +64,8 @@
                     IconWidth="15"
                     ToolTip.Tip="{CompiledBinding RotateRight,
                                                   Mode=OneWay}"
-                    Width="45" />
+                    Width="45"
+                    x:Name="RotateRightButton" />
 
                 <customControls:IconButton
                     Background="Transparent"