Skip to content

Commit

Permalink
Make the image overlay behave better
Browse files Browse the repository at this point in the history
Previously, the image would not try to stay centered; furthermore,
on mobile devices, it was impossible to zoom images without dropping
them at a slightly off rotation. This bothered me enough that I've
clamped images to the center of the screen (as long as they aren't
zoomed larger than the screen) and snapped rotations to 45-degree
increments (with a nice animation to boot).
  • Loading branch information
LorenDB committed Dec 23, 2023
1 parent 20b7ab1 commit 01fef9e
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions resources/qml/dialogs/ImageOverlay.qml
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,31 @@ Window {

property int imgSrcWidth: (imageOverlay.originalWidth && imageOverlay.originalWidth > 100) ? imageOverlay.originalWidth : Screen.width
property int imgSrcHeight: imageOverlay.proportionalHeight ? imgSrcWidth * imageOverlay.proportionalHeight : Screen.height
readonly property int physicalWidth: width * scale
readonly property int physicalHeight: height * scale

height: Math.min(parent.height || Screen.height, imgSrcHeight)
width: Math.min(parent.width || Screen.width, imgSrcWidth)

x: (parent.width - width) / 2
y: (parent.height - height) / 2

onXChanged: {
if (physicalWidth < Screen.width)
x = (parent.width - width) / 2;
}
onYChanged: {
if (physicalHeight < Screen.height)
y = (parent.height - height) / 2;
}

Behavior on rotation {
NumberAnimation {
duration: 100
easing.type: Easing.InOutQuad
}
}

Image {
id: img

Expand Down Expand Up @@ -94,6 +112,12 @@ Window {
target: imgContainer
maximumScale: 10
minimumScale: 0.1

onGrabChanged: (transition, point) => {
// snap to 45-degree angles
if (imgContainer.rotation % 45 != 0)
imgContainer.rotation -= imgContainer.rotation % 45;
}
}

WheelHandler {
Expand All @@ -107,6 +131,8 @@ Window {

DragHandler {
target: imgContainer
xAxis.enabled: imgContainer.physicalWidth > Screen.width
yAxis.enabled: imgContainer.physicalHeight > Screen.height
}

HoverHandler {
Expand Down

0 comments on commit 01fef9e

Please sign in to comment.