Repository Public
1// ORIX Docs2// File: App/Sources/DesignSystem/SheetComponents.swift3// Description: Reusable ORIX building blocks for sheets and tool panels: the sheet scaffold (header, content, default and cancel4// buttons), field labels, wrapping chip rows, notices, collapsible groups and formatted-run text.5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8import SwiftUI9import AppKit10import DocModel1112/// The common frame of every ORIX sheet: branded header, content, and a footer whose buttons answer Return and Escape.13struct SheetScaffold<Content: View, Accessory: View>: View {14 let title: LocalizedStringKey15 let symbol: String16 var subtitle: LocalizedStringKey?17 var width: CGFloat18 let primaryTitle: LocalizedStringKey19 var primaryEnabled: Bool20 let onPrimary: () -> Void21 let onCancel: () -> Void22 let accessory: Accessory23 let content: Content2425 init(title: LocalizedStringKey, symbol: String, subtitle: LocalizedStringKey? = nil, width: CGFloat = 520,26 primaryTitle: LocalizedStringKey, primaryEnabled: Bool = true, onPrimary: @escaping () -> Void, onCancel: @escaping () -> Void,27 @ViewBuilder accessory: () -> Accessory, @ViewBuilder content: () -> Content) {28 self.title = title; self.symbol = symbol; self.subtitle = subtitle; self.width = width29 self.primaryTitle = primaryTitle; self.primaryEnabled = primaryEnabled; self.onPrimary = onPrimary; self.onCancel = onCancel30 self.accessory = accessory(); self.content = content()31 }3233 var body: some View {34 VStack(spacing: 0) {35 HStack(spacing: ORIX.Space.m) {36 Image(systemName: symbol)37 .font(.system(size: 14, weight: .semibold)).foregroundStyle(.white)38 .frame(width: 30, height: 30)39 .background(RoundedRectangle(cornerRadius: ORIX.Radius.m).fill(ORIX.Palette.accentGradient))40 .accessibilityHidden(true)41 VStack(alignment: .leading, spacing: 1) {42 Text(title).font(ORIX.Font.title).foregroundStyle(ORIX.Palette.text).accessibilityAddTraits(.isHeader)43 if let subtitle { Text(subtitle).font(ORIX.Font.label).foregroundStyle(ORIX.Palette.textSecondary).fixedSize(horizontal: false, vertical: true) }44 }45 Spacer(minLength: 0)46 }47 .padding(.horizontal, ORIX.Space.xl).padding(.top, ORIX.Space.l + 2).padding(.bottom, ORIX.Space.m)48 Rectangle().fill(ORIX.Palette.hairline).frame(height: 1)49 content50 .padding(.horizontal, ORIX.Space.xl).padding(.vertical, ORIX.Space.l)51 .frame(maxWidth: .infinity, alignment: .leading)52 Rectangle().fill(ORIX.Palette.hairline).frame(height: 1)53 HStack(spacing: ORIX.Space.s) {54 accessory55 Spacer(minLength: ORIX.Space.m)56 Button("Cancel", action: onCancel)57 .buttonStyle(ORIXButtonStyle(prominent: false))58 .keyboardShortcut(.cancelAction)59 .accessibilityIdentifier("sheet-cancel")60 Button(primaryTitle, action: onPrimary)61 .buttonStyle(ORIXButtonStyle())62 .keyboardShortcut(.defaultAction)63 .disabled(!primaryEnabled)64 .opacity(primaryEnabled ? 1 : 0.45)65 .accessibilityIdentifier("sheet-primary")66 }67 .padding(.horizontal, ORIX.Space.xl).padding(.vertical, ORIX.Space.m)68 .background(ORIX.Palette.chrome)69 }70 .frame(width: width)71 .background(ORIX.Palette.surface)72 }73}7475extension SheetScaffold where Accessory == EmptyView {76 init(title: LocalizedStringKey, symbol: String, subtitle: LocalizedStringKey? = nil, width: CGFloat = 520,77 primaryTitle: LocalizedStringKey, primaryEnabled: Bool = true, onPrimary: @escaping () -> Void, onCancel: @escaping () -> Void,78 @ViewBuilder content: () -> Content) {79 self.init(title: title, symbol: symbol, subtitle: subtitle, width: width, primaryTitle: primaryTitle, primaryEnabled: primaryEnabled,80 onPrimary: onPrimary, onCancel: onCancel, accessory: { EmptyView() }, content: content)81 }82}8384/// A small caption above a field.85struct FieldLabel: View {86 let text: LocalizedStringKey87 init(_ text: LocalizedStringKey) { self.text = text }88 var body: some View {89 Text(text).font(ORIX.Font.caption).foregroundStyle(ORIX.Palette.textSecondary)90 }91}9293/// A labelled field stacked vertically.94struct LabeledField<Content: View>: View {95 let label: LocalizedStringKey96 @ViewBuilder var content: Content97 var body: some View {98 VStack(alignment: .leading, spacing: 3) {99 FieldLabel(label)100 content101 }102 }103}104105/// A rounded panel on the sheet surface (previews, lists).106struct WellBackground: ViewModifier {107 var radius: CGFloat = ORIX.Radius.m108 func body(content: Content) -> some View {109 content110 .background(RoundedRectangle(cornerRadius: radius).fill(ORIX.Palette.chrome))111 .overlay(RoundedRectangle(cornerRadius: radius).strokeBorder(ORIX.Palette.hairline))112 .clipShape(RoundedRectangle(cornerRadius: radius))113 }114}115116extension View {117 func well(radius: CGFloat = ORIX.Radius.m) -> some View { modifier(WellBackground(radius: radius)) }118}119120/// A message with an icon: information, warning or error. The icon and the wording carry the meaning, not the colour alone.121struct Notice: View {122 enum Kind { case info, warning, error, success }123 let kind: Kind124 let text: String125 var body: some View {126 HStack(alignment: .firstTextBaseline, spacing: 6) {127 Image(systemName: symbol).font(.system(size: 11, weight: .semibold)).foregroundStyle(color).accessibilityHidden(true)128 Text(text).font(ORIX.Font.label).foregroundStyle(ORIX.Palette.text).fixedSize(horizontal: false, vertical: true).textSelection(.enabled)129 Spacer(minLength: 0)130 }131 .padding(.horizontal, 10).padding(.vertical, 7)132 .background(RoundedRectangle(cornerRadius: ORIX.Radius.s).fill(color.opacity(0.10)))133 .accessibilityElement(children: .combine)134 .accessibilityLabel(Text(verbatim: "\(spoken): \(text)"))135 }136 private var symbol: String {137 switch kind { case .info: return "info.circle.fill"; case .warning: return "exclamationmark.triangle.fill"; case .error: return "xmark.octagon.fill"; case .success: return "checkmark.circle.fill" }138 }139 private var color: Color {140 switch kind { case .info: return ORIX.Palette.accent; case .warning: return ORIX.Palette.warning; case .error: return ORIX.Palette.critical; case .success: return ORIX.Palette.positive }141 }142 private var spoken: String {143 switch kind {144 case .info: return String(localized: "Information"); case .warning: return String(localized: "Warning")145 case .error: return String(localized: "Error"); case .success: return String(localized: "Done")146 }147 }148}149150/// Lays children out left to right and wraps them onto new lines.151struct FlowLayout: Layout {152 var spacing: CGFloat = 4153 var lineSpacing: CGFloat = 4154155 private func arrange(_ subviews: Subviews, width: CGFloat) -> (size: CGSize, frames: [CGRect]) {156 var frames: [CGRect] = []157 var x: CGFloat = 0, y: CGFloat = 0, lineHeight: CGFloat = 0, maxX: CGFloat = 0158 for s in subviews {159 let size = s.sizeThatFits(ProposedViewSize(width: width, height: nil))160 if x > 0, x + size.width > width { x = 0; y += lineHeight + lineSpacing; lineHeight = 0 }161 frames.append(CGRect(origin: CGPoint(x: x, y: y), size: size))162 x += size.width + spacing163 lineHeight = max(lineHeight, size.height)164 maxX = max(maxX, x - spacing)165 }166 return (CGSize(width: maxX, height: y + lineHeight), frames)167 }168169 func sizeThatFits(proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) -> CGSize {170 arrange(subviews, width: proposal.width ?? .greatestFiniteMagnitude).size171 }172173 func placeSubviews(in bounds: CGRect, proposal: ProposedViewSize, subviews: Subviews, cache: inout ()) {174 let frames = arrange(subviews, width: bounds.width).frames175 for (s, f) in zip(subviews, frames) {176 s.place(at: CGPoint(x: bounds.minX + f.minX, y: bounds.minY + f.minY), proposal: ProposedViewSize(f.size))177 }178 }179}180181/// A neutral chip (feature badges, keys, counts).182struct Chip: View {183 let text: String184 var symbol: String?185 var color: Color = ORIX.Palette.textSecondary186 var body: some View {187 HStack(spacing: 3) {188 if let symbol { Image(systemName: symbol).font(.system(size: 8.5, weight: .semibold)).accessibilityHidden(true) }189 Text(text).font(.system(size: 9.5, weight: .medium)).lineLimit(1)190 }191 .padding(.horizontal, 6).padding(.vertical, 2)192 .foregroundStyle(color)193 .background(Capsule().fill(color.opacity(0.11)))194 }195}196197/// A collapsible group with a title, a count and optional trailing content, used in the tool panels.198struct CollapsibleGroup<Content: View>: View {199 let title: String200 var symbol: String?201 var tint: Color = ORIX.Palette.textSecondary202 var count: Int?203 @Binding var expanded: Bool204 @ViewBuilder var content: Content205206 var body: some View {207 VStack(alignment: .leading, spacing: 0) {208 Button { withAnimation(ORIX.Motion.quick) { expanded.toggle() } } label: {209 HStack(spacing: 6) {210 Image(systemName: "chevron.right").font(.system(size: 8, weight: .bold)).foregroundStyle(ORIX.Palette.textTertiary)211 .rotationEffect(.degrees(expanded ? 90 : 0)).frame(width: 10)212 if let symbol { Image(systemName: symbol).font(.system(size: 10.5, weight: .semibold)).foregroundStyle(tint).frame(width: 14) }213 Text(title).font(.system(size: 11.5, weight: .semibold)).foregroundStyle(ORIX.Palette.text).lineLimit(2).multilineTextAlignment(.leading)214 Spacer(minLength: 4)215 if let count { Text("\(count)").font(.system(size: 10.5, weight: .semibold).monospacedDigit()).foregroundStyle(ORIX.Palette.textSecondary) }216 }217 .padding(.vertical, 5)218 .contentShape(Rectangle())219 }220 .buttonStyle(.plain)221 .accessibilityLabel(Text(verbatim: count.map { "\(title), \($0)" } ?? title))222 .accessibilityValue(expanded ? Text("Expanded") : Text("Collapsed"))223 if expanded { content.padding(.leading, 16).padding(.bottom, 6) }224 }225 }226}227228/// A full-width secondary button for tool panels.229struct PanelButton: View {230 let title: LocalizedStringKey231 let symbol: String232 var isEnabled = true233 let action: () -> Void234 @State private var hover = false235 var body: some View {236 Button(action: action) {237 HStack(spacing: 6) {238 Image(systemName: symbol).font(.system(size: 11, weight: .medium)).foregroundStyle(ORIX.Palette.accent).frame(width: 16)239 Text(title).font(ORIX.Font.label).foregroundStyle(ORIX.Palette.text).lineLimit(1).minimumScaleFactor(0.85)240 Spacer(minLength: 0)241 }242 .padding(.horizontal, 8).padding(.vertical, 6)243 .background(RoundedRectangle(cornerRadius: ORIX.Radius.s).fill(ORIX.Palette.text.opacity(hover && isEnabled ? 0.10 : 0.05)))244 .contentShape(Rectangle())245 }246 .buttonStyle(.plain)247 .disabled(!isEnabled)248 .opacity(isEnabled ? 1 : 0.4)249 .onHover { hover = $0 }250 }251}252253extension Array where Element == Run {254 /// The runs as attributed text, keeping italics and bold (formatted bibliography entries).255 var attributedText: AttributedString {256 var out = AttributedString()257 for r in self {258 var a = AttributedString(r.text)259 var intent: InlinePresentationIntent = []260 if r.props.italic == true { intent.insert(.emphasized) }261 if r.props.bold == true { intent.insert(.stronglyEmphasized) }262 if !intent.isEmpty { a.inlinePresentationIntent = intent }263 out += a264 }265 return out266 }267}268