Skip to content

Commit

Permalink
Add CanvasPointer callback for node widgets
Browse files Browse the repository at this point in the history
  • Loading branch information
webfiltered committed Nov 30, 2024
1 parent 2d6adf4 commit ced02c5
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
7 changes: 7 additions & 0 deletions src/LGraphCanvas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2517,6 +2517,13 @@ export class LGraphCanvas {

#processWidgetClick(e: CanvasPointerEvent, node: LGraphNode, widget: IWidget) {
const { pointer } = this

// Custom widget - CanvasPointer
if (typeof widget.onPointerDown === "function") {
const handled = widget.onPointerDown(pointer, node, this)
if (handled) return
}

const width = widget.width || node.width

const oldValue = widget.value
Expand Down
17 changes: 16 additions & 1 deletion src/types/widgets.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CanvasColour, Point, Size } from "../interfaces"
import type { LGraphCanvas, LGraphNode } from "../litegraph"
import type { CanvasPointer, LGraphCanvas, LGraphNode } from "../litegraph"
import type { CanvasMouseEvent, CanvasPointerEvent } from "./events"

export interface IWidgetOptions<TValue = unknown> extends Record<string, unknown> {
Expand Down Expand Up @@ -151,4 +151,19 @@ export interface IBaseWidget<TElement extends HTMLElement = HTMLElement> {
H: number,
): void
computeSize?(width: number): Size

/**
* Callback for pointerdown events, allowing custom widgets to register callbacks to occur
* for all {@link CanvasPointer} events.
*
* This callback is operated early in the pointerdown logic; actions that prevent it from firing are:
* - `Ctrl + Drag` Multi-select
* - `Alt + Click/Drag` Clone node
* @param pointer The CanvasPointer handling this event
* @param node The node this widget belongs to
* @param canvas The LGraphCanvas where this event originated
* @return Returning `true` from this callback forces Litegraph to ignore the event and
* not process it any further.
*/
onPointerDown(pointer: CanvasPointer, node: LGraphNode, canvas: LGraphCanvas): boolean
}

0 comments on commit ced02c5

Please sign in to comment.