Skip to content

Build your personal knowledge base with Trilium Notes

fix(client): land a wheel notch on the fitted view rather than over it

Elian Doran0fdf6ef584de
diff --git a/apps/client/src/widgets/react/zoom_pan.spec.ts b/apps/client/src/widgets/react/zoom_pan.spec.ts
index cc90d9a491..4814231acd 100644
--- a/apps/client/src/widgets/react/zoom_pan.spec.ts
+++ b/apps/client/src/widgets/react/zoom_pan.spec.ts
@@ -52,6 +52,20 @@ describe("wheelTargetScale", () => {
expect(scale).toBeCloseTo(1.1);
});
+ it("lands on the fitted view rather than stepping over it", () => {
+ // Scrolling in fast and out slowly sends different totals — Firefox accelerates a fast wheel
+ // — which leaves the ladder off the fitted view, and the factor then keeps it there.
+ expect(wheelTargetScale(1.09, OUT)).toBe(1);
+ expect(wheelTargetScale(0.99, IN)).toBe(1);
+ // From there the ladder is the usual one again.
+ expect(wheelTargetScale(1, IN)).toBeCloseTo(1.1);
+ expect(wheelTargetScale(1, OUT)).toBeCloseTo(1 / 1.1);
+
+ // A notch that stays on one side of the fitted view is left alone, in either direction.
+ expect(wheelTargetScale(1.21, OUT)).toBeCloseTo(1.1);
+ expect(wheelTargetScale(0.826, IN)).toBeCloseTo(0.9086);
+ });
+
it("reads deltaY in the unit deltaMode names, so a notch is a notch in Firefox too", () => {
// Firefox reports lines on Windows and Linux, three to a notch; Chromium reports pixels.
expect(wheelTargetScale(1, -3, WheelEvent.DOM_DELTA_LINE)).toBeCloseTo(wheelTargetScale(1, IN));
diff --git a/apps/client/src/widgets/react/zoom_pan.ts b/apps/client/src/widgets/react/zoom_pan.ts
index 3f978fd04f..483a1c02aa 100644
--- a/apps/client/src/widgets/react/zoom_pan.ts
+++ b/apps/client/src/widgets/react/zoom_pan.ts
@@ -107,6 +107,8 @@ const WHEEL_NOTCH_DELTA = 100;
* this a notch there moves the scale by a thirtieth of a notch's worth.
*/
const WHEEL_DELTA_MODE_PIXELS = [ 1, WHEEL_NOTCH_DELTA / 3, WHEEL_NOTCH_DELTA * 3 ];
+/** The scale the content is fitted to its pane at, which the readout calls 100%. */
+const FITTED_SCALE = 1;
/**
* How near a bound counts as reaching it. `animate()` computes its last frame as `scale + diff * 1`,
* which can land one float's width short of the bound.
@@ -133,11 +135,21 @@ export function zoomStep(scale: number, direction: "in" | "out") {
*
* `deltaMode` is `WheelEvent.deltaMode`, which says what unit `deltaY` is in (see
* {@link WHEEL_DELTA_MODE_PIXELS}).
+ *
+ * A notch that would step over {@link FITTED_SCALE} lands on it instead. Multiplying by a factor
+ * only reverses exactly when the `deltaY` scrolled each way adds up the same, and it does not:
+ * Firefox accelerates a fast wheel (`mousewheel.acceleration.*`) and a trackpad reports whatever the
+ * finger did. Any mismatch leaves the ladder a percent or two off, and the factor then keeps it
+ * there — 99% and 109% rather than 100% and 110% — so the fitted view needs a detent to stay
+ * reachable. Scrolling away from it is untouched, so the detent never holds the content back.
*/
export function wheelTargetScale(scale: number, deltaY: number, deltaMode = 0) {
const pixels = deltaY * (WHEEL_DELTA_MODE_PIXELS[deltaMode] ?? 1);
const factor = WHEEL_STEP ** (Math.abs(pixels) / WHEEL_NOTCH_DELTA);
- return pixels < 0 ? scale * factor : scale / factor;
+ const target = pixels < 0 ? scale * factor : scale / factor;
+
+ const stepsOver = Math.sign(scale - FITTED_SCALE) * Math.sign(target - FITTED_SCALE) < 0;
+ return stepsOver ? FITTED_SCALE : target;
}
interface PanBounds { minPositionX: number; maxPositionX: number; minPositionY: number; maxPositionY: number; }