SPB Git

Repository Public

Components.swift 9.1 KB 215 lines swift

1// ORIX Docs2// File: App/Sources/DesignSystem/Components.swift3// Description: Reusable ORIX controls: toolbar buttons, inspector sections and rows, numeric fields, colour menus, pills and empty states.4//5// Author: Simon-Pierre Boucher6// Contact: contact@spboucher.ai7import SwiftUI8import DocModel910/// A square, borderless toolbar button with hover and "on" states.11struct BarButton: View {12    let symbol: String13    let help: LocalizedStringKey14    var isOn = false15    var isEnabled = true16    let action: () -> Void17    @State private var hover = false1819    var body: some View {20        Button(action: action) {21            Image(systemName: symbol)22                .font(.system(size: 13, weight: .medium))23                .frame(width: 28, height: 26)24                .foregroundStyle(isOn ? ORIX.Palette.accent : ORIX.Palette.text.opacity(isEnabled ? 0.85 : 0.3))25                .background(RoundedRectangle(cornerRadius: ORIX.Radius.s).fill(isOn ? ORIX.Palette.accent.opacity(0.16) : (hover && isEnabled ? ORIX.Palette.text.opacity(0.07) : .clear)))26                .contentShape(Rectangle())27        }28        .buttonStyle(.plain)29        .disabled(!isEnabled)30        .onHover { hover = $0 }31        .help(help)32        .accessibilityLabel(Text(help))33        .accessibilityAddTraits(isOn ? .isSelected : [])34    }35}3637struct BarDivider: View {38    var body: some View { Rectangle().fill(ORIX.Palette.hairline).frame(width: 1, height: 18).padding(.horizontal, 4) }39}4041/// A titled group in an inspector.42struct InspectorSection<Content: View>: View {43    let title: LocalizedStringKey44    @ViewBuilder var content: Content45    var body: some View {46        VStack(alignment: .leading, spacing: ORIX.Space.s) {47            Text(title).font(ORIX.Font.sectionTitle).textCase(.uppercase).kerning(0.6).foregroundStyle(ORIX.Palette.textTertiary)48                .accessibilityAddTraits(.isHeader)49            content50        }51        .padding(.horizontal, ORIX.Space.l)52        .padding(.vertical, ORIX.Space.m)53        .frame(maxWidth: .infinity, alignment: .leading)54        .overlay(alignment: .bottom) { Rectangle().fill(ORIX.Palette.hairline.opacity(0.7)).frame(height: 1) }55    }56}5758/// Label on the left, control on the right.59struct InspectorRow<Content: View>: View {60    let label: LocalizedStringKey61    @ViewBuilder var content: Content62    var body: some View {63        HStack(spacing: ORIX.Space.s) {64            Text(label).font(ORIX.Font.label).foregroundStyle(ORIX.Palette.textSecondary).frame(minWidth: 84, alignment: .leading)65            Spacer(minLength: 0)66            content67        }68    }69}7071/// A compact numeric field with a stepper. Commits on Return or when focus leaves.72struct NumberField: View {73    let value: Double74    var range: ClosedRange<Double> = 0...10_00075    var step: Double = 176    var decimals = 177    var suffix = ""78    var width: CGFloat = 5879    let onCommit: (Double) -> Void80    @State private var text = ""81    @FocusState private var focused: Bool8283    private func format(_ v: Double) -> String {84        let f = NumberFormatter(); f.minimumFractionDigits = 0; f.maximumFractionDigits = decimals; f.usesGroupingSeparator = false85        return f.string(from: NSNumber(value: v)) ?? String(v)86    }87    private func commit() {88        let cleaned = text.replacingOccurrences(of: ",", with: ".").filter { $0.isNumber || $0 == "." || $0 == "-" }89        if let v = Double(cleaned) { onCommit(min(range.upperBound, max(range.lowerBound, v))) }90        text = format(value)91    }92    var body: some View {93        HStack(spacing: 2) {94            TextField("", text: $text)95                .textFieldStyle(.plain).multilineTextAlignment(.trailing).font(.system(size: 11.5).monospacedDigit())96                .frame(width: width - 26).focused($focused)97                .onSubmit { commit() }98                .onChange(of: focused) { _, f in if !f { commit() } }99            if !suffix.isEmpty { Text(suffix).font(.system(size: 10)).foregroundStyle(ORIX.Palette.textTertiary) }100            Stepper("", onIncrement: { onCommit(min(range.upperBound, value + step)) }, onDecrement: { onCommit(max(range.lowerBound, value - step)) })101                .labelsHidden().controlSize(.mini)102        }103        .padding(.leading, 6).padding(.vertical, 2)104        .background(RoundedRectangle(cornerRadius: ORIX.Radius.s).fill(ORIX.Palette.surface))105        .overlay(RoundedRectangle(cornerRadius: ORIX.Radius.s).strokeBorder(focused ? ORIX.Palette.accent : ORIX.Palette.hairline))106        .onAppear { text = format(value) }107        .onChange(of: value) { _, v in if !focused { text = format(v) } }108    }109}110111extension RGBA {112    var color: Color { Color(.sRGB, red: r, green: g, blue: b, opacity: a) }113}114115/// A swatch menu for text, highlight and fill colours.116struct ColorMenu: View {117    let symbol: String118    let help: LocalizedStringKey119    let current: RGBA?120    var allowsNone = false121    let onPick: (RGBA?) -> Void122123    static let inks: [(String, UInt32)] = [("Black", 0x16181D), ("Graphite", 0x4B5160), ("Slate", 0x8A8FA3), ("Indigo", 0x4F46E5), ("Blue", 0x2563EB), ("Teal", 0x0E7490),124                                           ("Green", 0x15803D), ("Amber", 0xB45309), ("Orange", 0xEA580C), ("Red", 0xBE123C), ("Purple", 0x7E22CE), ("Brown", 0x7C2D12)]125    static let highlights: [(String, UInt32)] = [("Yellow", 0xFFF1A8), ("Green", 0xC9F4D2), ("Blue", 0xCFE4FF), ("Pink", 0xFFD3E2), ("Purple", 0xE6D9FF), ("Orange", 0xFFDDB8), ("Grey", 0xE4E5EB)]126    var palette: [(String, UInt32)] = ColorMenu.inks127128    var body: some View {129        Menu {130            if allowsNone { Button("None") { onPick(nil) } }131            ForEach(palette, id: \.1) { name, hex in132                Button { onPick(RGBA(hex: hex)) } label: {133                    Label { Text(name) } icon: { Image(nsImage: ColorMenu.swatch(hex)) }134                }135            }136            Divider()137            Button("More Colours…") { ColorPanelBridge.shared.pick(onPick) }138        } label: {139            VStack(spacing: 1) {140                Image(systemName: symbol).font(.system(size: 12, weight: .medium))141                RoundedRectangle(cornerRadius: 1).fill(current?.color ?? ORIX.Palette.text.opacity(0.25)).frame(width: 14, height: 3)142            }143            .frame(width: 28, height: 26)144        }145        .menuStyle(.borderlessButton).menuIndicator(.hidden).fixedSize()146        .help(help).accessibilityLabel(Text(help))147    }148149    static func swatch(_ hex: UInt32) -> NSImage {150        let img = NSImage(size: NSSize(width: 14, height: 14), flipped: false) { rect in151            NSColor(hex: hex).setFill()152            NSBezierPath(roundedRect: rect.insetBy(dx: 1, dy: 1), xRadius: 3, yRadius: 3).fill()153            NSColor.black.withAlphaComponent(0.15).setStroke()154            NSBezierPath(roundedRect: rect.insetBy(dx: 1.5, dy: 1.5), xRadius: 3, yRadius: 3).stroke()155            return true156        }157        return img158    }159}160161/// Bridges the system colour panel to a callback.162final class ColorPanelBridge: NSObject {163    static let shared = ColorPanelBridge()164    private var handler: ((RGBA?) -> Void)?165    func pick(_ handler: @escaping (RGBA?) -> Void) {166        self.handler = handler167        let panel = NSColorPanel.shared168        panel.setTarget(self)169        panel.setAction(#selector(changed(_:)))170        panel.showsAlpha = false171        panel.orderFront(nil)172    }173    @objc private func changed(_ sender: NSColorPanel) {174        guard let c = sender.color.usingColorSpace(.sRGB) else { return }175        handler?(RGBA(r: Double(c.redComponent), g: Double(c.greenComponent), b: Double(c.blueComponent)))176    }177}178179struct Pill: View {180    let text: String181    var color: Color = ORIX.Palette.accent182    var body: some View {183        Text(text).font(.system(size: 10, weight: .semibold)).padding(.horizontal, 6).padding(.vertical, 2)184            .foregroundStyle(color).background(Capsule().fill(color.opacity(0.14)))185    }186}187188struct EmptyState: View {189    let symbol: String190    let title: LocalizedStringKey191    let message: LocalizedStringKey192    var body: some View {193        VStack(spacing: ORIX.Space.s) {194            Image(systemName: symbol).font(.system(size: 26, weight: .light)).foregroundStyle(ORIX.Palette.textTertiary)195            Text(title).font(.system(size: 12.5, weight: .semibold)).foregroundStyle(ORIX.Palette.text)196            Text(message).font(ORIX.Font.label).foregroundStyle(ORIX.Palette.textSecondary).multilineTextAlignment(.center)197        }198        .padding(ORIX.Space.xl).frame(maxWidth: .infinity, maxHeight: .infinity)199        .accessibilityElement(children: .combine)200    }201}202203/// A prominent ORIX action button.204struct ORIXButtonStyle: ButtonStyle {205    var prominent = true206    func makeBody(configuration: Configuration) -> some View {207        configuration.label208            .font(.system(size: 12, weight: .semibold))209            .padding(.horizontal, 12).padding(.vertical, 6)210            .foregroundStyle(prominent ? Color.white : ORIX.Palette.text)211            .background(RoundedRectangle(cornerRadius: ORIX.Radius.m).fill(prominent ? AnyShapeStyle(ORIX.Palette.accentGradient) : AnyShapeStyle(ORIX.Palette.text.opacity(0.08))))212            .opacity(configuration.isPressed ? 0.75 : 1)213    }214}215
Files main
Menu

Theme