[Refactor] Abstract legacy link colour properties (#972)

Replaces colour context interface with a simple abstraction object /
interface.
This commit is contained in:
filtered
2025-04-27 06:31:43 +10:00
committed by GitHub
parent 0b46288d32
commit 8e414ab133
4 changed files with 27 additions and 34 deletions

View File

@@ -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<CanvasColour>
default_connection_color_byTypeOff: Dictionary<CanvasColour>
/** 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,
})

View File

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

View File

@@ -410,3 +410,8 @@ export interface ContextMenuDivElement<TValue = unknown> extends HTMLDivElement
}
export type INodeSlotContextItem = [string, ISlotType, Partial<INodeInputSlot & INodeOutputSlot>]
export interface DefaultConnectionColors {
getConnectedColor(type: ISlotType): CanvasColour
getDisconnectedColor(type: ISlotType): CanvasColour
}

View File

@@ -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<CanvasColour>
default_connection_color_byTypeOff: Dictionary<CanvasColour>
}
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(