Skip to content

armgit / cindy

Public Imported from GitHub
0

Consider it done. The open-source AI agent that works out of the box · 想到,就能做到。开源、开箱即用的 AI Agent。

feat(desktop): 图片查看器区分鼠标滚轮与触控板,各平台一致

Dash1e16eb61e065
diff --git a/apps/desktop/src/renderer/components/chat/ImageLightbox.tsx b/apps/desktop/src/renderer/components/chat/ImageLightbox.tsx
index a2409de8c3..8af6a3c725 100644
--- a/apps/desktop/src/renderer/components/chat/ImageLightbox.tsx
+++ b/apps/desktop/src/renderer/components/chat/ImageLightbox.tsx
@@ -6,11 +6,10 @@
* - 遮罩:使用 `--overlay-lightbox` 主题 token,保持 lightbox 遮罩语义一致。
* - 图片:`object-fit: contain`,四周保留 40px 安全边距。
* - 缩放/平移(手势数学共用 `lightboxGestures`,与 MermaidLightbox 同一套):
- * - 触控板双指捏合(macOS、Windows 精密触摸板的 pinch 都由 Chromium 合成为带
- * ctrlKey 的 wheel 事件)/ Ctrl(⌘)+滚轮 → 以光标为焦点缩放。
- * - macOS:双指滑动(不带修饰键的 wheel)在放大后平移,适配态不响应(与
- * MermaidLightbox 同一惯例)。其它平台普通滚轮不区分修饰键也直接缩放
- * (Windows 看图器惯例,鼠标用户单手可缩放)。
+ * - 各平台一致:触控板双指捏合(macOS、Windows 精密触摸板的 pinch 都由 Chromium
+ * 合成为带 ctrlKey 的 wheel 事件)/ Ctrl(⌘)+滚轮 / 鼠标滚轮 → 以光标为焦点缩放;
+ * 触控板双指滑动在放大后平移,适配态不响应。鼠标还是触控板按事件特征推断
+ * (见 lightboxGestures.classifyWheelSample),判不准时按鼠标处理(缩放)。
* - 双击图片:1x ↔ 2x 切换(以双击点为焦点)。
* - 放大后左键拖拽平移;松手时若拖动超过阈值,该次 click 不触发关闭。
* - 键盘 +/- 步进缩放,0 重置。翻页(←/→ 或箭头按钮)时视口自动重置。
@@ -100,6 +99,7 @@ import {
LIGHTBOX_WHEEL_IDLE_MS,
LIGHTBOX_ZOOM_STEP,
clampScale,
+ createWheelSourceTracker,
imageLightboxWheelIntent,
type LightboxViewport,
normalizeWheelDelta,
@@ -730,16 +730,31 @@ export function ImageLightbox({
// 滚轮 / 触控板。native listener + passive:false 才能 preventDefault,阻止事件穿透
// 到底下的聊天区,也阻止 Chromium 对 ctrl+wheel 的页面级缩放。路由见
- // imageLightboxWheelIntent:macOS 双指捏合(Chromium 合成为 ctrlKey=true 的 wheel)
- // 与 ⌘/Ctrl+滚轮以光标为焦点缩放,双指滑动在放大后平移;其它平台照旧所有滚轮
- // 都缩放(Windows 看图器惯例)。
+ // imageLightboxWheelIntent(各平台一致):双指捏合(Chromium 合成为 ctrlKey=true 的
+ // wheel)、⌘/Ctrl+滚轮与鼠标滚轮以光标为焦点缩放;触控板双指滑动在放大后平移。
+ // 鼠标 / 触控板按事件特征推断(classifyWheelSample),每段连续滚动只判定一次。
useEffect(() => {
const overlay = overlayRef.current;
if (!overlay) return;
+ const trackWheelSource = createWheelSourceTracker();
const onWheel = (e: globalThis.WheelEvent) => {
e.preventDefault();
const current = viewportRef.current;
- const intent = imageLightboxWheelIntent(e, isMacPlatform(), current.scale);
+ const source =
+ e.ctrlKey || e.metaKey
+ ? 'mouse'
+ : trackWheelSource(
+ {
+ deltaMode: e.deltaMode,
+ deltaX: e.deltaX,
+ deltaY: e.deltaY,
+ shiftKey: e.shiftKey,
+ // 非标准的旧字段(Chromium 仍提供),推断设备类型的关键信号。
+ wheelDeltaY: (e as globalThis.WheelEvent & { wheelDeltaY?: number }).wheelDeltaY,
+ },
+ e.timeStamp,
+ );
+ const intent = imageLightboxWheelIntent(e, source, current.scale);
if (intent === 'none') return;
if (intent === 'pan') {
markWheeling();
diff --git a/apps/desktop/src/renderer/components/chat/__tests__/lightboxGestures.test.ts b/apps/desktop/src/renderer/components/chat/__tests__/lightboxGestures.test.ts
index ae1d48c9a2..c86037a074 100644
--- a/apps/desktop/src/renderer/components/chat/__tests__/lightboxGestures.test.ts
+++ b/apps/desktop/src/renderer/components/chat/__tests__/lightboxGestures.test.ts
@@ -7,6 +7,8 @@ import {
clampScale,
wheelZoomFactor,
zoomAtPoint,
+ classifyWheelSample,
+ createWheelSourceTracker,
imageLightboxWheelIntent,
} from '../lightboxGestures';
@@ -65,6 +67,66 @@ describe('lightboxGestures', () => {
});
});
+describe('classifyWheelSample', () => {
+ const sample = (over: Partial<Parameters<typeof classifyWheelSample>[0]>) => ({
+ deltaMode: 0,
+ deltaX: 0,
+ deltaY: 0,
+ shiftKey: false,
+ ...over,
+ });
+
+ it('treats notched wheels as a mouse', () => {
+ // Windows / Linux:每格 wheelDeltaY ±120。
+ expect(classifyWheelSample(sample({ deltaY: 100, wheelDeltaY: -120 }))).toBe('mouse');
+ expect(classifyWheelSample(sample({ deltaY: 125, wheelDeltaY: -120 }))).toBe('mouse');
+ // macOS 无加速的一格:deltaY 40、wheelDeltaY 120。
+ expect(classifyWheelSample(sample({ deltaY: -40, wheelDeltaY: 120 }))).toBe('mouse');
+ // macOS 加速滚轮:小数像素。
+ expect(classifyWheelSample(sample({ deltaY: 4.000244140625, wheelDeltaY: -12 }))).toBe('mouse');
+ // 行 / 页单位只来自滚轮。
+ expect(classifyWheelSample(sample({ deltaMode: 1, deltaY: 3 }))).toBe('mouse');
+ // Shift+滚轮的横向滚动仍是鼠标。
+ expect(classifyWheelSample(sample({ deltaX: 100, shiftKey: true, wheelDeltaY: 0 }))).toBe('mouse');
+ });
+
+ it('treats continuous two-finger scrolling as a trackpad', () => {
+ expect(classifyWheelSample(sample({ deltaY: 2, wheelDeltaY: -6 }))).toBe('trackpad');
+ expect(classifyWheelSample(sample({ deltaY: -7, wheelDeltaY: 21 }))).toBe('trackpad');
+ expect(classifyWheelSample(sample({ deltaX: 3, deltaY: 1 }))).toBe('trackpad');
+ });
+
+ it('falls back to mouse (zoom) when undecidable', () => {
+ expect(classifyWheelSample(sample({ deltaY: 7 }))).toBe('mouse');
+ });
+});
+
+describe('createWheelSourceTracker', () => {
+ const sample = (over: Partial<Parameters<typeof classifyWheelSample>[0]>) => ({
+ deltaMode: 0,
+ deltaX: 0,
+ deltaY: 0,
+ shiftKey: false,
+ ...over,
+ });
+
+ it('keeps one classification for a whole stream and re-classifies after idle', () => {
+ const track = createWheelSourceTracker(250);
+ expect(track(sample({ deltaY: 3, wheelDeltaY: -9 }), 0)).toBe('trackpad');
+ // 惯性尾巴里偶发一个恰好是 40 倍数的大 delta,不翻转。
+ expect(track(sample({ deltaY: 40, wheelDeltaY: -120 }), 16)).toBe('trackpad');
+ // 停顿后重新判定。
+ expect(track(sample({ deltaY: 100, wheelDeltaY: -120 }), 1000)).toBe('mouse');
+ });
+
+ it('upgrades a stream to trackpad once a horizontal component shows up', () => {
+ const track = createWheelSourceTracker(250);
+ expect(track(sample({ deltaY: 7 }), 0)).toBe('mouse');
+ expect(track(sample({ deltaX: 2, deltaY: 5 }), 16)).toBe('trackpad');
+ expect(track(sample({ deltaY: 5 }), 32)).toBe('trackpad');
+ });
+});
+
describe('imageLightboxWheelIntent', () => {
const wheel = (over: Partial<{ ctrlKey: boolean; metaKey: boolean; deltaX: number; deltaY: number }>) => ({
ctrlKey: false,
@@ -74,23 +136,22 @@ describe('imageLightboxWheelIntent', () => {
...over,
});
- it('macOS: pinch (ctrl) and ⌘ + wheel zoom', () => {
- expect(imageLightboxWheelIntent(wheel({ ctrlKey: true, deltaY: -3 }), true, 1)).toBe('zoom');
- expect(imageLightboxWheelIntent(wheel({ metaKey: true, deltaY: 40 }), true, 2)).toBe('zoom');
- expect(imageLightboxWheelIntent(wheel({ ctrlKey: true, deltaX: 5 }), true, 2)).toBe('none');
+ it('pinch (ctrl) and ⌘ + wheel zoom regardless of source', () => {
+ expect(imageLightboxWheelIntent(wheel({ ctrlKey: true, deltaY: -3 }), 'trackpad', 1)).toBe('zoom');
+ expect(imageLightboxWheelIntent(wheel({ metaKey: true, deltaY: 40 }), 'mouse', 2)).toBe('zoom');
+ expect(imageLightboxWheelIntent(wheel({ ctrlKey: true, deltaX: 5 }), 'trackpad', 2)).toBe('none');
});
- it('macOS: two-finger scroll pans only while zoomed in', () => {
- expect(imageLightboxWheelIntent(wheel({ deltaY: 12 }), true, 1)).toBe('none');
- expect(imageLightboxWheelIntent(wheel({ deltaX: -8 }), true, 2)).toBe('pan');
- expect(imageLightboxWheelIntent(wheel({ deltaY: 12 }), true, 2)).toBe('pan');
- expect(imageLightboxWheelIntent(wheel({}), true, 2)).toBe('none');
+ it('a mouse wheel zooms (same on every OS)', () => {
+ expect(imageLightboxWheelIntent(wheel({ deltaY: 100 }), 'mouse', 1)).toBe('zoom');
+ expect(imageLightboxWheelIntent(wheel({ deltaY: -100 }), 'mouse', 3)).toBe('zoom');
+ expect(imageLightboxWheelIntent(wheel({ deltaX: 30 }), 'mouse', 2)).toBe('none');
});
- it('other platforms: every vertical wheel zooms (unchanged)', () => {
- expect(imageLightboxWheelIntent(wheel({ deltaY: 100 }), false, 1)).toBe('zoom');
- expect(imageLightboxWheelIntent(wheel({ deltaY: -100 }), false, 3)).toBe('zoom');
- expect(imageLightboxWheelIntent(wheel({ ctrlKey: true, deltaY: -2 }), false, 1)).toBe('zoom');
- expect(imageLightboxWheelIntent(wheel({ deltaX: 30 }), false, 2)).toBe('none');
+ it('a trackpad two-finger scroll pans only while zoomed in', () => {
+ expect(imageLightboxWheelIntent(wheel({ deltaY: 12 }), 'trackpad', 1)).toBe('none');
+ expect(imageLightboxWheelIntent(wheel({ deltaX: -8 }), 'trackpad', 2)).toBe('pan');
+ expect(imageLightboxWheelIntent(wheel({ deltaY: 12 }), 'trackpad', 2)).toBe('pan');
+ expect(imageLightboxWheelIntent(wheel({}), 'trackpad', 2)).toBe('none');
});
});
diff --git a/apps/desktop/src/renderer/components/chat/lightboxGestures.ts b/apps/desktop/src/renderer/components/chat/lightboxGestures.ts
index 8f8be558e9..a337b631b4 100644
--- a/apps/desktop/src/renderer/components/chat/lightboxGestures.ts
+++ b/apps/desktop/src/renderer/components/chat/lightboxGestures.ts
@@ -104,24 +104,83 @@ export function zoomAtPoint(
};
}
+/** Physical device behind a wheel event, as far as it can be inferred. */
+export type WheelInputSource = 'mouse' | 'trackpad';
+
+/** The wheel event fields the source heuristic reads (legacy `wheelDeltaY` is Chromium-only). */
+export interface WheelSample {
+ deltaMode: number;
+ deltaX: number;
+ deltaY: number;
+ shiftKey: boolean;
+ wheelDeltaY?: number;
+}
+
+/** A gap longer than this starts a new wheel stream (re-classified from scratch). */
+export const WHEEL_SOURCE_IDLE_MS = 250;
+
+/**
+ * Best-effort guess of whether one wheel event came from a mouse wheel or a
+ * multi-touch surface (trackpad / precision touchpad). Browsers expose no
+ * device type, so this combines the signals that hold in Chromium:
+ *
+ * - line/page delta units only come from wheels;
+ * - a horizontal component without Shift is a two-finger scroll (wheels only
+ * scroll sideways with Shift or a tilt wheel);
+ * - macOS accelerated wheels produce fractional pixel deltas, trackpads whole ones;
+ * - Chromium encodes the legacy `wheelDeltaY` as ±120 per wheel notch, and as
+ * `-3 × deltaY` for continuous (precise) scrolling devices.
+ *
+ * Anything undecided counts as a mouse wheel, i.e. keeps the zoom behaviour.
+ */
+export function classifyWheelSample(e: WheelSample): WheelInputSource {
+ if (e.deltaMode !== WHEEL_DELTA_PIXEL) return 'mouse';
+ if (e.deltaX !== 0 && !e.shiftKey) return 'trackpad';
+ if (!Number.isInteger(e.deltaY)) return 'mouse';
+ const legacy = e.wheelDeltaY;
+ if (typeof legacy === 'number' && legacy !== 0) {
+ if (legacy % 120 === 0) return 'mouse';
+ if (legacy === -3 * e.deltaY) return 'trackpad';
+ }
+ return 'mouse';
+}
+
+/**
+ * Classify once per wheel stream so a gesture never flips mid-way (e.g. the
+ * momentum tail of a two-finger fling). A later horizontal component upgrades a
+ * stream to trackpad. `now` is the event timestamp in ms.
+ */
+export function createWheelSourceTracker(idleMs = WHEEL_SOURCE_IDLE_MS) {
+ let source: WheelInputSource | null = null;
+ let lastAt = Number.NEGATIVE_INFINITY;
+ return (e: WheelSample, now: number): WheelInputSource => {
+ if (source === null || now - lastAt > idleMs) {
+ source = classifyWheelSample(e);
+ } else if (source === 'mouse' && e.deltaMode === WHEEL_DELTA_PIXEL && e.deltaX !== 0 && !e.shiftKey) {
+ source = 'trackpad';
+ }
+ lastAt = now;
+ return source;
+ };
+}
+
/** What a single wheel event should do in the image lightbox. */
export type ImageLightboxWheelIntent = 'zoom' | 'pan' | 'none';
/**
- * Route one wheel event for ImageLightbox.
+ * Route one wheel event for ImageLightbox, the same on every OS:
*
- * - macOS follows the trackpad convention (same as MermaidLightbox): a pinch
- * (Chromium delivers it as wheel + ctrlKey) or ⌘/Ctrl + wheel zooms; a plain
- * two-finger scroll pans while zoomed in and does nothing at fit scale.
- * - Other platforms keep the Windows image-viewer convention: every wheel event
- * zooms (one-handed mouse zoom), including precision-touchpad pinch.
+ * - pinch (Chromium delivers it as wheel + ctrlKey), ⌘/Ctrl + wheel, and a plain
+ * mouse wheel zoom at the cursor;
+ * - a two-finger scroll on a trackpad pans while zoomed in and does nothing at
+ * fit scale.
*/
export function imageLightboxWheelIntent(
event: { ctrlKey: boolean; metaKey: boolean; deltaX: number; deltaY: number },
- isMac: boolean,
+ source: WheelInputSource,
scale: number,
): ImageLightboxWheelIntent {
- if (!isMac || event.ctrlKey || event.metaKey) {
+ if (event.ctrlKey || event.metaKey || source === 'mouse') {
return event.deltaY === 0 ? 'none' : 'zoom';
}
if (scale <= 1 || (event.deltaX === 0 && event.deltaY === 0)) return 'none';