From 8e414ab1331ba47016b84cdc65f2ee18161e86ed Mon Sep 17 00:00:00 2001 From: filtered <176114999+webfiltered@users.noreply.github.com> Date: Sun, 27 Apr 2025 06:31:43 +1000 Subject: [PATCH] [Refactor] Abstract legacy link colour properties (#972) Replaces colour context interface with a simple abstraction object / interface. --- src/LGraphCanvas.ts | 18 +++++++++++++++--- src/LGraphNode.ts | 4 ++-- src/interfaces.ts | 5 +++++ src/node/NodeSlot.ts | 34 +++++----------------------------- 4 files changed, 27 insertions(+), 34 deletions(-) diff --git a/src/LGraphCanvas.ts b/src/LGraphCanvas.ts index 10cc2f6cd..2e1b9682e 100644 --- a/src/LGraphCanvas.ts +++ b/src/LGraphCanvas.ts @@ -4,6 +4,7 @@ import type { ColorOption, ConnectingLink, ContextMenuDivElement, + DefaultConnectionColors, Dictionary, Direction, IBoundaryNodes, @@ -26,7 +27,6 @@ import type { Size, } from "./interfaces" import type { LGraph } from "./LGraph" -import type { ConnectionColorContext } from "./node/NodeSlot" import type { CanvasEventDetail, CanvasMouseEvent, @@ -201,7 +201,7 @@ interface ICreatePanelOptions { * This class is in charge of rendering one graph inside a canvas. And provides all the interaction required. * Valid callbacks are: onNodeSelected, onNodeDeselected, onShowNodePanel, onNodeDblClicked */ -export class LGraphCanvas implements ConnectionColorContext { +export class LGraphCanvas { // Optimised buffers used during rendering static #temp = new Float32Array(4) static #temp_vec2 = new Float32Array(2) @@ -399,6 +399,18 @@ export class LGraphCanvas implements ConnectionColorContext { default_connection_color_byType: Dictionary default_connection_color_byTypeOff: Dictionary + + /** Gets link colours. Extremely basic impl. until the legacy object dictionaries are removed. */ + colourGetter: DefaultConnectionColors = { + getConnectedColor: (type: string) => + this.default_connection_color_byType[type] || + this.default_connection_color.output_on, + getDisconnectedColor: (type: string) => + this.default_connection_color_byTypeOff[type] || + this.default_connection_color_byType[type] || + this.default_connection_color.output_off, + } + highquality_render: boolean use_gradients: boolean editor_alpha: number @@ -4368,7 +4380,7 @@ export class LGraphCanvas implements ConnectionColorContext { node.arrange() node.drawSlots(ctx, { fromSlot: this.linkConnector.renderLinks[0]?.fromSlot, - colorContext: this, + colorContext: this.colourGetter, editorAlpha: this.editor_alpha, lowQuality: this.low_quality, }) diff --git a/src/LGraphNode.ts b/src/LGraphNode.ts index 9d247a95c..d710979ce 100644 --- a/src/LGraphNode.ts +++ b/src/LGraphNode.ts @@ -2,6 +2,7 @@ import type { DragAndScale } from "./DragAndScale" import type { IDrawBoundingOptions } from "./draw" import type { ColorOption, + DefaultConnectionColors, Dictionary, IColorable, IContextMenuValue, @@ -20,7 +21,6 @@ import type { Size, } from "./interfaces" import type { LGraph } from "./LGraph" -import type { ConnectionColorContext } from "./node/NodeSlot" import type { Reroute, RerouteId } from "./Reroute" import type { CanvasMouseEvent } from "./types/events" import type { ISerialisedNode } from "./types/serialisation" @@ -92,7 +92,7 @@ export interface FindFreeSlotOptions { interface DrawSlotsOptions { fromSlot?: INodeInputSlot | INodeOutputSlot - colorContext: ConnectionColorContext + colorContext: DefaultConnectionColors editorAlpha: number lowQuality: boolean } diff --git a/src/interfaces.ts b/src/interfaces.ts index 15a3fccfc..cc53b3c8b 100644 --- a/src/interfaces.ts +++ b/src/interfaces.ts @@ -410,3 +410,8 @@ export interface ContextMenuDivElement extends HTMLDivElement } export type INodeSlotContextItem = [string, ISlotType, Partial] + +export interface DefaultConnectionColors { + getConnectedColor(type: ISlotType): CanvasColour + getDisconnectedColor(type: ISlotType): CanvasColour +} diff --git a/src/node/NodeSlot.ts b/src/node/NodeSlot.ts index 6a65f1a4a..c84f7d5dc 100644 --- a/src/node/NodeSlot.ts +++ b/src/node/NodeSlot.ts @@ -1,4 +1,4 @@ -import type { CanvasColour, Dictionary, INodeInputSlot, INodeOutputSlot, INodeSlot, ISlotType, IWidgetLocator, OptionalProps, Point, ReadOnlyPoint, Rect } from "@/interfaces" +import type { CanvasColour, DefaultConnectionColors, INodeInputSlot, INodeOutputSlot, INodeSlot, ISlotType, IWidgetLocator, OptionalProps, Point, ReadOnlyPoint, Rect } from "@/interfaces" import type { LGraphNode } from "@/LGraphNode" import { LabelPosition, SlotShape, SlotType } from "@/draw" @@ -8,19 +8,8 @@ import { LinkDirection, RenderShape } from "@/types/globalEnums" import { NodeInputSlot } from "./NodeInputSlot" -export interface ConnectionColorContext { - default_connection_color: { - input_off: string - input_on: string - output_off: string - output_on: string - } - default_connection_color_byType: Dictionary - default_connection_color_byTypeOff: Dictionary -} - export interface IDrawOptions { - colorContext: ConnectionColorContext + colorContext: DefaultConnectionColors labelPosition?: LabelPosition lowQuality?: boolean doStroke?: boolean @@ -94,23 +83,10 @@ export abstract class NodeSlot implements INodeSlot { abstract isConnected(): boolean - connectedColor(context: ConnectionColorContext): CanvasColour { - return this.color_on || - context.default_connection_color_byType[this.type] || - context.default_connection_color.output_on - } - - disconnectedColor(context: ConnectionColorContext): CanvasColour { - return this.color_off || - context.default_connection_color_byTypeOff[this.type] || - context.default_connection_color_byType[this.type] || - context.default_connection_color.output_off - } - - renderingColor(context: ConnectionColorContext): CanvasColour { + renderingColor(colorContext: DefaultConnectionColors): CanvasColour { return this.isConnected() - ? this.connectedColor(context) - : this.disconnectedColor(context) + ? this.color_on || colorContext.getConnectedColor(this.type) + : this.color_off || colorContext.getDisconnectedColor(this.type) } draw(