Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Properly update cursor style on mouse/keyboard events #407

Merged
merged 2 commits into from
Dec 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading