Branchwell

Bucksley / app · bucksley.ai / Sources / BucksleyUI / Theme.swift

1import SwiftUI
2import BucksleyKit
3
4/// The three looks the owner picked, each with light and dark. Same shape as Inkspring's StudioTheme:
5/// a raw value stored under `preferenceKey`, a default, a title, and a palette from the environment.
6public enum BucksleyTheme: String, CaseIterable, Identifiable, Sendable {
7 case afterhours, evergreen, almanac
8
9 public static let preferenceKey = "theme"
10 public static let defaultPreference: Self = .afterhours
11 public var id: Self { self }
12
13 public var title: String {
14 switch self {
15 case .afterhours: "Afterhours"
16 case .evergreen: "Evergreen"
17 case .almanac: "Almanac"
18 }
19 }
20
21 public var summary: String {
22 switch self {
23 case .afterhours: "Dark-first and vivid, with dense numbers"
24 case .evergreen: "Quiet and native; the charts carry the color"
25 case .almanac: "Warm and editorial, with serif figures"
26 }
27 }
28
29 /// The alternate app icon that matches the theme (nil is the primary, Afterhours, icon).
30 public var iconName: String? {
31 switch self {
32 case .afterhours: nil
33 case .evergreen: "AppIcon-Evergreen"
34 case .almanac: "AppIcon-Almanac"
35 }
36 }
37
38 public init(preference: Any?) {
39 self = (preference as? String).flatMap(Self.init(rawValue:)) ?? Self.defaultPreference
40 }
41}
42
43/// Where Bucksley keeps its preferences. A test run with its own workspace (BUCKSLEY_WORKSPACE, used by
44/// the screenshot tour) gets a separate store, so it never changes the theme or appearance of the
45/// Bucksley the person is using on the same Mac.
46public enum BucksleyPreferences {
47 nonisolated(unsafe) public static let store: UserDefaults = {
48 guard ProcessInfo.processInfo.environment["BUCKSLEY_WORKSPACE"] != nil,
49 let test = UserDefaults(suiteName: "com.protomota.bucksley.workspace-test") else { return .standard }
50 return test
51 }()
52}
53
54/// Light, dark or follow the system, like Jotley's appearance mode.
55public enum BucksleyAppearance: String, CaseIterable, Identifiable, Sendable {
56 case system, light, dark
57 public static let preferenceKey = "appearance"
58 public var id: Self { self }
59 public var title: String {
60 switch self { case .system: "Match System"; case .light: "Light"; case .dark: "Dark" }
61 }
62 public var colorScheme: ColorScheme? {
63 switch self { case .system: nil; case .light: .light; case .dark: .dark }
64 }
65}
66
67/// Every color, font and radius a screen uses, resolved for one theme and one color scheme.
68/// Tokens come from design/design-directions.html.
69public struct BucksleyPalette: Sendable {
70 public let theme: BucksleyTheme
71 public let dark: Bool
72
73 public init(theme: BucksleyTheme, dark: Bool) { self.theme = theme; self.dark = dark }
74 public var colorScheme: ColorScheme { dark ? .dark : .light }
75
76 public var canvas: Color { pick(0xFFFFFF, 0x0D0E17, 0xFFFFFF, 0x101513, 0xFAF6EC, 0x1B1815) }
77 public var sidebar: Color { pick(0xF2F1FB, 0x12131F, 0xF2F5F3, 0x161C19, 0xF1EADA, 0x221E1A) }
78 public var card: Color { pick(0xF5F4FC, 0x191B2B, 0xF6F8F7, 0x1B2320, 0xFFFDF6, 0x27221D) }
79 public var text: Color { pick(0x14132B, 0xF2F2FA, 0x14201B, 0xEEF3F0, 0x211D17, 0xF3ECDD) }
80 public var muted: Color { pick(0x66657F, 0x9092AC, 0x5F6E67, 0x96A49D, 0x756B5C, 0xAEA391) }
81 public var line: Color { pick(0xE1DFF0, 0x272A3F, 0xE1E6E3, 0x28322D, 0xDCD2BD, 0x3B352C) }
82 public var accent: Color { pick(0x5B3DF0, 0x8A6BFF, 0x17805E, 0x3DD6A0, 0xB4532A, 0xE38B5E) }
83 public var accentText: Color { pick(0xFFFFFF, 0xFFFFFF, 0xFFFFFF, 0x06241A, 0xFFF8EC, 0x2A150B) }
84 public var gain: Color { pick(0x3C8500, 0xB6F24A, 0x17805E, 0x3DD6A0, 0x4F6B3A, 0xA5C487) }
85 public var loss: Color { pick(0xDB2350, 0xFF5C7A, 0xC8442E, 0xFF7A66, 0x9B2C2C, 0xEC8080) }
86
87 /// Category and chart colors c1…c6.
88 public var series: [Color] {
89 switch (theme, dark) {
90 case (.afterhours, false): [0x5B3DF0, 0x5FA800, 0x0E9AC2, 0xE07A10, 0xDB2350, 0xA3A2B8].map(Color.init(hex:))
91 case (.afterhours, true): [0x8A6BFF, 0xB6F24A, 0x35C9F0, 0xFF9F43, 0xFF5C7A, 0x5B5E78].map(Color.init(hex:))
92 case (.evergreen, false): [0x17805E, 0x2F6FDE, 0xD98A1F, 0x8A5BD6, 0xD4577A, 0x98A49F].map(Color.init(hex:))
93 case (.evergreen, true): [0x3DD6A0, 0x6AA3FF, 0xF2B25C, 0xB08CF5, 0xF283A3, 0x6F7C76].map(Color.init(hex:))
94 case (.almanac, false): [0xB4532A, 0x4F6B3A, 0xC0902A, 0x3F5F7A, 0x8A4A6B, 0xA39A88].map(Color.init(hex:))
95 case (.almanac, true): [0xE38B5E, 0xA5C487, 0xE3BD5E, 0x86A9C7, 0xC58BAA, 0x7D7466].map(Color.init(hex:))
96 }
97 }
98
99 public func color(_ index: Int) -> Color { series[((index % series.count) + series.count) % series.count] }
100 public func color(for group: AccountGroup) -> Color {
101 switch group { case .investments: color(0); case .cash: color(1); case .credit: color(4); case .loans: color(3); case .other: color(2) }
102 }
103 public func amountColor(_ value: Decimal) -> Color { value > 0 ? gain : text }
104
105 // MARK: Type
106
107 /// Screen titles: SF Pro Display heavy and tight (Afterhours), SF Pro (Evergreen), New York (Almanac).
108 public func title(_ size: CGFloat) -> Font {
109 switch theme {
110 case .afterhours: .system(size: size, weight: .heavy, design: .default)
111 case .evergreen: .system(size: size, weight: .bold, design: .default)
112 case .almanac: .system(size: size, weight: .medium, design: .serif)
113 }
114 }
115
116 /// Dollar figures: SF Mono (Afterhours), SF Pro Rounded (Evergreen), New York (Almanac).
117 public func figure(_ size: CGFloat, weight: Font.Weight = .semibold) -> Font {
118 switch theme {
119 case .afterhours: .system(size: size, weight: weight == .semibold ? .medium : weight, design: .monospaced)
120 case .evergreen: .system(size: size, weight: weight, design: .rounded)
121 case .almanac: .system(size: size, weight: weight, design: .serif)
122 }
123 }
124
125 public func body(_ size: CGFloat = 13, weight: Font.Weight = .regular) -> Font { .system(size: size, weight: weight) }
126
127 /// Section labels: uppercase tracking (Afterhours), plain bold (Evergreen), serif italic (Almanac).
128 public var sectionFont: Font {
129 switch theme {
130 case .afterhours: .system(size: 11, weight: .bold)
131 case .evergreen: .system(size: 13, weight: .semibold)
132 case .almanac: .system(size: 14, weight: .regular, design: .serif).italic()
133 }
134 }
135
136 public var titleTracking: CGFloat { theme == .afterhours ? -0.6 : theme == .almanac ? -0.2 : -0.3 }
137 public var figureTracking: CGFloat { theme == .afterhours ? -0.5 : 0 }
138 public var cardRadius: CGFloat { theme == .almanac ? 4 : theme == .afterhours ? 14 : 12 }
139 public var pillNavigation: Bool { theme != .evergreen }
140 /// Almanac separates with rules instead of filled cards.
141 public var ruled: Bool { theme == .almanac }
142
143 private func pick(_ ahL: UInt32, _ ahD: UInt32, _ evL: UInt32, _ evD: UInt32, _ alL: UInt32, _ alD: UInt32) -> Color {
144 switch theme {
145 case .afterhours: Color(hex: dark ? ahD : ahL)
146 case .evergreen: Color(hex: dark ? evD : evL)
147 case .almanac: Color(hex: dark ? alD : alL)
148 }
149 }
150}
151
152public extension Color {
153 init(hex rgb: UInt32) {
154 self.init(.sRGB, red: Double((rgb >> 16) & 255) / 255, green: Double((rgb >> 8) & 255) / 255, blue: Double(rgb & 255) / 255)
155 }
156}
157
158private struct BucksleyPaletteKey: EnvironmentKey {
159 static let defaultValue = BucksleyPalette(theme: .afterhours, dark: true)
160}
161
162public extension EnvironmentValues {
163 var palette: BucksleyPalette {
164 get { self[BucksleyPaletteKey.self] }
165 set { self[BucksleyPaletteKey.self] = newValue }
166 }
167}
168
169/// Reads the saved theme and appearance, resolves the palette for the current color scheme, and
170/// hands it to every view below. Changing either preference restyles the app live.
171public struct ThemedRoot<Content: View>: View {
172 @AppStorage(BucksleyTheme.preferenceKey) private var themeRaw = BucksleyTheme.defaultPreference.rawValue
173 @AppStorage(BucksleyAppearance.preferenceKey) private var appearanceRaw = BucksleyAppearance.system.rawValue
174 @Environment(\.colorScheme) private var systemScheme
175 let content: Content
176
177 public init(@ViewBuilder content: () -> Content) { self.content = content() }
178
179 public var body: some View {
180 let appearance = BucksleyAppearance(rawValue: appearanceRaw) ?? .system
181 let scheme = appearance.colorScheme ?? systemScheme
182 let palette = BucksleyPalette(theme: BucksleyTheme(preference: themeRaw), dark: scheme == .dark)
183 content
184 .environment(\.palette, palette)
185 .preferredColorScheme(appearance.colorScheme)
186 .tint(palette.accent)
187 .foregroundStyle(palette.text)
188 }
189}