Repository Public
1import AppKit2import SwiftUI3import SheetCore4import SheetCharts5import SheetUI67/// Draws a `ChartSpec` (literal data) with the SheetCharts engine. The single place where result charts are rendered.8public struct ResultChart: View {9 let chart: ChartBlock10 public init(_ chart: ChartBlock) { self.chart = chart }1112 public var body: some View {13 ChartCanvasView(spec: chart.spec)14 .frame(maxWidth: .infinity, minHeight: chart.height, maxHeight: chart.height)15 .accessibilityElement(children: .ignore)16 .accessibilityAddTraits(.isImage)17 .accessibilityLabel(chart.title)18 .accessibilityValue(chart.summary)19 }20}2122struct ChartCanvasView: NSViewRepresentable {23 let spec: ChartSpec24 func makeNSView(context: Context) -> ChartHostView { ChartHostView() }25 func updateNSView(_ view: ChartHostView, context: Context) {26 if view.spec != spec { view.spec = spec; view.needsDisplay = true }27 }28}2930final class ChartHostView: NSView {31 var spec: ChartSpec?32 override var isFlipped: Bool { true }33 override func viewDidChangeEffectiveAppearance() { needsDisplay = true }3435 override func draw(_ dirtyRect: NSRect) {36 guard var spec, let ctx = NSGraphicsContext.current?.cgContext, bounds.width > 20, bounds.height > 20 else { return }37 let dark = effectiveAppearance.bestMatch(from: [.darkAqua, .vibrantDark, .accessibilityHighContrastDarkAqua]) != nil38 spec.theme = ResultChartTheme.theme(dark: dark, base: spec.theme)39 ctx.saveGState()40 let canvas = CGChartCanvas(context: ctx, height: bounds.height, flipped: true)41 ChartRenderer.draw(spec, source: LiteralDataSource(), in: canvas, size: bounds.size)42 ctx.restoreGState()43 }44}4546public enum ResultChartTheme {47 /// The ORIX panel theme: transparent-looking background matching the card surface, compact type.48 public static func theme(dark: Bool, base: ChartTheme = .light) -> ChartTheme {49 var t = base.variant(dark: dark)50 t.background = dark ? RGBA(hex: 0x141828) : RGBA(hex: 0xFFFFFF)51 t.gridColor = dark ? RGBA(hex: 0x262C42) : RGBA(hex: 0xE6E8F0)52 t.textColor = dark ? RGBA(hex: 0xF2F4FB) : RGBA(hex: 0x0E1222)53 t.secondaryTextColor = dark ? RGBA(hex: 0xA3AAC2) : RGBA(hex: 0x596078)54 t.fonts = t.fonts.scaled(to: 10)55 return t56 }57}5859/// Table of a result with the formats it will have in the sheet.60public struct ReportTableView: View {61 let table: ReportTable62 public init(_ table: ReportTable) { self.table = table }6364 public var body: some View {65 let rows = table.displayRows()66 VStack(alignment: .leading, spacing: 6) {67 if let title = table.title { BlockTitle(title) }68 ScrollView(.horizontal, showsIndicators: rows.first.map { $0.count > 8 } ?? false) {69 Grid(alignment: .leading, horizontalSpacing: 0, verticalSpacing: 0) {70 ForEach(Array(rows.prefix(Self.maxRows).enumerated()), id: \.offset) { ri, row in71 GridRow {72 ForEach(Array(row.enumerated()), id: \.offset) { ci, text in cell(text, ri, ci) }73 }74 }75 }76 .clipShape(RoundedRectangle(cornerRadius: ORIX.Radius.control))77 .overlay(RoundedRectangle(cornerRadius: ORIX.Radius.control).strokeBorder(ORIX.Palette.hairline))78 }79 if rows.count > Self.maxRows {80 Text(L("Showing the first %d of %d rows; the report written to the sheet contains all of them.", Self.maxRows - table.headerRows, rows.count - table.headerRows))81 .font(.system(size: 11)).foregroundStyle(ORIX.Palette.textTertiary)82 }83 if let note = table.note { Text(note).font(.system(size: 11)).foregroundStyle(ORIX.Palette.textTertiary).fixedSize(horizontal: false, vertical: true) }84 }85 .accessibilityElement(children: .contain)86 .accessibilityLabel(table.title ?? L("Table"))87 }8889 static let maxRows = 619091 private func cell(_ text: String, _ ri: Int, _ ci: Int) -> some View {92 let header = ri < table.headerRows93 let label = ci == 0 && table.headerColumn94 let numeric: Bool = { if case .number = table.rows[ri][ci] { true } else { false } }()95 return Text(text)96 .font(header ? .system(size: 11.5, weight: .semibold) : (label ? .system(size: 12) : .system(size: 12).monospacedDigit()))97 .foregroundStyle(header ? ORIX.Palette.textSecondary : ORIX.Palette.textPrimary)98 .lineLimit(1)99 .padding(.horizontal, 10).padding(.vertical, 5)100 .frame(minWidth: label ? 120 : 64, maxWidth: .infinity, alignment: (numeric || (header && ci > 0)) ? .trailing : .leading)101 .background(header ? ORIX.Palette.surfaceRaised : (ri % 2 == 0 ? ORIX.Palette.surface : ORIX.Palette.canvas.opacity(0.55)))102 .overlay(alignment: .bottom) { Rectangle().fill(ORIX.Palette.hairline.opacity(0.6)).frame(height: 0.5) }103 .textSelection(.enabled)104 }105}106107struct BlockTitle: View {108 let text: String109 init(_ text: String) { self.text = text }110 var body: some View {111 Text(text).font(.system(size: 12.5, weight: .semibold)).foregroundStyle(ORIX.Palette.textPrimary)112 .accessibilityAddTraits(.isHeader)113 }114}115116/// Row of headline figures.117public struct MetricsView: View {118 let metrics: [Metric]119 public init(_ metrics: [Metric]) { self.metrics = metrics }120 public var body: some View {121 FlowLayout(spacing: 8) {122 ForEach(metrics) { m in123 VStack(alignment: .leading, spacing: 2) {124 Text(m.label).font(.system(size: 10.5, weight: .medium)).foregroundStyle(ORIX.Palette.textTertiary).lineLimit(1)125 Text(m.display()).font(.system(size: 15, weight: .semibold).monospacedDigit()).foregroundStyle(ORIX.Palette.textPrimary).lineLimit(1)126 .textSelection(.enabled)127 }128 .padding(.horizontal, 11).padding(.vertical, 7)129 .frame(minWidth: 96, alignment: .leading)130 .background(RoundedRectangle(cornerRadius: ORIX.Radius.control).fill(ORIX.Palette.surfaceRaised.opacity(0.7)))131 .accessibilityElement(children: .combine)132 }133 }134 }135}136137struct TextBlockView: View {138 let block: TextBlock139 var body: some View {140 let (icon, tint): (String, Color) = {141 switch block.kind {142 case .interpretation: ("text.quote", ORIX.Palette.accent)143 case .note: ("info.circle", ORIX.Palette.textTertiary)144 case .warning: ("exclamationmark.triangle.fill", ORIX.Palette.warning)145 case .assumptions: ("checklist", ORIX.Palette.textTertiary)146 }147 }()148 HStack(alignment: .firstTextBaseline, spacing: 8) {149 Image(systemName: icon).font(.system(size: 11.5, weight: .medium)).foregroundStyle(tint).frame(width: 16).accessibilityHidden(true)150 VStack(alignment: .leading, spacing: 3) {151 if let title = block.title {152 Text(title).font(.system(size: 11.5, weight: .semibold)).foregroundStyle(ORIX.Palette.textSecondary)153 }154 ForEach(Array(block.lines.enumerated()), id: \.offset) { _, line in155 Text(block.lines.count > 1 && block.kind != .interpretation ? "• " + line : line)156 .font(.system(size: block.kind == .interpretation ? 13 : 12, weight: block.kind == .interpretation ? .medium : .regular))157 .foregroundStyle(block.kind == .interpretation || block.kind == .warning ? ORIX.Palette.textPrimary : ORIX.Palette.textSecondary)158 .fixedSize(horizontal: false, vertical: true)159 .textSelection(.enabled)160 }161 }162 Spacer(minLength: 0)163 }164 .padding(.horizontal, 10).padding(.vertical, 8)165 .background(RoundedRectangle(cornerRadius: ORIX.Radius.control).fill(block.kind == .interpretation ? ORIX.Palette.accent.opacity(0.08)166 : (block.kind == .warning ? ORIX.Palette.warning.opacity(0.10) : Color.clear)))167 .accessibilityElement(children: .combine)168 }169}170171/// A chart with its title and the "Insert into sheet" command.172struct ChartBlockView: View {173 let chart: ChartBlock174 let insert: (() -> Void)?175 var body: some View {176 VStack(alignment: .leading, spacing: 4) {177 HStack(spacing: 6) {178 BlockTitle(chart.title)179 Spacer(minLength: 4)180 if let insert {181 Button(action: insert) { Label(L("Insert Chart into Sheet"), systemImage: "chart.xyaxis.line") }182 .labelStyle(.iconOnly).buttonStyle(.borderless).controlSize(.small)183 .help(L("Insert Chart into Sheet"))184 .accessibilityLabel(L("Insert chart “%@” into the sheet", chart.title))185 }186 }187 ResultChart(chart)188 }189 .padding(10)190 .background(RoundedRectangle(cornerRadius: ORIX.Radius.control).fill(ORIX.Palette.surface))191 .overlay(RoundedRectangle(cornerRadius: ORIX.Radius.control).strokeBorder(ORIX.Palette.hairline))192 }193}194195/// All the blocks of a result, in order.196public struct ResultBlocksView: View {197 let result: ToolResult198 /// Called with a chart when the user asks to insert it into the sheet.199 var insertChart: ((ChartBlock) -> Void)?200 @Environment(\.compactForm) private var compact201202 public init(_ result: ToolResult, insertChart: ((ChartBlock) -> Void)? = nil) { self.result = result; self.insertChart = insertChart }203204 public var body: some View {205 VStack(alignment: .leading, spacing: 12) {206 ForEach(Array(result.blocks.enumerated()), id: \.offset) { _, block in207 switch block {208 case .metrics(let m): MetricsView(m)209 case .table(let t): ReportTableView(t)210 case .text(let t): TextBlockView(block: t)211 case .chart(let c): ChartBlockView(chart: c, insert: insertChart.map { f in { f(c) } })212 case .charts(let cs):213 let columns = compact ? 1 : min(cs.count, 2)214 LazyVGrid(columns: Array(repeating: GridItem(.flexible(minimum: 220), spacing: 10), count: max(columns, 1)), spacing: 10) {215 ForEach(Array(cs.enumerated()), id: \.offset) { _, c in ChartBlockView(chart: c, insert: insertChart.map { f in { f(c) } }) }216 }217 }218 }219 }220 }221}222