|
| 1 | +import type { Rect } from '@/lib/litegraph/src/interfaces' |
| 2 | +import { createBounds } from '@/lib/litegraph/src/measure' |
| 3 | +import { useSettingStore } from '@/platform/settings/settingStore' |
| 4 | +import { layoutStore } from '@/renderer/core/layout/store/layoutStore' |
| 5 | +import type { NodeBoundsUpdate } from '@/renderer/core/layout/types' |
| 6 | +import { app as comfyApp } from '@/scripts/app' |
| 7 | + |
| 8 | +const SCALE_FACTOR = 1.75 |
| 9 | + |
| 10 | +export function scaleLayoutForVueNodes() { |
| 11 | + const settingStore = useSettingStore() |
| 12 | + |
| 13 | + const autoScaleLayoutSetting = settingStore.get( |
| 14 | + 'Comfy.VueNodes.AutoScaleLayout' |
| 15 | + ) |
| 16 | + |
| 17 | + if (autoScaleLayoutSetting === false) { |
| 18 | + return |
| 19 | + } |
| 20 | + |
| 21 | + const canvas = comfyApp.canvas |
| 22 | + const graph = canvas.graph |
| 23 | + |
| 24 | + if (!graph || !graph.nodes) return |
| 25 | + |
| 26 | + const lgBounds = createBounds(graph.nodes) |
| 27 | + |
| 28 | + if (!lgBounds) return |
| 29 | + |
| 30 | + const allVueNodes = layoutStore.getAllNodes().value |
| 31 | + |
| 32 | + const lgBoundsCenterX = lgBounds![0] + lgBounds![2] / 2 |
| 33 | + const lgBoundsCenterY = lgBounds![1] + lgBounds![3] / 2 |
| 34 | + |
| 35 | + const lgNodesById = new Map( |
| 36 | + graph.nodes.map((node) => [String(node.id), node]) |
| 37 | + ) |
| 38 | + |
| 39 | + const yjsMoveNodeUpdates: NodeBoundsUpdate[] = [] |
| 40 | + const scaledNodesForBounds: Array<{ boundingRect: Rect }> = [] |
| 41 | + |
| 42 | + for (const vueNode of allVueNodes.values()) { |
| 43 | + const lgNode = lgNodesById.get(String(vueNode.id)) |
| 44 | + if (!lgNode) continue |
| 45 | + |
| 46 | + const vectorX = lgNode.pos[0] - lgBoundsCenterX |
| 47 | + const vectorY = lgNode.pos[1] - lgBoundsCenterY |
| 48 | + const newX = lgBoundsCenterX + vectorX * SCALE_FACTOR |
| 49 | + const newY = lgBoundsCenterY + vectorY * SCALE_FACTOR |
| 50 | + |
| 51 | + yjsMoveNodeUpdates.push({ |
| 52 | + nodeId: vueNode.id, |
| 53 | + bounds: { |
| 54 | + x: newX, |
| 55 | + y: newY, |
| 56 | + width: vueNode.bounds.width, |
| 57 | + height: vueNode.bounds.height |
| 58 | + } |
| 59 | + }) |
| 60 | + |
| 61 | + scaledNodesForBounds.push({ |
| 62 | + boundingRect: [newX, newY, vueNode.bounds.width, vueNode.bounds.height] |
| 63 | + }) |
| 64 | + } |
| 65 | + |
| 66 | + layoutStore.batchUpdateNodeBounds(yjsMoveNodeUpdates) |
| 67 | + |
| 68 | + const scaledLgBounds = createBounds(scaledNodesForBounds) |
| 69 | + |
| 70 | + graph.groups.forEach((group) => { |
| 71 | + const vectorX = group.pos[0] - lgBoundsCenterX |
| 72 | + const vectorY = group.pos[1] - lgBoundsCenterY |
| 73 | + |
| 74 | + group.pos = [ |
| 75 | + lgBoundsCenterX + vectorX * SCALE_FACTOR, |
| 76 | + lgBoundsCenterY + vectorY * SCALE_FACTOR |
| 77 | + ] |
| 78 | + group.size = [group.size[0] * SCALE_FACTOR, group.size[1] * SCALE_FACTOR] |
| 79 | + }) |
| 80 | + |
| 81 | + if (scaledLgBounds) { |
| 82 | + canvas.ds.fitToBounds(scaledLgBounds, { |
| 83 | + zoom: 0.5 //Makes it so the fit to view is slightly zoomed out and not edge to edge. |
| 84 | + }) |
| 85 | + } |
| 86 | +} |
0 commit comments