fix(viewers): zoom the image viewer a step per Ctrl+wheel notch (#8089)

One Ctrl+wheel notch could zoom the image viewer from 94% to 693% on Windows: the handler multiplied the zoom by exp(-deltaY * 0.01) with no per-event limit, so a large pixel-mode notch zoomed several times over. The wheel curve the Mermaid preview already used (about 15% a notch, at most x1.5 or /1.5 per event, trackpads continuous) is now shared as wheelZoomFactor and used by the image viewer and the database ER diagram, which had the same formula.

Co-authored-by: wislert <[email protected]>
Цей коміт міститься в:
Serbin Yaroslavтаwislert authored and GitHub committed 2026-12-05 00:56:28 +08:00
1 джерело 71bfc70996
коміт 3278eab0c8
5 files changed
+40 -11

No files matched your search

+10 -2
Переглянути файл
@@ -23,7 +23,15 @@ import { isMacHost } from "@/platform/keymap";
import { fontsLoaded } from "@/platform/fonts";
import { resolveTokenColor, toRgb } from "@/platform/cssColor";
import type { EditorInput } from "@/workbench/contributions";
import { fitZoom, stepZoom, clampZoom, zoomAt, zoomLabel, type View } from "@/features/viewers/imageZoom";
import {
fitZoom,
stepZoom,
clampZoom,
wheelZoomFactor,
zoomAt,
zoomLabel,
type View,
} from "@/features/viewers/imageZoom";
import { saveDiagram } from "@/features/viewers/markdown/mermaidTools";
import { closeDialog, revealInTree } from "./store";
import { diagramKey, diagramModel, type DiagramData, type DiagramNode } from "./erDiagram";
@@ -234,7 +242,7 @@ export function DiagramView({ data, onJump }: { data: DiagramData; onJump?: () =
e.preventDefault();
if (e.metaKey || e.ctrlKey) {
const r = el.getBoundingClientRect();
const factor = Math.exp(-e.deltaY * (e.deltaMode === 0 ? 0.01 : 0.33));
const factor = wheelZoomFactor(e.deltaY, e.deltaMode);
setView((v) => zoomAt(v, clampZoom(v.z * factor), e.clientX - r.left, e.clientY - r.top));
} else setView((v) => ({ ...v, x: v.x - e.deltaX, y: v.y - e.deltaY }));
};
+2 -2
Переглянути файл
@@ -20,7 +20,7 @@ import { setContext } from "@/platform/context";
import { editorService, type EditorInput } from "@/workbench/contributions";
import { IconButton, T } from "@/ui";
import { formatSize, isSvgPath } from "./fileKinds";
import { clampZoom, fitZoom, initialZoom, stepZoom, zoomAt, zoomLabel, type View } from "./imageZoom";
import { clampZoom, fitZoom, initialZoom, stepZoom, wheelZoomFactor, zoomAt, zoomLabel, type View } from "./imageZoom";
import { useForwardFocus } from "./forwardFocus";
import styles from "./Viewers.module.css";
import { tx } from "@/platform/i18n";
@@ -184,7 +184,7 @@ export function ImageViewer({ input, source }: { input: EditorInput; source?: st
e.preventDefault();
const rect = el.getBoundingClientRect();
if (e.metaKey || e.ctrlKey) {
const factor = Math.exp(-e.deltaY * (e.deltaMode === 0 ? 0.01 : 0.33));
const factor = wheelZoomFactor(e.deltaY, e.deltaMode);
setView((v) => v && zoomAt(v, clampZoom(v.z * factor), e.clientX - rect.left, e.clientY - rect.top));
} else {
setView((v) => v && { ...v, x: v.x - e.deltaX, y: v.y - e.deltaY });
+10
Переглянути файл
@@ -24,6 +24,16 @@ export function initialZoom(width: number, height: number, availW: number, avail
return Math.min(1, fitZoom(width, height, availW, availH));
}
/**
* Zoom multiplier for one ⌘/Ctrl+wheel event. A mouse notch (~100 px, 3 lines or a page)
* zooms about 15%; trackpad pixel deltas zoom continuously. The clamp keeps a large delta
* (fast flick, high wheel speed or display scaling on Windows) from jumping too far.
*/
export function wheelZoomFactor(deltaY: number, deltaMode: number): number {
const speed = deltaMode === 0 ? 0.0015 : deltaMode === 1 ? 0.05 : 0.15;
return Math.min(1.5, Math.max(2 / 3, Math.exp(-deltaY * speed)));
}
export const zoomLabel = (zoom: number) => `${Math.round(zoom * 100)}%`;
/** Pan/zoom state: image top-left offset (px, relative to the canvas) and scale. */
+2 -6
Переглянути файл
@@ -4,7 +4,7 @@
* scrolling the page; drag pans once zoomed in (or always, with `panAlways`),
* double-click resets. Listeners live as long as `viewport`.
*/
import { clampZoom, zoomAt, type View } from "./imageZoom";
import { clampZoom, wheelZoomFactor, zoomAt, type View } from "./imageZoom";
const IDENTITY: View = { z: 1, x: 0, y: 0 };
@@ -56,11 +56,7 @@ export function attachPanZoom(viewport: HTMLElement, options: PanZoomOptions = {
viewport.scrollTop = 0;
}
const rect = viewport.getBoundingClientRect();
// One wheel notch (~100 px, 3 lines or a page) zooms about 15%; a trackpad
// sends small pixel deltas, so zooming follows the gesture continuously.
// The clamp keeps a fast flick from jumping too far in one event.
const speed = e.deltaMode === 0 ? 0.0015 : e.deltaMode === 1 ? 0.05 : 0.15;
const factor = Math.min(1.5, Math.max(2 / 3, Math.exp(-e.deltaY * speed)));
const factor = wheelZoomFactor(e.deltaY, e.deltaMode);
apply(zoomAt(view, clampZoom(view.z * factor), e.clientX - rect.left, e.clientY - rect.top));
},
{ passive: false },
+16 -1
Переглянути файл
@@ -34,7 +34,7 @@ import {
rowCount,
scrollTopForRow,
} from "./hex";
import { fitZoom, initialZoom, stepZoom, zoomAt, zoomLabel } from "./imageZoom";
import { fitZoom, initialZoom, stepZoom, wheelZoomFactor, zoomAt, zoomLabel } from "./imageZoom";
describe("file kinds", () => {
it("routes by extension", () => {
@@ -243,4 +243,19 @@ describe("image zoom", () => {
expect(next.x + 100 * next.z).toBe(110);
expect(next.y + 50 * next.z).toBe(70);
});
it("zooms a little per wheel notch, however large the delta", () => {
const PIXEL = 0,
LINE = 1,
PAGE = 2;
// A Windows mouse notch at high pointer speed or display scaling: 94% must not jump to 693%.
expect(wheelZoomFactor(-200, PIXEL)).toBeCloseTo(Math.exp(0.3));
expect(wheelZoomFactor(-2000, PIXEL)).toBe(1.5);
expect(wheelZoomFactor(2000, PIXEL)).toBe(2 / 3);
expect(wheelZoomFactor(-3, LINE)).toBeCloseTo(Math.exp(0.15));
expect(wheelZoomFactor(-1, PAGE)).toBeCloseTo(Math.exp(0.15));
// Trackpad pixel deltas stay continuous.
expect(wheelZoomFactor(-4, PIXEL)).toBeCloseTo(Math.exp(0.006));
expect(wheelZoomFactor(0, PIXEL)).toBe(1);
});
});