From 9dcc3bf39a65098744b11e22386ae38a566caed5 Mon Sep 17 00:00:00 2001 From: filtered <176114999+webfiltered@users.noreply.github.com> Date: Sat, 3 May 2025 04:06:23 +1000 Subject: [PATCH] [Refactor] Remove redundant code (#1001) Removes redundant code. After being refactored out of LGraphCanvas, the class methods were being passed their own properties as params. --- src/LGraphNode.ts | 31 +++++++++++++++---------------- src/widgets/BaseWidget.ts | 4 +--- src/widgets/BooleanWidget.ts | 13 ++++++------- src/widgets/ButtonWidget.ts | 11 +++++------ src/widgets/ComboWidget.ts | 11 +++++------ src/widgets/KnobWidget.ts | 11 ++++++----- src/widgets/NumberWidget.ts | 11 +++++------ src/widgets/SliderWidget.ts | 11 +++++------ src/widgets/TextWidget.ts | 11 +++++------ 9 files changed, 53 insertions(+), 61 deletions(-) diff --git a/src/LGraphNode.ts b/src/LGraphNode.ts index 7c143e3d99..1371c036e8 100644 --- a/src/LGraphNode.ts +++ b/src/LGraphNode.ts @@ -3374,35 +3374,34 @@ export class LGraphNode implements Positionable, IPinnable, IColorable { }: DrawWidgetsOptions): void { if (!this.widgets) return - const width = this.size[0] - const widgets = this.widgets + const nodeWidth = this.size[0] + const { widgets } = this const H = LiteGraph.NODE_WIDGET_HEIGHT - const show_text = !lowQuality + const showText = !lowQuality ctx.save() ctx.globalAlpha = editorAlpha - const { margin } = BaseWidget - for (const w of widgets) { - if (!this.isWidgetVisible(w)) continue + for (const widget of widgets) { + if (!this.isWidgetVisible(widget)) continue - const y = w.y - const outline_color = w.advanced ? LiteGraph.WIDGET_ADVANCED_OUTLINE_COLOR : LiteGraph.WIDGET_OUTLINE_COLOR + const { y } = widget + const outlineColour = widget.advanced ? LiteGraph.WIDGET_ADVANCED_OUTLINE_COLOR : LiteGraph.WIDGET_OUTLINE_COLOR - w.last_y = y + widget.last_y = y // Disable widget if it is disabled or if the value is passed from socket connection. - w.computedDisabled = w.disabled || this.getSlotFromWidget(w)?.link != null + widget.computedDisabled = widget.disabled || this.getSlotFromWidget(widget)?.link != null - ctx.strokeStyle = outline_color + ctx.strokeStyle = outlineColour ctx.fillStyle = "#222" ctx.textAlign = "left" - if (w.computedDisabled) ctx.globalAlpha *= 0.5 - const widget_width = w.width || width + if (widget.computedDisabled) ctx.globalAlpha *= 0.5 + const width = widget.width || nodeWidth - const WidgetClass: typeof WIDGET_TYPE_MAP[string] = WIDGET_TYPE_MAP[w.type] + const WidgetClass: typeof WIDGET_TYPE_MAP[string] = WIDGET_TYPE_MAP[widget.type] if (WidgetClass) { - toClass(WidgetClass, w).drawWidget(ctx, { y, width: widget_width, show_text, margin }) + toClass(WidgetClass, widget).drawWidget(ctx, { width, showText }) } else { - w.draw?.(ctx, this, widget_width, y, H, lowQuality) + widget.draw?.(ctx, this, width, y, H, lowQuality) } ctx.globalAlpha = editorAlpha } diff --git a/src/widgets/BaseWidget.ts b/src/widgets/BaseWidget.ts index fefd6b0b41..2ed6aa33d9 100644 --- a/src/widgets/BaseWidget.ts +++ b/src/widgets/BaseWidget.ts @@ -6,10 +6,8 @@ import { Point } from "@/interfaces" import { LiteGraph } from "@/litegraph" export interface DrawWidgetOptions { - y: number width: number - show_text?: boolean - margin?: number + showText?: boolean } export interface WidgetEventOptions { diff --git a/src/widgets/BooleanWidget.ts b/src/widgets/BooleanWidget.ts index dd9d43c9dd..fd7839a756 100644 --- a/src/widgets/BooleanWidget.ts +++ b/src/widgets/BooleanWidget.ts @@ -14,23 +14,22 @@ export class BooleanWidget extends BaseWidget implements IBooleanWidget { } override drawWidget(ctx: CanvasRenderingContext2D, { - y, width, - show_text = true, - margin = BaseWidget.margin, + showText = true, }: DrawWidgetOptions) { - const { height } = this + const { height, y } = this + const { margin } = BaseWidget ctx.textAlign = "left" ctx.strokeStyle = this.outline_color ctx.fillStyle = this.background_color ctx.beginPath() - if (show_text) + if (showText) ctx.roundRect(margin, y, width - margin * 2, height, [height * 0.5]) else ctx.rect(margin, y, width - margin * 2, height) ctx.fill() - if (show_text && !this.computedDisabled) ctx.stroke() + if (showText && !this.computedDisabled) ctx.stroke() ctx.fillStyle = this.value ? "#89A" : "#333" ctx.beginPath() ctx.arc( @@ -41,7 +40,7 @@ export class BooleanWidget extends BaseWidget implements IBooleanWidget { Math.PI * 2, ) ctx.fill() - if (show_text) { + if (showText) { ctx.fillStyle = this.secondary_text_color const label = this.label || this.name if (label != null) { diff --git a/src/widgets/ButtonWidget.ts b/src/widgets/ButtonWidget.ts index 76a34bbea7..4f69eb9752 100644 --- a/src/widgets/ButtonWidget.ts +++ b/src/widgets/ButtonWidget.ts @@ -21,17 +21,16 @@ export class ButtonWidget extends BaseWidget implements IButtonWidget { * @param options The options for drawing the widget */ override drawWidget(ctx: CanvasRenderingContext2D, { - y, width, - show_text = true, - margin = BaseWidget.margin, + showText = true, }: DrawWidgetOptions) { // Store original context attributes const originalTextAlign = ctx.textAlign const originalStrokeStyle = ctx.strokeStyle const originalFillStyle = ctx.fillStyle - const { height } = this + const { height, y } = this + const { margin } = BaseWidget // Draw button background ctx.fillStyle = this.background_color @@ -42,13 +41,13 @@ export class ButtonWidget extends BaseWidget implements IButtonWidget { ctx.fillRect(margin, y, width - margin * 2, height) // Draw button outline if not disabled - if (show_text && !this.computedDisabled) { + if (showText && !this.computedDisabled) { ctx.strokeStyle = this.outline_color ctx.strokeRect(margin, y, width - margin * 2, height) } // Draw button text - if (show_text) { + if (showText) { ctx.textAlign = "center" ctx.fillStyle = this.text_color ctx.fillText( diff --git a/src/widgets/ComboWidget.ts b/src/widgets/ComboWidget.ts index 5f3e1a4e54..45e6c06de5 100644 --- a/src/widgets/ComboWidget.ts +++ b/src/widgets/ComboWidget.ts @@ -108,30 +108,29 @@ export class ComboWidget extends BaseSteppedWidget implements IComboWidget { * @param options The options for drawing the widget */ override drawWidget(ctx: CanvasRenderingContext2D, { - y, width, - show_text = true, - margin = BaseWidget.margin, + showText = true, }: DrawWidgetOptions) { // Store original context attributes const originalTextAlign = ctx.textAlign const originalStrokeStyle = ctx.strokeStyle const originalFillStyle = ctx.fillStyle - const { height } = this + const { height, y } = this + const { margin } = BaseWidget ctx.textAlign = "left" ctx.strokeStyle = this.outline_color ctx.fillStyle = this.background_color ctx.beginPath() - if (show_text) + if (showText) ctx.roundRect(margin, y, width - margin * 2, height, [height * 0.5]) else ctx.rect(margin, y, width - margin * 2, height) ctx.fill() - if (show_text) { + if (showText) { if (!this.computedDisabled) { ctx.stroke() this.drawArrowButtons(ctx, margin, y, width) diff --git a/src/widgets/KnobWidget.ts b/src/widgets/KnobWidget.ts index 65860d7fd1..45502b999a 100644 --- a/src/widgets/KnobWidget.ts +++ b/src/widgets/KnobWidget.ts @@ -43,10 +43,8 @@ export class KnobWidget extends BaseWidget implements IKnobWidget { drawWidget( ctx: CanvasRenderingContext2D, { - y, width, - show_text = true, - margin = BaseWidget.margin, + showText = true, }: DrawWidgetOptions, ): void { // Store original context attributes @@ -54,6 +52,9 @@ export class KnobWidget extends BaseWidget implements IKnobWidget { const originalStrokeStyle = ctx.strokeStyle const originalFillStyle = ctx.fillStyle + const { y } = this + const { margin } = BaseWidget + const { gradient_stops = "rgb(14, 182, 201); rgb(0, 216, 72)" } = this.options const effective_height = this.computedHeight || this.height // Draw background @@ -155,7 +156,7 @@ export class KnobWidget extends BaseWidget implements IKnobWidget { ctx.closePath() // Draw outline if not disabled - if (show_text && !this.computedDisabled) { + if (showText && !this.computedDisabled) { ctx.strokeStyle = this.outline_color // Draw value ctx.beginPath() @@ -177,7 +178,7 @@ export class KnobWidget extends BaseWidget implements IKnobWidget { // TODO: TBD later when options work // Draw text - if (show_text) { + if (showText) { ctx.textAlign = "center" ctx.fillStyle = this.text_color const fixedValue = Number(this.value).toFixed(this.options.precision ?? 3) diff --git a/src/widgets/NumberWidget.ts b/src/widgets/NumberWidget.ts index cc51eb9ff4..becb9f5103 100644 --- a/src/widgets/NumberWidget.ts +++ b/src/widgets/NumberWidget.ts @@ -52,30 +52,29 @@ export class NumberWidget extends BaseSteppedWidget implements INumericWidget { * @param options The options for drawing the widget */ override drawWidget(ctx: CanvasRenderingContext2D, { - y, width, - show_text = true, - margin = BaseWidget.margin, + showText = true, }: DrawWidgetOptions) { // Store original context attributes const originalTextAlign = ctx.textAlign const originalStrokeStyle = ctx.strokeStyle const originalFillStyle = ctx.fillStyle - const { height } = this + const { height, y } = this + const { margin } = BaseWidget ctx.textAlign = "left" ctx.strokeStyle = this.outline_color ctx.fillStyle = this.background_color ctx.beginPath() - if (show_text) + if (showText) ctx.roundRect(margin, y, width - margin * 2, height, [height * 0.5]) else ctx.rect(margin, y, width - margin * 2, height) ctx.fill() - if (show_text) { + if (showText) { if (!this.computedDisabled) { ctx.stroke() this.drawArrowButtons(ctx, margin, y, width) diff --git a/src/widgets/SliderWidget.ts b/src/widgets/SliderWidget.ts index dbc20e2914..0d496bb91a 100644 --- a/src/widgets/SliderWidget.ts +++ b/src/widgets/SliderWidget.ts @@ -25,17 +25,16 @@ export class SliderWidget extends BaseWidget implements ISliderWidget { * @param options The options for drawing the widget */ override drawWidget(ctx: CanvasRenderingContext2D, { - y, width, - show_text = true, - margin = BaseWidget.margin, + showText = true, }: DrawWidgetOptions) { // Store original context attributes const originalTextAlign = ctx.textAlign const originalStrokeStyle = ctx.strokeStyle const originalFillStyle = ctx.fillStyle - const { height } = this + const { height, y } = this + const { margin } = BaseWidget // Draw background ctx.fillStyle = this.background_color @@ -51,7 +50,7 @@ export class SliderWidget extends BaseWidget implements ISliderWidget { ctx.fillRect(margin, y, nvalue * (width - margin * 2), height) // Draw outline if not disabled - if (show_text && !this.computedDisabled) { + if (showText && !this.computedDisabled) { ctx.strokeStyle = this.outline_color ctx.strokeRect(margin, y, width - margin * 2, height) } @@ -70,7 +69,7 @@ export class SliderWidget extends BaseWidget implements ISliderWidget { } // Draw text - if (show_text) { + if (showText) { ctx.textAlign = "center" ctx.fillStyle = this.text_color const fixedValue = Number(this.value).toFixed(this.options.precision ?? 3) diff --git a/src/widgets/TextWidget.ts b/src/widgets/TextWidget.ts index 3b81a9cfd7..f374176743 100644 --- a/src/widgets/TextWidget.ts +++ b/src/widgets/TextWidget.ts @@ -20,30 +20,29 @@ export class TextWidget extends BaseWidget implements IStringWidget { * @param options The options for drawing the widget */ override drawWidget(ctx: CanvasRenderingContext2D, { - y, width, - show_text = true, - margin = BaseWidget.margin, + showText = true, }: DrawWidgetOptions) { // Store original context attributes const originalTextAlign = ctx.textAlign const originalStrokeStyle = ctx.strokeStyle const originalFillStyle = ctx.fillStyle - const { height } = this + const { height, y } = this + const { margin } = BaseWidget ctx.textAlign = "left" ctx.strokeStyle = this.outline_color ctx.fillStyle = this.background_color ctx.beginPath() - if (show_text) + if (showText) ctx.roundRect(margin, y, width - margin * 2, height, [height * 0.5]) else ctx.rect(margin, y, width - margin * 2, height) ctx.fill() - if (show_text) { + if (showText) { if (!this.computedDisabled) ctx.stroke() ctx.save() ctx.beginPath()