diff --git a/lib/components/canvas/canvas_gesture_detector.dart b/lib/components/canvas/canvas_gesture_detector.dart index 09ffc83ee..2f6ce6c87 100644 --- a/lib/components/canvas/canvas_gesture_detector.dart +++ b/lib/components/canvas/canvas_gesture_detector.dart @@ -328,6 +328,8 @@ class CanvasGestureDetectorState extends State { } } + Timer? _snapZoomTimer; + /// Corrects the transform if it's out of bounds. /// If the scale is less than 1, centers the pages horizontally. /// Otherwise, prevents the user from scrolling past the edges. @@ -338,14 +340,19 @@ class CanvasGestureDetectorState extends State { double adjustmentX = 0; double adjustmentY = 0; - // horizontally center pages if zoomed out + // snap to 1.0x zoom + _snapZoomTimer?.cancel(); + final diffFrom1 = (scale - 1).abs(); + // allow 0.001 leeway for floating point error + if (diffFrom1 < 0.05 && diffFrom1 > 0.001) + _snapZoomTimer = Timer(const Duration(milliseconds: 200), resetZoom); + if (scale < 1) { + // horizontally center pages if zoomed out final center = containerBounds.maxWidth * (1 - scale) / 2; adjustmentX = center - translation.x; - } - - // if zoomed in, don't allow scrolling past the edges - else { + } else { + // if zoomed in, don't allow scrolling past the edges late final minX = containerBounds.maxWidth * (1 - scale); if (translation.x > 0) { adjustmentX = -translation.x;