Line data Source code
1 : import type {
2 : WidgetSurface,
3 : WidgetSurfaceTier,
4 : WidgetSurfaces,
5 : WidgetVisibilityComponent
6 : } from '@comfyorg/graph-model/widgetVisibility'
7 : export type {
8 : WidgetSurface,
9 : WidgetSurfaces,
10 : WidgetVisibilityComponent
11 : } from '@comfyorg/graph-model/widgetVisibility'
12 : /**
13 : * Widget visibility as a data-oriented component (ADR-ECS-0008,
14 : * ADR-ECS-WIDGETS-0023).
15 : *
16 : * Static, registration-time policy lives in {@link WidgetSurfaces}: one tier
17 : * per rendering surface. Dynamic, runtime state lives in
18 : * {@link WidgetSuppression}: orthogonal boolean reasons a widget is currently
19 : * not rendered anywhere. Surfaces resolve visibility with
20 : * {@link isWidgetVisibleOnSurface}; legacy `hidden` / `hideInPanel` /
21 : * `advanced` / `canvasOnly` fields are facades over this component.
22 : */
23 :
24 : import { isPlainObject } from 'es-toolkit'
25 :
26 : import {
27 : WIDGET_SURFACES,
28 : WIDGET_SURFACE_TIERS
29 : } from '@comfyorg/graph-model/widgetSurfaces'
30 : export { WIDGET_SURFACES } from '@comfyorg/graph-model/widgetSurfaces'
31 :
32 96169 : function isWidgetSurfaces(value: unknown): value is WidgetSurfaces {
33 96169 : return (
34 : isPlainObject(value) &&
35 : WIDGET_SURFACES.every((surface) =>
36 : WIDGET_SURFACE_TIERS.some((tier) => value[surface] === tier)
37 : )
38 : )
39 : }
40 :
41 : export interface WidgetVisibilitySource {
42 : type: string
43 : hidden?: boolean
44 : advanced?: boolean
45 : options?: {
46 : hidden?: boolean
47 : advanced?: boolean
48 : surfaces?: WidgetSurfaces
49 : canvasOnly?: boolean
50 : hideInPanel?: boolean
51 : }
52 : }
53 :
54 152754 : export function isLegacyHiddenWidgetType(
55 : type: string | null | undefined
56 : ): boolean {
57 152754 : if (!type) return false
58 152751 : const normalized = type.toLowerCase()
59 152754 : return normalized.includes('hidden') || normalized.startsWith('tschide')
60 : }
61 :
62 53582 : export function isLegacyWidgetHidingType(
63 : type: string | null | undefined
64 : ): boolean {
65 53582 : if (!type) return false
66 53582 : return (
67 : isLegacyHiddenWidgetType(type) ||
68 : type === 'converted-widget' ||
69 : type.startsWith('converted-widget:')
70 : )
71 : }
72 :
73 : /**
74 : * Registration `advanced` metadata (`options.advanced`) gates only the Vue
75 : * node and panel surfaces: the legacy canvas has no advanced section, so it
76 : * renders spec-advanced widgets as plain rows. Only the runtime
77 : * `widget.advanced` property (an extension write) gates the canvas surface.
78 : */
79 96169 : export function deriveWidgetSurfaces(
80 : source: WidgetVisibilitySource
81 : ): WidgetSurfaces {
82 96169 : const canvas: WidgetSurfaceTier = source.advanced ? 'advanced' : 'shown'
83 96169 : const specTier: WidgetSurfaceTier =
84 : (source.options?.advanced ?? source.advanced) ? 'advanced' : 'shown'
85 96169 : const vueNode: WidgetSurfaceTier = source.options?.canvasOnly
86 : ? 'never'
87 : : specTier
88 96169 : const surfaces: WidgetSurfaces = {
89 : canvas,
90 : vueNode,
91 : panel: source.options?.hideInPanel ? 'never' : vueNode
92 : }
93 96169 : const declaredSurfaces = source.options?.surfaces
94 96169 : return isWidgetSurfaces(declaredSurfaces)
95 : ? {
96 : canvas: declaredSurfaces.canvas,
97 : vueNode: declaredSurfaces.vueNode,
98 : panel: declaredSurfaces.panel
99 : }
100 : : surfaces
101 : }
102 :
103 95882 : export function deriveWidgetVisibility(
104 : source: WidgetVisibilitySource
105 : ): WidgetVisibilityComponent {
106 95882 : const hidden =
107 : source.hidden ??
108 : source.options?.hidden ??
109 : isLegacyHiddenWidgetType(source.type)
110 95882 : return {
111 : surfaces: deriveWidgetSurfaces(source),
112 : suppression: { byExtension: hidden, byConnection: false }
113 : }
114 : }
115 :
116 1108220 : function isTierVisible(
117 : tier: WidgetSurfaceTier,
118 : view: { showAdvanced: boolean }
119 : ): boolean {
120 1108220 : switch (tier) {
121 : case 'shown':
122 1094687 : return true
123 : case 'advanced':
124 56 : return view.showAdvanced
125 : case 'never':
126 13477 : return false
127 : default: {
128 : const unreachable: never = tier
129 0 : return unreachable
130 : }
131 : }
132 : }
133 :
134 113146 : export function isWidgetVisibleOnSurface(
135 : visibility: WidgetVisibilityComponent,
136 : surface: WidgetSurface,
137 : view: { showAdvanced: boolean }
138 : ): boolean {
139 113146 : const { surfaces, suppression } = visibility
140 113146 : if (suppression.byExtension || suppression.byConnection) return false
141 111965 : return isTierVisible(surfaces[surface], view)
142 : }
143 :
144 : /**
145 : * Whether the widget keeps its legacy-canvas layout row. A
146 : * connection-suppressed widget renders no control but still occupies its row,
147 : * anchoring the connected input slot dot and the widget name.
148 : */
149 999868 : export function occupiesCanvasRow(
150 : visibility: WidgetVisibilityComponent,
151 : view: { showAdvanced: boolean }
152 : ): boolean {
153 999868 : if (visibility.suppression.byExtension) return false
154 999868 : return (
155 : visibility.suppression.byConnection ||
156 : isTierVisible(visibility.surfaces.canvas, view)
157 : )
158 : }
159 :
160 44766 : function isNeverShown(surfaces: WidgetSurfaces): boolean {
161 44766 : return (
162 : surfaces.canvas === 'never' &&
163 : surfaces.vueNode === 'never' &&
164 : surfaces.panel === 'never'
165 : )
166 : }
167 :
168 : /** Legacy `widget.hidden` read: suppressed, or statically hidden everywhere. */
169 45665 : export function isWidgetHidden(visibility: WidgetVisibilityComponent): boolean {
170 45665 : return (
171 : visibility.suppression.byExtension ||
172 : visibility.suppression.byConnection ||
173 : isNeverShown(visibility.surfaces)
174 : )
175 : }
176 :
177 : /** Legacy `options.hideInPanel` read. */
178 285 : export function isWidgetHiddenInPanel(
179 : visibility: WidgetVisibilityComponent
180 : ): boolean {
181 285 : return visibility.surfaces.panel === 'never'
182 : }
183 :
184 : /**
185 : * Legacy `options.hideInPanel` write. Un-hiding restores the panel to the
186 : * vueNode tier, which is the panel's baseline before `hideInPanel` applies.
187 : */
188 1 : export function setWidgetHiddenInPanel(
189 : visibility: WidgetVisibilityComponent,
190 : hidden: boolean
191 : ): void {
192 1 : visibility.surfaces.panel = hidden ? 'never' : visibility.surfaces.vueNode
193 : }
194 :
195 : /** Legacy `widget.advanced` read: any surface gated behind the advanced toggle. */
196 1541 : export function isWidgetAdvanced(
197 : visibility: WidgetVisibilityComponent
198 : ): boolean {
199 1541 : const { canvas, vueNode, panel } = visibility.surfaces
200 1541 : return canvas === 'advanced' || vueNode === 'advanced' || panel === 'advanced'
201 : }
202 :
203 : /**
204 : * Legacy `widget.advanced` write: toggles shown ⇄ advanced on the given
205 : * surfaces, never touches `never`.
206 : */
207 44126 : export function setWidgetAdvanced(
208 : visibility: WidgetVisibilityComponent,
209 : advanced: boolean,
210 : surfaces: readonly WidgetSurface[] = WIDGET_SURFACES
211 : ): void {
212 44126 : const from: WidgetSurfaceTier = advanced ? 'shown' : 'advanced'
213 44126 : const to: WidgetSurfaceTier = advanced ? 'advanced' : 'shown'
214 44126 : const { surfaces: widgetSurfaces } = visibility
215 44126 : for (const surface of surfaces) {
216 88252 : if (widgetSurfaces[surface] === from) widgetSurfaces[surface] = to
217 : }
218 : }
219 :
220 : /**
221 : * Legacy `widget.hidden` write. All legacy hiding — registration metadata,
222 : * legacy hidden type markers, and runtime extension writes — is extension
223 : * suppression, so un-hiding restores the widget's static surface tiers.
224 : */
225 300 : export function applyLegacyHiddenWrite(
226 : visibility: WidgetVisibilityComponent,
227 : hidden: boolean
228 : ): void {
229 300 : visibility.suppression.byExtension = hidden
230 : }
231 :
232 5 : export function applyLegacyCanvasOnlyWrite(
233 : visibility: WidgetVisibilityComponent,
234 : source: WidgetVisibilitySource
235 : ): void {
236 5 : const { vueNode, panel } = deriveWidgetSurfaces({
237 : ...source,
238 : advanced: visibility.surfaces.canvas === 'advanced'
239 : })
240 5 : visibility.surfaces.vueNode = vueNode
241 5 : visibility.surfaces.panel = panel
242 : }
243 :
244 : /**
245 : * Legacy runtime `widget.advanced` write. Runtime writes always gate the
246 : * canvas surface; they reach the Vue node and panel surfaces only when
247 : * registration metadata (`options.advanced`) is absent, matching the legacy
248 : * read precedence `options.advanced ?? widget.advanced`. Writing `undefined`
249 : * clears the runtime advanced flag.
250 : */
251 0 : export function applyLegacyAdvancedWrite(
252 : visibility: WidgetVisibilityComponent,
253 : advanced: boolean | undefined,
254 : hasRegistrationAdvanced: boolean
255 : ): void {
256 : const surfaces: readonly WidgetSurface[] = hasRegistrationAdvanced
257 : ? ['canvas']
258 : : WIDGET_SURFACES
259 0 : setWidgetAdvanced(visibility, advanced === true, surfaces)
260 : }
|