Skip to content

Commit

Permalink
feat: snap zoom to 1.0x (#1211)
Browse files Browse the repository at this point in the history
  • Loading branch information
adil192 authored Apr 7, 2024
1 parent b4f3307 commit f3afcea
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions lib/components/canvas/canvas_gesture_detector.dart
Original file line number Diff line number Diff line change
Expand Up @@ -328,6 +328,8 @@ class CanvasGestureDetectorState extends State<CanvasGestureDetector> {
}
}

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.
Expand All @@ -338,14 +340,19 @@ class CanvasGestureDetectorState extends State<CanvasGestureDetector> {
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;
Expand Down

0 comments on commit f3afcea

Please sign in to comment.