Skip to content

Commit

Permalink
Properly update cursor style on mouse/keyboard events (#407)
Browse files Browse the repository at this point in the history
* Properly update cursor style on mouse/keyboard events

* nit
  • Loading branch information
huchenlei authored Dec 30, 2024
1 parent 757560f commit 7d77fce
Showing 1 changed file with 29 additions and 15 deletions.
44 changes: 29 additions & 15 deletions src/LGraphCanvas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -238,6 +255,7 @@ export class LGraphCanvas {

set read_only(value: boolean) {
this.state.readOnly = value
this.#updateCursorStyle()
}

get isDragging(): boolean {
Expand All @@ -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}. */
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 7d77fce

Please sign in to comment.