Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 18 additions & 6 deletions src/LGraphNode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3411,42 +3411,54 @@ export class LGraphNode implements Positionable, IPinnable, IColorable {
return !isHidden
}

// sync number of widgets with the number of widgets in the node
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the comment referring to?

drawWidgets(ctx: CanvasRenderingContext2D, {
lowQuality = false,
editorAlpha = 1,
}: DrawWidgetsOptions): void {
if (!this.widgets) return

const nodeWidth = this.size[0]
const { widgets } = this
const H = LiteGraph.NODE_WIDGET_HEIGHT
const showText = !lowQuality
ctx.save()
ctx.globalAlpha = editorAlpha

for (const widget of widgets) {
if (!this.isWidgetVisible(widget)) continue

const { y } = widget
const outlineColour = widget.advanced ? LiteGraph.WIDGET_ADVANCED_OUTLINE_COLOR : LiteGraph.WIDGET_OUTLINE_COLOR
const outlineColour = widget.advanced
? LiteGraph.WIDGET_ADVANCED_OUTLINE_COLOR
: LiteGraph.WIDGET_OUTLINE_COLOR

widget.last_y = y
// Disable widget if it is disabled or if the value is passed from socket connection.
widget.computedDisabled = widget.disabled || this.getSlotFromWidget(widget)?.link != null

// Check if locked: either disabled or has an input connection
const connectedSlot = this.getSlotFromWidget(widget)
widget.computedDisabled = widget.disabled || connectedSlot?.link != null
;(widget as any).hideValue = connectedSlot?.link != null
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Never use type assertions.


ctx.strokeStyle = outlineColour
ctx.fillStyle = "#222"
ctx.textAlign = "left"

if (widget.computedDisabled) ctx.globalAlpha *= 0.5
const width = widget.width || nodeWidth

const forceHideValue = (widget as any).hideValue
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Never use type assertions. We need to look through the code to understand the classes, interfaces, types, and so on. After that, we should implement a solution that naturally extends the current system. If we do this successfuly, we may find that we never need to use type assertions.

To do this properly, it may require approaching the problem by doing research and asking LLM to explain the architecture and objects/classes in the codebase first.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure ty for the advice.


if (typeof widget.draw === "function") {
widget.draw(ctx, this, width, y, H, lowQuality)
} else {
toConcreteWidget(widget, this, false)?.drawWidget(ctx, { width, showText })
toConcreteWidget(widget, this, false)?.drawWidget(ctx, {
width,
showText: !forceHideValue,
})
}

ctx.globalAlpha = editorAlpha
}

ctx.restore()
}

Expand Down
15 changes: 15 additions & 0 deletions src/widgets/BaseSteppedWidget.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import type { IBaseWidget } from "@/types/widgets"

import { drawTextInArea } from "@/draw"
import { Rectangle } from "@/infrastructure/Rectangle"

import { BaseWidget, type DrawWidgetOptions, type WidgetEventOptions } from "./BaseWidget"

/**
Expand Down Expand Up @@ -64,6 +67,18 @@ export abstract class BaseSteppedWidget<TWidget extends IBaseWidget = IBaseWidge
if (!this.computedDisabled) this.drawArrowButtons(ctx, options.width)

this.drawTruncatingText({ ctx, width: options.width })
} else {
// Just draw the name, truncated
const { margin } = BaseWidget
const x = margin * 2
const area = new Rectangle(x, this.y, options.width - x - margin, this.height * 0.7)
ctx.fillStyle = this.secondary_text_color
drawTextInArea({
ctx,
text: this.displayName,
area,
align: "left",
})
Comment on lines +72 to +81
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It seems we are repeating this logic twice, can we extract to helper function or class method?

}

// Restore original context attributes
Expand Down
15 changes: 15 additions & 0 deletions src/widgets/TextWidget.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
import type { LGraphNode } from "@/LGraphNode"
import type { IStringWidget } from "@/types/widgets"

import { drawTextInArea } from "@/draw"
import { Rectangle } from "@/infrastructure/Rectangle"

import { BaseWidget, type DrawWidgetOptions, type WidgetEventOptions } from "./BaseWidget"

export class TextWidget extends BaseWidget<IStringWidget> implements IStringWidget {
Expand All @@ -26,6 +29,18 @@ export class TextWidget extends BaseWidget<IStringWidget> implements IStringWidg

if (showText) {
this.drawTruncatingText({ ctx, width, leftPadding: 0, rightPadding: 0 })
} else {
// Just draw the name, truncated
const { margin } = BaseWidget
const x = margin * 2
const area = new Rectangle(x, this.y, width - x - margin, this.height * 0.7)
ctx.fillStyle = this.secondary_text_color
drawTextInArea({
ctx,
text: this.displayName,
area,
align: "left",
})
}

// Restore original context attributes
Expand Down
Loading