|
| 1 | +import { useNodeImage } from '@/composables/node/useNodeImage' |
| 2 | +import { parseProxyWidgets } from '@/core/schemas/proxyWidget' |
| 3 | +import type { LGraph, LGraphNode } from '@/lib/litegraph/src/litegraph' |
| 4 | +import { SubgraphNode } from '@/lib/litegraph/src/subgraph/SubgraphNode' |
| 5 | +import type { IBaseWidget } from '@/lib/litegraph/src/types/widgets.ts' |
| 6 | +import { disconnectedWidget } from '@/lib/litegraph/src/widgets/DisconnectedWidget' |
| 7 | +import { useCanvasStore } from '@/renderer/core/canvas/canvasStore' |
| 8 | +import { DOMWidgetImpl } from '@/scripts/domWidget' |
| 9 | +import { useDomWidgetStore } from '@/stores/domWidgetStore' |
| 10 | +import { useNodeOutputStore } from '@/stores/imagePreviewStore' |
| 11 | +import { getNodeByExecutionId } from '@/utils/graphTraversalUtil' |
| 12 | + |
| 13 | +/** |
| 14 | + * @typedef {object} Overlay - Each proxy Widget has an associated overlay object |
| 15 | + * Accessing a property which exists in the overlay object will |
| 16 | + * instead result in the action being performed on the overlay object |
| 17 | + * 3 properties are added for locating the proxied widget |
| 18 | + * @property {LGraph} graph - The graph the widget resides in. Used for widget lookup |
| 19 | + * @property {string} nodeId - The NodeId the proxy Widget is located on |
| 20 | + * @property {string} widgetName - The name of the linked widget |
| 21 | + * |
| 22 | + * @property {boolean} isProxyWidget - Always true, used as type guard |
| 23 | + * @property {LGraphNode} node - not included on IBaseWidget, but required for overlay |
| 24 | + */ |
| 25 | +type Overlay = Partial<IBaseWidget> & { |
| 26 | + graph: LGraph |
| 27 | + nodeId: string |
| 28 | + widgetName: string |
| 29 | + isProxyWidget: boolean |
| 30 | + node?: LGraphNode |
| 31 | +} |
| 32 | +// A ProxyWidget can be treated like a normal widget. |
| 33 | +// the _overlay property can be used to directly access the Overlay object |
| 34 | +/** |
| 35 | + * @typedef {object} ProxyWidget - a reference to a widget that can |
| 36 | + * be displayed and owned by a separate node |
| 37 | + * @property {Overlay} _overlay - a special property to access the overlay of the widget |
| 38 | + * Any property that exists in the overlay will be accessed instead of the property |
| 39 | + * on the linked widget |
| 40 | + */ |
| 41 | +type ProxyWidget = IBaseWidget & { _overlay: Overlay } |
| 42 | +function isProxyWidget(w: IBaseWidget): w is ProxyWidget { |
| 43 | + return (w as { _overlay?: Overlay })?._overlay?.isProxyWidget ?? false |
| 44 | +} |
| 45 | + |
| 46 | +const originalOnConfigure = SubgraphNode.prototype.onConfigure |
| 47 | +SubgraphNode.prototype.onConfigure = function (serialisedNode) { |
| 48 | + if (!this.isSubgraphNode()) |
| 49 | + throw new Error("Can't add proxyWidgets to non-subgraphNode") |
| 50 | + |
| 51 | + const canvasStore = useCanvasStore() |
| 52 | + //Must give value to proxyWidgets prior to defining or it won't serialize |
| 53 | + this.properties.proxyWidgets ??= '[]' |
| 54 | + let proxyWidgets = this.properties.proxyWidgets |
| 55 | + |
| 56 | + originalOnConfigure?.call(this, serialisedNode) |
| 57 | + |
| 58 | + Object.defineProperty(this.properties, 'proxyWidgets', { |
| 59 | + get: () => { |
| 60 | + return proxyWidgets |
| 61 | + }, |
| 62 | + set: (property: string) => { |
| 63 | + const parsed = parseProxyWidgets(property) |
| 64 | + const { deactivateWidget, setWidget } = useDomWidgetStore() |
| 65 | + for (const w of this.widgets.filter((w) => isProxyWidget(w))) { |
| 66 | + if (w instanceof DOMWidgetImpl) deactivateWidget(w.id) |
| 67 | + } |
| 68 | + this.widgets = this.widgets.filter((w) => !isProxyWidget(w)) |
| 69 | + for (const [nodeId, widgetName] of parsed) { |
| 70 | + const w = addProxyWidget(this, `${nodeId}`, widgetName) |
| 71 | + if (w instanceof DOMWidgetImpl) setWidget(w) |
| 72 | + } |
| 73 | + proxyWidgets = property |
| 74 | + canvasStore.canvas?.setDirty(true, true) |
| 75 | + this._setConcreteSlots() |
| 76 | + this.arrange() |
| 77 | + } |
| 78 | + }) |
| 79 | + this.properties.proxyWidgets = proxyWidgets |
| 80 | +} |
| 81 | + |
| 82 | +function addProxyWidget( |
| 83 | + subgraphNode: SubgraphNode, |
| 84 | + nodeId: string, |
| 85 | + widgetName: string |
| 86 | +) { |
| 87 | + const name = `${nodeId}: ${widgetName}` |
| 88 | + const overlay = { |
| 89 | + nodeId, |
| 90 | + widgetName, |
| 91 | + graph: subgraphNode.subgraph, |
| 92 | + name, |
| 93 | + label: name, |
| 94 | + isProxyWidget: true, |
| 95 | + y: 0, |
| 96 | + last_y: undefined, |
| 97 | + width: undefined, |
| 98 | + computedHeight: undefined, |
| 99 | + afterQueued: undefined, |
| 100 | + onRemove: undefined, |
| 101 | + node: subgraphNode |
| 102 | + } |
| 103 | + return addProxyFromOverlay(subgraphNode, overlay) |
| 104 | +} |
| 105 | +function resolveLinkedWidget( |
| 106 | + overlay: Overlay |
| 107 | +): [LGraphNode | undefined, IBaseWidget | undefined] { |
| 108 | + const { graph, nodeId, widgetName } = overlay |
| 109 | + const n = getNodeByExecutionId(graph, nodeId) |
| 110 | + if (!n) return [undefined, undefined] |
| 111 | + return [n, n.widgets?.find((w: IBaseWidget) => w.name === widgetName)] |
| 112 | +} |
| 113 | +function addProxyFromOverlay(subgraphNode: SubgraphNode, overlay: Overlay) { |
| 114 | + let [linkedNode, linkedWidget] = resolveLinkedWidget(overlay) |
| 115 | + let backingWidget = linkedWidget ?? disconnectedWidget |
| 116 | + if (overlay.widgetName == '$$canvas-image-preview') |
| 117 | + overlay.node = new Proxy(subgraphNode, { |
| 118 | + get(_t, p) { |
| 119 | + if (p !== 'imgs') return Reflect.get(subgraphNode, p) |
| 120 | + if (!linkedNode) return [] |
| 121 | + const images = |
| 122 | + useNodeOutputStore().getNodeOutputs(linkedNode)?.images ?? [] |
| 123 | + if (images !== linkedNode.images) { |
| 124 | + linkedNode.images = images |
| 125 | + useNodeImage(linkedNode).showPreview() |
| 126 | + } |
| 127 | + return linkedNode.imgs |
| 128 | + } |
| 129 | + }) |
| 130 | + /** |
| 131 | + * A set of handlers which define widget interaction |
| 132 | + * Many arguments are shared between function calls |
| 133 | + * @param {IBaseWidget} _t - The "target" the call is originally made on. |
| 134 | + * This argument is never used, but must be defined for typechecking |
| 135 | + * @param {string} property - The name of the accessed value. |
| 136 | + * Checked for conditional logic, but never changed |
| 137 | + * @param {object} receiver - The object the result is set to |
| 138 | + * and the vlaue used as 'this' if property is a get/set method |
| 139 | + * @param {unknown} value - only used on set calls. The thing being assigned |
| 140 | + */ |
| 141 | + const handler = { |
| 142 | + get(_t: IBaseWidget, property: string, receiver: object) { |
| 143 | + let redirectedTarget: object = backingWidget |
| 144 | + let redirectedReceiver = receiver |
| 145 | + if (property == '_overlay') return overlay |
| 146 | + else if (property == 'value') redirectedReceiver = backingWidget |
| 147 | + if (Object.prototype.hasOwnProperty.call(overlay, property)) { |
| 148 | + redirectedTarget = overlay |
| 149 | + redirectedReceiver = overlay |
| 150 | + } |
| 151 | + return Reflect.get(redirectedTarget, property, redirectedReceiver) |
| 152 | + }, |
| 153 | + set(_t: IBaseWidget, property: string, value: unknown, receiver: object) { |
| 154 | + let redirectedTarget: object = backingWidget |
| 155 | + let redirectedReceiver = receiver |
| 156 | + if (property == 'value') redirectedReceiver = backingWidget |
| 157 | + else if (property == 'computedHeight') { |
| 158 | + //update linkage regularly, but no more than once per frame |
| 159 | + ;[linkedNode, linkedWidget] = resolveLinkedWidget(overlay) |
| 160 | + backingWidget = linkedWidget ?? disconnectedWidget |
| 161 | + } |
| 162 | + if (Object.prototype.hasOwnProperty.call(overlay, property)) { |
| 163 | + redirectedTarget = overlay |
| 164 | + redirectedReceiver = overlay |
| 165 | + } |
| 166 | + return Reflect.set(redirectedTarget, property, value, redirectedReceiver) |
| 167 | + }, |
| 168 | + getPrototypeOf() { |
| 169 | + return Reflect.getPrototypeOf(backingWidget) |
| 170 | + }, |
| 171 | + ownKeys() { |
| 172 | + return Reflect.ownKeys(backingWidget) |
| 173 | + }, |
| 174 | + has(_t: IBaseWidget, property: string) { |
| 175 | + let redirectedTarget: object = backingWidget |
| 176 | + if (Object.prototype.hasOwnProperty.call(overlay, property)) { |
| 177 | + redirectedTarget = overlay |
| 178 | + } |
| 179 | + return Reflect.has(redirectedTarget, property) |
| 180 | + } |
| 181 | + } |
| 182 | + const w = new Proxy(disconnectedWidget, handler) |
| 183 | + subgraphNode.widgets.push(w) |
| 184 | + return w |
| 185 | +} |
0 commit comments