Line data Source code
1 : import type { LGraphNode } from '@/lib/litegraph/src/LGraphNode'
2 : import { NodeSlotType } from '@comfyorg/graph-model/globalEnums'
3 : import type { IBaseWidget } from '@comfyorg/graph-model/widgets'
4 : import { useCanvasStore } from '@/renderer/core/canvas/canvasStore'
5 : import type { InputSpec } from '@comfyorg/object-info-parser/schemas/nodeDefSchemaV2'
6 : import { useDialogService } from '@/services/dialogService'
7 : import { useWidgetValueStore } from '@/stores/widgetValueStore'
8 :
9 : export type WidgetValue = boolean | number | string | object | undefined
10 :
11 6 : export function getWidgetDefaultValue(
12 : spec: InputSpec | undefined
13 : ): WidgetValue {
14 6 : if (!spec) return undefined
15 :
16 5 : if (spec.default !== undefined) return spec.default as WidgetValue
17 :
18 6 : switch (spec.type) {
19 : case 'INT':
20 : case 'FLOAT':
21 0 : return 0
22 : case 'BOOLEAN':
23 0 : return false
24 : case 'STRING':
25 1 : return ''
26 : default:
27 0 : if (Array.isArray(spec.options) && spec.options.length > 0) {
28 0 : return spec.options[0] as WidgetValue
29 : }
30 0 : return undefined
31 : }
32 : }
33 :
34 : /**
35 : * Renames a widget: a label-only write across the widget, its store state, and
36 : * its backing input slot (matched by widgetId). Never touches the widget/input
37 : * name or widgetId — those are the stable identity a promoted input derives from.
38 : *
39 : * @param widget The widget to rename
40 : * @param node The node containing the widget
41 : * @param newLabel The new label for the widget (empty string or undefined to clear)
42 : * @returns true if the rename was successful, false otherwise
43 : */
44 4 : export function renameWidget(
45 : widget: IBaseWidget,
46 : node: LGraphNode,
47 : newLabel: string
48 : ): boolean {
49 4 : const label = newLabel || undefined
50 : // TODO(ecs-widget-label-identity): the `input.label` write below is the interim
51 : // on-ramp. End-state is an ECS WidgetIdentity.label serialized field (name =
52 : // identity, label = display per ADR-SUBGRAPH-PROMOTION-0009) written via WidgetHandle.setLabel — the
53 : // widget entity owns its label, so the socketless/DOM-widget case (no input slot
54 : // to carry input.label, e.g. CLIPTextEncode `text`) is fixed by construction.
55 4 : const input =
56 : (widget.widgetId &&
57 : node.inputs.find((inp) => inp.widgetId === widget.widgetId)) ||
58 : node.inputs.find((inp) => inp.widget?.name === widget.name)
59 4 : const widgetState = widget.widgetId
60 : ? useWidgetValueStore().getWidget(widget.widgetId)
61 : : undefined
62 :
63 4 : widget.label = label
64 4 : if (widgetState) widgetState.label = label
65 4 : if (input) input.label = label
66 :
67 : // Fires for all node types; listeners guard against non-subgraph nodes.
68 4 : node.graph?.trigger('node:slot-label:changed', {
69 : nodeId: node.id,
70 : slotType: NodeSlotType.INPUT
71 : })
72 :
73 4 : return true
74 : }
75 :
76 2 : export async function promptWidgetLabel(
77 : widget: IBaseWidget,
78 : t: (key: string) => string
79 : ): Promise<string | null> {
80 2 : return useDialogService().prompt({
81 : title: t('g.rename'),
82 : message: t('g.enterNewNamePrompt'),
83 : defaultValue: widget.label,
84 : placeholder: widget.name
85 : })
86 : }
87 :
88 2 : export async function promptRenameWidget(
89 : widget: IBaseWidget,
90 : node: LGraphNode,
91 : t: (key: string) => string
92 : ): Promise<string | null> {
93 2 : const rawLabel = await promptWidgetLabel(widget, t)
94 2 : if (rawLabel === null) return null
95 :
96 2 : const normalizedLabel = rawLabel.trim()
97 2 : if (!normalizedLabel) return null
98 :
99 2 : if (!renameWidget(widget, node, normalizedLabel)) return null
100 :
101 2 : widget.callback?.(widget.value)
102 2 : useCanvasStore().canvas?.setDirty(true)
103 2 : return normalizedLabel
104 : }
|