SPB Git

Repository Public

InlineEditing.swift 7.6 KB 161 lines swift

1// Author: Simon-Pierre Boucher — contact@spboucher.ai2// File: Components/InlineEditing.swift3// Description: Small building blocks shared by Portfolios and Research Studio: a text field that edits a model value in place (commit on Return or when focus leaves), hairlines, quiet chips, list rows that select by their own action, and plain number text.4import SwiftUI5import VQuantCore6import VQuantKit7import VQuantUI89/// A cell that shows a value of the model and edits it in place. The text is committed on Return and when focus10/// leaves; a refused text is marked, then put back to the model's value when focus leaves.11struct InlineField: View {12    let value: String13    var placeholder = ""14    var alignment: TextAlignment = .leading15    var role: VQTokens.Typography.Role = VQTokens.Typography.bodySm16    /// Replaces the role's font (a size the type scale does not have).17    var font: Font?18    var color: Color = VQTokens.Palette.text119    var identifier: String?20    var axis: Axis = .horizontal21    /// Returns false when the text is refused (the caller explains why).22    let commit: (String) -> Bool2324    @State private var text = ""25    @State private var invalid = false26    @State private var hovering = false27    @FocusState private var focused: Bool2829    var body: some View {30        TextField(placeholder, text: $text, axis: axis)31            .textFieldStyle(.plain)32            .multilineTextAlignment(alignment)33            .font(font ?? role.font).tracking(font == nil ? role.tracking : 0)34            .foregroundStyle(color)35            .focused($focused)36            .padding(.horizontal, 6).padding(.vertical, 3)37            .background(RoundedRectangle(cornerRadius: 5, style: .continuous).fill(focused ? VQTokens.Palette.ink2 : hovering ? VQTokens.Palette.ink3.opacity(0.6) : .clear))38            .overlay(RoundedRectangle(cornerRadius: 5, style: .continuous).strokeBorder(invalid ? VQTokens.Palette.critical : focused ? VQTokens.Palette.accent.opacity(0.7) : .clear, lineWidth: 1))39            .onHover { hovering = $0 }40            .onAppear { text = value }41            // the model changed (another device, an undo): follow it unless the person is in the middle of typing something else42            .onChange(of: value) { old, v in if !focused || text == old { text = v } }43            .onChange(of: focused) { _, f in if !f { submit(); if invalid { text = value; invalid = false } } }44            .onSubmit { submit() }45            .accessibilityIdentifier(identifier ?? "")46    }4748    private func submit() {49        guard text != value else { invalid = false; return }50        invalid = !commit(text)51    }52}5354struct Hairline: View {55    var strong = false56    var body: some View { Rectangle().fill(strong ? VQTokens.Palette.lineStrong : VQTokens.Palette.line).frame(height: VQTokens.hairline).accessibilityHidden(true) }57}5859struct VHairline: View {60    var body: some View { Rectangle().fill(VQTokens.Palette.line).frame(width: VQTokens.hairline).accessibilityHidden(true) }61}6263/// A quiet label in a hairline capsule: a state, a count, a kind.64struct QuietChip: View {65    let text: String66    var systemImage: String?67    var tint: Color = VQTokens.Palette.text268    var body: some View {69        HStack(spacing: 4) {70            if let systemImage { Image(systemName: systemImage).font(.system(size: 9, weight: .semibold)) }71            Text(text).font(VQTokens.Typography.caption.font).lineLimit(1)72        }73        .foregroundStyle(tint)74        .padding(.horizontal, 7).padding(.vertical, 2)75        .overlay(Capsule().strokeBorder(tint.opacity(0.35), lineWidth: VQTokens.hairline))76        .fixedSize()77    }78}7980/// Small capitals above a block.81struct Overline: View {82    let text: String83    var color: Color = VQTokens.Palette.text384    init(_ text: String, color: Color = VQTokens.Palette.text3) { self.text = text; self.color = color }85    var body: some View {86        Text(text.uppercased()).font(VQTokens.Typography.overline.font).tracking(VQTokens.Typography.overline.tracking).foregroundStyle(color).lineLimit(1)87    }88}8990/// The mark of an object whose last change has not been accepted by the server yet.91struct PendingSyncMark: View {92    var body: some View {93        Image(systemName: "arrow.triangle.2.circlepath").font(.system(size: 10, weight: .semibold)).foregroundStyle(VQTokens.Palette.warn)94            .help("Saved on this Mac, waiting to sync with your account")95            .accessibilityLabel("Waiting to sync")96    }97}9899/// A row of a master list (portfolios, projects, notes) that selects by its own action (macOS 27: `List(selection:)`100/// does not select). Ink surface when selected, Ember rule on the leading edge.101struct MasterRow<Content: View>: View {102    let isSelected: Bool103    let action: () -> Void104    @ViewBuilder var content: Content105    @State private var hovering = false106107    var body: some View {108        Button(action: action) {109            content110                .padding(.horizontal, VQTokens.Space.s3).padding(.vertical, VQTokens.Space.s2)111                .frame(maxWidth: .infinity, alignment: .leading)112                .background(RoundedRectangle(cornerRadius: VQTokens.Radius.sm, style: .continuous).fill(isSelected ? VQTokens.Palette.ink3 : hovering ? VQTokens.Palette.ink3.opacity(0.45) : .clear))113                .overlay(alignment: .leading) { if isSelected { RoundedRectangle(cornerRadius: 1).fill(VQTokens.Palette.accent).frame(width: 2).padding(.vertical, 6) } }114                .contentShape(Rectangle())115        }116        .buttonStyle(.plain)117        .onHover { hovering = $0 }118        .accessibilityAddTraits(isSelected ? [.isSelected, .isButton] : .isButton)119    }120}121122/// The primary action of a pane: Ember, used once per screen.123struct EmberButtonStyle: ButtonStyle {124    @Environment(\.isEnabled) private var enabled125    func makeBody(configuration: Configuration) -> some View {126        configuration.label127            .font(VQTokens.Typography.bodySm.font.weight(.semibold))128            .foregroundStyle(VQTokens.Palette.accentFg)129            .padding(.horizontal, 12).padding(.vertical, 5)130            .background(RoundedRectangle(cornerRadius: VQTokens.Radius.sm, style: .continuous).fill(VQTokens.Palette.accent.opacity(enabled ? (configuration.isPressed ? 0.8 : 1) : 0.35)))131            .contentShape(Rectangle())132    }133}134135enum PlainNumber {136    /// A number as a person types it back: no grouping, a point, no trailing zeros ("10", "150.25").137    static func text(_ v: Double?) -> String {138        guard let v, v.isFinite else { return "" }139        if v.rounded() == v, abs(v) < 1e15 { return String(Int64(v)) }140        var s = String(format: "%.8f", v)141        while s.hasSuffix("0") { s.removeLast() }142        if s.hasSuffix(".") { s.removeLast() }143        return s144    }145146    static let day: DateFormatter = { let f = DateFormatter(); f.calendar = Calendar(identifier: .gregorian); f.locale = Locale(identifier: "en_US_POSIX"); f.timeZone = TimeZone(identifier: "UTC"); f.dateFormat = "yyyy-MM-dd"; return f }()147    static func day(_ d: Date?) -> String { d.map(day.string(from:)) ?? "" }148}149150extension AppEnvironment {151    /// Number formatting of the signed-in person (synced preference), for the figures the app formats itself.152    var numberLocale: VQNumberLocale { vqTheme.numberLocale }153}154155/// An empty state centred over the content area. As an overlay of a clear filler, never a flexible child of a stack:156/// `VQEmptyState` in a flexible frame can push the whole window content off the top (seen by a UI test).157struct CenteredEmpty<Content: View>: View {158    @ViewBuilder var content: Content159    var body: some View { Color.clear.frame(maxWidth: .infinity, maxHeight: .infinity).overlay { content } }160}161
Files main
Menu

Theme