diff --git a/browser_tests/tests/actionbar.spec.ts b/browser_tests/tests/actionbar.spec.ts index fe0f2ca6ed3..6938956019b 100644 --- a/browser_tests/tests/actionbar.spec.ts +++ b/browser_tests/tests/actionbar.spec.ts @@ -24,9 +24,9 @@ webSocketTest.describe( const sampler = window.app!.graph._nodes.find( (node) => node.type === 'KSampler' ) - const control = sampler?.widgets?.find( - (widget) => widget.name === 'control_after_generate' - ) + const control = sampler + ?.getLayoutWidgets() + .find((widget) => widget.name === 'control_after_generate') if (!control) throw new Error('seed control widget missing') control.value = 'fixed' ;( diff --git a/browser_tests/tests/selectionToolboxActions.spec.ts b/browser_tests/tests/selectionToolboxActions.spec.ts index 99a7321961d..df78aa6a0c1 100644 --- a/browser_tests/tests/selectionToolboxActions.spec.ts +++ b/browser_tests/tests/selectionToolboxActions.spec.ts @@ -320,9 +320,9 @@ test.describe('Selection Toolbox - Button Actions', { tag: '@ui' }, () => { const sampler = window.app!.graph._nodes.find( (node) => node.type === 'KSampler' ) - const control = sampler?.widgets?.find( - (widget) => widget.name === 'control_after_generate' - ) + const control = sampler + ?.getLayoutWidgets() + .find((widget) => widget.name === 'control_after_generate') if (!control) throw new Error('seed control widget missing') control.value = 'randomize' }) diff --git a/browser_tests/tests/subgraph/subgraphPromotion.spec.ts b/browser_tests/tests/subgraph/subgraphPromotion.spec.ts index 4c8fa2f427d..e942ff54110 100644 --- a/browser_tests/tests/subgraph/subgraphPromotion.spec.ts +++ b/browser_tests/tests/subgraph/subgraphPromotion.spec.ts @@ -244,7 +244,7 @@ test.describe( const ksampler = await comfyPage.nodeOps.getNodeRefById('1') await ksampler.click('title') - const stepsWidget = await ksampler.getWidget(2) + const stepsWidget = await ksampler.getWidgetByName('steps') const widgetPos = await stepsWidget.getPosition() await comfyPage.canvasOps.mouseClickAt(widgetPos, { button: 'right' }) @@ -270,7 +270,7 @@ test.describe( await subgraphNode.navigateIntoSubgraph() const ksampler = await comfyPage.nodeOps.getNodeRefById('1') - const stepsWidget = await ksampler.getWidget(2) + const stepsWidget = await ksampler.getWidgetByName('steps') const widgetPos = await stepsWidget.getPosition() await comfyPage.canvasOps.mouseClickAt(widgetPos, { button: 'right' }) diff --git a/docs/adr/ECS-WIDGETS-0023-widget-entities-with-a-legacy-layer.md b/docs/adr/ECS-WIDGETS-0023-widget-entities-with-a-legacy-layer.md index 8abb878eff8..6c32e8ad09e 100644 --- a/docs/adr/ECS-WIDGETS-0023-widget-entities-with-a-legacy-layer.md +++ b/docs/adr/ECS-WIDGETS-0023-widget-entities-with-a-legacy-layer.md @@ -100,6 +100,22 @@ may retain a partial registration or removal. measuring remaining use over a stated observation window. Define the threshold and rollback condition in the deprecation proposal. +### Migrate widget controls + +Control rows no longer appear in `node.widgets`. Extensions must use these +replacements: + +- Call `addValueControlWidget()` or `addValueControlWidgets()` to configure a + target widget. Read its `controlConfig` instead of finding a linked control + widget. +- Call `node.getLayoutWidgets()` only when code needs the transient rows used + by classic canvas layout. Do not store or serialize those projections. +- Keep workflow persistence positional. A controlled target serializes as + `[targetValue, controlMode]`, followed by the filter for controls that have + one. Control values are not part of API prompt serialization. +- Remove extension callbacks attached to a control row. The graph-level control + system updates the target and invokes the target widget callback. + Removing the compatibility adapter requires a follow-up ADR or an amendment to this one. diff --git a/docs/architecture/domains/boundaries.json b/docs/architecture/domains/boundaries.json index cb77f71973a..2ec53afd16d 100644 --- a/docs/architecture/domains/boundaries.json +++ b/docs/architecture/domains/boundaries.json @@ -1390,7 +1390,6 @@ "src/composables/node/useWatchWidget.test.ts", "src/core/graph/widgets/__fixtures__/**", "src/core/graph/widgets/comboWidgetInventory.ts", - "src/core/graph/widgets/controlWidgetMarker.ts", "src/core/graph/widgets/dynamicGroupWidget.ts", "src/core/graph/widgets/dynamicGroupWidget.test.ts", "src/core/graph/widgets/dynamicInputSpec.ts", @@ -1403,8 +1402,8 @@ "src/core/graph/widgets/nodeWidgetValues.test.ts", "src/core/graph/widgets/revealDynamicInputSlot.ts", "src/core/graph/widgets/revealDynamicInputSlot.test.ts", - "src/core/graph/widgets/valueControl.ts", - "src/core/graph/widgets/valueControl.test.ts", + "src/core/graph/widgets/control/valueControl.ts", + "src/core/graph/widgets/control/valueControl.test.ts", "src/core/graph/widgets/valueControlWidgets.ts", "src/core/graph/widgets/valueControlWidgets.controlAfterGenerateName.test.ts", "src/extensions/core/customWidgets.ts", @@ -1746,6 +1745,7 @@ "src/scripts/errorNodeWidgets.ts", "src/scripts/errorNodeWidgets.test.ts", "src/scripts/widgets.ts", + "src/scripts/widgets.test.ts", "src/stores/domWidgetStore.ts", "src/stores/domWidgetStore.test.ts", "src/stores/widgetStore.ts", diff --git a/docs/architecture/domains/records/node-widgets.domain.json b/docs/architecture/domains/records/node-widgets.domain.json index 68fb70e1ff2..5d267cc4240 100644 --- a/docs/architecture/domains/records/node-widgets.domain.json +++ b/docs/architecture/domains/records/node-widgets.domain.json @@ -12,9 +12,6 @@ { "path": "src/core/graph/widgets/comboWidgetInventory.ts" }, - { - "path": "src/core/graph/widgets/controlWidgetMarker.ts" - }, { "path": "src/core/graph/widgets/dynamicGroupWidget.ts" }, @@ -37,7 +34,7 @@ "path": "src/core/graph/widgets/revealDynamicInputSlot.ts" }, { - "path": "src/core/graph/widgets/valueControl.ts" + "path": "src/core/graph/widgets/control/valueControl.ts" }, { "path": "src/core/graph/widgets/valueControlWidgets.ts" @@ -135,7 +132,7 @@ "checks": [ "src/core/graph/widgets/dynamicWidgets.test.ts", "src/core/graph/widgets/dynamicGroupWidget.test.ts", - "src/core/graph/widgets/valueControl.test.ts", + "src/core/graph/widgets/control/valueControl.test.ts", "src/core/graph/widgets/valueControlWidgets.controlAfterGenerateName.test.ts" ] }, diff --git a/docs/architecture/domains/records/subgraph-promotion.domain.json b/docs/architecture/domains/records/subgraph-promotion.domain.json index 9676132a6b9..304d0ec8b33 100644 --- a/docs/architecture/domains/records/subgraph-promotion.domain.json +++ b/docs/architecture/domains/records/subgraph-promotion.domain.json @@ -32,7 +32,7 @@ "checks": [ "src/core/graph/subgraph/resolveConcretePromotedWidget.test.ts", "src/core/graph/subgraph/promotedInputWidget.test.ts", - "src/core/graph/subgraph/promotedWidgetControl.test.ts", + "src/core/graph/widgets/control/widgetControlSystem.promoted.test.ts", "src/core/graph/subgraph/promotionAfterReplacement.test.ts" ] }, diff --git a/docs/specs/widget-control-ecs.md b/docs/specs/widget-control-ecs.md new file mode 100644 index 00000000000..f61df35a91a --- /dev/null +++ b/docs/specs/widget-control-ecs.md @@ -0,0 +1,139 @@ +# Widget Control ECS + +## Objective + +Replace `control_after_generate` widgets with plain control state attached to +the widget they control and one graph-level system that advances controlled +values during queueing. + +The workflow format and user-visible controls remain compatible. The runtime +model changes from linked widgets with callbacks to a Component and System. + +## Model + +Each controlled target has one `WidgetControlState` keyed by the target's +canonical `WidgetId`: + +```ts +interface WidgetControlState { + mode: 'fixed' | 'increment' | 'increment-wrap' | 'decrement' | 'randomize' + filter?: string + hasExecuted: boolean +} +``` + +- `filter` exists only for combo controls that expose a filter field. +- `hasExecuted` implements the existing first-run skip in "before" mode. +- The component contains no widget, node, graph, callback, or DOM references. +- The target value and options remain in `widgetValueStore`. +- Popover, focus, and hover state remain local to the view. + +The component is registered when the target receives its `WidgetId`, released +with the target, renamed with the target, and cleared with the root graph. +Re-registration preserves the current component state. Replacing the target +with a different widget type removes stale control state. + +## System + +`runWidgetControl(rootGraph, phase, controlMode)` runs once in each existing queue +phase. + +1. Skip the phase not selected by the captured `controlMode`. +2. Query controls scoped to the root graph. +3. Derive link-fed target IDs from the live graph and skip them. +4. In "before" mode, mark the component executed and skip its first run. +5. Compute the next value with the existing pure value-control rules. +6. Write through `widgetValueStore`. + +There is no general ECS scheduler. The existing queue lifecycle is the system's +explicit scheduling boundary. + +## Presentation + +The control is not present in `node.widgets` and is not identified through +`linkedWidgets`. + +- Classic canvas rendering projects transient mode and filter rows beside the + target. Projection objects may be cached for stable layout but hold no source + of truth. +- Vue rendering reads and updates `WidgetControlState` directly by target + `WidgetId`. +- Control labels continue to reflect the before/after user setting. + +## Persistence + +Existing workflows retain their positional layout: + +- Number: `[targetValue, controlMode]` +- Combo: `[targetValue, controlMode, filter]` + +Serialization appends component values after each controlled target. +Configuration consumes valid control slots after registering the target. A +missing or invalid control slot must not consume the next target widget's +value. Control values remain absent from API prompt serialization. + +Named widget values include compatibility entries for each control mode and +optional filter, using the same stable names as the classic projections. +API prompt serialization excludes these control entries. + +## Promotion and graph scope + +A promoted target is controlled at the graph level where its authoritative +value lives. Promotion copies the source control configuration to a component +keyed by the host target's `WidgetId`; host and interior components are +independent after promotion. + +The system queries the root graph once and handles regular, nested, and +promoted targets uniformly. It must not traverse into an interior widget at +execution time to mutate a promoted value. + +## Compatibility constraints + +- Preserve fixed, increment, decrement, randomize, combo wrap, and combo + filtering behavior. +- Preserve before-mode first-run behavior and after-mode timing. +- Preserve control updates during partial execution and suppress link-fed + targets. +- Preserve positional workflow round trips, including older files with omitted + control slots. +- Preserve target widget callbacks required by custom widgets when a system + changes a value. +- Preserve the exported `addValueControlWidget(s)` call signatures and return + component-backed projections for legacy callers. +- Do not add state or behavior to `LGraphNode`, `LGraphCanvas`, `LGraph`, or + `Subgraph` beyond narrow projection and lifecycle integration. + +## Removal + +Once all presentation and persistence paths use the component, remove: + +- Real `control_after_generate` and filter widgets from `node.widgets`. +- `IS_CONTROL_WIDGET` and control-widget duck typing. +- Per-control `beforeQueued` and `afterQueued` closures. +- Runtime control identity through `linkedWidgets`. +- The separate promoted-widget control execution path and its `WeakSet`. + +## Acceptance criteria + +1. A regular number or combo control behaves identically before and after + workflow save/load. +2. Classic and Vue node renderers edit the same component state. +3. Partial executions advance controls, but link-fed targets do not advance. +4. Before mode skips the first execution and advances subsequent executions. +5. Promoted and nested promoted targets advance their authoritative host value + exactly once. +6. Deleting, replacing, renaming, or clearing a target cannot leave an active + orphan control. +7. Existing workflow fixtures round-trip without changing positional value + meaning. +8. No control form widget participates in prompt serialization or + `node.widgets` iteration. + +## Implementation order + +1. Component state and lifecycle in the widget value store. +2. Pure value transition and graph-level queue system. +3. Registration, classic projection, and positional persistence. +4. Vue component bridge. +5. Promotion copy and removal of the legacy promoted execution path. +6. Delete obsolete widget markers, callbacks, and compatibility code. diff --git a/packages/graph-model/src/widgetState.ts b/packages/graph-model/src/widgetState.ts index 0dbecedac96..2642f9d8b32 100644 --- a/packages/graph-model/src/widgetState.ts +++ b/packages/graph-model/src/widgetState.ts @@ -2,6 +2,13 @@ import type { IBaseWidget, IWidgetOptions } from './widgets' import type { NodeId } from './nodeId' import type { WidgetValue } from './widgetValue' +export type ValueControlMode = + | 'fixed' + | 'increment' + | 'increment-wrap' + | 'decrement' + | 'randomize' + export interface WidgetState< TValue = WidgetValue, TType extends string = string, @@ -28,3 +35,15 @@ export type WidgetStateInit< name?: string y?: number } + +export interface WidgetControlState { + mode: ValueControlMode + filter?: string + hasExecuted: boolean +} + +export interface WidgetControlConfig { + mode: ValueControlMode + hasFilter: boolean + filter?: string +} diff --git a/packages/graph-model/src/widgets.ts b/packages/graph-model/src/widgets.ts index 32e0fd77995..9ca413d448f 100644 --- a/packages/graph-model/src/widgets.ts +++ b/packages/graph-model/src/widgets.ts @@ -15,6 +15,7 @@ import type { BoundingBox } from './boundingBoxes' import type { LightInfoEntry } from '@comfyorg/viewport3d/lightInfo/types' import type { WidgetValue } from './widgetValue' import type { WidgetId } from './widgetId' +import type { WidgetControlConfig } from './widgetState' import type { WidgetSurfaces, WidgetVisibilityComponent @@ -538,6 +539,8 @@ export interface IBaseWidget< linkedWidgets?: IBaseWidget[] + controlConfig?: WidgetControlConfig + readonly widgetId?: WidgetId name: string diff --git a/src/components/graph/GraphCanvas.test.ts b/src/components/graph/GraphCanvas.test.ts index 409bbfb8120..ca3a6a70ceb 100644 --- a/src/components/graph/GraphCanvas.test.ts +++ b/src/components/graph/GraphCanvas.test.ts @@ -2,21 +2,11 @@ import { fromPartial } from '@total-typescript/shoehorn' import { getActivePinia } from 'pinia' import { cleanup, fireEvent, render, screen } from '@testing-library/vue' import type { RenderOptions } from '@testing-library/vue' -import { - assert, - beforeEach, - describe, - expect, - it, - onTestFinished, - vi -} from 'vitest' +import { beforeEach, describe, expect, it, onTestFinished, vi } from 'vitest' import { nextTick } from 'vue' import { createI18n } from 'vue-i18n' import type { Toast } from '@/components/ui/toast/toastStore' -import { IS_CONTROL_WIDGET } from '@/core/graph/widgets/controlWidgetMarker' -import type { IBaseWidget } from '@comfyorg/graph-model/widgets' import { LGraph, LGraphNode } from '@/lib/litegraph/src/litegraph' import { bootstrapTracer } from '@/platform/telemetry/perf/bootstrapTracer' import { useWorkspaceStore } from '@/stores/workspaceStore' @@ -605,67 +595,6 @@ describe('GraphCanvas execution progress updates', () => { }) }) -describe('GraphCanvas widget control mode watcher', () => { - async function cycleWidgetControlMode() { - const settingStore = useSettingStore() - settingStore.settingValues['Comfy.WidgetControlMode'] = 'before' - await nextTick() - settingStore.settingValues['Comfy.WidgetControlMode'] = 'after' - await nextTick() - } - - function addControlWidgetNode() { - const controlWidget = { - name: 'control_after_generate', - label: undefined as string | undefined, - [IS_CONTROL_WIDGET]: true - } - const node = new LGraphNode('Test node') - node.id = toNodeId(1) - node.widgets = [controlWidget as unknown as IBaseWidget] - - const graph = new LGraph() - graph._nodes.push(node) - - return { graph, controlWidget } - } - - // Defensive: the app only writes `canvasStore.canvas` after `setup()` has - // installed the root graph, so this state is not reachable in production. The - // guard keeps the watcher consistent with its siblings that read the graph. - it('defensively skips the control-widget sync when the root graph is not ready', async () => { - await mountGraphCanvas() - - useCanvasStore().canvas = app.canvas - expect(app.rootGraphOrUndefined).toBeUndefined() - - await expect(cycleWidgetControlMode()).resolves.toBeUndefined() - }) - - it('still syncs control-widget labels once the root graph is ready', async () => { - await mountGraphCanvas() - - useCanvasStore().canvas = app.canvas - const { graph, controlWidget } = addControlWidgetNode() - - await expect(cycleWidgetControlMode()).resolves.toBeUndefined() - expect(controlWidget.label).toBeUndefined() - - setRootGraph(graph) - - const settingStore = useSettingStore() - settingStore.settingValues['Comfy.WidgetControlMode'] = 'before' - await nextTick() - const labelBeforeMode = controlWidget.label - expect(labelBeforeMode).toBeTruthy() - - settingStore.settingValues['Comfy.WidgetControlMode'] = 'after' - await nextTick() - expect(controlWidget.label).toBeTruthy() - expect(controlWidget.label).not.toBe(labelBeforeMode) - }) -}) - describe('GraphCanvas selection watcher', () => { it('redraws the background when the selected nodes change', async () => { await mountGraphCanvas() diff --git a/src/components/graph/GraphCanvas.vue b/src/components/graph/GraphCanvas.vue index bb62b158fc2..408461411fc 100644 --- a/src/components/graph/GraphCanvas.vue +++ b/src/components/graph/GraphCanvas.vue @@ -189,8 +189,6 @@ import LGraphNode from '@/renderer/extensions/vueNodes/components/LGraphNode.vue import { UnauthorizedError } from '@comfyorg/api-client/ComfyApi' import { app as comfyApp } from '@/scripts/app' import { ChangeTracker } from '@/scripts/changeTracker' -import { IS_CONTROL_WIDGET } from '@/core/graph/widgets/controlWidgetMarker' -import { updateControlWidgetLabel } from '@/core/graph/widgets/valueControlWidgets' import { loadExtensions } from '@/platform/extensions/services/extensionLoader' import { useColorPaletteService } from '@/services/colorPaletteService' import { useNewUserService } from '@/services/useNewUserService' @@ -392,21 +390,6 @@ watch( () => settingStore.get('Comfy.WidgetControlMode'), () => { if (!canvasStore.canvas) return - - const rootGraph = comfyApp.rootGraphOrUndefined - if (!rootGraph) return - - forEachNode(rootGraph, (n) => { - if (!n.widgets) return - for (const w of n.widgets) { - if (!w[IS_CONTROL_WIDGET]) continue - updateControlWidgetLabel(w) - if (!w.linkedWidgets) continue - for (const l of w.linkedWidgets) { - updateControlWidgetLabel(l) - } - } - }) canvasStore.canvas.setDirty(true) } ) diff --git a/src/components/rightSidePanel/parameters/WidgetItem.vue b/src/components/rightSidePanel/parameters/WidgetItem.vue index 4f351e59912..d4fd1ef2a9e 100644 --- a/src/components/rightSidePanel/parameters/WidgetItem.vue +++ b/src/components/rightSidePanel/parameters/WidgetItem.vue @@ -3,6 +3,7 @@ import { computed, customRef, ref } from 'vue' import { useI18n } from 'vue-i18n' import EditableText from '@comfyorg/design-system/common/EditableText' +import { getWidgetControlView } from '@/core/graph/widgets/control/widgetControl' import type { LGraphNode } from '@/lib/litegraph/src/litegraph' import type { SubgraphNode } from '@/lib/litegraph/src/subgraph/SubgraphNode' import type { IBaseWidget } from '@comfyorg/graph-model/widgets' @@ -21,7 +22,6 @@ import { useWidgetValueStore } from '@/stores/widgetValueStore' import { useFavoritedWidgetsStore } from '@/stores/workspace/favoritedWidgetsStore' -import { getControlWidget } from '@/types/simplifiedWidget' import type { SimplifiedWidget, WidgetValue as SimplifiedWidgetValue @@ -107,7 +107,7 @@ const simplifiedWidget = computed((): SimplifiedWidget => { widget.callback?.(value, canvasStore.canvas ?? undefined, node) : undefined, spec: nodeDefStore.getInputSpecForWidget(node, widgetName), - controlWidget: getControlWidget(widget) + controlWidget: getWidgetControlView(widget) } }) diff --git a/src/core/graph/subgraph/promotedWidgetControl.test.ts b/src/core/graph/subgraph/promotedWidgetControl.test.ts deleted file mode 100644 index 35c0985fdec..00000000000 --- a/src/core/graph/subgraph/promotedWidgetControl.test.ts +++ /dev/null @@ -1,87 +0,0 @@ -import { describe, expect, it } from 'vitest' - -import { promoteValueWidgetViaSubgraphInput } from '@/core/graph/subgraph/promotionUtils' -import { IS_CONTROL_WIDGET } from '@/core/graph/widgets/controlWidgetMarker' -import { LGraphNode } from '@/lib/litegraph/src/litegraph' -import type { SubgraphNode } from '@/lib/litegraph/src/subgraph/SubgraphNode' -import { - createTestSubgraph, - createTestSubgraphNode -} from '@/lib/litegraph/src/subgraph/__fixtures__/subgraphHelpers' -import { useWidgetValueStore } from '@/stores/widgetValueStore' - -import { applyPromotedWidgetControl } from './promotedWidgetControl' - -class SeedNode extends LGraphNode { - constructor(controlMode: string) { - super('SeedNode') - const input = this.addInput('seed', 'INT') - input.widget = { name: 'seed' } - - const seed = this.addWidget('number', 'seed', 1, () => {}, { - min: 0, - max: 1_000_000, - step2: 1 - }) - const control = this.addWidget( - 'combo', - 'control_after_generate', - controlMode, - () => {}, - { values: ['fixed', 'increment', 'decrement', 'randomize'] } - ) - control[IS_CONTROL_WIDGET] = true - control.beforeQueued = () => {} - control.afterQueued = () => {} - seed.linkedWidgets = [control] - } -} - -function promotedSeedValue(host: SubgraphNode): unknown { - const input = host.inputs.find((input) => input.name === 'seed') - if (!input?.widgetId) throw new Error('seed was not promoted') - return useWidgetValueStore().getWidget(input.widgetId)?.value -} - -function createPromotedSeedHost(controlMode: string): SubgraphNode { - const subgraph = createTestSubgraph() - const seedNode = new SeedNode(controlMode) - subgraph.add(seedNode) - const host = createTestSubgraphNode(subgraph) - const seedWidget = seedNode.widgets!.find((w) => w.name === 'seed')! - const result = promoteValueWidgetViaSubgraphInput(host, seedNode, seedWidget) - if (!result.ok) throw new Error(`promotion failed: ${result.reason}`) - return host -} - -describe('applyPromotedWidgetControl', () => { - it('increments the host-owned value of a promoted seed after queueing', () => { - const host = createPromotedSeedHost('increment') - expect(promotedSeedValue(host)).toBe(1) - - applyPromotedWidgetControl(host, 'afterQueued') - - expect(promotedSeedValue(host)).toBe(2) - }) - - it('leaves the value unchanged when the control mode is fixed', () => { - const host = createPromotedSeedHost('fixed') - - applyPromotedWidgetControl(host, 'afterQueued') - - expect(promotedSeedValue(host)).toBe(1) - }) - - it('does not run control on a host input fed by an external link', () => { - const host = createPromotedSeedHost('increment') - const seedInput = host.inputs.find((input) => input.name === 'seed')! - const source = new LGraphNode('Source') - source.addOutput('out', 'INT') - host.graph!.add(source) - source.connect(0, host, host.inputs.indexOf(seedInput)) - - applyPromotedWidgetControl(host, 'afterQueued') - - expect(promotedSeedValue(host)).toBe(1) - }) -}) diff --git a/src/core/graph/subgraph/promotedWidgetControl.ts b/src/core/graph/subgraph/promotedWidgetControl.ts index f939adacb75..6863513e384 100644 --- a/src/core/graph/subgraph/promotedWidgetControl.ts +++ b/src/core/graph/subgraph/promotedWidgetControl.ts @@ -1,109 +1,48 @@ -import { promotedInputSource } from '@/core/graph/subgraph/promotedInputWidget' -import { resolveConcretePromotedWidget } from '@/core/graph/subgraph/resolveConcretePromotedWidget' -import { - isValueControlWidget, - nextValueForLinkedTarget -} from '@/core/graph/widgets/valueControl' -import type { LGraphNode } from '@/lib/litegraph/src/litegraph' +import type { INodeInputSlot } from '@comfyorg/graph-model/slots' +import type { WidgetId } from '@comfyorg/graph-model/widgetId' import type { IBaseWidget } from '@comfyorg/graph-model/widgets' -import { useSettingStore } from '@/platform/settings/settingStore' -/** - * A promoted subgraph host widget whose interior source widget is driven by a - * `control_after_generate` widget. The host owns the value (store-backed), while - * the interior linked widgets supply the control mode and filter (ADR-SUBGRAPH-PROMOTION-0009). - */ -interface PromotedControlTarget { - hostWidget: IBaseWidget - linkedWidgets: IBaseWidget[] +import { useWidgetValueStore } from '@/stores/widgetValueStore' + +export function getSerializablePromotedInputs( + inputs: readonly INodeInputSlot[] +) { + const store = useWidgetValueStore() + return inputs.filter( + (input): input is INodeInputSlot & { widgetId: WidgetId } => + input.widgetId !== undefined && + store.getWidget(input.widgetId)?.serialize !== false + ) } -function collectPromotedControlTargets( - node: LGraphNode -): PromotedControlTarget[] { - if (!node.isSubgraphNode()) return [] - - const targets: PromotedControlTarget[] = [] - for (const [index, input] of node.inputs.entries()) { - // Promoted inputs are store-backed and addressed by widgetId. - if (!input.widgetId) continue - - // An incoming link means the value is fed externally (or by an outer - // subgraph boundary in the nested case), so the host store is not the - // authoritative source and control must not run here. - if (node.isInputConnected(index)) continue - - const source = promotedInputSource(node, input) - if (!source) continue - - const resolution = resolveConcretePromotedWidget( - node, - source.nodeId, - source.widgetName - ) - if (resolution.status !== 'resolved') continue - - const { linkedWidgets } = resolution.resolved.widget - if (!linkedWidgets?.some(isValueControlWidget)) continue - - const hostWidget = node.getWidgetFromSlot(input) - if (!hostWidget) continue - - targets.push({ hostWidget, linkedWidgets }) +export function copyPromotedWidgetControl( + sourceWidget: Readonly, + targetId: WidgetId +): void { + if (!sourceWidget.widgetId) return + const store = useWidgetValueStore() + const sourceControl = store.getWidgetControl(sourceWidget.widgetId) + if (!sourceControl) { + store.deleteWidgetControl(targetId) + return } - return targets -} - -function applyTarget(target: PromotedControlTarget, nodeId: unknown): void { - const next = nextValueForLinkedTarget({ - target: target.hostWidget, - linkedWidgets: target.linkedWidgets, - nodeId + const existing = store.getWidgetControl(targetId) + store.registerWidgetControl(targetId, { + mode: sourceControl.mode, + filter: + sourceControl.filter === undefined + ? undefined + : (existing?.filter ?? sourceControl.filter) }) - if (next === undefined) return - - target.hostWidget.value = next - target.hostWidget.callback?.(next) -} - -function controlValueRunBefore(): boolean { - return useSettingStore().get('Comfy.WidgetControlMode') === 'before' } -/** - * Host widgets that have run control at least once, so `before` mode can skip - * the first execution (mirroring the interior control widget's HAS_EXECUTED). - * - * Keyed by the widget instance so entries are released when the graph is - * cleared and its widgets are recreated, avoiding stale state across reloads. - */ -const executedWidgets = new WeakSet() - -/** - * Applies `control_after_generate` to a subgraph host node's promoted widgets. - * - * The interior control widget short-circuits for promoted inputs because the - * interior widget is link-fed by the subgraph boundary, so its value is dead. - * The authoritative value lives on the host node (store-backed), and this - * applies the next controlled value there. - */ -export function applyPromotedWidgetControl( - node: LGraphNode, - phase: 'beforeQueued' | 'afterQueued' +export function clearPromotedWidget( + input: INodeInputSlot, + { retainControl = false }: { retainControl?: boolean } = {} ): void { - const runBefore = controlValueRunBefore() - if (phase === 'beforeQueued' && !runBefore) return - if (phase === 'afterQueued' && runBefore) return - - for (const target of collectPromotedControlTargets(node)) { - if (phase === 'afterQueued') { - applyTarget(target, node.id) - continue - } - - if (executedWidgets.has(target.hostWidget)) { - applyTarget(target, node.id) - } - executedWidgets.add(target.hostWidget) + input._widget?.onRemove?.() + input._widget = undefined + if (input.widgetId && !retainControl) { + useWidgetValueStore().deleteWidgetControl(input.widgetId) } } diff --git a/src/core/graph/widgets/control/controlProjection.test.ts b/src/core/graph/widgets/control/controlProjection.test.ts new file mode 100644 index 00000000000..7673002e68c --- /dev/null +++ b/src/core/graph/widgets/control/controlProjection.test.ts @@ -0,0 +1,162 @@ +import { assert, describe, expect, it } from 'vitest' + +import type { IBaseWidget } from '@comfyorg/graph-model/widgets' +import { LGraph, LGraphNode } from '@/lib/litegraph/src/litegraph' +import { toConcreteWidget } from '@/lib/litegraph/src/widgets/widgetMap' +import { useSettingStore } from '@/platform/settings/settingStore' +import { useWidgetValueStore } from '@/stores/widgetValueStore' + +import { + getControlProjectionBatch, + getControlProjections +} from './controlProjection' + +describe('classic control projection', () => { + it('normalizes ownerless siblings from their requested mode and filter names', () => { + const first = { + type: 'combo', + name: 'first', + value: 'a', + options: {}, + y: 0, + controlConfig: { mode: 'fixed', hasFilter: true } + } satisfies IBaseWidget + const second = { ...first, name: 'second' } + const [mode, filter] = getControlProjections(first, [first, second]) + assert.exists(mode) + assert.exists(filter) + mode.name = 'custom mode' + filter.name = 'custom filter' + const widgets = [ + first, + second, + { ...first, name: 'custom mode', controlConfig: undefined }, + { ...first, name: 'custom filter', controlConfig: undefined } + ] + + const batch = getControlProjectionBatch(widgets) + + expect(batch.get(first)?.map(({ name }) => name)).toEqual([ + 'custom mode#1', + 'custom filter#1' + ]) + expect(batch.get(second)?.map(({ name }) => name)).toEqual([ + 'control_after_generate', + 'control_filter_list' + ]) + expect( + getControlProjections(first, [first, second]).map(({ name }) => name) + ).toEqual(['custom mode', 'custom filter']) + }) + + it('retains externally defined getter names as the target changes', () => { + const node = new LGraphNode('DynamicSeed') + const target = node.addWidget('number', 'seeds.0.seed', 1, () => {}) + target.controlConfig = { mode: 'fixed', hasFilter: false } + const [mode] = getControlProjections(target) + assert.exists(mode) + Object.defineProperty(mode, 'name', { + get: () => `${target.name}.control_after_generate` + }) + target.name = 'seeds.1.seed' + + expect(getControlProjections(target)[0]?.name).toBe( + 'seeds.1.seed.control_after_generate' + ) + }) + + it('renders and edits component state without entering node.widgets', () => { + const graph = new LGraph() + const node = new LGraphNode('SeedNode') + const seed = node.addWidget('number', 'seed', 1, () => {}, {}) + seed.controlConfig = { mode: 'increment', hasFilter: false } + graph.add(node) + + const rendered = node.getLayoutWidgets() + expect(node.widgets?.map(({ name }) => name)).toEqual(['seed']) + expect(rendered.map(({ name }) => name)).toEqual([ + 'seed', + 'control_after_generate' + ]) + + rendered[1].callback?.('randomize') + expect( + seed.widgetId + ? useWidgetValueStore().getWidgetControl(seed.widgetId)?.mode + : undefined + ).toBe('randomize') + }) + + it('delegates target visibility to every projection', () => { + const node = new LGraphNode('SeedNode') + const seed = node.addWidget('combo', 'seed', 'one', () => {}, { + values: ['one', 'two'] + }) + seed.controlConfig = { + mode: 'increment', + hasFilter: true, + filter: '' + } + seed.hidden = true + seed.advanced = true + seed.connectionSuppressed = true + + const projections = getControlProjections(seed) + expect(projections).toHaveLength(2) + expect( + projections.map( + ({ visibility, hidden, advanced, connectionSuppressed }) => ({ + visibility, + hidden, + advanced, + connectionSuppressed + }) + ) + ).toEqual([ + { + visibility: seed.visibility, + hidden: true, + advanced: true, + connectionSuppressed: true + }, + { + visibility: seed.visibility, + hidden: true, + advanced: true, + connectionSuppressed: true + } + ]) + }) + + it('renders control labels from the current mode without rewriting widgets', () => { + const node = new LGraphNode('SeedNode') + const seed = node.addWidget('number', 'seed', 1, () => {}) + seed.controlConfig = { mode: 'fixed', hasFilter: false } + const [controlWidget] = getControlProjections(seed) + assert.exists(controlWidget) + const settingStore = useSettingStore() + + settingStore.settingValues['Comfy.WidgetControlMode'] = 'before' + const labelBeforeMode = controlWidget.label + expect(labelBeforeMode).toBeTruthy() + + settingStore.settingValues['Comfy.WidgetControlMode'] = 'after' + expect(controlWidget.label).toBeTruthy() + expect(controlWidget.label).not.toBe(labelBeforeMode) + expect(node.widgets?.map(({ name }) => name)).toEqual(['seed']) + }) + + it('converts projections for classic canvas drawing', () => { + const node = new LGraphNode('SeedNode') + const seed = node.addWidget('number', 'seed', 1, () => {}, {}) + seed.controlConfig = { mode: 'increment', hasFilter: false } + const [projection] = getControlProjections(seed) + if (!projection) throw new Error('Expected a control projection') + + const concrete = toConcreteWidget(projection, node) + + expect(concrete.visibility).toBe(seed.visibility) + seed.hidden = true + expect(concrete.hidden).toBe(true) + }) +}) diff --git a/src/core/graph/widgets/control/controlProjection.ts b/src/core/graph/widgets/control/controlProjection.ts new file mode 100644 index 00000000000..de0bc5cde88 --- /dev/null +++ b/src/core/graph/widgets/control/controlProjection.ts @@ -0,0 +1,243 @@ +import type { + IBaseWidget, + IComboWidget, + IStringWidget +} from '@comfyorg/graph-model/widgets' +import type { LGraphNode } from '@comfyorg/graph-model/entityRegistry' +import { t } from '@/i18n' +import { emitGraphIntent } from '@/lib/litegraph/src/graphIntents' +import { + ensureUniqueWidgetNames, + parseWidgetId +} from '@comfyorg/graph-model/widgetId' +import { useSettingStore } from '@/platform/settings/settingStore' +import { useWidgetValueStore } from '@/stores/widgetValueStore' + +import { + COMBO_CONTROL_MODES, + isValueControlMode, + NUMBER_CONTROL_MODES +} from './valueControl' + +const modeProjectionCache = new WeakMap() +const filterProjectionCache = new WeakMap() +const projectionNames = new WeakMap< + IBaseWidget, + { requested: string; resolved: string; get: () => string } +>() + +function trackProjectionName(projection: IBaseWidget): void { + const state = { + requested: projection.name, + resolved: projection.name, + get: () => state.resolved + } + projectionNames.set(projection, state) + Object.defineProperty(projection, 'name', { + configurable: true, + enumerable: true, + get: state.get, + set(name: string) { + state.requested = name + state.resolved = name + } + }) +} + +function announceControlValue( + target: IBaseWidget, + control: IBaseWidget, + previous: unknown +): void { + if (!target.widgetId) return + const state = useWidgetValueStore().getWidget(target.widgetId) + if (!state) return + emitGraphIntent({ + type: 'set_widget', + graphId: parseWidgetId(target.widgetId).graphId, + nodeId: state.nodeId, + name: control.name, + value: control.value, + previous + }) +} + +export function announceControlValues(target: IBaseWidget): void { + for (const control of getControlProjections(target)) { + announceControlValue(target, control, undefined) + } +} + +function createModeProjection(target: IBaseWidget): IComboWidget { + const store = useWidgetValueStore() + const projection: IComboWidget = { + type: 'combo', + name: 'control_after_generate', + options: { + values: + target.type === 'combo' + ? [...COMBO_CONTROL_MODES] + : [...NUMBER_CONTROL_MODES], + serialize: false + }, + serialize: false, + get tooltip() { + return t('widgets.valueControl.modeTooltip') + }, + y: 0, + get visibility() { + return target.visibility + }, + get hidden() { + return target.hidden + }, + get advanced() { + return target.advanced + }, + get connectionSuppressed() { + return target.connectionSuppressed + }, + get computedDisabled() { + return target.computedDisabled + }, + get label() { + return useSettingStore().get('Comfy.WidgetControlMode') === 'before' + ? t('g.control_before_generate') + : t('g.control_after_generate') + }, + get value() { + return target.widgetId + ? (store.getWidgetControl(target.widgetId)?.mode ?? + target.controlConfig?.mode ?? + 'fixed') + : (target.controlConfig?.mode ?? 'fixed') + }, + set value(value) { + if (isValueControlMode(value)) { + const previous = this.value + if (target.controlConfig) target.controlConfig.mode = value + if (target.widgetId) { + store.updateWidgetControl(target.widgetId, { mode: value }) + } + if (value !== previous) announceControlValue(target, this, previous) + } + }, + callback(value) { + if (isValueControlMode(value)) { + projection.value = value + } + } + } + trackProjectionName(projection) + return projection +} + +function createFilterProjection(target: IBaseWidget): IStringWidget { + const store = useWidgetValueStore() + const projection: IStringWidget = { + type: 'string', + name: 'control_filter_list', + options: { serialize: false }, + serialize: false, + get tooltip() { + return t('widgets.valueControl.filterTooltip') + }, + y: 0, + get visibility() { + return target.visibility + }, + get hidden() { + return target.hidden + }, + get advanced() { + return target.advanced + }, + get connectionSuppressed() { + return target.connectionSuppressed + }, + get computedDisabled() { + return target.computedDisabled + }, + get value() { + return target.widgetId + ? (store.getWidgetControl(target.widgetId)?.filter ?? + target.controlConfig?.filter ?? + '') + : (target.controlConfig?.filter ?? '') + }, + set value(value) { + const previous = this.value + const filter = value + if (target.controlConfig) target.controlConfig.filter = filter + if (target.widgetId) { + store.updateWidgetControl(target.widgetId, { filter }) + } + if (value !== previous) announceControlValue(target, this, previous) + }, + callback(value) { + projection.value = String(value) + } + } + trackProjectionName(projection) + return projection +} + +function getCachedControlProjections( + target: IBaseWidget +): [] | [IComboWidget, ...IStringWidget[]] { + const control = target.widgetId + ? useWidgetValueStore().getWidgetControl(target.widgetId) + : undefined + if (!control && !target.controlConfig) return [] + + let mode = modeProjectionCache.get(target) + if (!mode) { + mode = createModeProjection(target) + modeProjectionCache.set(target, mode) + } + const hasFilter = control + ? control.filter !== undefined + : target.controlConfig?.hasFilter + if (!hasFilter) { + return [mode] + } + + let filter = filterProjectionCache.get(target) + if (!filter) { + filter = createFilterProjection(target) + filterProjectionCache.set(target, filter) + } + return [mode, filter] +} + +export function getControlProjectionBatch(widgets: readonly IBaseWidget[]) { + const batch = new Map( + widgets.map((widget) => [widget, getCachedControlProjections(widget)]) + ) + const projections = [...batch.values()].flat() + const names = projections.map((projection) => { + const state = projectionNames.get(projection) + const tracked = + state && + Object.getOwnPropertyDescriptor(projection, 'name')?.get === state.get + return tracked ? { name: state.requested } : projection + }) + ensureUniqueWidgetNames([...widgets.map(({ name }) => ({ name })), ...names]) + projections.forEach((projection, index) => { + const state = projectionNames.get(projection) + if (state && names[index] !== projection) { + state.resolved = names[index].name + } + }) + return batch +} + +export function getControlProjections( + target: IBaseWidget & { readonly node?: Pick }, + widgets = target.node?.widgets ?? [target] +): [] | [IComboWidget, ...IStringWidget[]] { + return ( + getControlProjectionBatch(widgets).get(target) ?? + getCachedControlProjections(target) + ) +} diff --git a/src/core/graph/widgets/valueControl.test.ts b/src/core/graph/widgets/control/valueControl.test.ts similarity index 86% rename from src/core/graph/widgets/valueControl.test.ts rename to src/core/graph/widgets/control/valueControl.test.ts index 145d734135c..1e454a8120a 100644 --- a/src/core/graph/widgets/valueControl.test.ts +++ b/src/core/graph/widgets/control/valueControl.test.ts @@ -2,11 +2,7 @@ import { describe, expect, it, vi } from 'vitest' import type { IBaseWidget } from '@comfyorg/graph-model/widgets' -import { IS_CONTROL_WIDGET } from './controlWidgetMarker' -import { - computeNextControlledValue, - isValueControlWidget -} from './valueControl' +import { computeNextControlledValue } from './valueControl' const makeNumberWidget = ( value: number, @@ -30,32 +26,6 @@ const makeComboWidget = ( options: { values } }) as unknown as IBaseWidget -describe('isValueControlWidget', () => { - it('returns true for a marked widget with both lifecycle hooks', () => { - const widget = { - [IS_CONTROL_WIDGET]: true, - beforeQueued: () => {}, - afterQueued: () => {} - } as unknown as IBaseWidget - expect(isValueControlWidget(widget)).toBe(true) - }) - - it('returns false when the marker symbol is missing', () => { - const widget = { - beforeQueued: () => {}, - afterQueued: () => {} - } as unknown as IBaseWidget - expect(isValueControlWidget(widget)).toBe(false) - }) - - it('returns false when lifecycle hooks are missing', () => { - const widget = { - [IS_CONTROL_WIDGET]: true - } as unknown as IBaseWidget - expect(isValueControlWidget(widget)).toBe(false) - }) -}) - describe('computeNextControlledValue (number)', () => { it('returns undefined for fixed mode', () => { expect( diff --git a/src/core/graph/widgets/valueControl.ts b/src/core/graph/widgets/control/valueControl.ts similarity index 62% rename from src/core/graph/widgets/valueControl.ts rename to src/core/graph/widgets/control/valueControl.ts index 61a5586b78c..096544414a5 100644 --- a/src/core/graph/widgets/valueControl.ts +++ b/src/core/graph/widgets/control/valueControl.ts @@ -1,52 +1,47 @@ -import { isComboWidget } from '@/lib/litegraph/src/litegraph' -import type { IBaseWidget, IComboWidget } from '@comfyorg/graph-model/widgets' +import type { IBaseWidget } from '@comfyorg/graph-model/widgets' +import type { ValueControlMode } from '@comfyorg/graph-model/widgetState' import { findComboValueIndex } from '@/lib/litegraph/src/utils/widget' -import type { ControlOptions } from '@/types/simplifiedWidget' -import { isControlOption } from '@/types/simplifiedWidget' -import { IS_CONTROL_WIDGET } from './controlWidgetMarker' +export type { ValueControlMode } from '@comfyorg/graph-model/widgetState' -type ValueControlMode = ControlOptions | 'increment-wrap' +export const COMBO_CONTROL_MODES: readonly ValueControlMode[] = [ + 'fixed', + 'increment', + 'increment-wrap', + 'decrement', + 'randomize' +] + +export const NUMBER_CONTROL_MODES: readonly ValueControlMode[] = [ + 'fixed', + 'increment', + 'decrement', + 'randomize' +] + +const VALUE_CONTROL_MODES: ReadonlySet = new Set(COMBO_CONTROL_MODES) export function isValueControlMode(value: unknown): value is ValueControlMode { - return isControlOption(value) || value === 'increment-wrap' + return typeof value === 'string' && VALUE_CONTROL_MODES.has(value) } -export function nextValueForLinkedTarget(params: { - target: IBaseWidget - linkedWidgets: IBaseWidget[] | undefined - nodeId: unknown -}): IBaseWidget['value'] | undefined { - const linked = params.linkedWidgets - if (!linked) return undefined - - const controlWidget = linked.find(isValueControlWidget) - if (!controlWidget) return undefined +export function parseValueControlMode( + value: unknown +): ValueControlMode | undefined { + if (value === true) return 'randomize' + if (value === false) return 'fixed' + return isValueControlMode(value) ? value : undefined +} - const comboFilter = linked.find( - (w) => w !== controlWidget && w.type === 'string' - ) - const filterValue = - typeof comboFilter?.value === 'string' ? comboFilter.value : undefined - - const mode = controlWidget.value as ValueControlMode - return computeNextControlledValue(params.target, mode, { - comboFilter: filterValue, - nodeId: params.nodeId - }) +interface ValueControlTarget { + type: IBaseWidget['type'] + value?: unknown + options: IBaseWidget['options'] } const SAFE_INTEGER_MAX = 1125899906842624 const SAFE_INTEGER_MIN = -1125899906842624 -export function isValueControlWidget(widget: IBaseWidget): boolean { - return ( - (widget as Record)[IS_CONTROL_WIDGET] === true && - typeof widget.beforeQueued === 'function' && - typeof widget.afterQueued === 'function' - ) -} - function buildComboFilter( filter: string | undefined, nodeId?: unknown @@ -71,21 +66,19 @@ function buildComboFilter( } export function computeNextControlledValue( - target: IBaseWidget, + target: ValueControlTarget, mode: ValueControlMode, options: { comboFilter?: string; nodeId?: unknown } = {} ): IBaseWidget['value'] | undefined { if (mode === 'fixed') return undefined - - if (isComboWidget(target)) { + if (target.type === 'combo') { return computeNextComboValue(target, mode, options) } - return computeNextNumberValue(target, mode) } function computeNextComboValue( - target: IComboWidget, + target: ValueControlTarget, mode: ValueControlMode, { comboFilter, nodeId }: { comboFilter?: string; nodeId?: unknown } ): IBaseWidget['value'] | undefined { @@ -111,16 +104,17 @@ function computeNextComboValue( return undefined } - let currentIndex = findComboValueIndex(values, target.value) + let currentIndex = + typeof target.value === 'string' || typeof target.value === 'number' + ? findComboValueIndex(values, target.value) + : -1 const length = values.length - switch (mode) { case 'increment': currentIndex += 1 break case 'increment-wrap': - currentIndex += 1 - if (currentIndex >= length) currentIndex = 0 + currentIndex = (currentIndex + 1) % length break case 'decrement': currentIndex -= 1 @@ -128,14 +122,15 @@ function computeNextComboValue( case 'randomize': currentIndex = Math.floor(Math.random() * length) break + case 'fixed': + return undefined } - currentIndex = Math.max(0, Math.min(length - 1, currentIndex)) - return values[currentIndex] + return values[Math.max(0, Math.min(length - 1, currentIndex))] } function computeNextNumberValue( - target: IBaseWidget, + target: ValueControlTarget, mode: ValueControlMode ): number | undefined { if (typeof target.value !== 'number') return undefined @@ -144,8 +139,8 @@ function computeNextNumberValue( const max = Math.min(SAFE_INTEGER_MAX, rawMax) const min = Math.max(SAFE_INTEGER_MIN, rawMin) const range = (max - min) / step2 - let next = target.value + switch (mode) { case 'increment': case 'increment-wrap': @@ -157,6 +152,8 @@ function computeNextNumberValue( case 'randomize': next = Math.floor(Math.random() * range) * step2 + min break + case 'fixed': + return undefined } return Math.min(Math.max(next, min), max) diff --git a/src/core/graph/widgets/control/widgetControl.test.ts b/src/core/graph/widgets/control/widgetControl.test.ts new file mode 100644 index 00000000000..986039a18b7 --- /dev/null +++ b/src/core/graph/widgets/control/widgetControl.test.ts @@ -0,0 +1,344 @@ +import { assert, describe, expect, it, onTestFinished } from 'vitest' + +import { LGraph, LGraphNode, LiteGraph } from '@/lib/litegraph/src/litegraph' +import { createWidgetRestorationState } from '@/lib/litegraph/src/LGraphNode' +import { toConcreteWidget } from '@/lib/litegraph/src/widgets/widgetMap' +import { useWidgetValueStore } from '@/stores/widgetValueStore' + +import { getControlProjections } from './controlProjection' +import { + getWidgetControlView, + registerWidgetControlFromConfig +} from './widgetControl' +import { runWidgetControl } from './widgetControlSystem' + +function createControlledNode(): { graph: LGraph; node: LGraphNode } { + const graph = new LGraph() + const node = new LGraphNode('TestNode') + node.serialize_widgets = true + const seed = node.addWidget('number', 'seed', 0, () => {}, {}) + seed.controlConfig = { mode: 'randomize', hasFilter: false } + node.addWidget('text', 'prompt', '', () => {}, {}) + graph.add(node) + return { graph, node } +} + +describe('widget control persistence', () => { + it('preserves independent controls through named save and configure', () => { + const namedValuesRestore = LiteGraph.namedValuesRestore + onTestFinished(() => { + LiteGraph.namedValuesRestore = namedValuesRestore + }) + LiteGraph.namedValuesRestore = true + const { node } = createControlledNode() + const first = node.widgets?.[0] + assert.exists(first) + const second = node.addWidget('number', 'steps', 20, () => {}) + second.controlConfig = { mode: 'decrement', hasFilter: false } + registerWidgetControlFromConfig(second) + getControlProjections(first) + const reserved = node.addWidget( + 'text', + 'control_after_generate', + 'real', + () => {} + ) + const [secondControl] = getControlProjections(second) + const [firstControl] = getControlProjections(first) + assert.exists(firstControl) + assert.exists(secondControl) + firstControl.value = 'increment' + + const saved = node.serialize() + firstControl.value = 'fixed' + secondControl.value = 'fixed' + node.configure(saved) + + expect([firstControl.value, secondControl.value]).toEqual([ + 'increment', + 'decrement' + ]) + const { node: reconstructed } = createControlledNode() + const reconstructedFirst = reconstructed.widgets?.[0] + assert.exists(reconstructedFirst) + const reconstructedSecond = reconstructed.addWidget( + 'number', + 'steps', + 20, + () => {} + ) + reconstructedSecond.controlConfig = { mode: 'fixed', hasFilter: false } + registerWidgetControlFromConfig(reconstructedSecond) + reconstructed.addWidget('text', 'control_after_generate', '', () => {}) + reconstructed.configure(saved) + expect([ + getControlProjections(reconstructedFirst)[0]?.value, + getControlProjections(reconstructedSecond)[0]?.value + ]).toEqual(['increment', 'decrement']) + expect(saved.widgets_values_named).toMatchObject({ + control_after_generate: 'real', + 'control_after_generate#1': 'increment', + 'control_after_generate#2': 'decrement' + }) + expect(reserved.name).toBe('control_after_generate') + expect(reserved.value).toBe('real') + }) + + it('persists mode and filter edits through non-adopting classic wrappers', () => { + const graph = new LGraph() + const node = new LGraphNode('ComboNode') + const target = node.addWidget('combo', 'model', 'a', () => {}, { + values: ['a', 'b'] + }) + target.controlConfig = { mode: 'increment', hasFilter: true, filter: '' } + graph.add(node) + const [mode, filter] = getControlProjections(target) + assert.exists(mode) + assert.exists(filter) + + const modeWidget = toConcreteWidget(mode, node, false) + const filterWidget = toConcreteWidget(filter, node, false) + assert.exists(modeWidget) + assert.exists(filterWidget) + modeWidget.setValue('decrement', { node }) + filterWidget.setValue('/portrait/', { node }) + + expect(getControlProjections(target).map(({ value }) => value)).toEqual([ + 'decrement', + '/portrait/' + ]) + expect(target.controlConfig).toMatchObject({ + mode: 'decrement', + filter: '/portrait/' + }) + }) + + it('round-trips the component through the legacy positional layout', () => { + const { node } = createControlledNode() + + node.configure({ + ...node.serialize(), + widgets_values: [12345, 'increment', 'a prompt'] + }) + + const target = node.widgets?.[0] + expect(target?.value).toBe(12345) + expect(node.widgets?.[1].value).toBe('a prompt') + expect( + target?.widgetId + ? useWidgetValueStore().getWidgetControl(target.widgetId)?.mode + : undefined + ).toBe('increment') + expect(node.serialize().widgets_values).toEqual([ + 12345, + 'increment', + 'a prompt' + ]) + }) + + it.for([ + { value: true, mode: 'randomize' }, + { value: false, mode: 'fixed' } + ] as const)( + 'restores legacy boolean control value $value', + ({ value, mode }) => { + const { node } = createControlledNode() + + node.configure({ + ...node.serialize(), + widgets_values: [12345, value, 'a prompt'] + }) + + const target = node.widgets?.[0] + expect(node.widgets?.map((widget) => widget.value)).toEqual([ + 12345, + 'a prompt' + ]) + expect( + target?.widgetId + ? useWidgetValueStore().getWidgetControl(target.widgetId)?.mode + : undefined + ).toBe(mode) + } + ) + + it('restores a control before its node is attached to a graph', () => { + const node = new LGraphNode('TestNode') + node.serialize_widgets = true + const seed = node.addWidget('number', 'seed', 0, () => {}, {}) + seed.controlConfig = { mode: 'randomize', hasFilter: false } + node.addWidget('text', 'prompt', '', () => {}, {}) + + node.configure({ + ...node.serialize(), + widgets_values: [12345, true, 'a prompt'] + }) + const graph = new LGraph() + graph.add(node) + + expect(node.widgets?.map((widget) => widget.value)).toEqual([ + 12345, + 'a prompt' + ]) + expect( + seed.widgetId + ? useWidgetValueStore().getWidgetControl(seed.widgetId)?.mode + : undefined + ).toBe('randomize') + }) + + it('serializes configured controls before the widget has a store identity', () => { + const node = new LGraphNode('TestNode') + node.serialize_widgets = true + const seed = node.addWidget('number', 'seed', 0, () => {}, {}) + seed.controlConfig = { mode: 'increment', hasFilter: false } + + expect(node.serialize().widgets_values).toEqual([0, 'increment']) + }) + + it('does not consume the next widget when the control slot is absent', () => { + const { node } = createControlledNode() + + node.configure({ + ...node.serialize(), + widgets_values: [12345, 'increment'] + }) + + expect(node.widgets?.map(({ value }) => value)).toEqual([ + 12345, + 'increment' + ]) + }) + + it('aligns omitted controls when a later widget has control values', () => { + const { node } = createControlledNode() + const steps = node.addWidget('number', 'steps', 1, () => {}, {}) + steps.controlConfig = { mode: 'randomize', hasFilter: false } + registerWidgetControlFromConfig(steps) + + node.configure({ + ...node.serialize(), + widgets_values: [12345, 'increment', 7, 'decrement'] + }) + + expect(node.widgets?.map(({ value }) => value)).toEqual([ + 12345, + 'increment', + 7 + ]) + expect( + steps.widgetId + ? useWidgetValueStore().getWidgetControl(steps.widgetId)?.mode + : undefined + ).toBe('decrement') + }) + + it('exposes combo wrap mode through the Vue control view', () => { + const { node } = createControlledNode() + const target = node.widgets?.[0] + if (!target?.widgetId) throw new Error('Target widget was not registered') + useWidgetValueStore().updateWidgetControl(target.widgetId, { + mode: 'increment-wrap' + }) + + expect(getWidgetControlView(target)?.value).toBe('increment-wrap') + }) + + it('removes retained control state when a widget is re-registered without configuration', () => { + const { graph, node } = createControlledNode() + const target = node.widgets?.[0] + if (!target?.widgetId) throw new Error('Target widget was not registered') + useWidgetValueStore().updateWidgetControl(target.widgetId, { + mode: 'increment' + }) + + target.controlConfig = undefined + registerWidgetControlFromConfig(target) + runWidgetControl(graph, 'after', 'after') + + expect( + useWidgetValueStore().getWidgetControl(target.widgetId) + ).toBeUndefined() + expect(target.value).toBe(0) + }) + + it('adds a filter to retained control state when filtering is enabled', () => { + const { node } = createControlledNode() + const target = node.widgets?.[0] + if (!target?.widgetId) throw new Error('Target widget was not registered') + + target.controlConfig = { + mode: 'randomize', + hasFilter: true, + filter: '/portrait/' + } + registerWidgetControlFromConfig(target) + + expect( + useWidgetValueStore().getWidgetControl(target.widgetId)?.filter + ).toBe('/portrait/') + }) + + it('removes a retained filter when filtering is disabled', () => { + const { node } = createControlledNode() + const target = node.widgets?.[0] + if (!target?.widgetId) throw new Error('Target widget was not registered') + target.controlConfig = { + mode: 'randomize', + hasFilter: true, + filter: '/portrait/' + } + registerWidgetControlFromConfig(target) + + target.controlConfig = { mode: 'randomize', hasFilter: false } + registerWidgetControlFromConfig(target) + + expect( + useWidgetValueStore().getWidgetControl(target.widgetId)?.filter + ).toBeUndefined() + }) + + it('keeps legacy control config in sync with Vue edits', () => { + const { node } = createControlledNode() + const target = node.widgets?.[0] + if (!target) throw new Error('Target widget was not registered') + + getWidgetControlView(target)?.update('increment') + + expect(target.controlConfig?.mode).toBe('increment') + }) + + it('maps fallback names around embedded control values', () => { + const widgets = [ + { controlConfig: { mode: 'increment' as const, hasFilter: false } }, + { controlConfig: undefined } + ] + + const restoration = createWidgetRestorationState( + { widgets_values: [12345, 'increment', 20] }, + ['seed', 'steps'], + widgets + ) + + expect(restoration.named).toEqual({ seed: 12345, steps: 20 }) + }) + + it('moves and removes the component with its target widget', () => { + const { node } = createControlledNode() + const store = useWidgetValueStore() + const target = node.widgets?.[0] + const oldId = target?.widgetId + if (!target || !oldId) throw new Error('Target widget was not registered') + + target.name = 'renamed_seed' + const newId = target.widgetId + + expect(store.getWidgetControl(oldId)).toBeUndefined() + expect(newId ? store.getWidgetControl(newId)?.mode : undefined).toBe( + 'randomize' + ) + + node.removeWidget(target) + expect(newId ? store.getWidgetControl(newId) : undefined).toBeUndefined() + }) +}) diff --git a/src/core/graph/widgets/control/widgetControl.ts b/src/core/graph/widgets/control/widgetControl.ts new file mode 100644 index 00000000000..ca9f307ab13 --- /dev/null +++ b/src/core/graph/widgets/control/widgetControl.ts @@ -0,0 +1,234 @@ +import type { IBaseWidget } from '@comfyorg/graph-model/widgets' +import { useWidgetValueStore } from '@/stores/widgetValueStore' +import { normalizeControlOption } from '@/types/simplifiedWidget' +import type { SafeControlWidget } from '@/types/simplifiedWidget' +import type { WidgetId } from '@comfyorg/graph-model/widgetId' + +import { parseValueControlMode } from './valueControl' +import { getControlProjections } from './controlProjection' + +export function registerWidgetControlFromConfig(widget: IBaseWidget): void { + const config = widget.controlConfig + const targetId = widget.widgetId + if (!targetId) return + if (!config) { + useWidgetValueStore().deleteWidgetControl(targetId) + return + } + + useWidgetValueStore().registerWidgetControl(targetId, { + mode: config.mode, + filter: config.hasFilter ? (config.filter ?? '') : undefined + }) +} + +export function getWidgetControlView( + widget: IBaseWidget +): SafeControlWidget | undefined { + const targetId = widget.widgetId + if (!targetId) return undefined + const store = useWidgetValueStore() + const control = store.getWidgetControl(targetId) + if (!control) return undefined + return { + value: normalizeControlOption(control.mode), + update: (value) => { + const mode = normalizeControlOption(value) + const [projection] = getControlProjections(widget) + if (projection) projection.value = mode + } + } +} + +export function appendControlValues( + target: + | WidgetId + | Pick + | undefined, + values: unknown[] +): void { + const targetId = typeof target === 'string' ? target : target?.widgetId + const control = + (targetId ? useWidgetValueStore().getWidgetControl(targetId) : undefined) ?? + (typeof target === 'string' ? undefined : target?.controlConfig) + if (!control) return + values.push(control.mode) + const filter = + 'hasFilter' in control + ? control.hasFilter + ? (control.filter ?? '') + : undefined + : control.filter + if (filter !== undefined) values.push(filter) +} + +export function applyControlValues( + target: + | WidgetId + | Pick + | undefined, + values: readonly unknown[], + index: number, + valueCount?: 0 | 1 | 2 +): number { + if (valueCount === 0) return index + const targetId = typeof target === 'string' ? target : target?.widgetId + const config = typeof target === 'string' ? undefined : target?.controlConfig + const store = useWidgetValueStore() + const control = targetId ? store.getWidgetControl(targetId) : undefined + if (!control && !config) return index + + const mode = parseValueControlMode(values[index]) + if (!mode) return index + + const filter = values[index + 1] + const restoreFilter = + valueCount !== 1 && + (control?.filter !== undefined || config?.hasFilter === true) && + typeof filter === 'string' + const restored = restoreFilter ? { mode, filter } : { mode } + if (config) Object.assign(config, restored) + if (targetId) store.updateWidgetControl(targetId, restored) + return index + 1 + Number(restoreFilter) +} + +export interface WidgetValueLayoutEntry { + valueIndex: number + controlValueCount: 0 | 1 | 2 +} + +interface WidgetValueLayoutCandidate { + entries: WidgetValueLayoutEntry[] + preferredControlValueCount: number + targetValueCount: number + targetTypeMatchCount: number + controlValueCount: number +} + +function betterLayout( + current: WidgetValueLayoutCandidate | undefined, + candidate: WidgetValueLayoutCandidate | undefined +): WidgetValueLayoutCandidate | undefined { + if (!candidate) return current + if (!current) return candidate + for (const key of [ + 'preferredControlValueCount', + 'targetValueCount', + 'targetTypeMatchCount', + 'controlValueCount' + ] as const) { + const difference = candidate[key] - current[key] + if (difference !== 0) return difference > 0 ? candidate : current + } + return current +} + +export function decodeWidgetValueLayout( + widgets: readonly Pick[], + values: readonly unknown[], + preferControlValuesFor?: ReadonlySet< + Pick + > +): WidgetValueLayoutEntry[] { + const store = useWidgetValueStore() + const memo = new Map() + const constraints = widgets.map((widget) => { + const control = + (widget.widgetId ? store.getWidgetControl(widget.widgetId) : undefined) ?? + widget.controlConfig + return { + hasControl: !!control, + hasFilter: + control && 'hasFilter' in control + ? control.hasFilter + : control?.filter !== undefined, + targetValue: widget.widgetId + ? store.getWidget(widget.widgetId)?.value + : undefined, + preferControlValues: preferControlValuesFor?.has(widget) + } + }) + + const decode = ( + widgetIndex: number, + valueIndex: number + ): WidgetValueLayoutCandidate | undefined => { + const key = `${widgetIndex}:${valueIndex}` + if (memo.has(key)) return memo.get(key) + + if (widgetIndex === widgets.length) { + const result = + valueIndex === values.length + ? { + entries: [], + preferredControlValueCount: 0, + targetValueCount: 0, + targetTypeMatchCount: 0, + controlValueCount: 0 + } + : undefined + memo.set(key, result) + return result + } + + if (valueIndex >= values.length) { + const result = { + entries: widgets + .slice(widgetIndex) + .map(() => ({ valueIndex, controlValueCount: 0 as const })), + preferredControlValueCount: 0, + targetValueCount: 0, + targetTypeMatchCount: 0, + controlValueCount: 0 + } + memo.set(key, result) + return result + } + + const { hasControl, hasFilter, targetValue, preferControlValues } = + constraints[widgetIndex] + const nextValueIndex = valueIndex + 1 + let best: WidgetValueLayoutCandidate | undefined + + const addCandidate = (controlValueCount: 0 | 1 | 2) => { + const remainder = decode( + widgetIndex + 1, + nextValueIndex + controlValueCount + ) + if (!remainder) return + best = betterLayout(best, { + entries: [{ valueIndex, controlValueCount }, ...remainder.entries], + preferredControlValueCount: + remainder.preferredControlValueCount + + (preferControlValues ? controlValueCount : 0), + targetValueCount: remainder.targetValueCount + 1, + targetTypeMatchCount: + remainder.targetTypeMatchCount + + (targetValue != null && + typeof targetValue === typeof values[valueIndex] + ? 1 + : 0), + controlValueCount: remainder.controlValueCount + controlValueCount + }) + } + + addCandidate(0) + if (hasControl && parseValueControlMode(values[nextValueIndex])) { + addCandidate(1) + if (hasFilter && typeof values[nextValueIndex + 1] === 'string') { + addCandidate(2) + } + } + + memo.set(key, best) + return best + } + + return ( + decode(0, 0)?.entries ?? + widgets.map((_, valueIndex) => ({ + valueIndex, + controlValueCount: 0 + })) + ) +} diff --git a/src/core/graph/widgets/control/widgetControlSystem.promoted.test.ts b/src/core/graph/widgets/control/widgetControlSystem.promoted.test.ts new file mode 100644 index 00000000000..ca6d50c15da --- /dev/null +++ b/src/core/graph/widgets/control/widgetControlSystem.promoted.test.ts @@ -0,0 +1,142 @@ +import { assert, describe, expect, it } from 'vitest' + +import { promoteValueWidgetViaSubgraphInput } from '@/core/graph/subgraph/promotionUtils' +import { LGraphNode } from '@/lib/litegraph/src/litegraph' +import type { SubgraphNode } from '@/lib/litegraph/src/subgraph/SubgraphNode' +import { + createTestSubgraph, + createTestSubgraphNode +} from '@/lib/litegraph/src/subgraph/__fixtures__/subgraphHelpers' +import { useWidgetValueStore } from '@/stores/widgetValueStore' + +import { runWidgetControl } from './widgetControlSystem' + +class SeedNode extends LGraphNode { + constructor(controlMode: string) { + super('SeedNode') + const input = this.addInput('seed', 'INT') + input.widget = { name: 'seed' } + + const seed = this.addWidget('number', 'seed', 1, () => {}, { + min: 0, + max: 1_000_000, + step2: 1 + }) + seed.controlConfig = { + mode: controlMode === 'increment' ? 'increment' : 'fixed', + hasFilter: false + } + } +} + +function promotedSeedValue(host: SubgraphNode): unknown { + const input = host.inputs.find((input) => input.name === 'seed') + if (!input?.widgetId) throw new Error('seed was not promoted') + return useWidgetValueStore().getWidget(input.widgetId)?.value +} + +function createPromotedSeedHost(controlMode: string): SubgraphNode { + const subgraph = createTestSubgraph() + const seedNode = new SeedNode(controlMode) + subgraph.add(seedNode) + const host = createTestSubgraphNode(subgraph) + const seedWidget = seedNode.widgets!.find((w) => w.name === 'seed')! + const result = promoteValueWidgetViaSubgraphInput(host, seedNode, seedWidget) + if (!result.ok) throw new Error(`promotion failed: ${result.reason}`) + subgraph.rootGraph.add(host) + return host +} + +describe('runWidgetControl with promoted targets', () => { + it('preserves an edited host control and execution history through rewiring', async () => { + const host = createPromotedSeedHost('increment') + const source = host.subgraph.nodes.find((node) => node.title === 'SeedNode') + assert.exists(source) + const input = host.inputs.find((input) => input.name === 'seed') + assert.exists(input?.widgetId) + const store = useWidgetValueStore() + store.setValue(input.widgetId, 10) + runWidgetControl(host.rootGraph, 'before', 'before') + store.updateWidgetControl(input.widgetId, { mode: 'decrement' }) + + host.subgraph.inputNode.slots[0].disconnect() + host.subgraph.inputNode.slots[0].connect(source.inputs[0], source) + await Promise.resolve() + runWidgetControl(host.rootGraph, 'before', 'before') + + expect(promotedSeedValue(host)).toBe(9) + expect(source.widgets?.[0].value).toBe(1) + }) + + it('advances only the authoritative outer value through nested promotion', () => { + const outerGraph = createTestSubgraph() + const innerGraph = createTestSubgraph({ rootGraph: outerGraph.rootGraph }) + const leaf = new SeedNode('increment') + innerGraph.add(leaf) + const inner = createTestSubgraphNode(innerGraph, { + parentGraph: outerGraph + }) + outerGraph.add(inner) + const leafWidget = leaf.widgets?.[0] + assert.exists(leafWidget) + expect(promoteValueWidgetViaSubgraphInput(inner, leaf, leafWidget)).toEqual( + { + ok: true + } + ) + const innerWidget = inner.widgets[0] + innerWidget.value = 17 + const outer = createTestSubgraphNode(outerGraph) + outerGraph.rootGraph.add(outer) + expect( + promoteValueWidgetViaSubgraphInput(outer, inner, innerWidget) + ).toEqual({ + ok: true + }) + outer.widgets[0].value = 31 + + runWidgetControl(outer.rootGraph, 'after', 'after') + + expect(promotedSeedValue(outer)).toBe(32) + expect(promotedSeedValue(inner)).toBe(17) + expect(leafWidget.value).toBe(1) + }) + + it('increments the host-owned value of a promoted seed after queueing', () => { + const host = createPromotedSeedHost('increment') + expect(promotedSeedValue(host)).toBe(1) + + runWidgetControl(host.rootGraph, 'after', 'after') + + expect(promotedSeedValue(host)).toBe(2) + }) + + it('leaves the value unchanged when the control mode is fixed', () => { + const host = createPromotedSeedHost('fixed') + + runWidgetControl(host.rootGraph, 'after', 'after') + + expect(promotedSeedValue(host)).toBe(1) + }) + + it('does not run control on a host input fed by an external link', () => { + const host = createPromotedSeedHost('increment') + const seedInput = host.inputs.find((input) => input.name === 'seed')! + const source = new LGraphNode('Source') + source.addOutput('out', 'INT') + if (host.graph?.getNodeById(host.id) !== host) host.graph?.add(host) + host.graph!.add(source) + source.connect(0, host, host.inputs.indexOf(seedInput)) + expect(host.isInputConnected(host.inputs.indexOf(seedInput))).toBe(true) + expect(seedInput.widgetId).toBeDefined() + expect( + useWidgetValueStore() + .getWidgetControls(host.rootGraph.id) + .map(([id]) => id) + ).toContain(seedInput.widgetId) + + runWidgetControl(host.rootGraph, 'after', 'after') + + expect(promotedSeedValue(host)).toBe(1) + }) +}) diff --git a/src/core/graph/widgets/control/widgetControlSystem.test.ts b/src/core/graph/widgets/control/widgetControlSystem.test.ts new file mode 100644 index 00000000000..51be911d629 --- /dev/null +++ b/src/core/graph/widgets/control/widgetControlSystem.test.ts @@ -0,0 +1,94 @@ +import { beforeEach, describe, expect, it, vi } from 'vitest' + +import { LGraph, LGraphNode } from '@/lib/litegraph/src/litegraph' +import { useSettingStore } from '@/platform/settings/settingStore' + +import { runWidgetControl } from './widgetControlSystem' + +function createControlledSeed(callback = vi.fn()) { + const graph = new LGraph() + const node = new LGraphNode('SeedNode') + const seed = node.addWidget('number', 'seed', 1, callback, { + min: 0, + max: 100, + step2: 1 + }) + seed.controlConfig = { mode: 'increment', hasFilter: false } + graph.add(node) + return { callback, graph, node, seed } +} + +describe('runWidgetControl', () => { + beforeEach(() => { + useSettingStore().settingValues['Comfy.WidgetControlMode'] = 'after' + }) + + it('advances the target and invokes its compatibility callback', () => { + const { callback, graph, seed } = createControlledSeed() + + runWidgetControl(graph, 'after', 'after') + + expect(seed.value).toBe(2) + expect(callback).toHaveBeenCalledWith(2) + }) + + it('skips link-fed targets', () => { + const { graph, node, seed } = createControlledSeed() + node.addInput('seed', 'number', { widget: { name: 'seed' } }) + const source = new LGraphNode('Source') + source.addOutput('value', 'number') + graph.add(source) + source.connect(0, node, 0) + + runWidgetControl(graph, 'after', 'after') + expect(seed.value).toBe(1) + }) + + it('skips the first execution in before mode', () => { + useSettingStore().settingValues['Comfy.WidgetControlMode'] = 'before' + const { graph, seed } = createControlledSeed() + + runWidgetControl(graph, 'before', 'before') + expect(seed.value).toBe(1) + + runWidgetControl(graph, 'before', 'before') + expect(seed.value).toBe(2) + }) + + it('does not skip when switching from after mode to before mode', () => { + const { graph, seed } = createControlledSeed() + + runWidgetControl(graph, 'after', 'after') + runWidgetControl(graph, 'before', 'before') + + expect(seed.value).toBe(3) + }) + + it.for(['before', 'after'] as const)( + 'records a linked first queue in %s mode before disconnecting', + (mode) => { + const { graph, node, seed } = createControlledSeed() + node.addInput('seed', 'number', { widget: { name: 'seed' } }) + const source = new LGraphNode('Source') + source.addOutput('value', 'number') + graph.add(source) + source.connect(0, node, 0) + + runWidgetControl(graph, 'before', mode) + expect(seed.value).toBe(1) + node.disconnectInput(0) + runWidgetControl(graph, 'before', 'before') + + expect(seed.value).toBe(2) + } + ) + + it('does not advance retained state after its node leaves the graph', () => { + const { graph, node, seed } = createControlledSeed() + graph.remove(node) + + runWidgetControl(graph, 'after', 'after') + + expect(seed.value).toBe(1) + }) +}) diff --git a/src/core/graph/widgets/control/widgetControlSystem.ts b/src/core/graph/widgets/control/widgetControlSystem.ts new file mode 100644 index 00000000000..bd33cdebdfb --- /dev/null +++ b/src/core/graph/widgets/control/widgetControlSystem.ts @@ -0,0 +1,67 @@ +import type { LGraph } from '@/lib/litegraph/src/litegraph' +import type { IBaseWidget } from '@comfyorg/graph-model/widgets' +import { useWidgetValueStore } from '@/stores/widgetValueStore' +import type { WidgetId } from '@comfyorg/graph-model/widgetId' +import { parseWidgetId, widgetId } from '@comfyorg/graph-model/widgetId' +import { forEachNode } from '@/utils/graphTraversalUtil' +import { mapLiveWidgetsById } from '@/utils/litegraphUtil' + +import { computeNextControlledValue } from './valueControl' + +type WidgetControlPhase = 'before' | 'after' + +function collectGraphTargets(graph: LGraph): { + linkFed: Set + live: Map +} { + const graphId = graph.rootGraph.id + const linkFed = new Set() + const live = new Map() + + forEachNode(graph, (node) => { + for (const [id, widget] of mapLiveWidgetsById(node)) live.set(id, widget) + for (const [index, input] of node.inputs.entries()) { + if (input.widgetId) { + const widget = node.getWidgetFromSlot(input) + if (widget) live.set(input.widgetId, widget) + } + if (!node.isInputConnected(index)) continue + if (input.widgetId) linkFed.add(input.widgetId) + else if (input.widget?.name) { + linkFed.add(widgetId(graphId, node.id, input.widget.name)) + } + } + }) + + return { linkFed, live } +} + +export function runWidgetControl( + graph: LGraph, + phase: WidgetControlPhase, + controlMode: WidgetControlPhase +): void { + const store = useWidgetValueStore() + const { linkFed, live } = collectGraphTargets(graph) + for (const [targetId, control] of store.getWidgetControls( + graph.rootGraph.id + )) { + const widget = live.get(targetId) + const target = store.getWidget(targetId) + if (!widget || !target) continue + + const firstRun = !control.hasExecuted + store.updateWidgetControl(targetId, { hasExecuted: true }) + if (phase !== controlMode || linkFed.has(targetId)) continue + if (phase === 'before' && firstRun) continue + + const next = computeNextControlledValue(target, control.mode, { + comboFilter: control.filter, + nodeId: parseWidgetId(targetId).nodeId + }) + if (next === undefined) continue + + store.setValue(targetId, next) + widget.callback?.(next) + } +} diff --git a/src/core/graph/widgets/controlWidgetMarker.ts b/src/core/graph/widgets/controlWidgetMarker.ts deleted file mode 100644 index bf92fc90a2b..00000000000 --- a/src/core/graph/widgets/controlWidgetMarker.ts +++ /dev/null @@ -1 +0,0 @@ -export const IS_CONTROL_WIDGET = Symbol() diff --git a/src/core/graph/widgets/dynamicGroupWidget.test.ts b/src/core/graph/widgets/dynamicGroupWidget.test.ts index 7fe658ba210..14590ec060a 100644 --- a/src/core/graph/widgets/dynamicGroupWidget.test.ts +++ b/src/core/graph/widgets/dynamicGroupWidget.test.ts @@ -15,6 +15,7 @@ import { } from 'vitest' import { i18n } from '@/i18n' +import { getControlProjections } from '@/core/graph/widgets/control/controlProjection' import { LGraph, LGraphNode, LiteGraph } from '@/lib/litegraph/src/litegraph' import { useLitegraphService } from '@/services/litegraphService' @@ -273,7 +274,9 @@ describe('DynamicGroup widgets', () => { expect(widget('seeds').value).toBe(1) expect(widget('seeds.0.seed').value).toBe(12) - expect(widget('seeds.0.seed.0').value).toBe('fixed') + expect(getControlProjections(widget('seeds.0.seed'))[0]?.value).toBe( + 'fixed' + ) expect(widget('tail').value).toBe('untouched') }) @@ -1034,18 +1037,24 @@ describe('DynamicGroup widgets', () => { } }) widget('seeds').value = 2 - widget('seeds.1.seed.0').value = 'fixed' + const [control] = getControlProjections(widget('seeds.1.seed')) + assert.exists(control) + control.value = 'fixed' widget('seeds.0').callback?.(undefined) - expect(widget('seeds.0.seed.0').value).toBe('fixed') + expect(getControlProjections(widget('seeds.0.seed'))[0]?.value).toBe( + 'fixed' + ) const saved = node.serialize() widget('seeds').value = 0 node.configure(saved) - expect(widget('seeds.0.seed.0').value).toBe('fixed') + expect(getControlProjections(widget('seeds.0.seed'))[0]?.value).toBe( + 'fixed' + ) expect( node.widgets?.filter((w) => w.name.startsWith('seeds.')) - ).toHaveLength(5) + ).toHaveLength(4) }) it('restores rows and their edited values after switching the containing DynamicCombo', () => { diff --git a/src/core/graph/widgets/dynamicGroupWidget.ts b/src/core/graph/widgets/dynamicGroupWidget.ts index 4a6c6605e98..256b8bfa695 100644 --- a/src/core/graph/widgets/dynamicGroupWidget.ts +++ b/src/core/graph/widgets/dynamicGroupWidget.ts @@ -23,6 +23,10 @@ import { useWidgetValueStore } from '@/stores/widgetValueStore' import { deriveWidgetSurfaces } from '@/types/widgetVisibility' import { mapUniqueNodes } from '@/utils/graphTraversalUtil' import { isSubgraph } from '@/utils/typeGuardUtil' +import { + announceControlValues, + getControlProjections +} from './control/controlProjection' import { dynamicGroupRowAllocationBound, @@ -240,8 +244,10 @@ export function dynamicGroupWidget( widget.serialize !== false && widget.type !== 'button' && widget.widgetId - ) + ) { store.announceValue(widget.widgetId) + announceControlValues(widget) + } } } @@ -363,6 +369,13 @@ export function dynamicGroupWidget( widget.options = options if (widget.name === name) { if (input?.label) widget.label = input.label + getControlProjections(widget).forEach((control, index) => { + Object.defineProperty(control, 'name', { + configurable: true, + enumerable: true, + get: () => `${widget.name}.${index}` + }) + }) return } widget.label ??= widget.name @@ -379,8 +392,10 @@ export function dynamicGroupWidget( widget.serialize !== false && widget.type !== 'button' && widget.widgetId - ) + ) { store.announceValue(widget.widgetId) + announceControlValues(widget) + } } } diff --git a/src/core/graph/widgets/valueControlWidgets.controlAfterGenerateName.test.ts b/src/core/graph/widgets/valueControlWidgets.controlAfterGenerateName.test.ts index 9c45c474a69..416a530aa89 100644 --- a/src/core/graph/widgets/valueControlWidgets.controlAfterGenerateName.test.ts +++ b/src/core/graph/widgets/valueControlWidgets.controlAfterGenerateName.test.ts @@ -2,7 +2,6 @@ import { describe, expect, it } from 'vitest' import { LGraph, LGraphNode } from '@/lib/litegraph/src/litegraph' import type { InputSpec, IntInputSpec } from '@comfyorg/object-info-parser' -import { CONTROL_OPTIONS } from '@/types/simplifiedWidget' import { addValueControlWidget, addValueControlWidgets @@ -92,12 +91,12 @@ describe('value control widget naming', () => { expect(control.value).toBe('randomize') }) - it('keeps a default mode an extension passes that is not a known mode', () => { + it('keeps an unrecognised extension mode inactive', () => { const { node, target } = intNode() const [control] = addValueControlWidgets(node, target, 'Fixed') - expect(control.value).toBe('Fixed') + expect(control.value).toBe('fixed') }) it('treats a blank name override as absent', () => { @@ -137,10 +136,18 @@ describe('value control widget naming', () => { intSpec({ control_after_generate: 'fixed' }) ) - expect(intControl.options.values).toEqual(CONTROL_OPTIONS) + expect(intControl.options.values).toEqual([ + 'fixed', + 'increment', + 'decrement', + 'randomize' + ]) expect(comboControl.options.values).toEqual([ - ...CONTROL_OPTIONS, - 'increment-wrap' + 'fixed', + 'increment', + 'increment-wrap', + 'decrement', + 'randomize' ]) }) diff --git a/src/core/graph/widgets/valueControlWidgets.ts b/src/core/graph/widgets/valueControlWidgets.ts index 3416406ecb5..3433dcc64f5 100644 --- a/src/core/graph/widgets/valueControlWidgets.ts +++ b/src/core/graph/widgets/valueControlWidgets.ts @@ -1,3 +1,7 @@ +import { getControlProjections } from '@/core/graph/widgets/control/controlProjection' +import { registerWidgetControlFromConfig } from '@/core/graph/widgets/control/widgetControl' +import { isValueControlMode } from '@/core/graph/widgets/control/valueControl' +import type { ValueControlMode } from '@/core/graph/widgets/control/valueControl' import { t } from '@/i18n' import { isComboWidget } from '@/lib/litegraph/src/litegraph' import type { LGraphNode } from '@/lib/litegraph/src/litegraph' @@ -8,184 +12,181 @@ import type { } from '@comfyorg/graph-model/widgets' import { useSettingStore } from '@/platform/settings/settingStore' import type { InputSpec } from '@comfyorg/object-info-parser' -import { CONTROL_OPTIONS } from '@/types/simplifiedWidget' -import { IS_CONTROL_WIDGET } from './controlWidgetMarker' -import { isValueControlMode, nextValueForLinkedTarget } from './valueControl' +function toControlMode(value: string | undefined): ValueControlMode { + return isValueControlMode(value) ? value : value ? 'fixed' : 'randomize' +} -function controlValueRunBefore() { - return useSettingStore().get('Comfy.WidgetControlMode') === 'before' +interface ValueControlOptions extends Record { + addFilterList?: boolean + controlAfterGenerateName?: string + controlFilterListName?: string } -export function updateControlWidgetLabel(widget: IBaseWidget) { - if (controlValueRunBefore()) { - widget.label = t('g.control_before_generate') - } else { - widget.label = t('g.control_after_generate') +function configureValueControl( + targetWidget: IBaseWidget, + defaultValue: string | undefined, + addFilterList: boolean +): void { + targetWidget.controlConfig = { + mode: toControlMode(defaultValue), + hasFilter: isComboWidget(targetWidget) && addFilterList } + registerWidgetControlFromConfig(targetWidget) } -const HAS_EXECUTED = Symbol() +function resolveControlTarget( + nodeOrTarget: LGraphNode | IBaseWidget, + legacyTarget: IBaseWidget | undefined +): IBaseWidget { + if (legacyTarget) return legacyTarget + if ('options' in nodeOrTarget) return nodeOrTarget + throw new TypeError('Value control target widget is required') +} + +function configuredControlProjections( + targetWidget: IBaseWidget +): [IComboWidget, ...IStringWidget[]] | undefined { + const [mode, ...filters] = getControlProjections(targetWidget) + return mode ? [mode, ...filters] : undefined +} -/** - * `control_after_generate` is either a group-node widget name override or a - * control mode that core's `io.ControlAfterGenerate` enum serialises into - * `object_info`. Only a non-blank string that is not a mode is a name. - */ -function controlAfterGenerateNameOverride(value: unknown): string | undefined { - if (typeof value !== 'string' || value.trim() === '') return undefined - return isValueControlMode(value) ? undefined : value +function legacyControlName( + defaultName: string, + optionName: keyof ValueControlOptions, + options: ValueControlOptions, + inputData: InputSpec | undefined +): string { + const inputName = inputData?.[1]?.[defaultName] + const override = [ + options[optionName], + defaultName === 'control_after_generate' && isValueControlMode(inputName) + ? undefined + : inputName + ].find((name): name is string => typeof name === 'string' && !!name.trim()) + if (override) return override + const prefix = inputData?.[1]?.control_prefix + return prefix ? `${prefix} ${defaultName}` : defaultName } +export function addValueControlWidget( + targetWidget: IBaseWidget, + defaultValue?: string +): void export function addValueControlWidget( node: LGraphNode, targetWidget: IBaseWidget, defaultValue?: string, - _values?: unknown, + values?: unknown, widgetName?: string, inputData?: InputSpec -): IComboWidget { - const name = - controlAfterGenerateNameOverride(inputData?.[1]?.control_after_generate) ?? - widgetName - const widgets = addValueControlWidgets( - node, +): IComboWidget +export function addValueControlWidget( + nodeOrTarget: LGraphNode | IBaseWidget, + targetOrDefault?: IBaseWidget | string, + legacyDefault?: string, + _legacyValues?: unknown, + legacyWidgetName?: string, + legacyInputData?: InputSpec +): void | IComboWidget { + const legacyTarget = + typeof targetOrDefault === 'object' ? targetOrDefault : undefined + const targetWidget = resolveControlTarget(nodeOrTarget, legacyTarget) + const defaultValue = legacyTarget + ? legacyDefault + : typeof targetOrDefault === 'string' + ? targetOrDefault + : undefined + configureValueControl( targetWidget, - defaultValue ?? 'randomize', - { - addFilterList: false, - controlAfterGenerateName: name - }, - inputData + defaultValue === legacyInputData?.[1]?.control_after_generate && + !isValueControlMode(defaultValue) + ? undefined + : defaultValue, + false ) - return widgets[0] -} - -interface ValueControlWidgetOptions { - addFilterList?: boolean - controlAfterGenerateName?: string - controlFilterListName?: string + if (!legacyTarget) return + + const projections = getControlProjections(targetWidget, [targetWidget]) + const [mode] = projections + if (!mode) return + mode.name = legacyControlName( + 'control_after_generate', + 'controlAfterGenerateName', + { controlAfterGenerateName: legacyWidgetName }, + legacyInputData + ) + return configuredControlProjections(targetWidget)?.[0] } +export function addValueControlWidgets( + targetWidget: IBaseWidget, + defaultValue?: string, + options?: ValueControlOptions +): void export function addValueControlWidgets( node: LGraphNode, targetWidget: IBaseWidget, defaultValue?: string, - options: ValueControlWidgetOptions = {}, + options?: ValueControlOptions, inputData?: InputSpec -): [IComboWidget, ...IStringWidget[]] { - // `useIntWidget` forwards a group-node name override here as the mode. - const specNameOverride = controlAfterGenerateNameOverride( - inputData?.[1]?.control_after_generate +): [IComboWidget, ...IStringWidget[]] +export function addValueControlWidgets( + nodeOrTarget: LGraphNode | IBaseWidget, + targetOrDefault?: IBaseWidget | string, + defaultOrOptions?: string | ValueControlOptions, + legacyOptions: ValueControlOptions = {}, + legacyInputData?: InputSpec +): void | [IComboWidget, ...IStringWidget[]] { + const legacyTarget = + typeof targetOrDefault === 'object' ? targetOrDefault : undefined + const targetWidget = resolveControlTarget(nodeOrTarget, legacyTarget) + const defaultValue = legacyTarget + ? typeof defaultOrOptions === 'string' + ? defaultOrOptions + : undefined + : typeof targetOrDefault === 'string' + ? targetOrDefault + : undefined + const options = legacyTarget + ? legacyOptions + : typeof defaultOrOptions === 'object' + ? defaultOrOptions + : {} + configureValueControl( + targetWidget, + defaultValue === legacyInputData?.[1]?.control_after_generate && + !isValueControlMode(defaultValue) + ? undefined + : defaultValue, + options.addFilterList !== false ) - if (!defaultValue || defaultValue === specNameOverride) { - defaultValue = 'randomize' - } - - const getName = ( - defaultName: 'control_after_generate' | 'control_filter_list', - optionName: 'controlAfterGenerateName' | 'controlFilterListName' - ) => { - const nameOverride = options[optionName] - if (nameOverride) return nameOverride - const inputOptions = inputData?.[1] - const specValue = inputOptions?.[defaultName] - const defaultNameOverride = - defaultName === 'control_after_generate' - ? controlAfterGenerateNameOverride(specValue) - : typeof specValue === 'string' - ? specValue - : undefined - if (defaultNameOverride !== undefined) return defaultNameOverride - if (inputOptions?.control_prefix) { - return inputOptions.control_prefix + ' ' + defaultName - } - return defaultName - } - - const valueControl = node.addWidget( - 'combo', - getName('control_after_generate', 'controlAfterGenerateName'), - defaultValue, - function () {}, - { - values: [...CONTROL_OPTIONS], - serialize: false, // Don't include this in prompt. - surfaces: { canvas: 'shown', vueNode: 'never', panel: 'never' } - } - ) as IComboWidget - - valueControl.tooltip = - 'Allows the linked widget to be changed automatically, for example randomizing the noise seed.' - valueControl[IS_CONTROL_WIDGET] = true - updateControlWidgetLabel(valueControl) - Object.defineProperty(valueControl, 'disabled', { - get: () => targetWidget.computedDisabled - }) - const widgets: [IComboWidget, ...IStringWidget[]] = [valueControl] - - const isCombo = isComboWidget(targetWidget) - let comboFilter: IStringWidget - if (isCombo) { - const values = valueControl.options.values - if (Array.isArray(values)) values.push('increment-wrap') - } - if (isCombo && options.addFilterList !== false) { - comboFilter = node.addWidget( - 'string', - getName('control_filter_list', 'controlFilterListName'), - '', - function () {}, - { - serialize: false // Don't include this in prompt. - } - ) as IStringWidget - updateControlWidgetLabel(comboFilter) - comboFilter.tooltip = - "Allows for filtering the list of values when changing the value via the control generate mode. Allows for RegEx matches in the format /abc/ to only filter to values containing 'abc'." - Object.defineProperty(comboFilter, 'disabled', { - get: () => targetWidget.computedDisabled - }) - - widgets.push(comboFilter) - } - - function applyWidgetControl() { - if ( - node.inputs.some( - (input, index) => - input.widget?.name === targetWidget.name && - node.isInputConnected(index) - ) + if (!legacyTarget) return + + const projections = getControlProjections(targetWidget, [targetWidget]) + const [mode] = projections + if (!mode) return + mode.name = legacyControlName( + 'control_after_generate', + 'controlAfterGenerateName', + options, + legacyInputData + ) + const filter = projections.at(1) + if (filter) { + filter.name = legacyControlName( + 'control_filter_list', + 'controlFilterListName', + options, + legacyInputData ) - return - - const next = nextValueForLinkedTarget({ - target: targetWidget, - linkedWidgets: targetWidget.linkedWidgets, - nodeId: node.id - }) - if (next === undefined) return - - targetWidget.value = next - targetWidget.callback?.(next) - } - - valueControl.beforeQueued = () => { - if (controlValueRunBefore()) { - // Don't run on first execution - if (valueControl[HAS_EXECUTED]) { - applyWidgetControl() - } - } - valueControl[HAS_EXECUTED] = true - } - - valueControl.afterQueued = () => { - if (!controlValueRunBefore()) { - applyWidgetControl() - } } + return configuredControlProjections(targetWidget) +} - return widgets +export function updateControlWidgetLabel(widget: IBaseWidget): void { + widget.label = + useSettingStore().get('Comfy.WidgetControlMode') === 'before' + ? t('g.control_before_generate') + : t('g.control_after_generate') } diff --git a/src/extensions/core/widgetInputs.test.ts b/src/extensions/core/widgetInputs.test.ts index 408e6d7e8b0..ab6248b870d 100644 --- a/src/extensions/core/widgetInputs.test.ts +++ b/src/extensions/core/widgetInputs.test.ts @@ -15,6 +15,7 @@ import type { import type { IBaseWidget } from '@comfyorg/graph-model/widgets' import { LegacyWidget } from '@/lib/litegraph/src/widgets/LegacyWidget' import { assetService } from '@/platform/assets/services/assetService' +import { useWidgetValueStore } from '@/stores/widgetValueStore' import type { ComfyNodeDef, InputSpec } from '@comfyorg/object-info-parser' import { app } from '@/scripts/app' import { CONFIG, GET_CONFIG } from '@comfyorg/graph-model/widgets' @@ -186,7 +187,12 @@ describe('PrimitiveNode', () => { }) expect(primitive.widgets?.[0].value).toBe(333) - expect(primitive.widgets?.[1].value).toBe('fixed') + const targetWidget = primitive.widgets?.[0] + expect( + targetWidget?.widgetId + ? useWidgetValueStore().getWidgetControl(targetWidget.widgetId)?.mode + : undefined + ).toBe('fixed') }) it('does not apply a serialized value left over from an unresolved output type', () => { diff --git a/src/extensions/core/widgetInputs.ts b/src/extensions/core/widgetInputs.ts index 1e0225f61d7..f49b96440ac 100644 --- a/src/extensions/core/widgetInputs.ts +++ b/src/extensions/core/widgetInputs.ts @@ -1,6 +1,7 @@ import { intersection } from 'es-toolkit/compat' import { useChainCallback } from '@/composables/functional/useChainCallback' +import { applyControlValues } from '@/core/graph/widgets/control/widgetControl' import { createWidgetRestorationState } from '@/lib/litegraph/src/LGraphNode' import { LGraphNode, LiteGraph } from '@/lib/litegraph/src/litegraph' import type { @@ -23,6 +24,7 @@ import type { ComfyNodeDef, InputSpec } from '@comfyorg/object-info-parser' import { app } from '@/scripts/app' import { useWidgetValueStore } from '@/stores/widgetValueStore' import type { WidgetValue } from '@/types/simplifiedWidget' +import { zeroUuid } from '@/utils/uuid' import { addValueControlWidgets } from '@/core/graph/widgets/valueControlWidgets' import { ComfyWidgets, isValidWidgetType } from '@/scripts/widgets' import { CONFIG, GET_CONFIG } from '@comfyorg/graph-model/widgets' @@ -319,19 +321,34 @@ export class PrimitiveNode extends LGraphNode { !inputData[1]?.control_after_generate && (widget.type === 'number' || widget.type === 'combo') ) { - addValueControlWidgets(this, widget, 'fixed', undefined, inputData) - if (this.widgets?.[1]) widget.linkedWidgets = [this.widgets[1]] + addValueControlWidgets(widget, 'fixed') + } + + const store = useWidgetValueStore() + const graphId = this.graph?.rootGraph.id ?? zeroUuid + const restoredMode = store.getRestoredWidgetValue( + graphId, + this.id, + { name: 'control_after_generate', type: 'combo', options: {} }, + 1 + ) + const restoredFilter = store.getRestoredWidgetValue( + graphId, + this.id, + { name: 'control_filter_list', type: 'string', options: {} }, + 2 + ) + if (restoredMode) { + applyControlValues( + widget, + [restoredMode.value, restoredFilter?.value], + 0 + ) } const controlValues = this.controlValues - if ( - this.widgets && - this.lastType === this.widgets[0]?.type && - controlValues?.length === this.widgets.length - 1 - ) { - for (let i = 0; i < controlValues.length; i++) { - this.widgets[i + 1].value = controlValues[i] - } + if (this.lastType === this.widgets?.[0]?.type && controlValues?.length) { + applyControlValues(widget, controlValues, 0) } this._finalizeWidget(widget, oldWidth, oldHeight, recreating) @@ -455,12 +472,16 @@ export class PrimitiveNode extends LGraphNode { } } - // Temporarily store the current values in case the node is being recreated - this.controlValues = [] this.lastType = this.widgets[0]?.type - for (let i = 1; i < this.widgets.length; i++) { - this.controlValues.push(this.widgets[i].value) - } + const targetId = this.widgets[0]?.widgetId + const control = targetId + ? useWidgetValueStore().getWidgetControl(targetId) + : undefined + this.controlValues = control + ? control.filter === undefined + ? [control.mode] + : [control.mode, control.filter] + : [] setTimeout(() => { delete this.lastType delete this.controlValues diff --git a/src/lib/litegraph/src/LGraphNode.reactivity.test.ts b/src/lib/litegraph/src/LGraphNode.reactivity.test.ts index 7992d2bf5a4..7c54b558154 100644 --- a/src/lib/litegraph/src/LGraphNode.reactivity.test.ts +++ b/src/lib/litegraph/src/LGraphNode.reactivity.test.ts @@ -119,6 +119,17 @@ describe('_setConcreteSlots', () => { expect(runs).toBe(2) stop(runner) }) + + test('exposes widget positions after layout without requiring a draw', () => { + const graph = new LGraph() + const node = new LGraphNode('test') + const widget = node.addWidget('number', 'value', 0, () => undefined, {}) + graph.add(node) + + node.arrange() + + expect(widget.last_y).toBe(widget.y) + }) }) describe('widgets array reactivity', () => { diff --git a/src/lib/litegraph/src/LGraphNode.ts b/src/lib/litegraph/src/LGraphNode.ts index 6c6e79675d8..3830d8a9684 100644 --- a/src/lib/litegraph/src/LGraphNode.ts +++ b/src/lib/litegraph/src/LGraphNode.ts @@ -24,6 +24,12 @@ import { createNodeShellState, setTrackedNodeState } from '@/core/graph/nodeShell/nodeShellState' +import { getControlProjectionBatch } from '@/core/graph/widgets/control/controlProjection' +import { + appendControlValues, + applyControlValues, + decodeWidgetValueLayout +} from '@/core/graph/widgets/control/widgetControl' import { calculateInputSlotPosFromSlot, getSlotPosition @@ -219,6 +225,16 @@ interface IMouseOverData { overWidget?: IBaseWidget } +function renderWidgets(node: LGraphNode): IBaseWidget[] { + const widgets = node.widgets ?? [] + if (LiteGraph.vueNodesMode) return [...widgets] + const projections = getControlProjectionBatch(widgets) + return widgets.flatMap((widget) => [ + widget, + ...(projections.get(widget) ?? []) + ]) +} + function legacyArrayItem(items: readonly T[], index: number): T | undefined { return items[index] } @@ -241,16 +257,21 @@ function cloneWidgetValueTwice( function serialiseWidgetValues(widgets: IBaseWidget[]) { const positional: TWidgetValue[] = [] const named: Record = {} + const projections = getControlProjectionBatch(widgets) for (const widget of widgets) { if (widget.serialize === false) continue - const [positionalValue, namedValue] = cloneWidgetValueTwice(widget.value) - positional.push(positionalValue) - Object.defineProperty(named, widget.name, { - value: namedValue, - writable: true, - enumerable: true, - configurable: true - }) + for (const valueWidget of [widget, ...(projections.get(widget) ?? [])]) { + const [positionalValue, namedValue] = cloneWidgetValueTwice( + valueWidget.value + ) + positional.push(positionalValue) + Object.defineProperty(named, valueWidget.name, { + value: namedValue, + writable: true, + enumerable: true, + configurable: true + }) + } } return { widgets_values: positional, widgets_values_named: named } } @@ -283,15 +304,21 @@ function configureCanonicalField( export function createWidgetRestorationState( info: Pick, - fallbackNames?: readonly string[] + fallbackNames?: readonly string[], + widgets?: readonly Pick[] ) { const positional = Array.from(info.widgets_values ?? []) + const valueLayout = widgets + ? decodeWidgetValueLayout(widgets, positional) + : undefined const named = info.widgets_values_named ?? (info.widgets_values && fallbackNames ? Object.fromEntries( - positional.flatMap((value, index) => - fallbackNames[index] ? [[fallbackNames[index], value]] : [] + fallbackNames.flatMap((name, index) => + name + ? [[name, positional[valueLayout?.[index]?.valueIndex ?? index]]] + : [] ) ) : undefined) @@ -432,6 +459,10 @@ const NAMED_SHAPES = { */ const wireExecutionOrders = new WeakMap() +const widgetValuesAtRegistration = new WeakMap< + LGraphNode, + Map +>() export class LGraphNode implements NodeLike, Positionable, IPinnable, IColorable @@ -1337,10 +1368,13 @@ export class LGraphNode const restoration = createWidgetRestorationState( info, - this.constructor.nodeData?.fallbackWidgetsValuesNames + this.constructor.nodeData?.fallbackWidgetsValuesNames, + (this.widgets ?? []).filter((widget) => widget.serialize !== false) ) const namedValues = restoration.named const graphId = this.graph?.rootGraph.id ?? zeroUuid + const registeredValues = new Map() + widgetValuesAtRegistration.set(this, registeredValues) try { useWidgetValueStore().setNodeWidgetRestoration( graphId, @@ -1364,10 +1398,26 @@ export class LGraphNode JSON.stringify(this.properties[w.options.property]) ) } + } - let positionalIndex = 0 - for (const widget of this.widgets) { - if (widget.serialize === false) continue + const initialWidgets = new Set(this.widgets) + const restoredWidgets = new Set() + const restoreWidgets = () => { + const serializableWidgets = (this.widgets ?? []).filter( + (widget) => widget.serialize !== false + ) + const valueLayout = decodeWidgetValueLayout( + serializableWidgets, + restoration.positional, + new Set( + serializableWidgets.filter((widget) => !initialWidgets.has(widget)) + ) + ) + const projections = getControlProjectionBatch(this.widgets ?? []) + for (const [index, widget] of serializableWidgets.entries()) { + if (restoredWidgets.has(widget)) continue + restoredWidgets.add(widget) + const { valueIndex, controlValueCount } = valueLayout[index] const restored = useWidgetValueStore().getRestoredWidgetValue( graphId, this.id, @@ -1375,13 +1425,43 @@ export class LGraphNode getWidgetRestorationSlot( this, widget, - positionalIndex++, + valueIndex, restoration.positional.length ) ) - if (restored) widget.value = restored.value + if ( + restored && + (!registeredValues.has(widget) || + Object.is(widget.value, registeredValues.get(widget))) + ) + widget.value = restored.value + applyControlValues( + widget, + restoration.positional, + valueIndex + 1, + controlValueCount + ) + if (restoration.restoreNamed && namedValues) { + for (const control of projections.get(widget) ?? []) { + if (Object.hasOwn(namedValues, control.name)) { + const value = namedValues[control.name] + if (typeof value === 'string') control.value = value + } + } + } + const currentWidgets = (this.widgets ?? []).filter( + (widget) => widget.serialize !== false + ) + if ( + currentWidgets.length !== serializableWidgets.length || + currentWidgets.some( + (widget, i) => widget !== serializableWidgets[i] + ) + ) + break } } + restoreWidgets() if (this.pinned) this.resizable = false if (this.widgets_up) { @@ -1393,6 +1473,13 @@ export class LGraphNode } this.onConfigure?.(extensionConfigureView(this, info)) + while ( + (this.widgets ?? []).some( + (widget) => widget.serialize !== false && !restoredWidgets.has(widget) + ) + ) { + restoreWidgets() + } if (this.widgets && namedValues) { const legacyShadow = computeLegacyWidgetShadow( this.widgets, @@ -1405,6 +1492,7 @@ export class LGraphNode ) } } finally { + widgetValuesAtRegistration.delete(this) clearWidgetRestorationSlots(this) useWidgetValueStore().clearNodeWidgetRestoration(graphId, this.id) } @@ -2185,7 +2273,8 @@ export class LGraphNode const ctorSize = this.constructor.size if (ctorSize) return [ctorSize[0], ctorSize[1]] - const { inputs, outputs, widgets } = this + const { inputs, outputs } = this + const widgets = renderWidgets(this) let rows = Math.max( inputs.filter((input) => !isWidgetInputSlot(input)).length, outputs.length @@ -2222,7 +2311,7 @@ export class LGraphNode if (output_width < text_width) output_width = text_width } - const minWidth = LiteGraph.NODE_WIDTH * (widgets?.length ? 1.5 : 1) + const minWidth = LiteGraph.NODE_WIDTH * (widgets.length ? 1.5 : 1) // Text + slot width + centre padding const centrePadding = input_width && output_width ? 5 : 0 const slotsWidth = @@ -2243,7 +2332,7 @@ export class LGraphNode // Get widget height & expand size if necessary let widgets_height = 0 - if (widgets?.length) { + if (widgets.length) { for (const widget of widgets) { if (!this.isWidgetRowVisible(widget)) continue @@ -2446,9 +2535,14 @@ export class LGraphNode if (widget.serialize === false) return widget - const positionalIndex = - this.widgets.filter((candidate) => candidate.serialize !== false).length - - 1 + const precedingValues: unknown[] = [] + for (const candidate of this.widgets) { + if (candidate === widget) break + if (candidate.serialize === false) continue + precedingValues.push(null) + appendControlValues(candidate, precedingValues) + } + const positionalIndex = precedingValues.length const restored = useWidgetValueStore().getRestoredWidgetValue( this.graph?.rootGraph.id ?? zeroUuid, this.id, @@ -2456,6 +2550,7 @@ export class LGraphNode positionalIndex ) if (restored) widget.value = restored.value + widgetValuesAtRegistration.get(this)?.set(widget, widget.value) return widget } @@ -2692,8 +2787,9 @@ export class LGraphNode canvasY: number, includeDisabled = false ): IBaseWidget | undefined { - const { widgets, pos, size } = this - if (!widgets?.length) return + const { pos, size } = this + const widgets = renderWidgets(this) + if (!widgets.length) return const x = canvasX - pos[0] const y = canvasY - pos[1] @@ -4328,13 +4424,11 @@ export class LGraphNode * the connected input slot. */ getLayoutWidgets(): IBaseWidget[] { - return ( - this.widgets?.filter((widget) => { - const { surfaces, suppression } = - widget.visibility ?? deriveWidgetVisibility(widget) - return !suppression.byExtension && surfaces.canvas !== 'never' - }) ?? [] - ) + return renderWidgets(this).filter((widget) => { + const { surfaces, suppression } = + widget.visibility ?? deriveWidgetVisibility(widget) + return !suppression.byExtension && surfaces.canvas !== 'never' + }) } /** @@ -4376,7 +4470,7 @@ export class LGraphNode if (!this.widgets) return const nodeWidth = this.renderingSize[0] - const { widgets } = this + const widgets = renderWidgets(this) const H = LiteGraph.NODE_WIDGET_HEIGHT const showText = !lowQuality ctx.save() @@ -4619,6 +4713,7 @@ export class LGraphNode let y = startY for (const w of visibleWidgets) { w.y = y + w.last_y = y y += w.computedHeight ?? 0 } diff --git a/src/lib/litegraph/src/subgraph/SubgraphNode.ts b/src/lib/litegraph/src/subgraph/SubgraphNode.ts index 5922c4a501d..5c5e1f7a503 100644 --- a/src/lib/litegraph/src/subgraph/SubgraphNode.ts +++ b/src/lib/litegraph/src/subgraph/SubgraphNode.ts @@ -11,6 +11,16 @@ import type { import { once } from 'es-toolkit' import cloneDeep from 'es-toolkit/compat/cloneDeep' +import { + appendControlValues, + applyControlValues, + decodeWidgetValueLayout +} from '@/core/graph/widgets/control/widgetControl' +import { + clearPromotedWidget, + copyPromotedWidgetControl, + getSerializablePromotedInputs +} from '@/core/graph/subgraph/promotedWidgetControl' import type { LGraphButton } from '@/lib/litegraph/src/LGraphButton' import { LGraphNode } from '@/lib/litegraph/src/LGraphNode' @@ -444,7 +454,7 @@ export class SubgraphNode extends LGraphNode implements BaseLGraph { override configure(info: ExportedSubgraphInstance): void { for (const input of this.inputs) { - this._clearPromotedWidget(input) + clearPromotedWidget(input) if ( input._listenerController && typeof input._listenerController.abort === 'function' @@ -494,25 +504,35 @@ export class SubgraphNode extends LGraphNode implements BaseLGraph { ) super.configure(info) - if (!info.widgets_values_named || !LiteGraph.namedValuesRestore) - this._applyPromotedWidgetValues(info.widgets_values) + this._applyPromotedWidgetValues( + info.widgets_values, + !info.widgets_values_named || !LiteGraph.namedValuesRestore + ) } private _applyPromotedWidgetValues( - widgetValues: ExportedSubgraphInstance['widgets_values'] + widgetValues: ExportedSubgraphInstance['widgets_values'], + restoreTargetValues = true ): void { const quarantineValuesByInputName = this._readQuarantineHostValuesByName() - let valueIndex = 0 - for (const input of this.inputs) { - if (!input.widgetId) continue + const promotedInputs = getSerializablePromotedInputs(this.inputs) + const values = widgetValues ?? [] + const valueLayout = decodeWidgetValueLayout(promotedInputs, values) + for (const [index, input] of promotedInputs.entries()) { + const { valueIndex, controlValueCount } = valueLayout[index] const value = quarantineValuesByInputName.has(input.name) ? quarantineValuesByInputName.get(input.name) - : widgetValues?.[valueIndex] - if (value !== undefined) { + : values[valueIndex] + if (restoreTargetValues && value !== undefined) { useWidgetValueStore().restoreValue(input.widgetId, value) } - valueIndex += 1 + applyControlValues( + input.widgetId, + values, + valueIndex + 1, + controlValueCount + ) } } @@ -593,11 +613,20 @@ export class SubgraphNode extends LGraphNode implements BaseLGraph { for (const input of this.inputs) { const subgraphInput = input._subgraphSlot - if (!subgraphInput) continue - this._resolveInputWidget(subgraphInput, input) + if (!subgraphInput) { + clearPromotedWidget(input) + delete input.widget + delete input.pos + delete input.widgetId + continue + } + const resolved = this._resolveInputWidget(subgraphInput, input) const previous = previousBindings.get(input) - if (previous && input.widgetId) { - store.setValue(input.widgetId, previous.value) + if (resolved && previous) { + store.setValue( + widgetId(this.rootGraph.id, this.id, input.name), + previous.value + ) } } @@ -612,7 +641,7 @@ export class SubgraphNode extends LGraphNode implements BaseLGraph { private _resolveInputWidget( subgraphInput: SubgraphInput, input: INodeInputSlot - ) { + ): boolean { for (const linkId of subgraphInput.linkIds) { const link = this.subgraph.getLink(linkId) if (!link) { @@ -637,7 +666,7 @@ export class SubgraphNode extends LGraphNode implements BaseLGraph { const widget = inputNode.getWidgetFromSlot(targetInput) if (widget) { this._setWidget(subgraphInput, input, widget, targetInput.widget) - break + return Boolean(input.widgetId) } // Nested promotion: the source is itself a promoted subgraph input with @@ -646,9 +675,10 @@ export class SubgraphNode extends LGraphNode implements BaseLGraph { const nested = this._resolveNestedPromotedSource(inputNode, targetInput) if (nested) { this._setWidget(subgraphInput, input, nested.widget, targetInput.widget) - break + return Boolean(input.widgetId) } } + return false } private _resolveNestedPromotedSource( @@ -676,24 +706,20 @@ export class SubgraphNode extends LGraphNode implements BaseLGraph { ) { this.invalidatePromotedViews() - this._clearPromotedWidget(input) - input.widget ??= { name: subgraphInput.name } input.widget.name = subgraphInput.name if (inputWidget) Object.setPrototypeOf(input.widget, inputWidget) if (this.id === UNASSIGNED_NODE_ID) { - // Registering now would key the store under a construction-time id - // shared by every not-yet-added SubgraphNode (e.g. a clipboard clone - // that gets discarded), letting a later unrelated instance inherit - // this value. onAdded() performs the deferred registration once a - // real id is assigned. + clearPromotedWidget(input) input.widgetId = undefined return } const id = widgetId(this.rootGraph.id, this.id, subgraphInput.name) const store = useWidgetValueStore() + clearPromotedWidget(input, { retainControl: input.widgetId === id }) + const visibility = cloneDeep( interiorWidget.visibility ?? deriveWidgetVisibility(interiorWidget) ) @@ -720,6 +746,7 @@ export class SubgraphNode extends LGraphNode implements BaseLGraph { } input.widgetId = id + copyPromotedWidgetControl(interiorWidget, id) input._widget = this.createPromotedHostWidget(input, id, interiorWidget) ?? this._projectPromotedWidget(input) @@ -743,24 +770,11 @@ export class SubgraphNode extends LGraphNode implements BaseLGraph { return undefined } - /** - * Runs the host widget's `onRemove` (unregistering DOM widgets) and clears it. - * Unlike {@link ensureWidgetRemoved}, dispatches no demotion event, so it is - * safe on re-resolution. - */ - private _clearPromotedWidget(input: INodeInputSlot): void { - input._widget?.onRemove?.() - input._widget = undefined - } - override onAdded(_graph: LGraph): void { const store = useWidgetValueStore() for (const input of this.inputs) { const previousId = input.widgetId if (!previousId) { - // Registration deferred by _setWidget while this.id was still - // UNASSIGNED_NODE_ID (e.g. widget resolution during construction). - // Perform it now that a real id is assigned. if (input._subgraphSlot) this._resolveInputWidget(input._subgraphSlot, input) continue @@ -785,7 +799,7 @@ export class SubgraphNode extends LGraphNode implements BaseLGraph { store.setValue(nextId, state.value) store.deleteWidget(previousId) input.widgetId = nextId - this._clearPromotedWidget(input) + clearPromotedWidget(input) if (input._subgraphSlot) { this._resolveInputWidget(input._subgraphSlot, input) } @@ -975,7 +989,7 @@ export class SubgraphNode extends LGraphNode implements BaseLGraph { ) { input._listenerController.abort() } - this._clearPromotedWidget(input) + clearPromotedWidget(input) } for (const widget of this._extraWidgets) widget.onRemove?.() } @@ -1041,11 +1055,12 @@ export class SubgraphNode extends LGraphNode implements BaseLGraph { serialized.properties = serializedProperties - const widgetValues = this.inputs.flatMap((input) => { - if (!input.widgetId) return [] + const widgetValues: TWidgetValue[] = [] + for (const input of getSerializablePromotedInputs(this.inputs)) { const value = useWidgetValueStore().getWidget(input.widgetId)?.value - return [isWidgetValue(value) ? value : undefined] - }) + widgetValues.push(isWidgetValue(value) ? value : undefined) + appendControlValues(input.widgetId, widgetValues) + } if (widgetValues.some((value) => value !== undefined)) { serialized.widgets_values = widgetValues diff --git a/src/lib/litegraph/src/subgraph/SubgraphWidgetPromotion.test.ts b/src/lib/litegraph/src/subgraph/SubgraphWidgetPromotion.test.ts index ec75e6ecb42..609c058e4f7 100644 --- a/src/lib/litegraph/src/subgraph/SubgraphWidgetPromotion.test.ts +++ b/src/lib/litegraph/src/subgraph/SubgraphWidgetPromotion.test.ts @@ -27,7 +27,6 @@ import { } from '@/core/graph/subgraph/migration/proxyWidgetMigration' import { reorderSubgraphInputsByName } from '@/core/graph/subgraph/promotionUtils' import type { SerializedProxyWidgetTuple } from '@/core/schemas/promotionSchema' -import { IS_CONTROL_WIDGET } from '@/core/graph/widgets/controlWidgetMarker' import { getPreviewExposureHostLocator, usePreviewExposureStore @@ -963,20 +962,6 @@ describe('SubgraphWidgetPromotion', () => { if (r.kind === 'byName') reorderSubgraphInputsByName(host, r.order) } - function makeControlWidget( - value: 'increment' | 'fixed', - marker: boolean - ) { - const base = { - name: 'control_after_generate', - value, - serialize: false, - beforeQueued: () => {}, - afterQueued: () => {} - } - return marker ? { ...base, [IS_CONTROL_WIDGET]: true } : base - } - type ReorderCase = { name: string sources: SourceSpec[] @@ -1079,8 +1064,6 @@ describe('SubgraphWidgetPromotion', () => { type ControlCase = { name: string editVia: 'viewKey' | 'vue' - controlMode: 'increment' | 'fixed' - controlMarker: boolean seedHostValue: number mutateSourceSeedAfterReorder?: number expect: { @@ -1091,19 +1074,15 @@ describe('SubgraphWidgetPromotion', () => { const controlCases: ControlCase[] = [ { - name: 'ViewKey + increment: source seed mutation after reorder is ignored in prompt', + name: 'ViewKey: source seed mutation after reorder is ignored in prompt', editVia: 'viewKey', - controlMode: 'increment', - controlMarker: false, seedHostValue: 123456, mutateSourceSeedAfterReorder: 789, expect: { promptSeed: 123456 } }, { - name: 'Vue + fixed: host-wins — does not push Vue value into source seed', + name: 'Vue: host-wins — does not push Vue value into source seed', editVia: 'vue', - controlMode: 'fixed', - controlMarker: false, seedHostValue: 123456, expect: { sourceSeed: 0 } } @@ -1127,10 +1106,6 @@ describe('SubgraphWidgetPromotion', () => { writePromotedWidgetValue(host, 0, 'positive prompt') writePromotedWidgetValue(host, 1, 'negative prompt') writePromotedWidgetValue(host, 2, c.seedHostValue) - seed.widget.linkedWidgets = [ - makeControlWidget(c.controlMode, c.controlMarker) as never - ] - reorderSubgraphInputsByName(host, ['text_1', 'seed', 'text']) if (c.mutateSourceSeedAfterReorder !== undefined) { @@ -1148,6 +1123,51 @@ describe('SubgraphWidgetPromotion', () => { } }) + it.for([false, true])( + 'omits nonserializable promoted values and restores following controls with named restore %s', + (namedRestore, { onTestFinished }) => { + const previousNamedRestore = LiteGraph.namedValuesRestore + onTestFinished(() => { + LiteGraph.namedValuesRestore = previousNamedRestore + }) + LiteGraph.namedValuesRestore = namedRestore + const subgraph = createTestSubgraph() + const transient = createNodeWithWidget('Transient') + transient.widget.serialize = false + transient.widget.controlConfig = { + mode: 'decrement', + hasFilter: false + } + subgraph.add(transient.node) + subgraph + .addInput('transient', 'number') + .connect(transient.input, transient.node) + const saved = createNodeWithWidget('Saved') + saved.widget.controlConfig = { mode: 'increment', hasFilter: false } + subgraph.add(saved.node) + subgraph.addInput('saved', 'number').connect(saved.input, saved.node) + const host = createTestSubgraphNode(subgraph, { id: 101 }) + writePromotedWidgetValue(host, 0, 73) + writePromotedWidgetValue(host, 1, 29) + const store = useWidgetValueStore() + store.updateWidgetControl(promotedInputs(host)[1].widgetId, { + mode: 'randomize' + }) + + const serialized = host.serialize() + expect(serialized.widgets_values).toEqual([29, 'randomize']) + store.deleteWidget(promotedInputs(host)[0].widgetId) + store.deleteWidget(promotedInputs(host)[1].widgetId) + const reloaded = createTestSubgraphNode(subgraph, { id: 101 }) + reloaded.configure(serialized) + + expect( + promotedWidgetStates(reloaded).map((state) => state.value) + ).toEqual([42, 29]) + expect(reloaded.serialize().widgets_values).toEqual([29, 'randomize']) + } + ) + it('serializes promoted values from each host independently', () => { const subgraph = createTestSubgraph({ inputs: [{ name: 'value', type: 'number' }] diff --git a/src/lib/litegraph/src/subgraph/promotedWidgetStoreProjection.ts b/src/lib/litegraph/src/subgraph/promotedWidgetStoreProjection.ts index 9ccba906f27..4f191eb7b1a 100644 --- a/src/lib/litegraph/src/subgraph/promotedWidgetStoreProjection.ts +++ b/src/lib/litegraph/src/subgraph/promotedWidgetStoreProjection.ts @@ -36,6 +36,9 @@ export function createPromotedWidgetStoreProjection( set options(next) { store.setOptions(id, next) }, + get serialize() { + return store.getWidget(id)?.serialize + }, get value() { return store.getWidget(id)?.value }, diff --git a/src/lib/litegraph/src/widgets/BaseWidget.test.ts b/src/lib/litegraph/src/widgets/BaseWidget.test.ts index 6a58bbc0bb0..4384e399693 100644 --- a/src/lib/litegraph/src/widgets/BaseWidget.test.ts +++ b/src/lib/litegraph/src/widgets/BaseWidget.test.ts @@ -74,6 +74,17 @@ describe('BaseWidget store integration', () => { graph.add(node) }) + it('restores a connected widget after constructing a view of its visibility', () => { + const widget = createTestWidget(node) + widget.connectionSuppressed = true + + const view = new NumberWidget(widget, node) + widget.connectionSuppressed = false + + expect(node.isWidgetRowVisible(widget)).toBe(true) + expect(node.isWidgetVisible(view)).toBe(true) + }) + it('draws only the label for a connection-suppressed row', () => { const widget = createTestWidget(node, { label: 'Input label', diff --git a/src/lib/litegraph/src/widgets/BaseWidget.ts b/src/lib/litegraph/src/widgets/BaseWidget.ts index 7a2164c3a98..c084091c962 100644 --- a/src/lib/litegraph/src/widgets/BaseWidget.ts +++ b/src/lib/litegraph/src/widgets/BaseWidget.ts @@ -3,6 +3,7 @@ import type { LGraphNode } from '@comfyorg/graph-model/entityRegistry' import { t } from '@/i18n' +import { registerWidgetControlFromConfig } from '@/core/graph/widgets/control/widgetControl' import { drawTextInArea } from '@/lib/litegraph/src/draw' import { cachedMeasureText } from '@/lib/litegraph/src/utils/textMeasureCache' import { Rectangle } from '@/lib/litegraph/src/infrastructure/Rectangle' @@ -434,6 +435,7 @@ export abstract class BaseWidget ) if (!registered) return this.bindRegisteredState(nodeId) + registerWidgetControlFromConfig(this) } releaseRegisteredState(): void { @@ -462,20 +464,14 @@ export abstract class BaseWidget constructor(widget: TWidget & { node: LGraphNode }) constructor(widget: TWidget, node: LGraphNode) constructor(widget: TWidget & { node: LGraphNode }, node?: LGraphNode) { - // Private fields this._node = node ?? widget.node - this._visibility = deriveWidgetVisibility(widget) + this._visibility = widget.visibility ?? deriveWidgetVisibility(widget) - // The set and get functions for DOM widget values are hacked on to the options object; - // attempting to set value before options will throw. - // https://github.com/Comfy-Org/ComfyUI_frontend/blob/df86da3d672628a452baed3df3347a52c0c8d378/src/scripts/domWidget.ts#L125 this.name = widget.name this.installOptionsShim(widget.options) this.type = widget.type - // `node` has no setter - Object.assign will throw. - // TODO: Resolve this workaround. Ref: https://github.com/Comfy-Org/litegraph.js/issues/1022 const { label, hidden, disabled, value } = widget const safeValues = { ...widget } as unknown as Record for (const key of [ @@ -490,6 +486,7 @@ export abstract class BaseWidget 'displayValue', 'labelBaseline', 'label', + 'visibility', 'hidden', 'disabled', 'value', @@ -513,7 +510,7 @@ export abstract class BaseWidget options: this._rawOptions, y: this.y } - if (hidden !== undefined) this.hidden = hidden + if (hidden !== undefined && !widget.visibility) this.hidden = hidden } getOutlineColor() { diff --git a/src/locales/en/main.json b/src/locales/en/main.json index 2bdf4f0c472..0e40f8e0127 100644 --- a/src/locales/en/main.json +++ b/src/locales/en/main.json @@ -4293,10 +4293,13 @@ "randomizeDesc": "Shuffles the value randomly after each generation", "increment": "Increment Value", "incrementDesc": "Adds 1 to value or selects the next option", + "increment-wrap": "Increment with Wrap", "decrement": "Decrement Value", "decrementDesc": "Subtracts 1 from value or selects the previous option", "fixed": "Fixed Value", "fixedDesc": "Leaves value unchanged", + "modeTooltip": "Allows the linked widget to be changed automatically, for example randomizing the noise seed.", + "filterTooltip": "Allows for filtering the list of values when changing the value via the control generate mode. Allows for RegEx matches in the format /abc/ to only filter to values containing 'abc'.", "editSettings": "Edit control settings" } }, diff --git a/src/platform/workflowErrors/nodeReplacement/useNodeReplacement.test.ts b/src/platform/workflowErrors/nodeReplacement/useNodeReplacement.test.ts index 1907851d606..47a93aad939 100644 --- a/src/platform/workflowErrors/nodeReplacement/useNodeReplacement.test.ts +++ b/src/platform/workflowErrors/nodeReplacement/useNodeReplacement.test.ts @@ -731,7 +731,7 @@ describe('useNodeReplacement', () => { it('should transfer widget values using old_widget_ids', () => { const placeholder = createPlaceholderNode(1, 'ResizeImagesByLongerEdge') // Set widget values in serialized data - placeholder.last_serialization!.widgets_values = [512] + placeholder.last_serialization!.widgets_values = [512, 'increment', 20] const graph = createMockGraph([placeholder]) placeholder.graph = graph @@ -742,7 +742,8 @@ describe('useNodeReplacement', () => { const newNode = createNewNode( [ { name: 'image', link: null }, - { name: 'largest_size', link: null } + { name: 'largest_size', link: null }, + { name: 'face_point_size', link: null } ], [{ name: 'IMAGE', links: null }], [ @@ -759,10 +760,11 @@ describe('useNodeReplacement', () => { makeMissingNodeType('ResizeImagesByLongerEdge', { new_node_id: 'ImageScaleToMaxDimension', old_node_id: 'ResizeImagesByLongerEdge', - old_widget_ids: ['longer_edge'], + old_widget_ids: ['longer_edge', 'face_point_size'], input_mapping: [ { new_id: 'image', old_id: 'images' }, { new_id: 'largest_size', old_id: 'longer_edge' }, + { new_id: 'face_point_size', old_id: 'face_point_size' }, { new_id: 'upscale_method', set_value: 'lanczos' } ], output_mapping: [{ new_idx: 0, old_idx: 0 }] @@ -770,6 +772,7 @@ describe('useNodeReplacement', () => { ]) expect(newNode.widgets![0].value).toBe(512) + expect(newNode.widgets![1].value).toBe(20) expect(setNodeId).toHaveBeenCalledWith(1) }) diff --git a/src/platform/workflowErrors/nodeReplacement/useNodeReplacement.ts b/src/platform/workflowErrors/nodeReplacement/useNodeReplacement.ts index d3fe0676275..c17b6f45b93 100644 --- a/src/platform/workflowErrors/nodeReplacement/useNodeReplacement.ts +++ b/src/platform/workflowErrors/nodeReplacement/useNodeReplacement.ts @@ -2,6 +2,7 @@ import { canTransferReplacementOwnership, transferReplacementOwnership } from '@/core/graph/nodeShell/nodeShellState' +import { decodeWidgetValueLayout } from '@/core/graph/widgets/control/widgetControl' import type { LGraph, LGraphNode } from '@/lib/litegraph/src/litegraph' import { LiteGraph } from '@/lib/litegraph/src/litegraph' import { inputLinkId, outputLinks } from '@/lib/litegraph/src/node/slotLinks' @@ -163,7 +164,14 @@ function transferWidgetValue( const oldWidgetIdx = oldWidgetIds.indexOf(oldInputName) if (oldWidgetIdx === -1) return - const oldValue = serialized.widgets_values[oldWidgetIdx] + const valueLayout = decodeWidgetValueLayout( + oldWidgetIds.map(() => ({ + controlConfig: { mode: 'fixed' as const, hasFilter: true } + })), + serialized.widgets_values + ) + const oldValue = + serialized.widgets_values[valueLayout[oldWidgetIdx]?.valueIndex] if (oldValue === undefined) return const newWidget = newNode.widgets?.find((w) => w.name === newInputName) diff --git a/src/renderer/extensions/vueNodes/composables/processedWidgetRenderModel.ts b/src/renderer/extensions/vueNodes/composables/processedWidgetRenderModel.ts index ddb45ce166d..379da13d625 100644 --- a/src/renderer/extensions/vueNodes/composables/processedWidgetRenderModel.ts +++ b/src/renderer/extensions/vueNodes/composables/processedWidgetRenderModel.ts @@ -1,4 +1,5 @@ import { showNodeOptions } from '@/composables/graph/useMoreOptionsMenu' +import { getWidgetControlView } from '@/core/graph/widgets/control/widgetControl' import { resolvePromotedWidgetSource } from '@/core/graph/subgraph/resolvePromotedWidgetSource' import type { INodeInputSlot } from '@comfyorg/graph-model/slots' import type { LGraph, LGraphNode } from '@/lib/litegraph/src/litegraph' @@ -32,7 +33,6 @@ import type { import type { NodeId } from '@comfyorg/graph-model/nodeId' import type { NodeState } from '@comfyorg/graph-model/nodeState' import type { LinkTopology } from '@comfyorg/graph-model/linkTopology' -import { getControlWidget } from '@/types/simplifiedWidget' import { isWidgetVisibleOnSurface } from '@/types/widgetVisibility' import type { WidgetVisibilityComponent } from '@/types/widgetVisibility' import type { @@ -302,9 +302,9 @@ function resolveLiveWidgetContext( } : undefined const controlWidget = - getControlWidget(liveWidget) ?? + getWidgetControlView(liveWidget) ?? (promotedSource?.sourceWidget - ? getControlWidget(promotedSource.sourceWidget) + ? getWidgetControlView(promotedSource.sourceWidget) : undefined) return { diff --git a/src/renderer/extensions/vueNodes/widgets/components/ValueControlButton.vue b/src/renderer/extensions/vueNodes/widgets/components/ValueControlButton.vue index f0c7d203198..cb1d5fe3c89 100644 --- a/src/renderer/extensions/vueNodes/widgets/components/ValueControlButton.vue +++ b/src/renderer/extensions/vueNodes/widgets/components/ValueControlButton.vue @@ -15,11 +15,13 @@ const iconMap: Record = { fixed: 'icon-[lucide--pencil-off]', randomize: 'icon-[lucide--shuffle]', increment: null, + 'increment-wrap': null, decrement: null } const textMap: Record = { increment: '+1', + 'increment-wrap': '+1↻', decrement: '-1', fixed: null, randomize: null @@ -30,7 +32,11 @@ const textMap: Record = {