Skip to content

Commit 8ff5f07

Browse files
authored
Add canvasNavigationMode for changing left click pan behaviour (#1108)
1 parent 5bed360 commit 8ff5f07

File tree

3 files changed

+53
-27
lines changed

3 files changed

+53
-27
lines changed

src/LGraphCanvas.ts

Lines changed: 45 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2083,21 +2083,9 @@ export class LGraphCanvas implements CustomEventDispatcher<LGraphCanvasEventMap>
20832083
const ctrlOrMeta = e.ctrlKey || e.metaKey
20842084

20852085
// Multi-select drag rectangle
2086-
if (ctrlOrMeta && !e.altKey) {
2087-
const dragRect = new Float32Array(4)
2088-
dragRect[0] = x
2089-
dragRect[1] = y
2090-
dragRect[2] = 1
2091-
dragRect[3] = 1
2092-
2093-
pointer.onClick = (eUp) => {
2094-
// Click, not drag
2095-
const clickedItem = node ?? this.#getPositionableOnPos(eUp.canvasX, eUp.canvasY)
2096-
this.processSelect(clickedItem, eUp)
2097-
}
2098-
pointer.onDragStart = () => this.dragging_rectangle = dragRect
2099-
pointer.onDragEnd = upEvent => this.#handleMultiSelect(upEvent, dragRect)
2100-
pointer.finally = () => this.dragging_rectangle = null
2086+
if (ctrlOrMeta && !e.altKey && LiteGraph.canvasNavigationMode === "legacy") {
2087+
this.#setupNodeSelectionDrag(e, pointer, node)
2088+
21012089
return
21022090
}
21032091

@@ -2321,12 +2309,35 @@ export class LGraphCanvas implements CustomEventDispatcher<LGraphCanvasEventMap>
23212309
!pointer.onDrag &&
23222310
this.allow_dragcanvas
23232311
) {
2324-
pointer.onClick = () => this.processSelect(null, e)
2325-
pointer.finally = () => this.dragging_canvas = false
2326-
this.dragging_canvas = true
2312+
// allow dragging canvas if canvas is not in standard, or read-only (pan mode in standard)
2313+
if (LiteGraph.canvasNavigationMode !== "standard" || this.read_only) {
2314+
pointer.onClick = () => this.processSelect(null, e)
2315+
pointer.finally = () => this.dragging_canvas = false
2316+
this.dragging_canvas = true
2317+
} else {
2318+
this.#setupNodeSelectionDrag(e, pointer)
2319+
}
23272320
}
23282321
}
23292322

2323+
#setupNodeSelectionDrag(e: CanvasPointerEvent, pointer: CanvasPointer, node?: LGraphNode | undefined): void {
2324+
const dragRect = new Float32Array(4)
2325+
2326+
dragRect[0] = e.canvasX
2327+
dragRect[1] = e.canvasY
2328+
dragRect[2] = 1
2329+
dragRect[3] = 1
2330+
2331+
pointer.onClick = (eUp) => {
2332+
// Click, not drag
2333+
const clickedItem = node ?? this.#getPositionableOnPos(eUp.canvasX, eUp.canvasY)
2334+
this.processSelect(clickedItem, eUp)
2335+
}
2336+
pointer.onDragStart = () => this.dragging_rectangle = dragRect
2337+
pointer.onDragEnd = upEvent => this.#handleMultiSelect(upEvent, dragRect)
2338+
pointer.finally = () => this.dragging_rectangle = null
2339+
}
2340+
23302341
/**
23312342
* Processes a pointerdown event inside the bounds of a node. Part of {@link processMouseDown}.
23322343
* @param e The pointerdown event
@@ -3192,24 +3203,31 @@ export class LGraphCanvas implements CustomEventDispatcher<LGraphCanvasEventMap>
31923203

31933204
let { scale } = this.ds
31943205

3195-
if (
3206+
if (LiteGraph.canvasNavigationMode === "legacy" || (LiteGraph.canvasNavigationMode === "standard" && e.ctrlKey)) {
3207+
if (delta > 0) {
3208+
scale *= this.zoom_speed
3209+
} else if (delta < 0) {
3210+
scale *= 1 / (this.zoom_speed)
3211+
}
3212+
this.ds.changeScale(scale, [e.clientX, e.clientY])
3213+
} else if (
31963214
LiteGraph.macTrackpadGestures &&
31973215
(!LiteGraph.macGesturesRequireMac || navigator.userAgent.includes("Mac"))
31983216
) {
3199-
if (e.ctrlKey) {
3217+
if (e.metaKey && !e.ctrlKey && !e.shiftKey && !e.altKey) {
3218+
if (e.deltaY > 0) {
3219+
scale *= 1 / this.zoom_speed
3220+
} else if (e.deltaY < 0) {
3221+
scale *= this.zoom_speed
3222+
}
3223+
this.ds.changeScale(scale, [e.clientX, e.clientY])
3224+
} else if (e.ctrlKey) {
32003225
scale *= 1 + e.deltaY * (1 - this.zoom_speed) * 0.18
32013226
this.ds.changeScale(scale, [e.clientX, e.clientY], false)
32023227
} else {
32033228
this.ds.offset[0] -= e.deltaX * 1.18 * (1 / scale)
32043229
this.ds.offset[1] -= e.deltaY * 1.18 * (1 / scale)
32053230
}
3206-
} else {
3207-
if (delta > 0) {
3208-
scale *= this.zoom_speed
3209-
} else if (delta < 0) {
3210-
scale *= 1 / (this.zoom_speed)
3211-
}
3212-
this.ds.changeScale(scale, [e.clientX, e.clientY])
32133231
}
32143232

32153233
this.graph.change()

src/LiteGraphGlobal.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -295,6 +295,13 @@ export class LiteGraphGlobal {
295295
*/
296296
macGesturesRequireMac: boolean = true
297297

298+
/**
299+
* "standard": change the dragging on left mouse button click to select, enable middle-click or spacebar+left-click dragging
300+
* "legacy": Enable dragging on left-click (original behavior)
301+
* @default "legacy"
302+
*/
303+
canvasNavigationMode: "standard" | "legacy" = "legacy"
304+
298305
/**
299306
* If `true`, widget labels and values will both be truncated (proportionally to size),
300307
* until they fit within the widget.

test/__snapshots__/litegraph.test.ts.snap

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ LiteGraphGlobal {
151151
"alwaysRepeatWarnings": false,
152152
"alwaysSnapToGrid": undefined,
153153
"auto_load_slot_types": false,
154+
"canvasNavigationMode": "legacy",
154155
"catch_exceptions": true,
155156
"click_do_break_link_to": false,
156157
"context_menu_scaling": false,

0 commit comments

Comments
 (0)