From 7d77fceffa187f17bf25c49983d5510711a01581 Mon Sep 17 00:00:00 2001 From: Chenlei Hu Date: Mon, 30 Dec 2024 00:10:26 -0500 Subject: [PATCH] Properly update cursor style on mouse/keyboard events (#407) * Properly update cursor style on mouse/keyboard events * nit --- src/LGraphCanvas.ts | 44 +++++++++++++++++++++++++++++--------------- 1 file changed, 29 insertions(+), 15 deletions(-) diff --git a/src/LGraphCanvas.ts b/src/LGraphCanvas.ts index 27ef37a..dca2dc4 100644 --- a/src/LGraphCanvas.ts +++ b/src/LGraphCanvas.ts @@ -226,6 +226,23 @@ export class LGraphCanvas { shouldSetCursor: true, } + #updateCursorStyle() { + if (this.state.shouldSetCursor) { + let cursor = "default" + if (this.state.draggingCanvas) { + cursor = "grabbing" + } else if (this.state.readOnly) { + cursor = "grab" + } else if (this.state.hoveringOver & CanvasItem.ResizeSe) { + cursor = "se-resize" + } else if (this.state.hoveringOver & CanvasItem.Node) { + cursor = "crosshair" + } + + this.canvas.style.cursor = cursor + } + } + // Whether the canvas was previously being dragged prior to pressing space key. // null if space key is not pressed. private _previously_dragging_canvas: boolean | null = null @@ -238,6 +255,7 @@ export class LGraphCanvas { set read_only(value: boolean) { this.state.readOnly = value + this.#updateCursorStyle() } get isDragging(): boolean { @@ -246,6 +264,16 @@ export class LGraphCanvas { set isDragging(value: boolean) { this.state.draggingItems = value + this.#updateCursorStyle() + } + + get hoveringOver(): CanvasItem { + return this.state.hoveringOver + } + + set hoveringOver(value: CanvasItem) { + this.state.hoveringOver = value + this.#updateCursorStyle() } /** @deprecated Replace all references with {@link pointer}.{@link CanvasPointer.isDown isDown}. */ @@ -3070,21 +3098,7 @@ export class LGraphCanvas { if (this.resizing_node) underPointer |= CanvasItem.ResizeSe } - this.state.hoveringOver = underPointer - - if (this.state.shouldSetCursor) { - if (this.state.draggingCanvas) { - this.canvas.style.cursor = "grabbing" - } else if (this.state.readOnly) { - this.canvas.style.cursor = "grab" - } else if (!underPointer) { - this.canvas.style.cursor = "default" - } else if (underPointer & CanvasItem.ResizeSe) { - this.canvas.style.cursor = "se-resize" - } else if (underPointer & CanvasItem.Node) { - this.canvas.style.cursor = "crosshair" - } - } + this.hoveringOver = underPointer e.preventDefault() return