From 5edfc185c5573a913f5909622ffe9f224bf9dd9b Mon Sep 17 00:00:00 2001 From: Chenlei Hu Date: Mon, 4 Nov 2024 15:09:17 -0500 Subject: [PATCH] Unify pin/unpin interface between LGraphNode and LGraphGroup (#274) --- src/LGraphGroup.ts | 19 +++++++++++++------ src/LGraphNode.ts | 9 +++++++-- src/interfaces.ts | 15 +++++++++++++-- 3 files changed, 33 insertions(+), 10 deletions(-) diff --git a/src/LGraphGroup.ts b/src/LGraphGroup.ts index 6f096ee1a..9d7f6c9f3 100644 --- a/src/LGraphGroup.ts +++ b/src/LGraphGroup.ts @@ -1,9 +1,9 @@ -import type { IContextMenuValue, Point, Positionable, Size } from "./interfaces" +import type { IContextMenuValue, IPinnable, Point, Positionable, Size } from "./interfaces" import type { LGraph } from "./LGraph" import type { ISerialisedGroup } from "./types/serialisation" import { LiteGraph } from "./litegraph" import { LGraphCanvas } from "./LGraphCanvas" -import { isInsideRectangle, containsCentre, containsRect, isPointInRectangle } from "./measure" +import { isInsideRectangle, containsCentre, containsRect } from "./measure" import { LGraphNode } from "./LGraphNode" import { RenderShape, TitleMode } from "./types/globalEnums" @@ -11,7 +11,7 @@ export interface IGraphGroupFlags extends Record { pinned?: true } -export class LGraphGroup implements Positionable { +export class LGraphGroup implements Positionable, IPinnable { static minWidth = 140 static minHeight = 80 static resizeLength = 10 @@ -85,12 +85,19 @@ export class LGraphGroup implements Positionable { return !!this.flags.pinned } - pin(): void { - this.flags.pinned = true + /** + * Prevents the group being accidentally moved or resized by mouse interaction. + * Toggles pinned state if no value is provided. + **/ + pin(value?: boolean): void { + const newState = value === undefined ? !this.pinned : value + + if (newState) this.flags.pinned = true + else delete this.flags.pinned } unpin(): void { - delete this.flags.pinned + this.pin(false) } configure(o: ISerialisedGroup): void { diff --git a/src/LGraphNode.ts b/src/LGraphNode.ts index 61a21ce50..a72e2e69d 100644 --- a/src/LGraphNode.ts +++ b/src/LGraphNode.ts @@ -1,4 +1,4 @@ -import type { Dictionary, IContextMenuValue, IFoundSlot, INodeFlags, INodeInputSlot, INodeOutputSlot, IOptionalSlotData, ISlotType, Point, Positionable, ReadOnlyRect, Rect, Size } from "./interfaces" +import type { Dictionary, IContextMenuValue, IFoundSlot, INodeFlags, INodeInputSlot, INodeOutputSlot, IOptionalSlotData, IPinnable, ISlotType, Point, Positionable, ReadOnlyRect, Rect, Size } from "./interfaces" import type { LGraph } from "./LGraph" import type { IWidget, TWidgetValue } from "./types/widgets" import type { ISerialisedNode } from "./types/serialisation" @@ -111,7 +111,7 @@ export interface LGraphNode { * Base Class for all the node type classes * @param {String} name a name for the node */ -export class LGraphNode implements Positionable { +export class LGraphNode implements Positionable, IPinnable { // Static properties used by dynamic child classes static title?: string static MAX_CONSOLE?: number @@ -2329,6 +2329,7 @@ export class LGraphNode implements Positionable { /** * Prevents the node being accidentally moved or resized by mouse interaction. + * Toggles pinned state if no value is provided. **/ pin(v?: boolean): void { this.graph._version++ @@ -2342,6 +2343,10 @@ export class LGraphNode implements Positionable { delete this.flags.pinned } + unpin(): void { + this.pin(false) + } + localToScreen(x: number, y: number, dragAndScale: DragAndScale): Point { return [ (x + this.pos[0]) * dragAndScale.scale + dragAndScale.offset[0], diff --git a/src/interfaces.ts b/src/interfaces.ts index d7103328e..4ef9311d9 100644 --- a/src/interfaces.ts +++ b/src/interfaces.ts @@ -12,9 +12,9 @@ export type NullableProperties = { export type CanvasColour = string | CanvasGradient | CanvasPattern -/** +/** * An object that can be positioned, selected, and moved. - * + * * May contain other {@link Positionable} objects. */ export interface Positionable { @@ -46,6 +46,17 @@ export interface Positionable { onDeselected?(): void } +/** + * An object that can be pinned. + * + * Prevents the object being accidentally moved or resized by mouse interaction. + */ +export interface IPinnable { + pinned: boolean + pin(value?: boolean): void + unpin(): void +} + export interface IInputOrOutput { // If an input, this will be defined input?: INodeInputSlot