Add CanvasPointer callback for node widgets (#363)

* Add CanvasPointer callback for node widgets

* Add API documentation

* nit - Docs
This commit is contained in:
filtered
2024-12-02 12:31:48 +11:00
committed by GitHub
parent a0b50dcf15
commit 8cda1ac48a
4 changed files with 228 additions and 1 deletions

View File

@@ -5,6 +5,11 @@ import { dist2 } from "./measure"
/**
* Allows click and drag actions to be declared ahead of time during a pointerdown event.
*
* By default, it retains the most recent event of each type until it is reset (on pointerup).
* - {@link eDown}
* - {@link eMove}
* - {@link eUp}
*
* Depending on whether the user clicks or drags the pointer, only the appropriate callbacks are called:
* - {@link onClick}
* - {@link onDoubleClick}

View File

@@ -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

View File

@@ -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> {
@@ -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
}