[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.
This commit is contained in:
filtered
2025-05-03 04:06:23 +10:00
committed by GitHub
parent b838a70ed2
commit 9dcc3bf39a
9 changed files with 53 additions and 61 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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