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
```
This commit is contained in:
filtered
2025-05-04 11:58:24 +10:00
committed by GitHub
parent bfc87af9d1
commit 81eed7a1fa
3 changed files with 33 additions and 2 deletions

View File

@@ -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])

View File

@@ -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

View File

@@ -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,