From e9a4c44d2252d3d67eda2eb82774095ab405aa63 Mon Sep 17 00:00:00 2001 From: Ruben Date: Sat, 7 Sep 2024 17:00:51 +0200 Subject: [PATCH] Fix rotation when key is held down --- .../Views/ImageViewer.axaml.cs | 37 ++++++++++++++----- .../ImageSizeCalculationHelper.cs | 25 +++++++++++-- 2 files changed, 50 insertions(+), 12 deletions(-) diff --git a/src/PicView.Avalonia/Views/ImageViewer.axaml.cs b/src/PicView.Avalonia/Views/ImageViewer.axaml.cs index 8ead84f49..ed46a0d54 100644 --- a/src/PicView.Avalonia/Views/ImageViewer.axaml.cs +++ b/src/PicView.Avalonia/Views/ImageViewer.axaml.cs @@ -33,6 +33,8 @@ public partial class ImageViewer : UserControl public ImageViewer() { + // TODO: Make a zoom and pan custom control that will work for MVVM + InitializeComponent(); AddHandler(PointerWheelChangedEvent, PreviewOnPointerWheelChanged, RoutingStrategies.Tunnel); AddHandler(Gestures.PointerTouchPadGestureMagnifyEvent, TouchMagnifyEvent, RoutingStrategies.Bubble); @@ -443,21 +445,39 @@ public void Rotate(bool clockWise) { return; } - if (RotationHelper.IsValidRotation(vm.RotationAngle)) + + if (MainKeyboardShortcuts.IsKeyHeldDown) { - var nextAngle = RotationHelper.Rotate(vm.RotationAngle, clockWise); - vm.RotationAngle = nextAngle switch + vm.RotationAngle = clockWise ? vm.RotationAngle + 1 : vm.RotationAngle -1; + if (vm.RotationAngle < 0) { - 360 => 0, - -90 => 270, - _ => nextAngle - }; + vm.RotationAngle = 359; + } + + if (vm.RotationAngle > 359) + { + vm.RotationAngle = 0; + } } else { - vm.RotationAngle = RotationHelper.NextRotationAngle(vm.RotationAngle, true); + if (RotationHelper.IsValidRotation(vm.RotationAngle)) + { + var nextAngle = RotationHelper.Rotate(vm.RotationAngle, clockWise); + vm.RotationAngle = nextAngle switch + { + 360 => 0, + -90 => 270, + _ => nextAngle + }; + } + else + { + vm.RotationAngle = RotationHelper.NextRotationAngle(vm.RotationAngle, true); + } } + var rotateTransform = new RotateTransform(vm.RotationAngle); if (Dispatcher.UIThread.CheckAccess()) @@ -473,7 +493,6 @@ public void Rotate(bool clockWise) } WindowHelper.SetSize(vm); - MainImage.InvalidateVisual(); } public void Rotate(double angle) diff --git a/src/PicView.Core/Calculations/ImageSizeCalculationHelper.cs b/src/PicView.Core/Calculations/ImageSizeCalculationHelper.cs index 5832f91f9..79a6e49cc 100644 --- a/src/PicView.Core/Calculations/ImageSizeCalculationHelper.cs +++ b/src/PicView.Core/Calculations/ImageSizeCalculationHelper.cs @@ -5,8 +5,12 @@ namespace PicView.Core.Calculations { public static class ImageSizeCalculationHelper { + /// + /// Returns the interface size of the titlebar based on OS + /// public static double GetInterfaceSize() { + // TODO: find a more elegant solution return RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? 165 : 228; } @@ -271,9 +275,24 @@ public static double GetTitleMaxWidth(double rotationAngle, double width, double if (SettingsHelper.Settings.WindowProperties.AutoFit && !maximized) { - titleMaxWidth = rotationAngle is 0 or 180 - ? Math.Max(width, monitorMinWidth) - : Math.Max(height, monitorMinHeight); + switch (rotationAngle) + { + case 0 or 180: + titleMaxWidth = Math.Max(width, monitorMinWidth); + break; + case 90 or 270: + titleMaxWidth = Math.Max(height, monitorMinHeight); + break; + default: + { + var rotationRadians = rotationAngle * Math.PI / 180; + var newWidth = Math.Abs(width * Math.Cos(rotationRadians)) + + Math.Abs(height * Math.Sin(rotationRadians)); + + titleMaxWidth = Math.Max(newWidth, monitorMinWidth); + break; + } + } titleMaxWidth = titleMaxWidth - interfaceSize < interfaceSize ? interfaceSize