Skip to content

Commit 3e42571

Browse files
committed
fix(mermaid): improve PDF render timing for complex diagrams
1 parent 2c2d714 commit 3e42571

1 file changed

Lines changed: 18 additions & 4 deletions

File tree

Sources/UserInterface/Chat/Mermaid/MermaidImageRenderer.swift

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,27 @@ struct MermaidImageRenderer {
3333

3434
// Create NSHostingView to convert SwiftUI to NSView
3535
let hostingView = NSHostingView(rootView: containerView)
36-
hostingView.frame = CGRect(x: 0, y: 0, width: width + 40, height: 800)
36+
hostingView.frame = CGRect(x: 0, y: 0, width: width + 40, height: 1500) // Generous initial height
3737

38-
// Force layout multiple times to ensure SwiftUI renders
39-
for _ in 0..<3 {
38+
// Force layout with longer delays for complex diagrams
39+
// Complex flowcharts need time for edge routing and obstacle avoidance calculations
40+
for cycle in 0..<5 {
4041
hostingView.layout()
4142
hostingView.layoutSubtreeIfNeeded()
42-
RunLoop.current.run(until: Date(timeIntervalSinceNow: 0.1))
43+
44+
// Longer delay for complex layouts (0.15s vs 0.1s)
45+
RunLoop.current.run(until: Date(timeIntervalSinceNow: 0.15))
46+
47+
// Check if rendering is stabilizing (fittingSize not changing much)
48+
let currentSize = hostingView.fittingSize
49+
if cycle > 2 && currentSize.height > 100 {
50+
// If we have a reasonable height after 3 cycles, we're probably done
51+
// One more cycle to be sure
52+
hostingView.layout()
53+
hostingView.layoutSubtreeIfNeeded()
54+
RunLoop.current.run(until: Date(timeIntervalSinceNow: 0.15))
55+
break
56+
}
4357
}
4458

4559
// Calculate actual needed height after layout

0 commit comments

Comments
 (0)