mirror of
https://github.com/l0ng-ai/openidea.git
synced 2026-11-24 08:03:19 +00:00
fix(editor): keep scroll inertia out of ⌘/Ctrl+wheel font zoom (#3985)
WebViews don't expose macOS momentum phases, so a trackpad scroll still coasting when ⌘ was pressed zoomed the text, and the inertia after a ⌘ swipe kept zooming. Wheel events less than 150 ms apart now count as one gesture: only a gesture that started with ⌘/Ctrl zooms, and once its deltas shrink four events in a row the rest is inertia. All ⌘/Ctrl+wheel font-zoom surfaces use it (editor, terminal, agent chat, Markdown preview, plugin and services log editors). Co-authored-by: pravdomil <2387356+pravdomil@users.noreply.github.com>
This commit is contained in:
@@ -57,6 +57,7 @@ import { stringClipboard } from "./stringClipboard";
|
||||
import { filterCopiedText } from "./clipboard";
|
||||
import { LINE_INSET } from "./lineInset";
|
||||
import { codeLensPositions } from "./codeLens";
|
||||
import { wheelZoomStep } from "@/platform/wheelZoom";
|
||||
|
||||
// --- soft wraps ---------------------------------------------------------------
|
||||
|
||||
@@ -452,9 +453,10 @@ export function resetConfiguredEditorsFontZoom(origin: EditorView): void {
|
||||
/** Exported for tests: the ⌘/Ctrl+wheel handler of the prefs compartment. */
|
||||
export const wheelZoom = EditorView.domEventHandlers({
|
||||
wheel(e, view) {
|
||||
if (!e.metaKey && !e.ctrlKey) return false;
|
||||
const step = wheelZoomStep(e);
|
||||
if (step === null) return false;
|
||||
e.preventDefault();
|
||||
stepConfiguredEditorsFontZoom(view, e.deltaY < 0 ? 1 : -1);
|
||||
if (step) stepConfiguredEditorsFontZoom(view, step);
|
||||
return true;
|
||||
},
|
||||
});
|
||||
|
||||
@@ -4,6 +4,7 @@ import { EditorView } from "@codemirror/view";
|
||||
import { useEditorRegistry, type TextEditorHandle } from "@/editor/api";
|
||||
import { useProject } from "@/platform/project";
|
||||
import { fontZoom, resetAllEditorsFontZoom, setFontZoom, stepAllEditorsFontZoom, wheelZoom } from "./cm/prefs";
|
||||
import { resetWheelZoomGesture } from "@/platform/wheelZoom";
|
||||
|
||||
const zoomOf = (view: EditorView) => view.state.field(fontZoom, false);
|
||||
|
||||
@@ -19,6 +20,7 @@ describe("editor font zoom (active editor by default, all editors when configure
|
||||
return view;
|
||||
};
|
||||
beforeEach(() => {
|
||||
resetWheelZoomGesture();
|
||||
resetAllEditorsFontZoom();
|
||||
useProject.setState({ settings: null });
|
||||
});
|
||||
@@ -45,6 +47,16 @@ describe("editor font zoom (active editor by default, all editors when configure
|
||||
expect(zoomOf(second)).toBe(-1);
|
||||
});
|
||||
|
||||
it("doesn't zoom from the inertia of a scroll when ⌘/Ctrl is pressed during it", () => {
|
||||
const view = makeView();
|
||||
const plain = new WheelEvent("wheel", { bubbles: true, cancelable: true, deltaY: -40 });
|
||||
view.contentDOM.dispatchEvent(plain);
|
||||
const coasting = new WheelEvent("wheel", { bubbles: true, cancelable: true, metaKey: true, deltaY: -20 });
|
||||
view.contentDOM.dispatchEvent(coasting);
|
||||
expect(coasting.defaultPrevented).toBe(false);
|
||||
expect(zoomOf(view)).toBe(0);
|
||||
});
|
||||
|
||||
it("zooms every open editor from ⌘/Ctrl+wheel when wheelZoomScope is 'all' (#2480)", () => {
|
||||
useProject.setState({ settings: { editor: { general: { wheelZoomScope: "all" } } } } as never);
|
||||
const first = makeView();
|
||||
|
||||
@@ -3,6 +3,7 @@ import { create } from "zustand";
|
||||
import { editorPrefs } from "@/platform/editorSettings";
|
||||
import { useProject } from "@/platform/project";
|
||||
import { registerFontZoomSurface, useEditorShown, useEditorTabId, useToolWindowShown } from "@/workbench/contributions";
|
||||
import { wheelZoomStep } from "@/platform/wheelZoom";
|
||||
|
||||
const MIN_FONT_ZOOM = -6;
|
||||
const MAX_FONT_ZOOM = 36;
|
||||
@@ -74,10 +75,12 @@ export function useAgentFontZoomSurface(
|
||||
if (!active || !el) return;
|
||||
const step = (delta: number) => stepAgentSurfaceFontZoom(surfaceKey, delta);
|
||||
const onWheel = (e: WheelEvent) => {
|
||||
if ((!e.ctrlKey && !e.metaKey) || e.deltaY === 0 || !editorPrefs().general.wheelZoom) return;
|
||||
if (!editorPrefs().general.wheelZoom) return;
|
||||
const zoom = wheelZoomStep(e);
|
||||
if (zoom === null) return;
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
step(e.deltaY < 0 ? 1 : -1);
|
||||
if (zoom) step(zoom);
|
||||
};
|
||||
el.addEventListener("wheel", onWheel, { capture: true, passive: false });
|
||||
const release = registerFontZoomSurface(el, step, { isolated: true });
|
||||
|
||||
@@ -32,6 +32,7 @@ import {
|
||||
openPluginDiff,
|
||||
type PluginContentEditorData,
|
||||
} from "./contentTab";
|
||||
import { wheelZoomStep } from "@/platform/wheelZoom";
|
||||
|
||||
export function pluginContentEditorTitle(input: EditorInput): string {
|
||||
const s = sessions.get(input.key);
|
||||
@@ -171,9 +172,10 @@ function isActionStroke(stroke: string | null, actionId: string): boolean {
|
||||
|
||||
const wheelZoom = EditorView.domEventHandlers({
|
||||
wheel(e, view) {
|
||||
if (!e.metaKey && !e.ctrlKey) return false;
|
||||
const step = wheelZoomStep(e);
|
||||
if (step === null) return false;
|
||||
e.preventDefault();
|
||||
stepFontZoom(view, e.deltaY < 0 ? 1 : -1);
|
||||
if (step) stepFontZoom(view, step);
|
||||
return true;
|
||||
},
|
||||
});
|
||||
|
||||
@@ -26,6 +26,7 @@ import { boundedLog, type PluginLogEditorData } from "./logTab";
|
||||
import { logTabPtyBackend, webPtyBackend } from "./ptyBackend";
|
||||
import { outputKey, usePluginOutput } from "./outputStore";
|
||||
import { ptyBridge } from "./ptyBridge";
|
||||
import { wheelZoomStep } from "@/platform/wheelZoom";
|
||||
|
||||
export const PLUGIN_LOG_EDITOR_KIND = "pluginLog";
|
||||
|
||||
@@ -42,9 +43,10 @@ function isActionStroke(stroke: string | null, actionId: string): boolean {
|
||||
|
||||
const wheelZoom = EditorView.domEventHandlers({
|
||||
wheel(e, view) {
|
||||
if (!e.metaKey && !e.ctrlKey) return false;
|
||||
const step = wheelZoomStep(e);
|
||||
if (step === null) return false;
|
||||
e.preventDefault();
|
||||
stepFontZoom(view, e.deltaY < 0 ? 1 : -1);
|
||||
if (step) stepFontZoom(view, step);
|
||||
return true;
|
||||
},
|
||||
});
|
||||
|
||||
@@ -28,6 +28,7 @@ import logStyles from "@/ui/LogPane.module.css";
|
||||
import { editorService, type EditorInput } from "@/workbench/contributions";
|
||||
import { isRunning } from "./model";
|
||||
import { connectionState, endpointOf, errorMessage, useServices } from "./store";
|
||||
import { wheelZoomStep } from "@/platform/wheelZoom";
|
||||
|
||||
export const DOCKER_LOG_EDITOR_KIND = "dockerLog";
|
||||
|
||||
@@ -71,9 +72,10 @@ function isActionStroke(stroke: string | null, actionId: string): boolean {
|
||||
|
||||
const wheelZoom = EditorView.domEventHandlers({
|
||||
wheel(e, view) {
|
||||
if (!e.metaKey && !e.ctrlKey) return false;
|
||||
const step = wheelZoomStep(e);
|
||||
if (step === null) return false;
|
||||
e.preventDefault();
|
||||
stepFontZoom(view, e.deltaY < 0 ? 1 : -1);
|
||||
if (step) stepFontZoom(view, step);
|
||||
return true;
|
||||
},
|
||||
});
|
||||
|
||||
@@ -42,6 +42,7 @@ import {
|
||||
type TerminalFontSettings,
|
||||
MIN_CONTRAST_RATIO,
|
||||
} from "./theme";
|
||||
import { wheelZoomStep } from "@/platform/wheelZoom";
|
||||
|
||||
export interface TerminalSearchOptions {
|
||||
caseSensitive: boolean;
|
||||
@@ -464,10 +465,12 @@ export class TerminalSession {
|
||||
}
|
||||
|
||||
private onWheelZoom = (e: WheelEvent) => {
|
||||
if ((!e.ctrlKey && !e.metaKey) || e.deltaY === 0 || !editorPrefs().general.wheelZoom) return;
|
||||
if (!editorPrefs().general.wheelZoom) return;
|
||||
const zoom = wheelZoomStep(e);
|
||||
if (zoom === null) return;
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
this.stepFontSize(e.deltaY < 0 ? 1 : -1);
|
||||
if (zoom) this.stepFontSize(zoom);
|
||||
};
|
||||
|
||||
private onMiddleClick = (e: MouseEvent) => {
|
||||
|
||||
@@ -54,6 +54,7 @@ import {
|
||||
import { applyImageZoomWidth, svgDataUrlSize } from "@/ui/svgSize";
|
||||
import styles from "./Markdown.module.css";
|
||||
import { tx } from "@/platform/i18n";
|
||||
import { wheelZoomStep } from "@/platform/wheelZoom";
|
||||
|
||||
/** Loaded local images by absolute path (data URLs), shared by all previews. */
|
||||
const imageCache = new Map<string, Promise<string>>();
|
||||
@@ -380,11 +381,13 @@ function MarkdownPreview({ path, sync }: { path: string; sync: boolean }) {
|
||||
const root = ref.current;
|
||||
if (!root) return;
|
||||
const onWheel = (e: WheelEvent) => {
|
||||
if ((!e.ctrlKey && !e.metaKey) || e.deltaY === 0 || !editorPrefs().general.wheelZoom) return;
|
||||
if (!editorPrefs().general.wheelZoom) return;
|
||||
const zoom = wheelZoomStep(e);
|
||||
if (zoom === null) return;
|
||||
if ((e.target as Element | null)?.closest?.('.mermaid-output[data-zoomed="true"]')) return;
|
||||
e.preventDefault();
|
||||
e.stopPropagation();
|
||||
stepMarkdownPreviewFontZoom(path, root, e.deltaY < 0 ? 1 : -1);
|
||||
if (zoom) stepMarkdownPreviewFontZoom(path, root, zoom);
|
||||
};
|
||||
root.addEventListener("wheel", onWheel, { capture: true, passive: false });
|
||||
const release = registerFontZoomSurface(root, (delta) => stepMarkdownPreviewFontZoom(path, root, delta));
|
||||
|
||||
@@ -0,0 +1,53 @@
|
||||
import { beforeEach, describe, expect, it } from "vitest";
|
||||
import { resetWheelZoomGesture, wheelZoomStep } from "./wheelZoom";
|
||||
|
||||
let t = 0;
|
||||
const wheel = (deltaY: number, mod = true, gap = 16) => {
|
||||
t += gap;
|
||||
return wheelZoomStep({ deltaY, metaKey: mod, ctrlKey: false, timeStamp: t });
|
||||
};
|
||||
|
||||
describe("wheelZoomStep", () => {
|
||||
beforeEach(() => {
|
||||
resetWheelZoomGesture();
|
||||
t = 1000;
|
||||
});
|
||||
|
||||
it("zooms in and out with ⌘ held from the start of the gesture", () => {
|
||||
expect(wheel(-4)).toBe(1);
|
||||
expect(wheel(-6)).toBe(1);
|
||||
expect(wheel(10, true, 500)).toBe(-1);
|
||||
});
|
||||
|
||||
it("leaves plain scrolling alone", () => {
|
||||
expect(wheel(-10, false)).toBeNull();
|
||||
});
|
||||
|
||||
it("keeps scrolling the inertia of a plain scroll when ⌘ is pressed during it", () => {
|
||||
expect(wheel(-40, false)).toBeNull();
|
||||
expect(wheel(-30)).toBeNull();
|
||||
expect(wheel(-20)).toBeNull();
|
||||
expect(wheel(-10, true, 500)).toBe(1);
|
||||
});
|
||||
|
||||
it("stops zooming once the deltas decay like inertia, without scrolling", () => {
|
||||
for (const d of [-8, -12, -15]) expect(wheel(d)).toBe(1);
|
||||
expect(wheel(-14)).toBe(1);
|
||||
expect(wheel(-12)).toBe(1);
|
||||
expect(wheel(-9)).toBe(1);
|
||||
expect(wheel(-6)).toBe(0);
|
||||
expect(wheel(-4)).toBe(0);
|
||||
expect(wheel(-5)).toBe(0);
|
||||
});
|
||||
|
||||
it("never swallows a plain wheel event, even during a zoom gesture", () => {
|
||||
expect(wheel(-10)).toBe(1);
|
||||
expect(wheel(-10, false)).toBeNull();
|
||||
});
|
||||
|
||||
it("gives the same answer for repeated calls with one event", () => {
|
||||
const e = { deltaY: -10, metaKey: true, ctrlKey: false, timeStamp: 5000 };
|
||||
expect(wheelZoomStep(e)).toBe(1);
|
||||
expect(wheelZoomStep(e)).toBe(1);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,59 @@
|
||||
/**
|
||||
* ⌘/Ctrl+wheel font zoom that ignores scroll inertia.
|
||||
*
|
||||
* WebViews don't expose macOS momentum phases, so a gesture is a run of wheel events
|
||||
* less than `GESTURE_GAP_MS` apart (momentum continues the run of the swipe that started it).
|
||||
* A run that started without ⌘/Ctrl (a scroll still coasting when ⌘ is pressed) never zooms
|
||||
* and keeps scrolling; a run that started with it stops zooming (and scrolling) once the deltas
|
||||
* keep shrinking, which is the inertia after the fingers left the trackpad. A wheel event
|
||||
* without ⌘/Ctrl always scrolls.
|
||||
*/
|
||||
|
||||
const GESTURE_GAP_MS = 150;
|
||||
/** Consecutive shrinking deltas after which the rest of the run is inertia. */
|
||||
const INERTIA_SHRINKS = 4;
|
||||
|
||||
type WheelLike = Pick<WheelEvent, "deltaY" | "metaKey" | "ctrlKey" | "timeStamp">;
|
||||
|
||||
let lastTime = Number.NEGATIVE_INFINITY;
|
||||
let run = { zoom: false, prev: 0, shrinks: 0, inertia: false };
|
||||
let lastEvent: WheelLike | null = null;
|
||||
let lastResult: number | null = null;
|
||||
|
||||
/**
|
||||
* The font zoom step for a wheel event: `1` / `-1` to zoom in / out, `0` for a ⌘/Ctrl
|
||||
* event of a zoom gesture that must neither zoom nor scroll (its inertia), `null` for one
|
||||
* that isn't a zoom (scroll as usual), which includes every event without ⌘/Ctrl. Repeated calls for one event agree.
|
||||
*/
|
||||
export function wheelZoomStep(e: WheelLike): number | null {
|
||||
if (e === lastEvent) return lastResult;
|
||||
const mod = e.metaKey || e.ctrlKey;
|
||||
const abs = Math.abs(e.deltaY);
|
||||
if (e.timeStamp - lastTime > GESTURE_GAP_MS) {
|
||||
run = { zoom: mod, prev: abs, shrinks: 0, inertia: false };
|
||||
} else {
|
||||
run.shrinks = abs < run.prev ? run.shrinks + 1 : 0;
|
||||
run.prev = abs;
|
||||
if (run.shrinks >= INERTIA_SHRINKS) run.inertia = true;
|
||||
}
|
||||
lastTime = e.timeStamp;
|
||||
lastEvent = e;
|
||||
lastResult = !mod || !run.zoom ? null : e.deltaY === 0 || run.inertia ? 0 : e.deltaY < 0 ? 1 : -1;
|
||||
return lastResult;
|
||||
}
|
||||
|
||||
// Track every wheel event, not only those reaching a zoom surface, so a scroll that started
|
||||
// elsewhere and coasts into one is still known as a scroll.
|
||||
if (typeof window !== "undefined") {
|
||||
window.addEventListener("wheel", (e) => void wheelZoomStep(e), {
|
||||
capture: true,
|
||||
passive: true,
|
||||
});
|
||||
}
|
||||
|
||||
/** Tests: forget the current gesture. */
|
||||
export function resetWheelZoomGesture(): void {
|
||||
lastTime = Number.NEGATIVE_INFINITY;
|
||||
lastEvent = null;
|
||||
lastResult = null;
|
||||
}
|
||||
Reference in New Issue
Block a user