Line data Source code
1 : import type { CompactRect } from '@comfyorg/shared-frontend-utils/geometry'
2 : import { toLightThemeNodeColor } from '@/renderer/extensions/vueNodes/utils/nodeStyleUtils'
3 : import type { LGraph } from '@/lib/litegraph/src/litegraph'
4 : import { LGraphEventMode } from '@/lib/litegraph/src/litegraph'
5 : import { useThemeStore } from '@/platform/theme/themeStore'
6 :
7 : import { MinimapDataSource } from './data/MinimapDataSource'
8 : import type {
9 : MinimapNodeData,
10 : MinimapNodeExecutionState,
11 : MinimapRenderContext
12 : } from './types'
13 :
14 : export const MINIMAP_DECORATION_POP_MS = 260
15 :
16 2356 : const BYPASS_COLOR = '#6a246a'
17 :
18 : type MinimapColors = ReturnType<typeof readMinimapColors>
19 :
20 2356 : let cachedColors: { key: string; colors: MinimapColors } | undefined
21 :
22 6426 : function resolveThemeColor(token: string): string {
23 6426 : const probe = document.createElement('span')
24 6426 : probe.style.color = `var(--${token})`
25 6426 : document.body.append(probe)
26 6426 : const { color } = getComputedStyle(probe)
27 6426 : probe.remove()
28 6426 : return color
29 : }
30 :
31 2142 : function readMinimapColors(isLightTheme: boolean, activityOutline: string) {
32 2142 : const primary = resolveThemeColor('primary')
33 2142 : const mutedForeground = resolveThemeColor('muted-foreground')
34 2142 : return {
35 : nodeColor: `color-mix(in srgb, ${primary} 60%, transparent)`,
36 : nodeColorDefault: resolveThemeColor('muted'),
37 : linkColor: mutedForeground,
38 : slotColor: mutedForeground,
39 : groupColor: `color-mix(in srgb, ${primary} 40%, transparent)`,
40 : groupColorDefault: mutedForeground,
41 : bypassColor: BYPASS_COLOR,
42 : errorColor: '#FF0000',
43 : runningColor: '#00FF00',
44 : successColor: '#239B23',
45 : activityOutlineColor: activityOutline,
46 : isLightTheme
47 : }
48 : }
49 :
50 9330 : function getMinimapColors(): MinimapColors {
51 9330 : const themeStore = useThemeStore()
52 9330 : const palette = themeStore.activeTheme
53 9330 : const key = `${palette.id}:${themeStore.colorScheme}`
54 9330 : if (cachedColors?.key !== key) {
55 2142 : cachedColors = {
56 : key,
57 : colors: readMinimapColors(
58 : themeStore.colorScheme === 'light',
59 : palette.canvas.nodeSelectedTitleColor
60 : )
61 : }
62 : }
63 9330 : return cachedColors.colors
64 : }
65 :
66 : /**
67 : * Get node color based on settings and node properties (Single Responsibility)
68 : */
69 39480 : function getNodeColor(
70 : node: MinimapNodeData,
71 : settings: MinimapRenderContext['settings'],
72 : colors: MinimapColors
73 : ): string {
74 39480 : if (settings.renderBypass && node.mode === LGraphEventMode.BYPASS) {
75 61 : return colors.bypassColor
76 : }
77 :
78 39480 : if (settings.nodeColors) {
79 9048 : if (node.bgcolor) {
80 25 : return colors.isLightTheme
81 : ? toLightThemeNodeColor(node.bgcolor)
82 : : node.bgcolor
83 : }
84 9023 : return colors.nodeColorDefault
85 : }
86 :
87 30371 : return colors.nodeColor
88 : }
89 :
90 9330 : function renderGroups(
91 : ctx: CanvasRenderingContext2D,
92 : dataSource: MinimapDataSource,
93 : offsetX: number,
94 : offsetY: number,
95 : context: MinimapRenderContext,
96 : colors: MinimapColors
97 : ) {
98 9330 : const groups = dataSource.getGroups()
99 9330 : if (groups.length === 0) return
100 :
101 175 : for (const group of groups) {
102 152 : const x = (group.x - context.bounds.minX) * context.scale + offsetX
103 152 : const y = (group.y - context.bounds.minY) * context.scale + offsetY
104 152 : const w = group.width * context.scale
105 152 : const h = group.height * context.scale
106 :
107 152 : let color = colors.groupColor
108 :
109 152 : if (context.settings.nodeColors) {
110 23 : color = group.color ?? colors.groupColorDefault
111 :
112 23 : if (colors.isLightTheme) {
113 0 : color = `color-mix(in srgb, ${color} 50%, transparent)`
114 : }
115 : }
116 :
117 152 : ctx.fillStyle = color
118 152 : ctx.fillRect(x, y, w, h)
119 : }
120 : }
121 :
122 9330 : function renderNodeDecorations(
123 : ctx: CanvasRenderingContext2D,
124 : nodes: readonly MinimapNodeData[],
125 : offsetX: number,
126 : offsetY: number,
127 : context: MinimapRenderContext,
128 : colors: MinimapColors
129 : ) {
130 9330 : const decorations = new Map(
131 : (context.decorations ?? []).map((decoration) => [
132 : decoration.target.nodeId,
133 : decoration
134 : ])
135 : )
136 9330 : if (decorations.size === 0) return
137 :
138 9330 : const now = context.now ?? performance.now()
139 9330 : ctx.fillStyle = colors.nodeColor
140 9330 : ctx.strokeStyle = colors.activityOutlineColor
141 9330 : ctx.lineWidth = 1
142 9330 : for (const node of nodes) {
143 0 : const decoration = decorations.get(node.id)
144 0 : if (!decoration) continue
145 0 : const elapsed = Math.max(0, now - (decoration.enteredAt ?? now))
146 : const progress =
147 : decoration.enter === 'pop'
148 : ? Math.min(1, elapsed / MINIMAP_DECORATION_POP_MS)
149 : : 1
150 0 : const eased = 1 - (1 - progress) ** 3
151 0 : const x = (node.x - context.bounds.minX) * context.scale + offsetX
152 0 : const y = (node.y - context.bounds.minY) * context.scale + offsetY
153 0 : const width = Math.max(2, node.width * context.scale * eased)
154 0 : const height = Math.max(2, node.height * context.scale * eased)
155 0 : const centerX = x + (node.width * context.scale) / 2
156 0 : const centerY = y + (node.height * context.scale) / 2
157 0 : ctx.fillRect(centerX - width / 2, centerY - height / 2, width, height)
158 0 : ctx.strokeRect(centerX - width / 2, centerY - height / 2, width, height)
159 : }
160 : }
161 :
162 9330 : function renderNodes(
163 : ctx: CanvasRenderingContext2D,
164 : dataSource: MinimapDataSource,
165 : offsetX: number,
166 : offsetY: number,
167 : context: MinimapRenderContext,
168 : colors: MinimapColors
169 : ) {
170 9330 : const nodes = dataSource.getNodes()
171 9330 : if (nodes.length === 0) return
172 :
173 9330 : ctx.save()
174 :
175 9330 : const nodesByColor = new Map<
176 : string,
177 : Array<
178 : CompactRect & {
179 : hasErrors?: boolean
180 : executionState?: MinimapNodeExecutionState | null
181 : }
182 : >
183 : >()
184 :
185 9330 : for (const node of nodes) {
186 39480 : const x = (node.x - context.bounds.minX) * context.scale + offsetX
187 39480 : const y = (node.y - context.bounds.minY) * context.scale + offsetY
188 39480 : const w = node.width * context.scale
189 39480 : const h = node.height * context.scale
190 :
191 39480 : const color = getNodeColor(node, context.settings, colors)
192 :
193 39480 : if (!nodesByColor.has(color)) {
194 9359 : nodesByColor.set(color, [])
195 : }
196 :
197 39480 : nodesByColor.get(color)!.push({
198 : x,
199 : y,
200 : w,
201 : h,
202 : hasErrors: node.hasErrors,
203 : executionState: node.executionState
204 : })
205 : }
206 :
207 9330 : for (const [color, nodes] of nodesByColor) {
208 9359 : ctx.fillStyle = color
209 9359 : for (const node of nodes) {
210 39480 : ctx.fillRect(node.x, node.y, node.w, node.h)
211 : }
212 : }
213 :
214 9330 : renderNodeDecorations(ctx, nodes, offsetX, offsetY, context, colors)
215 :
216 9330 : ctx.lineWidth = 0.3
217 9330 : for (const nodes of nodesByColor.values()) {
218 9359 : for (const node of nodes) {
219 39480 : if (node.hasErrors && context.settings.renderError) {
220 353 : ctx.strokeStyle = colors.errorColor
221 353 : ctx.strokeRect(node.x, node.y, node.w, node.h)
222 39127 : } else if (node.executionState === 'running') {
223 33 : ctx.strokeStyle = colors.runningColor
224 33 : ctx.strokeRect(node.x, node.y, node.w, node.h)
225 39094 : } else if (node.executionState === 'finished') {
226 34 : ctx.strokeStyle = colors.successColor
227 34 : ctx.strokeRect(node.x, node.y, node.w, node.h)
228 39060 : } else if (
229 : node.executionState === 'error' &&
230 : context.settings.renderError
231 : ) {
232 0 : ctx.strokeStyle = colors.errorColor
233 0 : ctx.strokeRect(node.x, node.y, node.w, node.h)
234 : }
235 : }
236 : }
237 :
238 9330 : ctx.restore()
239 : }
240 :
241 : /**
242 : * Render connections on the minimap
243 : */
244 7929 : function renderConnections(
245 : ctx: CanvasRenderingContext2D,
246 : dataSource: MinimapDataSource,
247 : offsetX: number,
248 : offsetY: number,
249 : context: MinimapRenderContext,
250 : colors: MinimapColors
251 : ) {
252 7929 : const links = dataSource.getLinks()
253 7929 : if (links.length === 0) return
254 :
255 3955 : ctx.strokeStyle = colors.linkColor
256 3955 : ctx.lineWidth = 0.3
257 :
258 3955 : const slotRadius = Math.max(context.scale, 0.5)
259 3955 : const connections: Array<{
260 : x1: number
261 : y1: number
262 : x2: number
263 : y2: number
264 : }> = []
265 :
266 7922 : for (const link of links) {
267 31943 : const x1 =
268 : (link.sourceNode.x - context.bounds.minX) * context.scale + offsetX
269 31943 : const y1 =
270 : (link.sourceNode.y - context.bounds.minY) * context.scale + offsetY
271 31943 : const x2 =
272 : (link.targetNode.x - context.bounds.minX) * context.scale + offsetX
273 31943 : const y2 =
274 : (link.targetNode.y - context.bounds.minY) * context.scale + offsetY
275 :
276 31943 : const outputX = x1 + link.sourceNode.width * context.scale
277 31943 : const outputY = y1 + link.sourceNode.height * context.scale * 0.2
278 31943 : const inputX = x2
279 31943 : const inputY = y2 + link.targetNode.height * context.scale * 0.2
280 :
281 : // Draw connection line
282 31943 : ctx.beginPath()
283 31943 : ctx.moveTo(outputX, outputY)
284 31943 : ctx.lineTo(inputX, inputY)
285 31943 : ctx.stroke()
286 :
287 31943 : connections.push({ x1: outputX, y1: outputY, x2: inputX, y2: inputY })
288 : }
289 :
290 : // Render connection slots on top
291 3955 : ctx.fillStyle = colors.slotColor
292 7854 : for (const conn of connections) {
293 : // Output slot
294 31943 : ctx.beginPath()
295 31943 : ctx.arc(conn.x1, conn.y1, slotRadius, 0, Math.PI * 2)
296 31943 : ctx.fill()
297 :
298 : // Input slot
299 31943 : ctx.beginPath()
300 31943 : ctx.arc(conn.x2, conn.y2, slotRadius, 0, Math.PI * 2)
301 31943 : ctx.fill()
302 : }
303 : }
304 :
305 : /**
306 : * Render a graph to a minimap canvas
307 : */
308 9330 : export function renderMinimapToCanvas(
309 : canvas: HTMLCanvasElement,
310 : graph: LGraph,
311 : context: MinimapRenderContext
312 : ) {
313 9330 : const ctx = canvas.getContext('2d')
314 9330 : if (!ctx) return
315 :
316 : // Clear canvas
317 9330 : ctx.clearRect(0, 0, context.width, context.height)
318 :
319 9330 : const dataSource = new MinimapDataSource(graph, context.nodeProgressStates)
320 :
321 : // Fast path for empty graph
322 9330 : if (!dataSource.hasData()) {
323 0 : return
324 : }
325 :
326 9330 : const colors = getMinimapColors()
327 9330 : const offsetX = (context.width - context.bounds.width * context.scale) / 2
328 9330 : const offsetY = (context.height - context.bounds.height * context.scale) / 2
329 :
330 : // Render in correct order: groups -> links -> nodes
331 9330 : if (context.settings.showGroups) {
332 9330 : renderGroups(ctx, dataSource, offsetX, offsetY, context, colors)
333 : }
334 :
335 9330 : if (context.settings.showLinks) {
336 7929 : renderConnections(ctx, dataSource, offsetX, offsetY, context, colors)
337 : }
338 :
339 9330 : renderNodes(ctx, dataSource, offsetX, offsetY, context, colors)
340 : }
|