mirror of
https://github.com/Comfy-Org/ComfyUI_frontend.git
synced 2026-02-26 09:44:06 +00:00
## Summary Replace the Proxy-based proxy widget system with a store-driven architecture where `promotionStore` and `widgetValueStore` are the single sources of truth for subgraph widget promotion and widget values, and `SubgraphNode.widgets` is a synthetic getter composing lightweight `PromotedWidgetView` objects from store state. ## Motivation The subgraph widget promotion system previously scattered state across multiple unsynchronized layers: - **Persistence**: `node.properties.proxyWidgets` (tuples on the LiteGraph node) - **Runtime**: Proxy-based `proxyWidget.ts` with `Overlay` objects, `DisconnectedWidget` singleton, and `isProxyWidget` type guards - **UI**: Each Vue component independently calling `parseProxyWidgets()` via `customRef` hacks - **Mutation flags**: Imperative `widget.promoted = true/false` set on `subgraph-opened` events This led to 4+ independent parsings of the same data, complex cache invalidation, and no reactive contract between the promotion state and the rendering layer. Widget values were similarly owned by LiteGraph with no Vue-reactive backing. The core principle driving these changes: **Vue owns truth**. Pinia stores are the canonical source; LiteGraph objects delegate to stores via getters/setters; Vue components react to store state directly. ## Changes ### New stores (single sources of truth) - **`promotionStore`** — Reactive `Map<NodeId, PromotionEntry[]>` tracking which interior widgets are promoted on which SubgraphNode instances. Graph-scoped by root graph ID to prevent cross-workflow state collision. Replaces `properties.proxyWidgets` parsing, `customRef` hacks, `widget.promoted` mutation, and the `subgraph-opened` event listener. - **`widgetValueStore`** — Graph-scoped `Map<WidgetKey, WidgetState>` that is the canonical owner of widget values. `BaseWidget.value` delegates to this store via getter/setter when a node ID is assigned. Eliminates the need for Proxy-based value forwarding. ### Synthetic widgets getter (SubgraphNode) `SubgraphNode.widgets` is now a getter that reads `promotionStore.getPromotions(rootGraphId, nodeId)` and returns cached `PromotedWidgetView` objects. No stubs, no Proxies, no fake widgets persisted in the array. The setter is a no-op — mutations go through `promotionStore`. ### PromotedWidgetView A class behind a `createPromotedWidgetView` factory, implementing the `PromotedWidgetView` interface. Delegates value/type/options/drawing to the resolved interior widget and stores. Owns positional state (`y`, `computedHeight`) for canvas layout. Cached by `PromotedWidgetViewManager` for object-identity stability across frames. ### DOM widget promotion Promoted DOM widgets (textarea, image upload, etc.) render on the SubgraphNode surface via `positionOverride` in `domWidgetStore`. `DomWidgets.vue` checks for overrides and uses the SubgraphNode's coordinates instead of the interior node's. ### Promoted previews New `usePromotedPreviews` composable resolves image/audio/video preview widgets from promoted entries, enabling SubgraphNodes to display previews of interior preview nodes. ### Deleted - `proxyWidget.ts` (257 lines) — Proxy handler, `Overlay`, `newProxyWidget`, `isProxyWidget` - `DisconnectedWidget.ts` (39 lines) — Singleton Proxy target - `useValueTransform.ts` (32 lines) — Replaced by store delegation ### Key architectural changes - `BaseWidget.value` getter/setter delegates to `widgetValueStore` when node ID is set - `LGraph.add()` reordered: `node.graph` assigned before widget `setNodeId` (enables store registration) - `LGraph.clear()` cleans up graph-scoped stores to prevent stale entries across workflow switches - `promotionStore` and `widgetValueStore` state nested under root graph UUID for multi-workflow isolation - `SubgraphNode.serialize()` writes promotions back to `properties.proxyWidgets` for persistence compatibility - Legacy `-1` promotion entries resolved and migrated on first load with dev warning ## Test coverage - **3,700+ lines of new/updated tests** across 36 test files - **Unit**: `promotionStore.test.ts`, `widgetValueStore.test.ts`, `promotedWidgetView.test.ts` (921 lines), `subgraphNodePromotion.test.ts`, `proxyWidgetUtils.test.ts`, `DomWidgets.test.ts`, `PromotedWidgetViewManager.test.ts`, `usePromotedPreviews.test.ts`, `resolvePromotedWidget.test.ts`, `subgraphPseudoWidgetCache.test.ts` - **E2E**: `subgraphPromotion.spec.ts` (622 lines) — promote/demote, manual/auto promotion, paste preservation, seed control augmentation, image preview promotion; `imagePreview.spec.ts` extended with multi-promoted-preview coverage - **Fixtures**: 2 new subgraph workflow fixtures for preview promotion scenarios ## Review focus - Graph-scoped store keying (`rootGraphId`) — verify isolation across workflows/tabs and cleanup on `LGraph.clear()` - `PromotedWidgetView` positional stability — `_arrangeWidgets` writes to `y`/`computedHeight` on cached objects; getter returns fresh array but stable object references - DOM widget position override lifecycle — overrides set on promote, cleared on demote/removal/subgraph navigation - Legacy `-1` entry migration — resolved and written back on first load; unresolvable entries dropped with dev warning - Serialization round-trip — `promotionStore` state → `properties.proxyWidgets` on serialize, hydrated back on configure ## Diff breakdown (excluding lockfile) - 153 files changed, ~7,500 insertions, ~1,900 deletions (excluding pnpm-lock.yaml churn) - ~3,700 lines are tests - ~300 lines deleted (proxyWidget.ts, DisconnectedWidget.ts, useValueTransform.ts) <!-- Fixes #ISSUE_NUMBER --> ┆Issue is synchronized with this [Notion page](https://www.notion.so/PR-8856-feat-synthetic-widgets-getter-for-SubgraphNode-proxy-widget-v2-3076d73d365081c7b517f5ec7cb514f3) by [Unito](https://www.unito.io) --------- Co-authored-by: Amp <amp@ampcode.com> Co-authored-by: github-actions <github-actions@github.com> Co-authored-by: GitHub Action <action@github.com>
381 lines
10 KiB
TypeScript
381 lines
10 KiB
TypeScript
import { BaseWidget, LiteGraph } from '@/lib/litegraph/src/litegraph'
|
||
import type { CanvasPointer, LGraphNode } from '@/lib/litegraph/src/litegraph'
|
||
import type {
|
||
IBaseWidget,
|
||
IWidgetOptions
|
||
} from '@/lib/litegraph/src/types/widgets'
|
||
import { useSettingStore } from '@/platform/settings/settingStore'
|
||
import { useCanvasStore } from '@/renderer/core/canvas/canvasStore'
|
||
import type { InputSpec } from '@/schemas/nodeDef/nodeDefSchemaV2'
|
||
import { app } from '@/scripts/app'
|
||
import { calculateImageGrid } from '@/scripts/ui/imagePreview'
|
||
import type { ComfyWidgetConstructorV2 } from '@/scripts/widgets'
|
||
import { is_all_same_aspect_ratio } from '@/utils/imageUtil'
|
||
|
||
/**
|
||
* Workaround for Chrome GPU bug:
|
||
* When Chrome is maximized with GPU acceleration and high DPR, calling
|
||
* drawImage(canvas) + drawImage(img) in the same frame causes severe
|
||
* performance degradation (FPS drops to 2-10, memory spikes ~18GB).
|
||
*
|
||
* Solution: Defer image rendering using queueMicrotask to separate
|
||
* the two drawImage calls into different tasks.
|
||
*
|
||
* Note: As tested, requestAnimationFrame delays rendering to the next frame,
|
||
* causing visible image flickering. queueMicrotask executes within the same
|
||
* frame, avoiding flicker while still separating the drawImage calls.
|
||
*/
|
||
let deferredImageRenders: Array<() => void> = []
|
||
let deferredRenderScheduled = false
|
||
|
||
function scheduleDeferredImageRender() {
|
||
if (deferredRenderScheduled) return
|
||
deferredRenderScheduled = true
|
||
|
||
queueMicrotask(() => {
|
||
const renders = deferredImageRenders
|
||
deferredImageRenders = []
|
||
deferredRenderScheduled = false
|
||
|
||
for (const render of renders) {
|
||
render()
|
||
}
|
||
})
|
||
}
|
||
|
||
const renderPreview = (
|
||
ctx: CanvasRenderingContext2D,
|
||
node: LGraphNode,
|
||
shiftY: number,
|
||
computedHeight: number | undefined
|
||
) => {
|
||
if (!node.size) return
|
||
|
||
const canvas = useCanvasStore().getCanvas()
|
||
const mouse = canvas.graph_mouse
|
||
|
||
if (!canvas.pointer_is_down && node.pointerDown) {
|
||
if (
|
||
mouse[0] === node.pointerDown.pos[0] &&
|
||
mouse[1] === node.pointerDown.pos[1]
|
||
) {
|
||
node.imageIndex = node.pointerDown.index
|
||
}
|
||
node.pointerDown = null
|
||
}
|
||
|
||
const imgs = node.imgs ?? []
|
||
let { imageIndex } = node
|
||
const numImages = imgs.length
|
||
if (numImages === 1 && !imageIndex) {
|
||
// This skips the thumbnail render section below
|
||
node.imageIndex = imageIndex = 0
|
||
}
|
||
|
||
const settingStore = useSettingStore()
|
||
const allowImageSizeDraw = settingStore.get('Comfy.Node.AllowImageSizeDraw')
|
||
const IMAGE_TEXT_SIZE_TEXT_HEIGHT = allowImageSizeDraw ? 15 : 0
|
||
const dw = node.size[0]
|
||
const dh = computedHeight ? computedHeight - IMAGE_TEXT_SIZE_TEXT_HEIGHT : 0
|
||
|
||
if (imageIndex == null) {
|
||
// No image selected; draw thumbnails of all
|
||
let cellWidth: number
|
||
let cellHeight: number
|
||
let shiftX: number
|
||
let cell_padding: number
|
||
let cols: number
|
||
|
||
const compact_mode = is_all_same_aspect_ratio(imgs)
|
||
if (!compact_mode) {
|
||
// use rectangle cell style and border line
|
||
cell_padding = 2
|
||
// Prevent infinite canvas2d scale-up
|
||
const largestDimension = imgs.reduce(
|
||
(acc, current) =>
|
||
Math.max(acc, current.naturalWidth, current.naturalHeight),
|
||
0
|
||
)
|
||
const fakeImgs = []
|
||
fakeImgs.length = imgs.length
|
||
fakeImgs[0] = {
|
||
naturalWidth: largestDimension,
|
||
naturalHeight: largestDimension
|
||
}
|
||
;({ cellWidth, cellHeight, cols, shiftX } = calculateImageGrid(
|
||
fakeImgs,
|
||
dw,
|
||
dh
|
||
))
|
||
} else {
|
||
cell_padding = 0
|
||
;({ cellWidth, cellHeight, cols, shiftX } = calculateImageGrid(
|
||
imgs,
|
||
dw,
|
||
dh
|
||
))
|
||
}
|
||
|
||
let anyHovered = false
|
||
node.imageRects = []
|
||
for (let i = 0; i < numImages; i++) {
|
||
const img = imgs[i]
|
||
const row = Math.floor(i / cols)
|
||
const col = i % cols
|
||
const x = col * cellWidth + shiftX
|
||
const y = row * cellHeight + shiftY
|
||
if (!anyHovered) {
|
||
anyHovered = LiteGraph.isInsideRectangle(
|
||
mouse[0],
|
||
mouse[1],
|
||
x + node.pos[0],
|
||
y + node.pos[1],
|
||
cellWidth,
|
||
cellHeight
|
||
)
|
||
if (anyHovered) {
|
||
node.overIndex = i
|
||
let value = 110
|
||
if (canvas.pointer_is_down) {
|
||
if (!node.pointerDown || node.pointerDown.index !== i) {
|
||
node.pointerDown = { index: i, pos: [...mouse] }
|
||
}
|
||
value = 125
|
||
}
|
||
ctx.filter = `contrast(${value}%) brightness(${value}%)`
|
||
canvas.canvas.style.cursor = 'pointer'
|
||
}
|
||
}
|
||
node.imageRects.push([x, y, cellWidth, cellHeight])
|
||
|
||
const wratio = cellWidth / img.width
|
||
const hratio = cellHeight / img.height
|
||
const ratio = Math.min(wratio, hratio)
|
||
|
||
const imgHeight = ratio * img.height
|
||
const imgY = row * cellHeight + shiftY + (cellHeight - imgHeight) / 2
|
||
const imgWidth = ratio * img.width
|
||
const imgX = col * cellWidth + shiftX + (cellWidth - imgWidth) / 2
|
||
|
||
// Defer image rendering to work around Chrome GPU bug
|
||
const transform = ctx.getTransform()
|
||
const filter = ctx.filter
|
||
const drawParams = {
|
||
img,
|
||
x: imgX + cell_padding,
|
||
y: imgY + cell_padding,
|
||
w: imgWidth - cell_padding * 2,
|
||
h: imgHeight - cell_padding * 2
|
||
}
|
||
deferredImageRenders.push(() => {
|
||
ctx.save()
|
||
ctx.setTransform(transform)
|
||
ctx.filter = filter
|
||
ctx.drawImage(
|
||
drawParams.img,
|
||
drawParams.x,
|
||
drawParams.y,
|
||
drawParams.w,
|
||
drawParams.h
|
||
)
|
||
ctx.restore()
|
||
})
|
||
scheduleDeferredImageRender()
|
||
|
||
if (!compact_mode) {
|
||
// rectangle cell and border line style
|
||
ctx.strokeStyle = '#8F8F8F'
|
||
ctx.lineWidth = 1
|
||
ctx.strokeRect(
|
||
x + cell_padding,
|
||
y + cell_padding,
|
||
cellWidth - cell_padding * 2,
|
||
cellHeight - cell_padding * 2
|
||
)
|
||
}
|
||
|
||
ctx.filter = 'none'
|
||
}
|
||
|
||
if (!anyHovered) {
|
||
node.pointerDown = null
|
||
node.overIndex = null
|
||
}
|
||
|
||
return
|
||
}
|
||
// Draw individual
|
||
const img = imgs[imageIndex]
|
||
let w = img.naturalWidth
|
||
let h = img.naturalHeight
|
||
|
||
const scaleX = dw / w
|
||
const scaleY = dh / h
|
||
const scale = Math.min(scaleX, scaleY, 1)
|
||
|
||
w *= scale
|
||
h *= scale
|
||
|
||
const x = (dw - w) / 2
|
||
const y = (dh - h) / 2 + shiftY
|
||
|
||
// Defer image rendering to work around Chrome GPU bug
|
||
const transform = ctx.getTransform()
|
||
deferredImageRenders.push(() => {
|
||
ctx.save()
|
||
ctx.setTransform(transform)
|
||
ctx.drawImage(img, x, y, w, h)
|
||
ctx.restore()
|
||
})
|
||
scheduleDeferredImageRender()
|
||
|
||
// Draw image size text below the image
|
||
if (allowImageSizeDraw) {
|
||
ctx.fillStyle = LiteGraph.NODE_TEXT_COLOR
|
||
ctx.textAlign = 'center'
|
||
ctx.font = '10px sans-serif'
|
||
const sizeText = `${Math.round(img.naturalWidth)} × ${Math.round(img.naturalHeight)}`
|
||
const textY = y + h + 10
|
||
ctx.fillText(sizeText, x + w / 2, textY)
|
||
}
|
||
|
||
const drawButton = (
|
||
x: number,
|
||
y: number,
|
||
sz: number,
|
||
text: string
|
||
): boolean => {
|
||
const hovered = LiteGraph.isInsideRectangle(
|
||
mouse[0],
|
||
mouse[1],
|
||
x + node.pos[0],
|
||
y + node.pos[1],
|
||
sz,
|
||
sz
|
||
)
|
||
let fill = '#333'
|
||
let textFill = '#fff'
|
||
let isClicking = false
|
||
if (hovered) {
|
||
canvas.canvas.style.cursor = 'pointer'
|
||
if (canvas.pointer_is_down) {
|
||
fill = '#1e90ff'
|
||
isClicking = true
|
||
} else {
|
||
fill = '#eee'
|
||
textFill = '#000'
|
||
}
|
||
}
|
||
|
||
deferredImageRenders.push(() => {
|
||
ctx.save()
|
||
ctx.setTransform(transform)
|
||
ctx.fillStyle = fill
|
||
ctx.beginPath()
|
||
ctx.roundRect(x, y, sz, sz, [4])
|
||
ctx.fill()
|
||
ctx.fillStyle = textFill
|
||
ctx.font = '12px Inter, sans-serif'
|
||
ctx.textAlign = 'center'
|
||
ctx.fillText(text, x + 15, y + 20)
|
||
ctx.restore()
|
||
})
|
||
|
||
return isClicking
|
||
}
|
||
|
||
if (!(numImages > 1)) return
|
||
|
||
const imageNum = (node.imageIndex ?? 0) + 1
|
||
if (drawButton(dw - 40, dh + shiftY - 40, 30, `${imageNum}/${numImages}`)) {
|
||
const i = imageNum >= numImages ? 0 : imageNum
|
||
if (!node.pointerDown || node.pointerDown.index !== i) {
|
||
node.pointerDown = { index: i, pos: [...mouse] }
|
||
}
|
||
}
|
||
|
||
if (drawButton(dw - 40, shiftY + 10, 30, `x`)) {
|
||
if (!node.pointerDown || node.pointerDown.index !== null) {
|
||
node.pointerDown = { index: null, pos: [...mouse] }
|
||
}
|
||
}
|
||
}
|
||
|
||
class ImagePreviewWidget extends BaseWidget {
|
||
constructor(
|
||
node: LGraphNode,
|
||
name: string,
|
||
options: IWidgetOptions<string | object>
|
||
) {
|
||
const widget: IBaseWidget = {
|
||
name,
|
||
options,
|
||
type: 'custom',
|
||
/** Dummy value to satisfy type requirements. */
|
||
value: '',
|
||
y: 0
|
||
}
|
||
super(widget, node)
|
||
|
||
// Don't serialize the widget value
|
||
this.serialize = false
|
||
}
|
||
|
||
override drawWidget(ctx: CanvasRenderingContext2D): void {
|
||
renderPreview(ctx, this.node, this.y, this.computedHeight)
|
||
}
|
||
|
||
override onPointerDown(pointer: CanvasPointer, node: LGraphNode): boolean {
|
||
pointer.onDragStart = () => {
|
||
const { canvas } = app
|
||
const { graph } = canvas
|
||
canvas.emitBeforeChange()
|
||
graph?.beforeChange()
|
||
// Ensure that dragging is properly cleaned up, on success or failure.
|
||
pointer.finally = () => {
|
||
canvas.isDragging = false
|
||
graph?.afterChange()
|
||
canvas.emitAfterChange()
|
||
}
|
||
|
||
canvas.processSelect(node, pointer.eDown)
|
||
canvas.isDragging = true
|
||
}
|
||
|
||
pointer.onDragEnd = (e) => {
|
||
const { canvas } = app
|
||
if (e.shiftKey || LiteGraph.alwaysSnapToGrid)
|
||
canvas.graph?.snapToGrid(canvas.selectedItems)
|
||
|
||
canvas.setDirty(true, true)
|
||
}
|
||
|
||
return true
|
||
}
|
||
|
||
override onClick(): void {}
|
||
|
||
override computeLayoutSize() {
|
||
return {
|
||
minHeight: 220,
|
||
minWidth: 1
|
||
}
|
||
}
|
||
}
|
||
|
||
export const useImagePreviewWidget = () => {
|
||
const widgetConstructor: ComfyWidgetConstructorV2 = (
|
||
node: LGraphNode,
|
||
inputSpec: InputSpec
|
||
) => {
|
||
return node.addCustomWidget(
|
||
new ImagePreviewWidget(node, inputSpec.name, {
|
||
serialize: false,
|
||
canvasOnly: true
|
||
})
|
||
)
|
||
}
|
||
|
||
return widgetConstructor
|
||
}
|