From 81eed7a1fa125207eb2ff9297a5649a1b5a7c79e Mon Sep 17 00:00:00 2001 From: filtered <176114999+webfiltered@users.noreply.github.com> Date: Sun, 4 May 2025 11:58:24 +1000 Subject: [PATCH] Add macOS trackpad gesture support (#1007) Adds trackpad gesture support to canvas interaction. Supports: - Pinch to zoom - Two-finger panning Feature is off by default. Also by default, the browser user-agent must include "Mac" (even if the feature has been enabled). ```ts // Enable mac trackpad gestures LiteGraph.macTrackpadGestures = true // Disable the default Mac user-agent check LiteGraph.macGesturesRequireMac = false ``` --- src/LGraphCanvas.ts | 17 +++++++++++++++-- src/LiteGraphGlobal.ts | 16 ++++++++++++++++ test/__snapshots__/litegraph.test.ts.snap | 2 ++ 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/src/LGraphCanvas.ts b/src/LGraphCanvas.ts index 8c9f64750e..7e1fbb9f41 100644 --- a/src/LGraphCanvas.ts +++ b/src/LGraphCanvas.ts @@ -2968,8 +2968,21 @@ export class LGraphCanvas { let { scale } = this.ds - if (delta > 0) scale *= this.zoom_speed - else if (delta < 0) scale *= 1 / this.zoom_speed + if ( + LiteGraph.macTrackpadGestures && + (!LiteGraph.macGesturesRequireMac || navigator.userAgent.includes("Mac")) + ) { + if (e.ctrlKey && !Number.isInteger(e.deltaY)) { + scale *= 1 + e.deltaY * (1 - this.zoom_speed) * 0.18 + } else { + this.ds.offset[0] -= e.deltaX * 1.18 * (1 / scale) + this.ds.offset[1] -= e.deltaY * 1.18 * (1 / scale) + } + } else if (delta > 0) { + scale *= this.zoom_speed + } else if (delta < 0) { + scale *= 1 / (this.zoom_speed) + } this.ds.changeScale(scale, [e.clientX, e.clientY]) diff --git a/src/LiteGraphGlobal.ts b/src/LiteGraphGlobal.ts index 8ad891c60b..863588703d 100644 --- a/src/LiteGraphGlobal.ts +++ b/src/LiteGraphGlobal.ts @@ -276,6 +276,22 @@ export class LiteGraphGlobal { */ onDeprecationWarning: ((message: string, source?: object) => void)[] = [console.warn] + /** + * If `true`, mouse wheel events will be interpreted as trackpad gestures. + * Tested on MacBook M4 Pro. + * @default false + * @see macGesturesRequireMac + */ + macTrackpadGestures: boolean = false + + /** + * If both this setting and {@link macTrackpadGestures} are `true`, trackpad gestures will + * only be enabled when the browser user agent includes "Mac". + * @default true + * @see macTrackpadGestures + */ + macGesturesRequireMac: boolean = true + // TODO: Remove legacy accessors LGraph = LGraph LLink = LLink diff --git a/test/__snapshots__/litegraph.test.ts.snap b/test/__snapshots__/litegraph.test.ts.snap index 97bb5ed405..226749622b 100644 --- a/test/__snapshots__/litegraph.test.ts.snap +++ b/test/__snapshots__/litegraph.test.ts.snap @@ -158,6 +158,8 @@ LiteGraphGlobal { "do_add_triggers_slots": false, "highlight_selected_group": true, "isInsideRectangle": [Function], + "macGesturesRequireMac": true, + "macTrackpadGestures": false, "middle_click_slot_add_default_node": false, "node_box_coloured_by_mode": false, "node_box_coloured_when_on": false,