Repository Public License
1// ORIX Clean — App/Sources/DesignSystem/Components.swift2// Description: Reusable ORIX components: cards, badges, size labels, bars, empty states, file rows.3// Author: Simon-Pierre Boucher — contact@spboucher.ai4import AppKit5import ORIXCleanCore6import QuickLookThumbnailing7import SwiftUI89struct Card<Content: View>: View {10 var padding: CGFloat = ORIX.Space.l11 @ViewBuilder var content: Content12 var body: some View {13 content14 .padding(padding)15 .frame(maxWidth: .infinity, alignment: .leading)16 .background(ORIX.Palette.surface, in: RoundedRectangle(cornerRadius: ORIX.Radius.l, style: .continuous))17 .overlay(RoundedRectangle(cornerRadius: ORIX.Radius.l, style: .continuous).strokeBorder(ORIX.Palette.hairline, lineWidth: 1))18 }19}2021struct SectionTitle: View {22 var title: String23 var subtitle: String?24 var body: some View {25 VStack(alignment: .leading, spacing: 2) {26 Text(title).font(.title3.weight(.semibold))27 if let subtitle { Text(subtitle).font(.callout).foregroundStyle(ORIX.Palette.textSecondary).fixedSize(horizontal: false, vertical: true) }28 }29 .accessibilityElement(children: .combine)30 .accessibilityAddTraits(.isHeader)31 }32}3334struct RiskBadge: View {35 var risk: Risk36 var body: some View {37 Label(risk.title, systemImage: risk.symbol)38 .font(.caption.weight(.medium))39 .foregroundStyle(risk.color)40 .padding(.horizontal, 7).padding(.vertical, 2)41 .background(risk.color.opacity(0.13), in: Capsule())42 .accessibilityLabel(risk.title)43 }44}4546struct SizeText: View {47 var bytes: Int6448 var weight: Font.Weight = .medium49 var body: some View {50 Text(ByteFormat.string(bytes)).font(.body.monospacedDigit().weight(weight))51 }52}5354/// Proportional bar. The value is always also given as text next to it.55struct ProportionBar: View {56 var fraction: Double57 var color: Color = ORIX.Palette.accent58 var height: CGFloat = 659 var body: some View {60 GeometryReader { geo in61 ZStack(alignment: .leading) {62 Capsule().fill(ORIX.Palette.hairline)63 Capsule().fill(color).frame(width: max(2, geo.size.width * min(1, max(0, fraction))))64 }65 }66 .frame(height: height)67 .accessibilityHidden(true)68 }69}7071struct EmptyState: View {72 var symbol: String73 var title: String74 var message: String75 var actionTitle: String?76 var action: (() -> Void)?77 var body: some View {78 VStack(spacing: ORIX.Space.m) {79 Image(systemName: symbol).font(.system(size: 40, weight: .light)).foregroundStyle(ORIX.Palette.accent).accessibilityHidden(true)80 Text(title).font(.title3.weight(.semibold))81 Text(message).font(.callout).foregroundStyle(ORIX.Palette.textSecondary).multilineTextAlignment(.center).frame(maxWidth: 460)82 if let actionTitle, let action {83 Button(actionTitle, action: action).buttonStyle(.borderedProminent).controlSize(.large).padding(.top, 4)84 }85 }86 .padding(ORIX.Space.xxl)87 .frame(maxWidth: .infinity, maxHeight: .infinity)88 }89}9091/// A banner that states a fact and offers one action. Never styled as an alarm.92struct InfoBanner: View {93 var symbol: String = "info.circle"94 var text: String95 var actionTitle: String?96 var action: (() -> Void)?97 var body: some View {98 HStack(alignment: .firstTextBaseline, spacing: ORIX.Space.s) {99 Image(systemName: symbol).foregroundStyle(ORIX.Palette.accentSecondary).accessibilityHidden(true)100 Text(text).font(.callout).foregroundStyle(ORIX.Palette.textSecondary).fixedSize(horizontal: false, vertical: true)101 Spacer(minLength: ORIX.Space.s)102 if let actionTitle, let action { Button(actionTitle, action: action).controlSize(.small) }103 }104 .padding(ORIX.Space.m)105 .background(ORIX.Palette.accentSecondary.opacity(0.08), in: RoundedRectangle(cornerRadius: ORIX.Radius.m, style: .continuous))106 }107}108109struct ProgressLine: View {110 var title: String111 var detail: String112 var fraction: Double?113 var onCancel: (() -> Void)?114 var body: some View {115 HStack(spacing: ORIX.Space.m) {116 VStack(alignment: .leading, spacing: 4) {117 Text(title).font(.callout.weight(.medium))118 if let fraction { ProgressView(value: min(1, max(0, fraction))).progressViewStyle(.linear) }119 else { ProgressView().progressViewStyle(.linear) }120 Text(detail).font(.caption.monospacedDigit()).foregroundStyle(ORIX.Palette.textSecondary).lineLimit(1).truncationMode(.middle)121 }122 if let onCancel { Button(L("Cancel"), action: onCancel) }123 }124 .padding(ORIX.Space.m)125 .background(ORIX.Palette.surface, in: RoundedRectangle(cornerRadius: ORIX.Radius.m, style: .continuous))126 .overlay(RoundedRectangle(cornerRadius: ORIX.Radius.m, style: .continuous).strokeBorder(ORIX.Palette.hairline))127 }128}129130/// Finder icon of a file, loaded lazily.131struct FileIcon: View {132 var path: String133 var size: CGFloat = 20134 var body: some View {135 Image(nsImage: NSWorkspace.shared.icon(forFile: path))136 .resizable().interpolation(.high).frame(width: size, height: size).accessibilityHidden(true)137 }138}139140/// Quick Look thumbnail of a file (images, documents), with the Finder icon as fallback.141struct Thumbnail: View {142 var path: String143 var side: CGFloat144 @State private var image: NSImage?145 var body: some View {146 ZStack {147 if let image {148 Image(nsImage: image).resizable().aspectRatio(contentMode: .fit)149 } else {150 FileIcon(path: path, size: min(side, 64))151 }152 }153 .frame(width: side, height: side)154 .background(ORIX.Palette.canvas, in: RoundedRectangle(cornerRadius: ORIX.Radius.s))155 .clipShape(RoundedRectangle(cornerRadius: ORIX.Radius.s))156 .task(id: path) {157 let request = QLThumbnailGenerator.Request(fileAt: URL(fileURLWithPath: path), size: CGSize(width: side, height: side),158 scale: NSScreen.main?.backingScaleFactor ?? 2, representationTypes: .thumbnail)159 image = try? await QLThumbnailGenerator.shared.generateBestRepresentation(for: request).nsImage160 }161 .accessibilityHidden(true)162 }163}164165struct PathText: View {166 var path: String167 var body: some View {168 Text(((path as NSString).deletingLastPathComponent).abbreviatingHome)169 .font(.caption).foregroundStyle(ORIX.Palette.textSecondary).lineLimit(1).truncationMode(.middle)170 .help(path)171 }172}173174/// Standard context menu for anything that is a file on disk.175struct FileMenu: View {176 var path: String177 var body: some View {178 Button(L("Reveal in Finder")) { NSWorkspace.shared.activateFileViewerSelecting([URL(fileURLWithPath: path)]) }179 Button(L("Quick Look")) { QuickLook.show([path]) }180 Button(L("Copy Path")) {181 NSPasteboard.general.clearContents()182 NSPasteboard.general.setString(path, forType: .string)183 }184 }185}186187struct Checkbox: View {188 var isOn: Bool189 var label: String190 var toggle: () -> Void191 var body: some View {192 Button(action: toggle) {193 Image(systemName: isOn ? "checkmark.square.fill" : "square")194 .font(.system(size: 15)).foregroundStyle(isOn ? ORIX.Palette.accent : ORIX.Palette.textTertiary)195 }196 .buttonStyle(.plain)197 .accessibilityLabel(label)198 .accessibilityValue(isOn ? L("Selected") : L("Not selected"))199 .accessibilityAddTraits(.isToggle)200 }201}202203struct StatTile: View {204 var title: String205 var value: String206 var detail: String?207 var body: some View {208 VStack(alignment: .leading, spacing: 2) {209 Text(title).font(.caption).foregroundStyle(ORIX.Palette.textSecondary)210 Text(value).font(.title2.weight(.semibold).monospacedDigit()).lineLimit(1).minimumScaleFactor(0.7)211 if let detail { Text(detail).font(.caption).foregroundStyle(ORIX.Palette.textTertiary).lineLimit(2) }212 }213 .frame(maxWidth: .infinity, alignment: .leading)214 .accessibilityElement(children: .combine)215 }216}217