Files
byre/mac/Sources/Byre/Diagram.swift
T

37 lines
1.2 KiB
Swift
Raw Normal View History

2026-10-16 23:30:57 -07:00
import AppKit
import CoreText
import SwiftUI
struct DiagramError: Error {
let reason: String
}
func textSize(_ text: String, _ font: CTFont) -> CGSize {
let lines = text.components(separatedBy: "\n")
var width: CGFloat = 0
for l in lines {
let line = CTLineCreateWithAttributedString(NSAttributedString(string: l, attributes: [.font: font as NSFont]))
width = max(width, CGFloat(CTLineGetTypographicBounds(line, nil, nil, nil)))
}
let lineHeight = CTFontGetAscent(font) + CTFontGetDescent(font) + CTFontGetLeading(font) + 3
return CGSize(width: ceil(width), height: ceil(lineHeight * CGFloat(lines.count)))
}
struct DiagramFailure: View {
let source: String
let reason: String
var body: some View {
VStack(alignment: .leading, spacing: 4) {
Text(verbatim: source)
.textSelection(.enabled)
.padding(.horizontal, 8)
.padding(.vertical, 6)
.frame(maxWidth: .infinity, alignment: .leading)
.background(Palette.surface, in: RoundedRectangle(cornerRadius: 6))
Text(verbatim: "diagram not rendered: \(reason)")
.foregroundStyle(Palette.muted)
2026-10-16 23:30:57 -07:00
}
}
}