Branchwell

Bucksley / app / Sources / BucksleyUI / Components.swift

1import SwiftUI
2import BucksleyKit
3
4/// A dollar figure in the theme's figure face, with tabular digits.
5public struct MoneyText: View {
6 @Environment(\.palette) private var palette
7 let value: Decimal
8 let size: CGFloat
9 var cents = true
10 var signed = false
11 var colored = false
12 var weight: Font.Weight = .semibold
13
14 public init(_ value: Decimal, size: CGFloat = 13, cents: Bool = true, signed: Bool = false, colored: Bool = false, weight: Font.Weight = .semibold) {
15 self.value = value; self.size = size; self.cents = cents; self.signed = signed; self.colored = colored; self.weight = weight
16 }
17
18 public var body: some View {
19 Text(Money.format(value, cents: cents, signed: signed))
20 .font(palette.figure(size, weight: weight))
21 .tracking(palette.figureTracking)
22 .monospacedDigit()
23 .foregroundStyle(colored ? (value > 0 ? palette.gain : value < 0 ? palette.loss : palette.text) : (value > 0 && signed ? palette.gain : palette.text))
24 .lineLimit(1)
25 .fixedSize(horizontal: true, vertical: false)
26 }
27}
28
29/// The big net worth number: "$184,046" with smaller cents, like the Afterhours hero.
30public struct HeroMoney: View {
31 @Environment(\.palette) private var palette
32 let value: Decimal
33 let size: CGFloat
34 public init(_ value: Decimal, size: CGFloat = 40) { self.value = value; self.size = size }
35 public var body: some View {
36 let text = Money.format(value)
37 let parts = text.split(separator: ".", maxSplits: 1)
38 HStack(alignment: .firstTextBaseline, spacing: 0) {
39 Text(parts.first.map(String.init) ?? text).font(palette.figure(size, weight: .semibold)).tracking(palette.theme == .afterhours ? -2 : -0.5)
40 if parts.count == 2 {
41 Text("." + parts[1]).font(palette.figure(size * 0.5, weight: .medium)).foregroundStyle(palette.muted)
42 }
43 }
44 .monospacedDigit()
45 .lineLimit(1)
46 .minimumScaleFactor(0.5)
47 .accessibilityLabel(text)
48 }
49}
50
51public struct SectionHeader<Trailing: View>: View {
52 @Environment(\.palette) private var palette
53 let title: String
54 let trailing: Trailing
55 public init(_ title: String, @ViewBuilder trailing: () -> Trailing = { EmptyView() }) { self.title = title; self.trailing = trailing() }
56 public var body: some View {
57 HStack(alignment: .firstTextBaseline) {
58 Group {
59 if palette.theme == .afterhours { Text(title.uppercased()).tracking(1.1).foregroundStyle(palette.muted) }
60 else { Text(title).foregroundStyle(palette.theme == .almanac ? palette.text : palette.text) }
61 }
62 .font(palette.sectionFont)
63 .accessibilityAddTraits(.isHeader)
64 Spacer(minLength: 8)
65 trailing
66 }
67 .padding(.bottom, palette.ruled ? 6 : 2)
68 .overlay(alignment: .bottom) {
69 if palette.ruled { Rectangle().fill(palette.text).frame(height: 1) }
70 }
71 }
72}
73
74/// A themed surface: filled rounded card (Afterhours, Evergreen) or a ruled block (Almanac).
75public struct Panel<Content: View>: View {
76 @Environment(\.palette) private var palette
77 let padding: CGFloat
78 let content: Content
79 public init(padding: CGFloat = 16, @ViewBuilder content: () -> Content) { self.padding = padding; self.content = content() }
80 public var body: some View {
81 content
82 .padding(palette.ruled ? EdgeInsets(top: padding * 0.75, leading: 0, bottom: padding * 0.75, trailing: 0) : EdgeInsets(top: padding, leading: padding, bottom: padding, trailing: padding))
83 .frame(maxWidth: .infinity, alignment: .leading)
84 .background {
85 if palette.ruled {
86 VStack { Rectangle().fill(palette.text).frame(height: 2); Spacer() }
87 } else {
88 RoundedRectangle(cornerRadius: palette.cardRadius, style: .continuous).fill(palette.card)
89 }
90 }
91 }
92}
93
94/// Initials in a colored circle (rounded square in Afterhours), as in the design's transaction rows.
95public struct MerchantBadge: View {
96 @Environment(\.palette) private var palette
97 let name: String
98 let colorIndex: Int
99 var size: CGFloat = 30
100 var brand: Color?
101 public init(name: String, colorIndex: Int, size: CGFloat = 30, brand: Color? = nil) { self.name = name; self.colorIndex = colorIndex; self.size = size; self.brand = brand }
102 public var body: some View {
103 let words = name.split(separator: " ").filter { $0.first?.isLetter == true }
104 let initials = (words.count >= 2 ? String(words[0].prefix(1) + words[1].prefix(1)) : String(name.filter(\.isLetter).prefix(1))).uppercased()
105 let tint = brand ?? palette.color(colorIndex)
106 Text(initials.isEmpty ? "$" : initials)
107 .font(.system(size: size * 0.38, weight: .bold))
108 .foregroundStyle(palette.dark ? tint : tint.opacity(0.95))
109 .frame(width: size, height: size)
110 .background(tint.opacity(palette.dark ? 0.2 : 0.14), in: RoundedRectangle(cornerRadius: palette.theme == .evergreen ? size / 2 : size * 0.28, style: .continuous))
111 .accessibilityHidden(true)
112 }
113}
114
115/// A category as a small colored pill.
116public struct CategoryTag: View {
117 @Environment(\.palette) private var palette
118 let category: BucksleyKit.Category?
119 public init(_ category: BucksleyKit.Category?) { self.category = category }
120 public var body: some View {
121 let tint = palette.color(category?.colorIndex ?? 5)
122 HStack(spacing: 4) {
123 Text(category?.icon ?? "❔").font(.system(size: 10.5)).italic(false)
124 Text(category?.name ?? "Uncategorized")
125 }
126 .font(.system(size: 11, weight: palette.theme == .almanac ? .regular : .bold, design: palette.theme == .almanac ? .serif : .default))
127 .italic(palette.theme == .almanac)
128 .padding(.horizontal, palette.theme == .almanac ? 0 : 9).padding(.vertical, palette.theme == .almanac ? 0 : 4)
129 .foregroundStyle(palette.theme == .almanac ? palette.muted : tint)
130 .background(palette.theme == .almanac ? Color.clear : tint.opacity(palette.dark ? 0.2 : 0.14), in: Capsule())
131 .lineLimit(1)
132 }
133}
134
135/// One transaction line: date, badge, name with category and account, amount.
136public struct TransactionRow: View {
137 @Environment(\.palette) private var palette
138 let entry: LedgerEntry
139 let workspace: Workspace
140 var showDate = true
141 var showAccount = false
142 public init(_ entry: LedgerEntry, workspace: Workspace, showDate: Bool = true, showAccount: Bool = false) {
143 self.entry = entry; self.workspace = workspace; self.showDate = showDate; self.showAccount = showAccount
144 }
145 public var body: some View {
146 let category = workspace.category(entry.categoryID)
147 HStack(spacing: 12) {
148 if showDate {
149 Text(Self.shortDay(entry.day)).font(palette.theme == .afterhours ? palette.figure(11, weight: .regular) : .system(size: 11))
150 .foregroundStyle(palette.muted).frame(width: 44, alignment: .leading)
151 }
152 MerchantBadge(name: entry.name, colorIndex: category?.colorIndex ?? 5)
153 VStack(alignment: .leading, spacing: 6) {
154 HStack(spacing: 6) {
155 Text(entry.name).font(.system(size: 13, weight: .semibold)).lineLimit(1)
156 if entry.transaction.pending { Text("Pending").font(.system(size: 10, weight: .semibold)).foregroundStyle(palette.muted) }
157 if !entry.reviewed { Circle().fill(palette.accent).frame(width: 6, height: 6).accessibilityLabel("Not reviewed") }
158 }
159 HStack(spacing: 8) {
160 CategoryTag(category)
161 if showAccount, let account = workspace.account(entry.accountID) {
162 Text(account.displayName).font(.system(size: 11)).foregroundStyle(palette.muted).lineLimit(1)
163 }
164 if entry.note != nil { Image(systemName: "text.bubble").font(.system(size: 10)).foregroundStyle(palette.muted) }
165 if !entry.splits.isEmpty { Image(systemName: "arrow.triangle.branch").font(.system(size: 10)).foregroundStyle(palette.muted) }
166 }
167 }
168 Spacer(minLength: 8)
169 MoneyText(entry.amount, size: 13, signed: true)
170 .strikethrough(entry.excluded)
171 }
172 .padding(.vertical, 10)
173 .contentShape(Rectangle())
174 }
175
176 public static func shortDay(_ day: String) -> String {
177 guard let d = DayKey.date(day) else { return day }
178 return d.formatted(.dateTime.month(.abbreviated).day())
179 }
180}
181
182/// A small swatch of a theme for the Settings rows, the same idea as Inkspring's theme previews.
183public struct ThemeSwatch: View {
184 let theme: BucksleyTheme
185 let dark: Bool
186 public init(theme: BucksleyTheme, dark: Bool) { self.theme = theme; self.dark = dark }
187 public var body: some View {
188 let p = BucksleyPalette(theme: theme, dark: dark)
189 VStack(alignment: .leading, spacing: 3) {
190 Capsule().fill(p.accent).frame(width: 18, height: 3)
191 HStack(alignment: .bottom, spacing: 2) {
192 ForEach(0..<4, id: \.self) { i in RoundedRectangle(cornerRadius: 1).fill(p.color(i)).frame(width: 4, height: CGFloat(6 + i * 3)) }
193 }
194 RoundedRectangle(cornerRadius: 2).fill(p.card).frame(width: 30, height: 6)
195 .overlay { RoundedRectangle(cornerRadius: 2).strokeBorder(p.line) }
196 }
197 .padding(6)
198 .frame(width: 46, height: 38, alignment: .topLeading)
199 .background(p.canvas, in: .rect(cornerRadius: 6))
200 .overlay { RoundedRectangle(cornerRadius: 6).strokeBorder(p.line) }
201 .accessibilityHidden(true)
202 }
203}
204
205/// The primary-colored button used for main actions.
206public struct PrimaryButtonStyle: ButtonStyle {
207 @Environment(\.palette) private var palette
208 public init() {}
209 public func makeBody(configuration: Configuration) -> some View {
210 configuration.label
211 .font(.system(size: 13, weight: .semibold))
212 .padding(.horizontal, 14).padding(.vertical, 7)
213 .foregroundStyle(palette.theme == .almanac ? palette.canvas : palette.accentText)
214 .background(palette.theme == .almanac ? palette.text : palette.accent, in: Capsule())
215 .opacity(configuration.isPressed ? 0.8 : 1)
216 }
217}
218
219public struct SecondaryButtonStyle: ButtonStyle {
220 @Environment(\.palette) private var palette
221 public init() {}
222 public func makeBody(configuration: Configuration) -> some View {
223 configuration.label
224 .font(.system(size: 13, weight: .semibold))
225 .padding(.horizontal, 14).padding(.vertical, 7)
226 .foregroundStyle(palette.text)
227 .background(palette.card, in: Capsule())
228 .overlay { Capsule().strokeBorder(palette.line) }
229 .opacity(configuration.isPressed ? 0.8 : 1)
230 }
231}
232
233/// Pill segmented control in the theme's style (range pickers, tabs).
234public struct PillPicker<Value: Hashable>: View {
235 @Environment(\.palette) private var palette
236 let options: [(Value, String)]
237 @Binding var selection: Value
238 public init(_ options: [(Value, String)], selection: Binding<Value>) { self.options = options; _selection = selection }
239 public var body: some View {
240 HStack(spacing: 2) {
241 ForEach(options.indices, id: \.self) { i in
242 let (value, label) = options[i]
243 let on = value == selection
244 Button { selection = value } label: {
245 Text(label).font(.system(size: 11, weight: .bold))
246 .padding(.horizontal, 10).padding(.vertical, 4)
247 .foregroundStyle(on ? (palette.theme == .almanac ? palette.canvas : palette.accentText) : palette.muted)
248 .background(on ? (palette.theme == .almanac ? palette.text : palette.accent) : .clear, in: Capsule())
249 .contentShape(Capsule())
250 }
251 .buttonStyle(.plain)
252 .accessibilityAddTraits(on ? .isSelected : [])
253 }
254 }
255 .padding(2)
256 .background(palette.card, in: Capsule())
257 }
258}
259
260/// The shared "A Project ATOM app" brand pill, copied from Inkspring and Jotley: black capsule,
261/// 22 pt logo, white semibold label, the same in every theme and appearance.
262public struct ProtomotaProductPill: View {
263 public init() {}
264 public var body: some View {
265 Link(destination: URL(string: "https://jobley.ai/volunteer/")!) {
266 HStack(spacing: 10) {
267 Image("ProjectAtomLogo")
268 .renderingMode(.original)
269 .resizable()
270 .scaledToFit()
271 .frame(width: 22, height: 22)
272 .clipShape(.rect(cornerRadius: 6))
273 .accessibilityHidden(true)
274 Text("A Project ATOM app")
275 .font(.system(size: 13.5, weight: .semibold))
276 .lineLimit(1)
277 Image(systemName: "arrow.up.right")
278 .font(.caption.weight(.medium))
279 .accessibilityHidden(true)
280 }
281 .fixedSize()
282 .padding(.leading, 10)
283 .padding(.trailing, 16)
284 .padding(.vertical, 9)
285 .foregroundStyle(.white)
286 .background(Color(hex: 0x0B0E16), in: Capsule())
287 .overlay { Capsule().strokeBorder(Color(hex: 0x1D2230)) }
288 .contentShape(Capsule())
289 }
290 .buttonStyle(.plain)
291 .accessibilityIdentifier("settings.protomotaProduct")
292 #if os(macOS)
293 .help("Explore Project ATOM volunteer work")
294 #endif
295 }
296}