Skip to content

Commit 640fb97

Browse files
committed
refactor: Reorganizing scaling logic.
Consistent names and order of operations
1 parent 9da82f4 commit 640fb97

File tree

2 files changed

+65
-103
lines changed

2 files changed

+65
-103
lines changed

src/lib/litegraph/src/LGraph.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ export type {
7979
LGraphTriggerParam
8080
} from './types/graphTriggers'
8181

82-
export type rendererType = 'LG' | 'Vue'
82+
export type RendererType = 'LG' | 'Vue'
8383

8484
export interface LGraphState {
8585
lastGroupId: number
@@ -106,7 +106,7 @@ export interface LGraphExtra extends Dictionary<unknown> {
106106
reroutes?: SerialisableReroute[]
107107
linkExtensions?: { id: number; parentId: number | undefined }[]
108108
ds?: DragAndScaleState
109-
workflowRendererVersion?: rendererType
109+
workflowRendererVersion?: RendererType
110110
}
111111

112112
export interface BaseLGraph {
Lines changed: 63 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { useVueFeatureFlags } from '@/composables/useVueFeatureFlags'
2-
import type { LGraph, rendererType } from '@/lib/litegraph/src/LGraph'
2+
import type { LGraph, RendererType } from '@/lib/litegraph/src/LGraph'
33
import { LiteGraph } from '@/lib/litegraph/src/litegraph'
44
import { createBounds } from '@/lib/litegraph/src/measure'
55
import { useSettingStore } from '@/platform/settings/settingStore'
@@ -13,135 +13,108 @@ import type { SubgraphOutputNode } from '@/lib/litegraph/src/subgraph/SubgraphOu
1313
const SCALE_FACTOR = 1.2
1414

1515
export function ensureCorrectLayoutScale(
16-
renderer?: rendererType,
16+
renderer: RendererType = 'LG',
1717
targetGraph?: LGraph
1818
) {
19-
const settingStore = useSettingStore()
20-
21-
const autoScaleLayoutSetting = settingStore.get(
19+
const autoScaleLayoutSetting = useSettingStore().get(
2220
'Comfy.VueNodes.AutoScaleLayout'
2321
)
2422

25-
if (autoScaleLayoutSetting === false) {
26-
return
27-
}
28-
29-
const { shouldRenderVueNodes } = useVueFeatureFlags()
23+
if (!autoScaleLayoutSetting) return
3024

3125
const canvas = comfyApp.canvas
3226
const graph = targetGraph ?? canvas?.graph
3327

34-
if (!graph || !graph.nodes) return
28+
if (!graph?.nodes) return
3529

36-
// Use renderer from graph, default to 'LG' for the check (but don't modify graph yet)
37-
if (!renderer) {
38-
// Always assume legacy LG format when unknown (pre-dates this feature)
39-
renderer = 'LG'
40-
}
30+
const { shouldRenderVueNodes } = useVueFeatureFlags()
4131

42-
const doesntNeedScale =
43-
(renderer === 'LG' && shouldRenderVueNodes.value === false) ||
44-
(renderer === 'Vue' && shouldRenderVueNodes.value === true)
32+
const needsUpscale = renderer === 'LG' && shouldRenderVueNodes.value
33+
const needsDownscale = renderer === 'Vue' && !shouldRenderVueNodes.value
4534

46-
if (doesntNeedScale) {
35+
if (!needsUpscale && !needsDownscale) {
4736
// Don't scale, but ensure workflowRendererVersion is set for future checks
48-
if (!graph.extra.workflowRendererVersion) {
49-
graph.extra.workflowRendererVersion = renderer
50-
}
37+
graph.extra.workflowRendererVersion ??= renderer
5138
return
5239
}
5340

54-
const needsUpscale = renderer === 'LG' && shouldRenderVueNodes.value === true
55-
const needsDownscale =
56-
renderer === 'Vue' && shouldRenderVueNodes.value === false
57-
5841
const lgBounds = createBounds(graph.nodes)
5942

6043
if (!lgBounds) return
6144

62-
const originX = lgBounds[0]
63-
const originY = lgBounds[1]
45+
const [originX, originY] = lgBounds
6446

6547
const lgNodesById = new Map(graph.nodes.map((node) => [node.id, node]))
6648

6749
const yjsMoveNodeUpdates: NodeBoundsUpdate[] = []
6850

69-
const scaleFactor = needsUpscale
70-
? SCALE_FACTOR
71-
: needsDownscale
72-
? 1 / SCALE_FACTOR
73-
: 1
51+
const scaleFactor = needsUpscale ? SCALE_FACTOR : 1 / SCALE_FACTOR
52+
53+
const onActiveGraph = !targetGraph || targetGraph === canvas?.graph
7454

7555
//TODO: once we remove the need for LiteGraph.NODE_TITLE_HEIGHT in vue nodes we nned to remove everything here.
7656
for (const node of graph.nodes) {
7757
const lgNode = lgNodesById.get(node.id)
7858
if (!lgNode) continue
7959

80-
const lgBodyY = lgNode.pos[1]
60+
const [lgBodyX, lgBodyY] = lgNode.pos
8161

82-
const adjustedY = needsDownscale
83-
? lgBodyY - LiteGraph.NODE_TITLE_HEIGHT / 2
84-
: lgBodyY
62+
const adjustedY = lgBodyY - (needsUpscale ? LiteGraph.NODE_TITLE_HEIGHT : 0)
8563

86-
const relativeX = lgNode.pos[0] - originX
64+
const relativeX = lgBodyX - originX
8765
const relativeY = adjustedY - originY
88-
const newX = originX + relativeX * scaleFactor
89-
const scaledY = originY + relativeY * scaleFactor
90-
const newWidth = lgNode.width * scaleFactor
91-
const newHeight = lgNode.height * scaleFactor
9266

93-
const finalY = needsUpscale
94-
? scaledY + LiteGraph.NODE_TITLE_HEIGHT / 2
95-
: scaledY
67+
const scaledX = (originX + relativeX) * scaleFactor
68+
const scaledY = (originY + relativeY) * scaleFactor
69+
70+
const scaledWidth = lgNode.width * scaleFactor
71+
const scaledHeight =
72+
lgNode.height * scaleFactor -
73+
(needsUpscale ? 0 : LiteGraph.NODE_TITLE_HEIGHT)
74+
75+
const finalY = scaledY + (needsUpscale ? 0 : LiteGraph.NODE_TITLE_HEIGHT) // Litegraph Position further down
9676

9777
// Directly update LiteGraph node to ensure immediate consistency
9878
// Dont need to reference vue directly because the pos and dims are already in yjs
99-
lgNode.pos[0] = newX
79+
lgNode.pos[0] = scaledX
10080
lgNode.pos[1] = finalY
101-
lgNode.size[0] = newWidth
102-
lgNode.size[1] =
103-
newHeight - (needsDownscale ? LiteGraph.NODE_TITLE_HEIGHT : 0)
81+
lgNode.size[0] = scaledWidth
82+
lgNode.size[1] = scaledHeight
10483

10584
// Track updates for layout store (only if this is the active graph)
106-
if (!targetGraph || targetGraph === canvas?.graph) {
85+
if (onActiveGraph) {
10786
yjsMoveNodeUpdates.push({
10887
nodeId: String(lgNode.id),
10988
bounds: {
110-
x: newX,
89+
x: scaledX,
11190
y: finalY,
112-
width: newWidth,
113-
height: newHeight - (needsDownscale ? LiteGraph.NODE_TITLE_HEIGHT : 0)
91+
width: scaledWidth,
92+
height: scaledHeight
11493
}
11594
})
11695
}
11796
}
11897

119-
if (
120-
(!targetGraph || targetGraph === canvas?.graph) &&
121-
yjsMoveNodeUpdates.length > 0
122-
) {
98+
if (onActiveGraph && yjsMoveNodeUpdates.length > 0) {
12399
layoutStore.batchUpdateNodeBounds(yjsMoveNodeUpdates)
124100
}
125101

126102
for (const reroute of graph.reroutes.values()) {
127-
const oldX = reroute.pos[0]
128-
const oldY = reroute.pos[1]
103+
const [oldX, oldY] = reroute.pos
129104

130105
const relativeX = oldX - originX
131106
const relativeY = oldY - originY
132-
const newX = originX + relativeX * scaleFactor
133-
const newY = originY + relativeY * scaleFactor
134107

135-
reroute.pos = [newX, newY]
108+
const scaledX = (originX + relativeX) * scaleFactor
109+
const scaledY = (originY + relativeY) * scaleFactor
136110

137-
if (
138-
(!targetGraph || targetGraph === canvas?.graph) &&
139-
shouldRenderVueNodes.value
140-
) {
111+
reroute.pos = [scaledX, scaledY]
112+
113+
if (onActiveGraph && shouldRenderVueNodes.value) {
141114
const layoutMutations = useLayoutMutations()
142115
layoutMutations.moveReroute(
143116
reroute.id,
144-
{ x: newX, y: newY },
117+
{ x: scaledX, y: scaledY },
145118
{ x: oldX, y: oldY }
146119
)
147120
}
@@ -153,60 +126,49 @@ export function ensureCorrectLayoutScale(
153126
graph.outputNode as SubgraphOutputNode
154127
]
155128
for (const ioNode of ioNodes) {
156-
const oldX = ioNode.pos[0]
157-
const oldY = ioNode.pos[1]
158-
const oldWidth = ioNode.size[0]
159-
const oldHeight = ioNode.size[1]
129+
const [oldX, oldY] = ioNode.pos
130+
const [oldWidth, oldHeight] = ioNode.size
160131

161132
const relativeX = oldX - originX
162133
const relativeY = oldY - originY
163-
const newX = originX + relativeX * scaleFactor
164-
const newY = originY + relativeY * scaleFactor
165-
const newWidth = oldWidth * scaleFactor
166-
const newHeight = oldHeight * scaleFactor
167134

168-
ioNode.pos = [newX, newY]
169-
ioNode.size = [newWidth, newHeight]
135+
const scaledX = (originX + relativeX) * scaleFactor
136+
const scaledY = (originY + relativeY) * scaleFactor
137+
138+
const scaledWidth = oldWidth * scaleFactor
139+
const scaledHeight = oldHeight * scaleFactor
140+
141+
ioNode.pos = [scaledX, scaledY]
142+
ioNode.size = [scaledWidth, scaledHeight]
170143
}
171144
}
172145

173146
graph.groups.forEach((group) => {
174-
const originalPosX = group.pos[0]
175-
const originalPosY = group.pos[1]
176-
const originalWidth = group.size[0]
177-
const originalHeight = group.size[1]
147+
const [originalPosX, originalPosY] = group.pos
148+
const [originalWidth, originalHeight] = group.size
178149

179-
const adjustedY = needsDownscale
180-
? originalPosY - LiteGraph.NODE_TITLE_HEIGHT
181-
: originalPosY
150+
const adjustedY =
151+
originalPosY - (needsUpscale ? LiteGraph.NODE_TITLE_HEIGHT : 0)
182152

183153
const relativeX = originalPosX - originX
184154
const relativeY = adjustedY - originY
185155

186-
const newWidth = originalWidth * scaleFactor
187-
const newHeight = originalHeight * scaleFactor
156+
const scaledX = (originX + relativeX) * scaleFactor
157+
const scaledY = (originY + relativeY) * scaleFactor
188158

189-
const scaledX = originX + relativeX * scaleFactor
190-
const scaledY = originY + relativeY * scaleFactor
159+
const scaledWidth = originalWidth * scaleFactor
160+
const scaledHeight = originalHeight * scaleFactor
191161

192-
const finalY = needsUpscale
193-
? scaledY + LiteGraph.NODE_TITLE_HEIGHT
194-
: scaledY
162+
const finalY = scaledY + (needsUpscale ? 0 : LiteGraph.NODE_TITLE_HEIGHT)
195163

196164
group.pos = [scaledX, finalY]
197-
group.size = [newWidth, newHeight]
165+
group.size = [scaledWidth, scaledHeight]
198166
})
199167

200-
if ((!targetGraph || targetGraph === canvas?.graph) && canvas) {
168+
if (onActiveGraph && canvas) {
201169
const originScreen = canvas.ds.convertOffsetToCanvas([originX, originY])
202170
canvas.ds.changeScale(canvas.ds.scale / scaleFactor, originScreen)
203171
}
204172

205-
if (needsUpscale) {
206-
graph.extra.workflowRendererVersion = 'Vue'
207-
}
208-
209-
if (needsDownscale) {
210-
graph.extra.workflowRendererVersion = 'LG'
211-
}
173+
graph.extra.workflowRendererVersion = needsUpscale ? 'Vue' : 'LG'
212174
}

0 commit comments

Comments
 (0)