Line data Source code
1 : import type { PositionedRect as Positioned } from '@comfyorg/shared-frontend-utils/geometry'
2 : import type { LGraph, LGraphExtra } from '@/lib/litegraph/src/LGraph'
3 : import { LiteGraph } from '@/lib/litegraph/src/litegraph'
4 : import { snapPoint } from '@/lib/litegraph/src/measure'
5 : import {
6 : RENDER_SCALE_FACTOR,
7 : getGraphRenderAnchor,
8 : unprojectBounds,
9 : unprojectPoint
10 : } from '@/renderer/core/layout/transform/graphRenderTransform'
11 : import type { SubgraphInputNode } from '@/lib/litegraph/src/subgraph/SubgraphInputNode'
12 : import type { SubgraphOutputNode } from '@/lib/litegraph/src/subgraph/SubgraphOutputNode'
13 :
14 : /**
15 : * One-time legacy normalizer for workflows saved with Vue-scaled coordinates.
16 : *
17 : * Detects workflows saved in the old Vue coordinate space (where positions
18 : * were mutated by 1.2x at runtime) and normalizes them back to canonical
19 : * LiteGraph coordinates. Runs once per graph, then marks it as normalized.
20 : *
21 : * After normalization, rendering applies the 1.2x scale visually via CSS
22 : * transforms rather than mutating persisted geometry.
23 : *
24 : * @param rendererVersion - Override for the renderer version check. When
25 : * graph metadata is missing, this value is used as a fallback.
26 : * @param targetGraph - The graph to normalize.
27 : */
28 4453 : export function ensureCorrectLayoutScale(
29 : rendererVersion: LGraphExtra['workflowRendererVersion'],
30 : graph: LGraph
31 : ): boolean {
32 4453 : const currentRenderer = graph.extra.workflowRendererVersion
33 4453 : if (currentRenderer === 'Vue-corrected') return false
34 :
35 : const renderer = currentRenderer ?? rendererVersion
36 4453 : if (renderer !== 'Vue') return false
37 :
38 15 : const anchor = getGraphRenderAnchor(graph)
39 :
40 110 : function applySnap(
41 : pos: [number, number],
42 : method: 'round' | 'ceil' | 'floor' = 'round'
43 : ) {
44 110 : if (LiteGraph.alwaysSnapToGrid) {
45 0 : const snapTo = graph.getSnapToGridSize()
46 0 : if (snapTo) {
47 0 : snapPoint(pos, snapTo, method)
48 : }
49 : }
50 : }
51 :
52 55 : function normalizedBounds(
53 : item: Positioned
54 : ): [x: number, y: number, width: number, height: number] {
55 55 : const c = unprojectBounds(
56 : {
57 : x: item.pos[0],
58 : y: item.pos[1],
59 : width: item.size[0],
60 : height: item.size[1]
61 : },
62 : anchor,
63 : RENDER_SCALE_FACTOR
64 : )
65 55 : const pos: [number, number] = [c.x, c.y]
66 55 : const size: [number, number] = [c.width, c.height]
67 55 : applySnap(pos)
68 55 : applySnap(size, 'ceil')
69 :
70 55 : return [pos[0], pos[1], size[0], size[1]]
71 : }
72 :
73 41 : function normalize(item: Positioned) {
74 41 : const [x, y, width, height] = normalizedBounds(item)
75 41 : item.pos = [x, y]
76 41 : item.size = [width, height]
77 : }
78 :
79 17 : for (const node of graph.nodes) {
80 33 : normalize(node)
81 : }
82 :
83 1344 : for (const reroute of graph.reroutes.values()) {
84 0 : const p = unprojectPoint(
85 : { x: reroute.pos[0], y: reroute.pos[1] },
86 : anchor,
87 : RENDER_SCALE_FACTOR
88 : )
89 0 : const pos: [number, number] = [p.x, p.y]
90 0 : applySnap(pos)
91 0 : reroute.pos = pos
92 : }
93 :
94 27 : for (const group of graph.groups) {
95 14 : group.boundingRect.set(normalizedBounds(group))
96 : }
97 :
98 4453 : if ('inputNode' in graph && 'outputNode' in graph) {
99 4 : for (const ioNode of [
100 : graph.inputNode as SubgraphInputNode | null,
101 : graph.outputNode as SubgraphOutputNode | null
102 : ]) {
103 8 : if (ioNode) normalize(ioNode)
104 : }
105 : }
106 :
107 15 : graph.extra.workflowRendererVersion = 'Vue-corrected'
108 15 : return true
109 : }
|