Skip to content

Commit 6a01b08

Browse files
authored
Subgraph widget promotion - Part 1 (#5537)
* Prerequisite tweaks for subgraph widget promotion * Clean up DOMWidget tracking on graph change * Mark migrated CombOWidget functions private * Cleanup placeholder node cast
1 parent ede43c5 commit 6a01b08

File tree

5 files changed

+70
-22
lines changed

5 files changed

+70
-22
lines changed

knip.config.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@ const config: KnipConfig = {
2525
'src/types/generatedManagerTypes.ts',
2626
'src/types/comfyRegistryTypes.ts',
2727
// Used by a custom node (that should move off of this)
28-
'src/scripts/ui/components/splitButton.ts'
28+
'src/scripts/ui/components/splitButton.ts',
29+
// Staged for for use with subgraph widget promotion
30+
'src/lib/litegraph/src/widgets/DisconnectedWidget.ts'
2931
],
3032
compilers: {
3133
// https://github.com/webpro-nl/knip/issues/1008#issuecomment-3207756199

src/components/graph/DomWidgets.vue

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ const updateWidgets = () => {
3434
const widget = widgetState.widget
3535
3636
// Early exit for non-visible widgets
37-
if (!widget.isVisible()) {
37+
if (!widget.isVisible() || !widgetState.active) {
3838
widgetState.visible = false
3939
continue
4040
}

src/lib/litegraph/src/widgets/ComboWidget.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ export class ComboWidget
4545
return typeof this.value === 'number' ? String(this.value) : this.value
4646
}
4747

48-
#getValues(node: LGraphNode): Values {
48+
private getValues(node: LGraphNode): Values {
4949
const { values } = this.options
5050
if (values == null) throw new Error('[ComboWidget]: values is required')
5151

@@ -57,7 +57,7 @@ export class ComboWidget
5757
* @param increment `true` if checking the use of the increment button, `false` for decrement
5858
* @returns `true` if the value is at the given index, otherwise `false`.
5959
*/
60-
#canUseButton(increment: boolean): boolean {
60+
private canUseButton(increment: boolean): boolean {
6161
const { values } = this.options
6262
// If using legacy duck-typed method, false is the most permissive return value
6363
if (typeof values === 'function') return false
@@ -78,23 +78,23 @@ export class ComboWidget
7878
* Handles edge case where the value is both the first and last item in the list.
7979
*/
8080
override canIncrement(): boolean {
81-
return this.#canUseButton(true)
81+
return this.canUseButton(true)
8282
}
8383

8484
override canDecrement(): boolean {
85-
return this.#canUseButton(false)
85+
return this.canUseButton(false)
8686
}
8787

8888
override incrementValue(options: WidgetEventOptions): void {
89-
this.#tryChangeValue(1, options)
89+
this.tryChangeValue(1, options)
9090
}
9191

9292
override decrementValue(options: WidgetEventOptions): void {
93-
this.#tryChangeValue(-1, options)
93+
this.tryChangeValue(-1, options)
9494
}
9595

96-
#tryChangeValue(delta: number, options: WidgetEventOptions): void {
97-
const values = this.#getValues(options.node)
96+
private tryChangeValue(delta: number, options: WidgetEventOptions): void {
97+
const values = this.getValues(options.node)
9898
const indexedValues = toArray(values)
9999

100100
// avoids double click event
@@ -128,7 +128,7 @@ export class ComboWidget
128128
if (x > width - 40) return this.incrementValue({ e, node, canvas })
129129

130130
// Otherwise, show dropdown menu
131-
const values = this.#getValues(node)
131+
const values = this.getValues(node)
132132
const values_list = toArray(values)
133133

134134
// Handle center click - show dropdown menu
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { LGraphNode } from '@/lib/litegraph/src/LGraphNode'
2+
import type { IButtonWidget } from '@/lib/litegraph/src/types/widgets'
3+
4+
import { BaseWidget, type DrawWidgetOptions } from './BaseWidget'
5+
6+
class DisconnectedWidget extends BaseWidget<IButtonWidget> {
7+
constructor(widget: IButtonWidget) {
8+
super(widget, new LGraphNode('DisconnectedPlaceholder'))
9+
this.disabled = true
10+
}
11+
12+
override drawWidget(
13+
ctx: CanvasRenderingContext2D,
14+
{ width, showText = true }: DrawWidgetOptions
15+
) {
16+
ctx.save()
17+
this.drawWidgetShape(ctx, { width, showText })
18+
if (showText) {
19+
this.drawTruncatingText({ ctx, width, leftPadding: 0, rightPadding: 0 })
20+
}
21+
ctx.restore()
22+
}
23+
24+
override onClick() {}
25+
26+
override get _displayValue() {
27+
return 'Disconnected'
28+
}
29+
}
30+
const conf: IButtonWidget = {
31+
type: 'button',
32+
value: undefined,
33+
name: 'Disconnected',
34+
options: {},
35+
y: 0,
36+
clicked: false
37+
}
38+
export const disconnectedWidget = new DisconnectedWidget(conf)

src/scripts/app.ts

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import {
3535
isComboInputSpecV1,
3636
isComboInputSpecV2
3737
} from '@/schemas/nodeDefSchema'
38+
import { type BaseDOMWidget, DOMWidgetImpl } from '@/scripts/domWidget'
3839
import { getFromWebmFile } from '@/scripts/metadata/ebml'
3940
import { getGltfBinaryMetadata } from '@/scripts/metadata/gltf'
4041
import { getFromIsobmffFile } from '@/scripts/metadata/isobmff'
@@ -837,22 +838,29 @@ export class ComfyApp {
837838
this.canvas.canvas.addEventListener<'litegraph:set-graph'>(
838839
'litegraph:set-graph',
839840
(e) => {
840-
// Assertion: Not yet defined in litegraph.
841841
const { newGraph } = e.detail
842842

843-
const nodeSet = new Set(newGraph.nodes)
844843
const widgetStore = useDomWidgetStore()
845844

846-
// Assertions: UnwrapRef
847-
for (const { widget } of widgetStore.activeWidgetStates) {
848-
if (!nodeSet.has(widget.node)) {
849-
widgetStore.deactivateWidget(widget.id)
850-
}
851-
}
845+
const activeWidgets: Record<
846+
string,
847+
BaseDOMWidget<object | string>
848+
> = Object.fromEntries(
849+
newGraph.nodes
850+
.flatMap((node) => node.widgets ?? [])
851+
.filter((w) => w instanceof DOMWidgetImpl)
852+
.map((w) => [w.id, w])
853+
)
852854

853-
for (const { widget } of widgetStore.inactiveWidgetStates) {
854-
if (nodeSet.has(widget.node)) {
855-
widgetStore.activateWidget(widget.id)
855+
for (const [
856+
widgetId,
857+
widgetState
858+
] of widgetStore.widgetStates.entries()) {
859+
if (widgetId in activeWidgets) {
860+
widgetState.active = true
861+
widgetState.widget = activeWidgets[widgetId]
862+
} else {
863+
widgetState.active = false
856864
}
857865
}
858866
}

0 commit comments

Comments
 (0)