Upstream widgets layout to litegraph (#2557)

This commit is contained in:
Chenlei Hu
2025-02-14 19:40:31 -05:00
committed by GitHub
parent 5dc4a1b9cd
commit 0e2ce5e1ca
6 changed files with 5 additions and 221 deletions

View File

@@ -8,7 +8,6 @@ import type {
} from '@comfyorg/litegraph/dist/types/widgets'
import { useSettingStore } from '@/stores/settingStore'
import { distributeSpace } from '@/utils/spaceDistribution'
import { ANIM_PREVIEW_WIDGET, app } from './app'
@@ -128,79 +127,6 @@ function getClipPath(
return ''
}
function computeSize(this: LGraphNode, size: Size): void {
if (!this.widgets?.[0]?.last_y) return
let y = this.widgets[0].last_y
let freeSpace = size[1] - y
// Collect fixed height widgets first
let fixedWidgetHeight = 0
const layoutWidgets: SizeInfo[] = []
for (const w of this.widgets) {
// @ts-expect-error custom widget type
if (w.type === 'converted-widget') {
// Ignore
// @ts-expect-error custom widget type
delete w.computedHeight
} else if (w.computeLayoutSize) {
const { minHeight, maxHeight } = w.computeLayoutSize(this)
layoutWidgets.push({
minHeight,
prefHeight: maxHeight,
w
})
} else if (w.computeSize) {
fixedWidgetHeight += w.computeSize()[1] + 4
} else {
fixedWidgetHeight += LiteGraph.NODE_WIDGET_HEIGHT + 4
}
}
if (this.imgs && !this.widgets?.find((w) => w.name === ANIM_PREVIEW_WIDGET)) {
fixedWidgetHeight += 220
}
// Calculate remaining space for DOM widgets
freeSpace -= fixedWidgetHeight
this.freeWidgetSpace = freeSpace
// Prepare space requests for distribution
const spaceRequests = layoutWidgets.map((d) => ({
minSize: d.minHeight,
maxSize: d.prefHeight
}))
// Distribute space among DOM widgets
const allocations = distributeSpace(Math.max(0, freeSpace), spaceRequests)
// Apply computed heights
layoutWidgets.forEach((d, i) => {
d.w.computedHeight = allocations[i]
})
// If we need more space, grow the node
const totalNeeded =
fixedWidgetHeight + allocations.reduce((sum, h) => sum + h, 0)
if (totalNeeded > size[1] - this.widgets[0].last_y) {
size[1] = totalNeeded + this.widgets[0].last_y
this.graph?.setDirtyCanvas(true)
}
// Position widgets
for (const w of this.widgets) {
w.y = y
if (w.computedHeight) {
y += w.computedHeight
} else if (w.computeSize) {
y += w.computeSize()[1] + 4
} else {
y += LiteGraph.NODE_WIDGET_HEIGHT + 4
}
}
}
// Override the compute visible nodes function to allow us to hide/show DOM elements when the node goes offscreen
const elementWidgets = new Set<LGraphNode>()
const computeVisibleNodes = LGraphCanvas.prototype.computeVisibleNodes
@@ -315,10 +241,6 @@ export class DOMWidgetImpl<T extends HTMLElement, V extends object | string>
widgetWidth: number,
y: number
): void {
if (this.computedHeight == null) {
computeSize.call(node, node.size)
}
const { offset, scale } = app.canvas.ds
const hidden =
(!!this.options.hideOnZoom && app.canvas.low_quality) ||
@@ -461,7 +383,6 @@ LGraphNode.prototype.addDOMWidget = function <
const onResize = this.onResize
this.onResize = function (this: LGraphNode, size: Size) {
options.beforeResize?.call(widget, this)
computeSize.call(this, size)
onResize?.call(this, size)
options.afterResize?.call(widget, this)
}