refactor: Reorganizing scaling logic.

Consistent names and order of operations
This commit is contained in:
DrJKL
2025-11-18 22:06:28 -08:00
committed by Alexander Brown
parent 9da82f47ef
commit 640fb97760
2 changed files with 65 additions and 103 deletions

View File

@@ -79,7 +79,7 @@ export type {
LGraphTriggerParam LGraphTriggerParam
} from './types/graphTriggers' } from './types/graphTriggers'
export type rendererType = 'LG' | 'Vue' export type RendererType = 'LG' | 'Vue'
export interface LGraphState { export interface LGraphState {
lastGroupId: number lastGroupId: number
@@ -106,7 +106,7 @@ export interface LGraphExtra extends Dictionary<unknown> {
reroutes?: SerialisableReroute[] reroutes?: SerialisableReroute[]
linkExtensions?: { id: number; parentId: number | undefined }[] linkExtensions?: { id: number; parentId: number | undefined }[]
ds?: DragAndScaleState ds?: DragAndScaleState
workflowRendererVersion?: rendererType workflowRendererVersion?: RendererType
} }
export interface BaseLGraph { export interface BaseLGraph {

View File

@@ -1,5 +1,5 @@
import { useVueFeatureFlags } from '@/composables/useVueFeatureFlags' import { useVueFeatureFlags } from '@/composables/useVueFeatureFlags'
import type { LGraph, rendererType } from '@/lib/litegraph/src/LGraph' import type { LGraph, RendererType } from '@/lib/litegraph/src/LGraph'
import { LiteGraph } from '@/lib/litegraph/src/litegraph' import { LiteGraph } from '@/lib/litegraph/src/litegraph'
import { createBounds } from '@/lib/litegraph/src/measure' import { createBounds } from '@/lib/litegraph/src/measure'
import { useSettingStore } from '@/platform/settings/settingStore' import { useSettingStore } from '@/platform/settings/settingStore'
@@ -13,135 +13,108 @@ import type { SubgraphOutputNode } from '@/lib/litegraph/src/subgraph/SubgraphOu
const SCALE_FACTOR = 1.2 const SCALE_FACTOR = 1.2
export function ensureCorrectLayoutScale( export function ensureCorrectLayoutScale(
renderer?: rendererType, renderer: RendererType = 'LG',
targetGraph?: LGraph targetGraph?: LGraph
) { ) {
const settingStore = useSettingStore() const autoScaleLayoutSetting = useSettingStore().get(
const autoScaleLayoutSetting = settingStore.get(
'Comfy.VueNodes.AutoScaleLayout' 'Comfy.VueNodes.AutoScaleLayout'
) )
if (autoScaleLayoutSetting === false) { if (!autoScaleLayoutSetting) return
return
}
const { shouldRenderVueNodes } = useVueFeatureFlags()
const canvas = comfyApp.canvas const canvas = comfyApp.canvas
const graph = targetGraph ?? canvas?.graph const graph = targetGraph ?? canvas?.graph
if (!graph || !graph.nodes) return if (!graph?.nodes) return
// Use renderer from graph, default to 'LG' for the check (but don't modify graph yet) const { shouldRenderVueNodes } = useVueFeatureFlags()
if (!renderer) {
// Always assume legacy LG format when unknown (pre-dates this feature)
renderer = 'LG'
}
const doesntNeedScale = const needsUpscale = renderer === 'LG' && shouldRenderVueNodes.value
(renderer === 'LG' && shouldRenderVueNodes.value === false) || const needsDownscale = renderer === 'Vue' && !shouldRenderVueNodes.value
(renderer === 'Vue' && shouldRenderVueNodes.value === true)
if (doesntNeedScale) { if (!needsUpscale && !needsDownscale) {
// Don't scale, but ensure workflowRendererVersion is set for future checks // Don't scale, but ensure workflowRendererVersion is set for future checks
if (!graph.extra.workflowRendererVersion) { graph.extra.workflowRendererVersion ??= renderer
graph.extra.workflowRendererVersion = renderer
}
return return
} }
const needsUpscale = renderer === 'LG' && shouldRenderVueNodes.value === true
const needsDownscale =
renderer === 'Vue' && shouldRenderVueNodes.value === false
const lgBounds = createBounds(graph.nodes) const lgBounds = createBounds(graph.nodes)
if (!lgBounds) return if (!lgBounds) return
const originX = lgBounds[0] const [originX, originY] = lgBounds
const originY = lgBounds[1]
const lgNodesById = new Map(graph.nodes.map((node) => [node.id, node])) const lgNodesById = new Map(graph.nodes.map((node) => [node.id, node]))
const yjsMoveNodeUpdates: NodeBoundsUpdate[] = [] const yjsMoveNodeUpdates: NodeBoundsUpdate[] = []
const scaleFactor = needsUpscale const scaleFactor = needsUpscale ? SCALE_FACTOR : 1 / SCALE_FACTOR
? SCALE_FACTOR
: needsDownscale const onActiveGraph = !targetGraph || targetGraph === canvas?.graph
? 1 / SCALE_FACTOR
: 1
//TODO: once we remove the need for LiteGraph.NODE_TITLE_HEIGHT in vue nodes we nned to remove everything here. //TODO: once we remove the need for LiteGraph.NODE_TITLE_HEIGHT in vue nodes we nned to remove everything here.
for (const node of graph.nodes) { for (const node of graph.nodes) {
const lgNode = lgNodesById.get(node.id) const lgNode = lgNodesById.get(node.id)
if (!lgNode) continue if (!lgNode) continue
const lgBodyY = lgNode.pos[1] const [lgBodyX, lgBodyY] = lgNode.pos
const adjustedY = needsDownscale const adjustedY = lgBodyY - (needsUpscale ? LiteGraph.NODE_TITLE_HEIGHT : 0)
? lgBodyY - LiteGraph.NODE_TITLE_HEIGHT / 2
: lgBodyY
const relativeX = lgNode.pos[0] - originX const relativeX = lgBodyX - originX
const relativeY = adjustedY - originY const relativeY = adjustedY - originY
const newX = originX + relativeX * scaleFactor
const scaledY = originY + relativeY * scaleFactor
const newWidth = lgNode.width * scaleFactor
const newHeight = lgNode.height * scaleFactor
const finalY = needsUpscale const scaledX = (originX + relativeX) * scaleFactor
? scaledY + LiteGraph.NODE_TITLE_HEIGHT / 2 const scaledY = (originY + relativeY) * scaleFactor
: scaledY
const scaledWidth = lgNode.width * scaleFactor
const scaledHeight =
lgNode.height * scaleFactor -
(needsUpscale ? 0 : LiteGraph.NODE_TITLE_HEIGHT)
const finalY = scaledY + (needsUpscale ? 0 : LiteGraph.NODE_TITLE_HEIGHT) // Litegraph Position further down
// Directly update LiteGraph node to ensure immediate consistency // Directly update LiteGraph node to ensure immediate consistency
// Dont need to reference vue directly because the pos and dims are already in yjs // Dont need to reference vue directly because the pos and dims are already in yjs
lgNode.pos[0] = newX lgNode.pos[0] = scaledX
lgNode.pos[1] = finalY lgNode.pos[1] = finalY
lgNode.size[0] = newWidth lgNode.size[0] = scaledWidth
lgNode.size[1] = lgNode.size[1] = scaledHeight
newHeight - (needsDownscale ? LiteGraph.NODE_TITLE_HEIGHT : 0)
// Track updates for layout store (only if this is the active graph) // Track updates for layout store (only if this is the active graph)
if (!targetGraph || targetGraph === canvas?.graph) { if (onActiveGraph) {
yjsMoveNodeUpdates.push({ yjsMoveNodeUpdates.push({
nodeId: String(lgNode.id), nodeId: String(lgNode.id),
bounds: { bounds: {
x: newX, x: scaledX,
y: finalY, y: finalY,
width: newWidth, width: scaledWidth,
height: newHeight - (needsDownscale ? LiteGraph.NODE_TITLE_HEIGHT : 0) height: scaledHeight
} }
}) })
} }
} }
if ( if (onActiveGraph && yjsMoveNodeUpdates.length > 0) {
(!targetGraph || targetGraph === canvas?.graph) &&
yjsMoveNodeUpdates.length > 0
) {
layoutStore.batchUpdateNodeBounds(yjsMoveNodeUpdates) layoutStore.batchUpdateNodeBounds(yjsMoveNodeUpdates)
} }
for (const reroute of graph.reroutes.values()) { for (const reroute of graph.reroutes.values()) {
const oldX = reroute.pos[0] const [oldX, oldY] = reroute.pos
const oldY = reroute.pos[1]
const relativeX = oldX - originX const relativeX = oldX - originX
const relativeY = oldY - originY const relativeY = oldY - originY
const newX = originX + relativeX * scaleFactor
const newY = originY + relativeY * scaleFactor
reroute.pos = [newX, newY] const scaledX = (originX + relativeX) * scaleFactor
const scaledY = (originY + relativeY) * scaleFactor
if ( reroute.pos = [scaledX, scaledY]
(!targetGraph || targetGraph === canvas?.graph) &&
shouldRenderVueNodes.value if (onActiveGraph && shouldRenderVueNodes.value) {
) {
const layoutMutations = useLayoutMutations() const layoutMutations = useLayoutMutations()
layoutMutations.moveReroute( layoutMutations.moveReroute(
reroute.id, reroute.id,
{ x: newX, y: newY }, { x: scaledX, y: scaledY },
{ x: oldX, y: oldY } { x: oldX, y: oldY }
) )
} }
@@ -153,60 +126,49 @@ export function ensureCorrectLayoutScale(
graph.outputNode as SubgraphOutputNode graph.outputNode as SubgraphOutputNode
] ]
for (const ioNode of ioNodes) { for (const ioNode of ioNodes) {
const oldX = ioNode.pos[0] const [oldX, oldY] = ioNode.pos
const oldY = ioNode.pos[1] const [oldWidth, oldHeight] = ioNode.size
const oldWidth = ioNode.size[0]
const oldHeight = ioNode.size[1]
const relativeX = oldX - originX const relativeX = oldX - originX
const relativeY = oldY - originY const relativeY = oldY - originY
const newX = originX + relativeX * scaleFactor
const newY = originY + relativeY * scaleFactor
const newWidth = oldWidth * scaleFactor
const newHeight = oldHeight * scaleFactor
ioNode.pos = [newX, newY] const scaledX = (originX + relativeX) * scaleFactor
ioNode.size = [newWidth, newHeight] const scaledY = (originY + relativeY) * scaleFactor
const scaledWidth = oldWidth * scaleFactor
const scaledHeight = oldHeight * scaleFactor
ioNode.pos = [scaledX, scaledY]
ioNode.size = [scaledWidth, scaledHeight]
} }
} }
graph.groups.forEach((group) => { graph.groups.forEach((group) => {
const originalPosX = group.pos[0] const [originalPosX, originalPosY] = group.pos
const originalPosY = group.pos[1] const [originalWidth, originalHeight] = group.size
const originalWidth = group.size[0]
const originalHeight = group.size[1]
const adjustedY = needsDownscale const adjustedY =
? originalPosY - LiteGraph.NODE_TITLE_HEIGHT originalPosY - (needsUpscale ? LiteGraph.NODE_TITLE_HEIGHT : 0)
: originalPosY
const relativeX = originalPosX - originX const relativeX = originalPosX - originX
const relativeY = adjustedY - originY const relativeY = adjustedY - originY
const newWidth = originalWidth * scaleFactor const scaledX = (originX + relativeX) * scaleFactor
const newHeight = originalHeight * scaleFactor const scaledY = (originY + relativeY) * scaleFactor
const scaledX = originX + relativeX * scaleFactor const scaledWidth = originalWidth * scaleFactor
const scaledY = originY + relativeY * scaleFactor const scaledHeight = originalHeight * scaleFactor
const finalY = needsUpscale const finalY = scaledY + (needsUpscale ? 0 : LiteGraph.NODE_TITLE_HEIGHT)
? scaledY + LiteGraph.NODE_TITLE_HEIGHT
: scaledY
group.pos = [scaledX, finalY] group.pos = [scaledX, finalY]
group.size = [newWidth, newHeight] group.size = [scaledWidth, scaledHeight]
}) })
if ((!targetGraph || targetGraph === canvas?.graph) && canvas) { if (onActiveGraph && canvas) {
const originScreen = canvas.ds.convertOffsetToCanvas([originX, originY]) const originScreen = canvas.ds.convertOffsetToCanvas([originX, originY])
canvas.ds.changeScale(canvas.ds.scale / scaleFactor, originScreen) canvas.ds.changeScale(canvas.ds.scale / scaleFactor, originScreen)
} }
if (needsUpscale) { graph.extra.workflowRendererVersion = needsUpscale ? 'Vue' : 'LG'
graph.extra.workflowRendererVersion = 'Vue'
}
if (needsDownscale) {
graph.extra.workflowRendererVersion = 'LG'
}
} }