Skip to content

Commit

Permalink
Fix rotation when key is held down
Browse files Browse the repository at this point in the history
  • Loading branch information
Ruben2776 committed Sep 7, 2024
1 parent 32d31ef commit e9a4c44
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 12 deletions.
37 changes: 28 additions & 9 deletions src/PicView.Avalonia/Views/ImageViewer.axaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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())
Expand All @@ -473,7 +493,6 @@ public void Rotate(bool clockWise)
}

WindowHelper.SetSize(vm);
MainImage.InvalidateVisual();
}

public void Rotate(double angle)
Expand Down
25 changes: 22 additions & 3 deletions src/PicView.Core/Calculations/ImageSizeCalculationHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,12 @@ namespace PicView.Core.Calculations
{
public static class ImageSizeCalculationHelper
{
/// <summary>
/// Returns the interface size of the titlebar based on OS
/// </summary>
public static double GetInterfaceSize()
{
// TODO: find a more elegant solution
return RuntimeInformation.IsOSPlatform(OSPlatform.OSX) ? 165 : 228;
}

Expand Down Expand Up @@ -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
Expand Down

0 comments on commit e9a4c44

Please sign in to comment.