Line data Source code
1 : import cloneDeep from 'es-toolkit/compat/cloneDeep'
2 : import { addBreadcrumb } from '@sentry/vue'
3 : import type { PromotedWidgetSource } from '@/core/graph/subgraph/promotedWidgetTypes'
4 : import { t } from '@/i18n'
5 : import type { IContextMenuValue } from '@/lib/litegraph/src/litegraph'
6 : import { LGraphNode } from '@/lib/litegraph/src/litegraph'
7 : import type { SubgraphNode } from '@/lib/litegraph/src/subgraph/SubgraphNode'
8 : import type { LinkId } from '@comfyorg/graph-model/linkId'
9 : import { reorderSubgraphInputs } from '@/lib/litegraph/src/subgraph/subgraphUtils'
10 : import type { IBaseWidget } from '@comfyorg/graph-model/widgets'
11 : import { nextUniqueName } from '@/lib/litegraph/src/strings'
12 : import { useToast } from '@/components/ui/toast/toastStore'
13 : import {
14 : CANVAS_IMAGE_PREVIEW_WIDGET,
15 : supportsVirtualCanvasImagePreview
16 : } from '@/composables/node/canvasImagePreviewTypes'
17 : import {
18 : providePromoteRecommendedWidgets,
19 : useCanvasStore
20 : } from '@/renderer/core/canvas/canvasStore'
21 : import { useLitegraphServiceAccess } from '@/core/graph/litegraphServiceAccess'
22 : import {
23 : getPreviewExposureHostLocator,
24 : usePreviewExposureStore
25 : } from '@/stores/previewExposureStore'
26 : import { useSubgraphNavigationStore } from '@/stores/subgraphNavigationStore'
27 : import { UNASSIGNED_NODE_ID, toNodeId } from '@comfyorg/graph-model/nodeId'
28 : import type { SerializedNodeId } from '@comfyorg/graph-model/nodeId'
29 : import type { WidgetId } from '@comfyorg/graph-model/widgetId'
30 : import { widgetId } from '@comfyorg/graph-model/widgetId'
31 : import { useWidgetValueStore } from '@/stores/widgetValueStore'
32 :
33 : type PartialNode = Pick<LGraphNode, 'title' | 'id' | 'type'>
34 : type RuntimeWidget = Omit<IBaseWidget, 'options'> &
35 : Partial<Pick<IBaseWidget, 'options'>>
36 :
37 : export type WidgetItem = [LGraphNode, IBaseWidget]
38 : export { CANVAS_IMAGE_PREVIEW_WIDGET }
39 :
40 530 : function isLinkedPromotion(
41 : subgraphNode: SubgraphNode,
42 : sourceNodeId: SerializedNodeId,
43 : sourceWidgetName: string
44 : ): boolean {
45 530 : return (
46 : findHostInputForPromotion(subgraphNode, sourceNodeId, sourceWidgetName) !==
47 : undefined
48 : )
49 : }
50 :
51 809 : export function findHostInputForPromotion(
52 : subgraphNode: SubgraphNode,
53 : rawSourceNodeId: SerializedNodeId,
54 : sourceWidgetName: string
55 : ) {
56 809 : const sourceNodeId = toNodeId(rawSourceNodeId)
57 809 : return subgraphNode.inputs.find((input) => {
58 2250 : const source = input._subgraphSlot
59 : ? resolvePromotionSource(subgraphNode, input._subgraphSlot)
60 : : undefined
61 2250 : return (
62 : source?.sourceNodeId === sourceNodeId &&
63 : source.sourceWidgetName === sourceWidgetName
64 : )
65 : })
66 : }
67 :
68 : /**
69 : * Host-first resolver for promoted widget value keys.
70 : *
71 : * Anchors on the host `SubgraphNode.inputs` (which own the promoted `widgetId`
72 : * per ADR-SUBGRAPH-PROMOTION-0009) and walks host -> interior only to key each host widget by the
73 : * interior source it projects. Consumers that discover an interior source can
74 : * then look up the host value key without a per-source reverse walk.
75 : */
76 4 : export function createPromotedHostWidgetIdLookup(
77 : subgraphNode: SubgraphNode
78 : ): (
79 : sourceNodeId: SerializedNodeId,
80 : sourceWidgetName: string
81 : ) => WidgetId | undefined {
82 4 : const key = (nodeId: SerializedNodeId, name: string): string =>
83 : `${toNodeId(nodeId)}\u0000${name}`
84 :
85 4 : const hostWidgetIdBySource = new Map<string, WidgetId>()
86 4 : for (const input of subgraphNode.inputs) {
87 1 : const hostWidgetId = input.widgetId
88 1 : const subgraphSlot = input._subgraphSlot
89 1 : if (!hostWidgetId || !subgraphSlot) continue
90 :
91 1 : const source = resolvePromotionSource(subgraphNode, subgraphSlot)
92 1 : if (!source) continue
93 :
94 1 : hostWidgetIdBySource.set(
95 : key(source.sourceNodeId, source.sourceWidgetName),
96 : hostWidgetId
97 : )
98 : }
99 :
100 4 : return (sourceNodeId, sourceWidgetName) =>
101 : hostWidgetIdBySource.get(key(sourceNodeId, sourceWidgetName))
102 : }
103 :
104 2288 : function resolvePromotionSource(
105 : subgraphNode: SubgraphNode,
106 : subgraphInput: { linkIds: readonly LinkId[] }
107 : ): PromotedWidgetSource | undefined {
108 2288 : for (const linkId of subgraphInput.linkIds) {
109 2291 : const link = subgraphNode.subgraph.getLink(linkId)
110 2291 : if (!link) continue
111 :
112 2291 : const { inputNode, input: targetInput } = link.resolve(
113 : subgraphNode.subgraph
114 : )
115 2291 : if (!inputNode || !targetInput) continue
116 :
117 2291 : if (inputNode.isSubgraphNode()) {
118 124 : return {
119 : sourceNodeId: inputNode.id,
120 : sourceWidgetName: targetInput.name
121 : }
122 : }
123 :
124 2166 : const targetWidget = inputNode.getWidgetFromSlot(targetInput)
125 2284 : if (!targetWidget) continue
126 :
127 1082 : return {
128 : sourceNodeId: inputNode.id,
129 : sourceWidgetName: targetWidget.name
130 : }
131 : }
132 : }
133 :
134 : export function reorderSubgraphInputsByName(
135 : subgraphNode: SubgraphNode,
136 : orderedInputNames: readonly string[]
137 : ): void {
138 : const order = new Map(
139 : orderedInputNames.map((name, index) => [name, index] as const)
140 : )
141 : const byOrder = <T extends { name: string }>(left: T, right: T) => {
142 : const leftOrder = order.get(left.name) ?? Number.MAX_SAFE_INTEGER
143 : const rightOrder = order.get(right.name) ?? Number.MAX_SAFE_INTEGER
144 : return leftOrder - rightOrder
145 : }
146 :
147 : const orderedIndices = subgraphNode.subgraph.inputs
148 : .map((input, index) => ({ input, index }))
149 : .sort((left, right) => byOrder(left.input, right.input))
150 : .map(({ index }) => index)
151 : applySubgraphInputOrder(subgraphNode, orderedIndices)
152 : }
153 :
154 2 : export function reorderSubgraphInputsByWidgetOrder(
155 : subgraphNode: SubgraphNode,
156 : orderedWidgets: readonly Pick<IBaseWidget, 'widgetId'>[]
157 : ): void {
158 2 : const remainingIndices = new Set(subgraphNode.inputs.keys())
159 2 : const orderedIndices = orderedWidgets.flatMap((orderedWidget) => {
160 5 : for (const index of remainingIndices) {
161 14 : if (isSamePromotedInput(subgraphNode, index, orderedWidget)) {
162 5 : remainingIndices.delete(index)
163 5 : return [index]
164 : }
165 : }
166 0 : return []
167 : })
168 :
169 5 : for (const index of remainingIndices) orderedIndices.push(index)
170 :
171 2 : applySubgraphInputOrder(subgraphNode, orderedIndices)
172 : }
173 :
174 2 : function applySubgraphInputOrder(
175 : subgraphNode: SubgraphNode,
176 : orderedIndices: readonly number[]
177 : ): void {
178 2 : const widgetValues = subgraphNode.inputs.map((input) => {
179 10 : const id = input.widgetId
180 10 : if (!id) return undefined
181 10 : return useWidgetValueStore().getWidget(id)?.value
182 : })
183 :
184 2 : reorderSubgraphInputs(subgraphNode, orderedIndices)
185 2 : useWidgetValueStore().setNodeWidgetOrder(
186 : subgraphNode.rootGraph.id,
187 : subgraphNode.id,
188 : subgraphNode.inputs.flatMap((input) =>
189 : input.widgetId ? [input.widgetId] : []
190 : )
191 : )
192 :
193 2 : for (const [newIndex, oldIndex] of orderedIndices.entries()) {
194 10 : const value = widgetValues[oldIndex]
195 10 : const id = subgraphNode.inputs[newIndex]?.widgetId
196 10 : if (value === undefined || !id) continue
197 5 : useWidgetValueStore().setValue(id, value)
198 : }
199 : }
200 :
201 14 : function isSamePromotedInput(
202 : subgraphNode: SubgraphNode,
203 : inputIndex: number,
204 : orderedWidget: Pick<IBaseWidget, 'widgetId'>
205 : ): boolean {
206 14 : const input = subgraphNode.inputs.at(inputIndex)
207 14 : const linkedInput = input?._subgraphSlot
208 14 : if (!input || !linkedInput) return false
209 :
210 14 : for (const linkId of linkedInput.linkIds) {
211 14 : const link = subgraphNode.subgraph.getLink(linkId)
212 14 : if (!link) continue
213 :
214 14 : const { inputNode, input: targetInput } = link.resolve(
215 : subgraphNode.subgraph
216 : )
217 14 : if (!inputNode || !targetInput) continue
218 :
219 : const targetWidget = inputNode.getWidgetFromSlot(targetInput)
220 14 : if (targetWidget === orderedWidget) return true
221 :
222 14 : if (input.widgetId && input.widgetId === orderedWidget.widgetId) return true
223 : }
224 :
225 9 : return false
226 : }
227 :
228 373 : function isPreviewExposed(
229 : subgraphNode: SubgraphNode,
230 : source: PromotedWidgetSource
231 : ): boolean {
232 373 : const hostLocator = getPreviewExposureHostLocator(subgraphNode)
233 373 : if (!hostLocator) return false
234 373 : return usePreviewExposureStore()
235 : .getExposures(subgraphNode.rootGraph.id, hostLocator)
236 : .some(
237 : (exposure) =>
238 : exposure.sourceNodeId === source.sourceNodeId &&
239 : exposure.sourcePreviewName === source.sourceWidgetName
240 : )
241 : }
242 :
243 434 : export function isWidgetPromotedOnSubgraphNode(
244 : subgraphNode: SubgraphNode,
245 : source: PromotedWidgetSource,
246 : widget?: IBaseWidget
247 : ): boolean {
248 434 : if (widget && isPreviewPseudoWidget(widget))
249 14 : return isPreviewExposed(subgraphNode, source)
250 434 : return (
251 : isLinkedPromotion(
252 : subgraphNode,
253 : source.sourceNodeId,
254 : source.sourceWidgetName
255 : ) || isPreviewExposed(subgraphNode, source)
256 : )
257 : }
258 :
259 154 : function toPromotionSource(
260 : node: PartialNode,
261 : widget: IBaseWidget
262 : ): PromotedWidgetSource {
263 154 : return {
264 : sourceNodeId: node.id,
265 : sourceWidgetName: widget.name
266 : }
267 : }
268 :
269 54 : export function refreshPromotedWidgetRendering(parents: SubgraphNode[]): void {
270 54 : for (const parent of parents) {
271 54 : parent.expandToFitContent()
272 54 : parent.setDirtyCanvas(true, true)
273 : }
274 54 : useCanvasStore().canvas?.setDirty(true, true)
275 : }
276 :
277 : type CanonicalPromotionResult =
278 : | { ok: true }
279 : | { ok: false; reason: 'missingSourceSlot' | 'connectFailed' }
280 :
281 110 : export function promoteValueWidgetViaSubgraphInput(
282 : subgraphNode: SubgraphNode,
283 : sourceNode: LGraphNode,
284 : sourceWidget: IBaseWidget
285 : ): CanonicalPromotionResult {
286 110 : const sourceWidgetName = sourceWidget.name
287 110 : if (isLinkedPromotion(subgraphNode, sourceNode.id, sourceWidgetName)) {
288 0 : return { ok: true }
289 : }
290 :
291 110 : const sourceSlot = sourceNode.getSlotFromWidget(sourceWidget)
292 110 : if (!sourceSlot) return { ok: false, reason: 'missingSourceSlot' }
293 :
294 110 : const existingNames = subgraphNode.subgraph.inputs.map((input) => input.name)
295 110 : const inputName = nextUniqueName(sourceWidgetName, existingNames)
296 110 : const subgraphInput = subgraphNode.subgraph.addInput(
297 : inputName,
298 : // oxlint-disable-next-line typescript/no-unnecessary-condition -- legacy extension slots may omit type at runtime
299 : String(sourceSlot.type ?? '*')
300 : )
301 110 : subgraphInput.label = sourceSlot.label
302 : const link = subgraphInput.connect(sourceSlot, sourceNode)
303 110 : if (!link) {
304 0 : subgraphNode.subgraph.removeInput(subgraphInput)
305 0 : return { ok: false, reason: 'connectFailed' }
306 : }
307 :
308 110 : const hostInput = subgraphNode.inputs.find(
309 : (input) => input._subgraphSlot === subgraphInput
310 : )
311 110 : if (hostInput) {
312 110 : hostInput.label = sourceSlot.label
313 110 : if (hostInput.widgetId && sourceSlot.label)
314 9 : useWidgetValueStore().setLabel(hostInput.widgetId, sourceSlot.label)
315 : }
316 :
317 110 : seedNestedPromotedInputState(subgraphNode, subgraphInput.name, sourceSlot)
318 :
319 110 : return { ok: true }
320 : }
321 :
322 110 : export function seedNestedPromotedInputState(
323 : subgraphNode: SubgraphNode,
324 : inputName: string,
325 : sourceSlot: { widgetId?: WidgetId; label?: string }
326 : ): void {
327 110 : if (!sourceSlot.widgetId) return
328 0 : if (subgraphNode.id === UNASSIGNED_NODE_ID) return
329 :
330 0 : const hostInput = subgraphNode.inputs.find(
331 : (input) => input._subgraphSlot?.name === inputName
332 : )
333 110 : if (!hostInput || hostInput.widgetId) return
334 :
335 0 : const store = useWidgetValueStore()
336 0 : const sourceState = store.getWidget(sourceSlot.widgetId)
337 0 : const sourceVisibility = store.getWidgetVisibility(sourceSlot.widgetId)
338 110 : if (!sourceState || !sourceVisibility) return
339 0 : const visibility = cloneDeep(sourceVisibility)
340 0 : visibility.suppression.byConnection = false
341 :
342 0 : const id = widgetId(subgraphNode.rootGraph.id, subgraphNode.id, inputName)
343 0 : hostInput.widget ??= { name: inputName }
344 0 : hostInput.widget.name = inputName
345 : const registered = store.registerWidget(
346 : id,
347 : {
348 : type: sourceState.type,
349 : value: sourceState.value,
350 : options: cloneDeep(sourceState.options),
351 : label: hostInput.label ?? sourceSlot.label ?? inputName,
352 : serialize: sourceState.serialize,
353 : disabled: sourceState.disabled
354 : },
355 : store.getWidgetRenderState(sourceSlot.widgetId) ?? {},
356 : visibility
357 : )
358 110 : if (!registered) {
359 0 : delete hostInput.widget
360 0 : delete hostInput.widgetId
361 0 : hostInput._widget = undefined
362 0 : return
363 : }
364 :
365 0 : hostInput.widgetId = id
366 : }
367 :
368 182 : function promotePreviewViaExposure(
369 : subgraphNode: SubgraphNode,
370 : sourceNode: LGraphNode,
371 : sourcePreviewName: string
372 : ): void {
373 182 : const store = usePreviewExposureStore()
374 182 : const rootGraphId = subgraphNode.rootGraph.id
375 182 : const hostLocator = getPreviewExposureHostLocator(subgraphNode)
376 182 : if (!hostLocator) return
377 : const existing = store
378 : .getExposures(rootGraphId, hostLocator)
379 : .some(
380 : (exposure) =>
381 : exposure.sourceNodeId === String(sourceNode.id) &&
382 : exposure.sourcePreviewName === sourcePreviewName
383 : )
384 182 : if (existing) return
385 :
386 172 : store.addExposure(rootGraphId, hostLocator, {
387 : sourceNodeId: sourceNode.id,
388 : sourcePreviewName
389 : })
390 : }
391 :
392 2356 : const PREVIEW_WIDGET_TYPES = new Set(['preview', 'video', 'audioUI'])
393 :
394 4770 : export function isPreviewPseudoWidget(widget: RuntimeWidget): boolean {
395 4770 : if (widget.name.startsWith('$$')) return true
396 4770 : if (widget.serialize !== false && widget.options?.serialize !== false)
397 4329 : return false
398 4770 : if (typeof widget.type === 'string' && PREVIEW_WIDGET_TYPES.has(widget.type))
399 0 : return true
400 414 : return false
401 : }
402 :
403 35 : export function promoteWidget(
404 : node: PartialNode,
405 : widget: IBaseWidget,
406 : parents: SubgraphNode[]
407 : ) {
408 35 : const source = toPromotionSource(node, widget)
409 35 : if (!(node instanceof LGraphNode)) return
410 35 : for (const parent of parents) {
411 35 : if (isPreviewPseudoWidget(widget)) {
412 2 : promotePreviewViaExposure(parent, node, source.sourceWidgetName)
413 2 : continue
414 : }
415 33 : const result = promoteValueWidgetViaSubgraphInput(parent, node, widget)
416 33 : if (!result.ok) {
417 0 : addBreadcrumb({
418 : category: 'subgraph',
419 : level: 'warning',
420 : message: `Failed to promote widget "${source.sourceWidgetName}" on node ${node.id}: ${result.reason}`
421 : })
422 : }
423 : }
424 35 : refreshPromotedWidgetRendering(parents)
425 35 : addBreadcrumb({
426 : category: 'subgraph',
427 : message: `Promoted widget "${source.sourceWidgetName}" on node ${node.id}`,
428 : level: 'info'
429 : })
430 : }
431 :
432 : /**
433 : * Removes the host input projecting a linked promotion identified by source.
434 : * Returns true when an input was found and demoted.
435 : */
436 2 : function demotePromotedInput(
437 : subgraphNode: SubgraphNode,
438 : source: PromotedWidgetSource
439 : ): boolean {
440 2 : const hostInput = findHostInputForPromotion(
441 : subgraphNode,
442 : source.sourceNodeId,
443 : source.sourceWidgetName
444 : )
445 2 : const linkedInput = hostInput?._subgraphSlot
446 2 : if (!linkedInput) return false
447 1 : const hostWidgetId = hostInput.widgetId
448 :
449 1 : if (subgraphNode.isInputConnected(subgraphNode.inputs.indexOf(hostInput))) {
450 0 : linkedInput.disconnect()
451 : } else {
452 1 : subgraphNode.subgraph.removeInput(linkedInput)
453 : }
454 1 : if (hostWidgetId) useWidgetValueStore().deleteWidget(hostWidgetId)
455 1 : return true
456 : }
457 :
458 2 : export function demoteWidget(
459 : node: PartialNode,
460 : widget: IBaseWidget,
461 : parents: SubgraphNode[]
462 : ) {
463 2 : const source = toPromotionSource(node, widget)
464 2 : for (const parent of parents) {
465 2 : if (demotePromotedInput(parent, source)) continue
466 :
467 1 : if (isPreviewPseudoWidget(widget)) {
468 1 : const previewStore = usePreviewExposureStore()
469 1 : const hostLocator = getPreviewExposureHostLocator(parent)
470 1 : if (!hostLocator) continue
471 1 : const exposure = previewStore
472 : .getExposures(parent.rootGraph.id, hostLocator)
473 : .find(
474 : (entry) =>
475 : entry.sourceNodeId === source.sourceNodeId &&
476 : entry.sourcePreviewName === source.sourceWidgetName
477 : )
478 1 : if (exposure) {
479 1 : previewStore.removeExposure(
480 : parent.rootGraph.id,
481 : hostLocator,
482 : exposure.name
483 : )
484 1 : continue
485 : }
486 : }
487 : }
488 2 : refreshPromotedWidgetRendering(parents)
489 2 : addBreadcrumb({
490 : category: 'subgraph',
491 : message: `Demoted widget "${source.sourceWidgetName}" on node ${node.id}`,
492 : level: 'info'
493 : })
494 : }
495 :
496 117 : function getParentNodes(): SubgraphNode[] {
497 117 : const { navigationStack } = useSubgraphNavigationStore()
498 117 : const subgraph = navigationStack.at(-1)
499 117 : if (!subgraph) {
500 0 : useToast().error(t('g.error'), {
501 : description: t('subgraphStore.promoteOutsideSubgraph')
502 : })
503 0 : return []
504 : }
505 : const parentGraph = navigationStack.at(-2) ?? subgraph.rootGraph
506 117 : return parentGraph.nodes.filter(
507 : (node): node is SubgraphNode =>
508 : node.type === subgraph.id && node.isSubgraphNode()
509 : )
510 : }
511 :
512 117 : export function addWidgetPromotionOptions(
513 : options: (IContextMenuValue | null)[],
514 : widget: IBaseWidget,
515 : node: LGraphNode
516 : ) {
517 117 : const parents = getParentNodes()
518 117 : const source = toPromotionSource(node, widget)
519 117 : const promotableParents = parents.filter(
520 : (parent) => !isWidgetPromotedOnSubgraphNode(parent, source, widget)
521 : )
522 117 : if (promotableParents.length > 0)
523 61 : options.unshift({
524 : content: t('subgraphStore.promoteWidget', {
525 : name: widget.label ?? widget.name
526 : }),
527 : callback: () => {
528 29 : promoteWidget(node, widget, promotableParents)
529 29 : widget.callback?.(widget.value)
530 : }
531 : })
532 : else {
533 56 : options.unshift({
534 : content: t('subgraphStore.unpromoteWidget', {
535 : name: widget.label ?? widget.name
536 : }),
537 : callback: () => {
538 1 : demoteWidget(node, widget, parents)
539 1 : widget.callback?.(widget.value)
540 : }
541 : })
542 : }
543 : }
544 :
545 0 : export function tryToggleWidgetPromotion() {
546 0 : const canvas = useCanvasStore().getCanvas()
547 0 : const [x, y] = canvas.graph_mouse
548 0 : const node = canvas.graph?.getNodeOnPos(x, y, canvas.visible_nodes)
549 0 : if (!node) return
550 0 : const widget = node.getWidgetOnPos(x, y, true)
551 0 : const parents = getParentNodes()
552 0 : if (!parents.length || !widget) return
553 0 : const source = toPromotionSource(node, widget)
554 0 : const promotableParents = parents.filter(
555 : (parent) => !isWidgetPromotedOnSubgraphNode(parent, source, widget)
556 : )
557 0 : if (promotableParents.length > 0)
558 0 : promoteWidget(node, widget, promotableParents)
559 0 : else demoteWidget(node, widget, parents)
560 : }
561 :
562 2356 : const recommendedNodes = [
563 : 'CLIPTextEncode',
564 : 'LoadImage',
565 : 'SaveImage',
566 : 'PreviewImage'
567 : ]
568 2356 : const recommendedWidgetNames = ['seed']
569 542 : export function isRecommendedWidget([node, widget]: WidgetItem) {
570 542 : return (
571 : !widget.computedDisabled &&
572 : (recommendedNodes.includes(node.type) ||
573 : recommendedWidgetNames.includes(widget.name))
574 : )
575 : }
576 :
577 575 : function supportsVirtualPreviewWidget(node: LGraphNode): boolean {
578 575 : return supportsVirtualCanvasImagePreview(node)
579 : }
580 :
581 242 : function createVirtualCanvasImagePreviewWidget(): IBaseWidget {
582 242 : return {
583 : name: CANVAS_IMAGE_PREVIEW_WIDGET,
584 : type: 'IMAGE_PREVIEW',
585 : options: { serialize: false },
586 : serialize: false,
587 : y: 0,
588 : computedDisabled: false
589 : }
590 : }
591 :
592 575 : export function getPromotableWidgets(node: LGraphNode): IBaseWidget[] {
593 575 : const widgets = [...(node.widgets ?? [])]
594 :
595 575 : const hasCanvasPreviewWidget = widgets.some(
596 : (widget) => widget.name === CANVAS_IMAGE_PREVIEW_WIDGET
597 : )
598 575 : const supportsVirtualPreview = supportsVirtualPreviewWidget(node)
599 575 : if (!hasCanvasPreviewWidget && supportsVirtualPreview) {
600 242 : widgets.push(createVirtualCanvasImagePreviewWidget())
601 : }
602 :
603 575 : return widgets
604 : }
605 :
606 118 : function nodeWidgets(n: LGraphNode): WidgetItem[] {
607 118 : return getPromotableWidgets(n).map((w: IBaseWidget) => [n, w])
608 : }
609 :
610 512 : export function autoExposeKnownPreviewNodes(subgraphNode: SubgraphNode): void {
611 512 : if (subgraphNode.properties.previewExposures !== undefined) return
612 409 : const { updatePreviews } = useLitegraphServiceAccess()
613 409 : const interiorNodes = subgraphNode.subgraph.nodes
614 502 : for (const node of interiorNodes) {
615 823 : node.updateComputedDisabled()
616 :
617 823 : const hasPreviewWidget = () =>
618 : node.widgets?.some(isPreviewPseudoWidget) ?? false
619 :
620 833 : function promotePreviewWidget() {
621 833 : const widget = node.widgets?.find(isPreviewPseudoWidget)
622 833 : if (!widget) return
623 0 : promotePreviewViaExposure(subgraphNode, node, widget.name)
624 : }
625 823 : promotePreviewWidget()
626 :
627 823 : if (hasPreviewWidget()) continue
628 :
629 823 : if (supportsVirtualCanvasImagePreview(node)) {
630 180 : promotePreviewViaExposure(subgraphNode, node, CANVAS_IMAGE_PREVIEW_WIDGET)
631 180 : continue
632 : }
633 :
634 643 : requestAnimationFrame(() => updatePreviews(node, promotePreviewWidget))
635 : }
636 : }
637 :
638 63 : export function promoteRecommendedWidgets(subgraphNode: SubgraphNode) {
639 63 : autoExposeKnownPreviewNodes(subgraphNode)
640 : const interiorNodes = subgraphNode.subgraph.nodes
641 63 : const filteredWidgets: WidgetItem[] = interiorNodes
642 : .flatMap(nodeWidgets)
643 : .filter(isRecommendedWidget)
644 : .filter(([, widget]) => !isPreviewPseudoWidget(widget))
645 63 : for (const [n, w] of filteredWidgets) {
646 77 : const result = promoteValueWidgetViaSubgraphInput(subgraphNode, n, w)
647 77 : if (!result.ok) {
648 0 : addBreadcrumb({
649 : category: 'subgraph',
650 : level: 'warning',
651 : message: `Failed to promote widget "${w.name}" on node ${n.id}: ${result.reason}`
652 : })
653 : }
654 : }
655 63 : subgraphNode.expandToFitContent()
656 : }
657 :
658 16 : export function pruneDisconnected(subgraphNode: SubgraphNode) {
659 16 : const subgraph = subgraphNode.subgraph
660 16 : const removedEntries: PromotedWidgetSource[] = []
661 :
662 16 : const staleInputs = subgraph.inputs.filter((input) => {
663 : const source = resolvePromotionSource(subgraphNode, input)
664 37 : if (source) return false
665 :
666 20 : const hostInput = subgraphNode.inputs.find(
667 : (entry) => entry._subgraphSlot === input
668 : )
669 37 : if (!hostInput?.widgetId && !hostInput?._widget) return false
670 :
671 0 : removedEntries.push({
672 : sourceNodeId: subgraphNode.id,
673 : sourceWidgetName: input.name
674 : })
675 0 : return true
676 : })
677 :
678 16 : for (const input of staleInputs) {
679 0 : subgraph.removeInput(input)
680 : }
681 :
682 16 : if (removedEntries.length > 0 && import.meta.env.DEV) {
683 : console.warn(
684 : '[subgraphInputs] Pruned disconnected promoted widget inputs',
685 : removedEntries,
686 : {
687 : graphId: subgraphNode.rootGraph.id,
688 : subgraphNodeId: subgraphNode.id
689 : }
690 : )
691 : }
692 :
693 16 : refreshPromotedWidgetRendering([subgraphNode])
694 16 : addBreadcrumb({
695 : category: 'subgraph',
696 : message: `Pruned ${removedEntries.length} disconnected promoted widget input(s) from subgraph node ${subgraphNode.id}`,
697 : level: 'info'
698 : })
699 : }
700 :
701 309 : export function hasUnpromotedWidgets(subgraphNode: SubgraphNode): boolean {
702 309 : if (subgraphNode.isDetached) return false
703 303 : const { subgraph } = subgraphNode
704 :
705 303 : return subgraph.nodes.some((interiorNode) =>
706 : getPromotableWidgets(interiorNode).some(
707 : (widget) =>
708 : !widget.computedDisabled &&
709 : !isWidgetPromotedOnSubgraphNode(
710 : subgraphNode,
711 : {
712 : sourceNodeId: interiorNode.id,
713 : sourceWidgetName: widget.name
714 : },
715 : widget
716 : )
717 : )
718 : )
719 : }
720 :
721 2356 : providePromoteRecommendedWidgets(promoteRecommendedWidgets)
|