import SwiftUI import BucksleyKit /// The three looks the owner picked, each with light and dark. Same shape as Inkspring's StudioTheme: /// a raw value stored under `preferenceKey`, a default, a title, and a palette from the environment. public enum BucksleyTheme: String, CaseIterable, Identifiable, Sendable { case afterhours, evergreen, almanac public static let preferenceKey = "theme" public static let defaultPreference: Self = .afterhours public var id: Self { self } public var title: String { switch self { case .afterhours: "Afterhours" case .evergreen: "Evergreen" case .almanac: "Almanac" } } public var summary: String { switch self { case .afterhours: "Dark-first and vivid, with dense numbers" case .evergreen: "Quiet and native; the charts carry the color" case .almanac: "Warm and editorial, with serif figures" } } /// The alternate app icon that matches the theme (nil is the primary, Afterhours, icon). public var iconName: String? { switch self { case .afterhours: nil case .evergreen: "AppIcon-Evergreen" case .almanac: "AppIcon-Almanac" } } public init(preference: Any?) { self = (preference as? String).flatMap(Self.init(rawValue:)) ?? Self.defaultPreference } } /// Where Bucksley keeps its preferences. A test run with its own workspace (BUCKSLEY_WORKSPACE, used by /// the screenshot tour) gets a separate store, so it never changes the theme or appearance of the /// Bucksley the person is using on the same Mac. public enum BucksleyPreferences { nonisolated(unsafe) public static let store: UserDefaults = { guard ProcessInfo.processInfo.environment["BUCKSLEY_WORKSPACE"] != nil, let test = UserDefaults(suiteName: "com.protomota.bucksley.workspace-test") else { return .standard } return test }() } /// Light, dark or follow the system, like Jotley's appearance mode. public enum BucksleyAppearance: String, CaseIterable, Identifiable, Sendable { case system, light, dark public static let preferenceKey = "appearance" public var id: Self { self } public var title: String { switch self { case .system: "Match System"; case .light: "Light"; case .dark: "Dark" } } public var colorScheme: ColorScheme? { switch self { case .system: nil; case .light: .light; case .dark: .dark } } } /// Every color, font and radius a screen uses, resolved for one theme and one color scheme. /// Tokens come from design/design-directions.html. public struct BucksleyPalette: Sendable { public let theme: BucksleyTheme public let dark: Bool public init(theme: BucksleyTheme, dark: Bool) { self.theme = theme; self.dark = dark } public var colorScheme: ColorScheme { dark ? .dark : .light } public var canvas: Color { pick(0xFFFFFF, 0x0D0E17, 0xFFFFFF, 0x101513, 0xFAF6EC, 0x1B1815) } public var sidebar: Color { pick(0xF2F1FB, 0x12131F, 0xF2F5F3, 0x161C19, 0xF1EADA, 0x221E1A) } public var card: Color { pick(0xF5F4FC, 0x191B2B, 0xF6F8F7, 0x1B2320, 0xFFFDF6, 0x27221D) } public var text: Color { pick(0x14132B, 0xF2F2FA, 0x14201B, 0xEEF3F0, 0x211D17, 0xF3ECDD) } public var muted: Color { pick(0x66657F, 0x9092AC, 0x5F6E67, 0x96A49D, 0x756B5C, 0xAEA391) } public var line: Color { pick(0xE1DFF0, 0x272A3F, 0xE1E6E3, 0x28322D, 0xDCD2BD, 0x3B352C) } public var accent: Color { pick(0x5B3DF0, 0x8A6BFF, 0x17805E, 0x3DD6A0, 0xB4532A, 0xE38B5E) } public var accentText: Color { pick(0xFFFFFF, 0xFFFFFF, 0xFFFFFF, 0x06241A, 0xFFF8EC, 0x2A150B) } public var gain: Color { pick(0x3C8500, 0xB6F24A, 0x17805E, 0x3DD6A0, 0x4F6B3A, 0xA5C487) } public var loss: Color { pick(0xDB2350, 0xFF5C7A, 0xC8442E, 0xFF7A66, 0x9B2C2C, 0xEC8080) } /// Category and chart colors c1…c6. public var series: [Color] { switch (theme, dark) { case (.afterhours, false): [0x5B3DF0, 0x5FA800, 0x0E9AC2, 0xE07A10, 0xDB2350, 0xA3A2B8].map(Color.init(hex:)) case (.afterhours, true): [0x8A6BFF, 0xB6F24A, 0x35C9F0, 0xFF9F43, 0xFF5C7A, 0x5B5E78].map(Color.init(hex:)) case (.evergreen, false): [0x17805E, 0x2F6FDE, 0xD98A1F, 0x8A5BD6, 0xD4577A, 0x98A49F].map(Color.init(hex:)) case (.evergreen, true): [0x3DD6A0, 0x6AA3FF, 0xF2B25C, 0xB08CF5, 0xF283A3, 0x6F7C76].map(Color.init(hex:)) case (.almanac, false): [0xB4532A, 0x4F6B3A, 0xC0902A, 0x3F5F7A, 0x8A4A6B, 0xA39A88].map(Color.init(hex:)) case (.almanac, true): [0xE38B5E, 0xA5C487, 0xE3BD5E, 0x86A9C7, 0xC58BAA, 0x7D7466].map(Color.init(hex:)) } } public func color(_ index: Int) -> Color { series[((index % series.count) + series.count) % series.count] } public func color(for group: AccountGroup) -> Color { switch group { case .investments: color(0); case .cash: color(1); case .credit: color(4); case .loans: color(3); case .other: color(2) } } public func amountColor(_ value: Decimal) -> Color { value > 0 ? gain : text } // MARK: Type /// Screen titles: SF Pro Display heavy and tight (Afterhours), SF Pro (Evergreen), New York (Almanac). public func title(_ size: CGFloat) -> Font { switch theme { case .afterhours: .system(size: size, weight: .heavy, design: .default) case .evergreen: .system(size: size, weight: .bold, design: .default) case .almanac: .system(size: size, weight: .medium, design: .serif) } } /// Dollar figures: SF Mono (Afterhours), SF Pro Rounded (Evergreen), New York (Almanac). public func figure(_ size: CGFloat, weight: Font.Weight = .semibold) -> Font { switch theme { case .afterhours: .system(size: size, weight: weight == .semibold ? .medium : weight, design: .monospaced) case .evergreen: .system(size: size, weight: weight, design: .rounded) case .almanac: .system(size: size, weight: weight, design: .serif) } } public func body(_ size: CGFloat = 13, weight: Font.Weight = .regular) -> Font { .system(size: size, weight: weight) } /// Section labels: uppercase tracking (Afterhours), plain bold (Evergreen), serif italic (Almanac). public var sectionFont: Font { switch theme { case .afterhours: .system(size: 11, weight: .bold) case .evergreen: .system(size: 13, weight: .semibold) case .almanac: .system(size: 14, weight: .regular, design: .serif).italic() } } public var titleTracking: CGFloat { theme == .afterhours ? -0.6 : theme == .almanac ? -0.2 : -0.3 } public var figureTracking: CGFloat { theme == .afterhours ? -0.5 : 0 } public var cardRadius: CGFloat { theme == .almanac ? 4 : theme == .afterhours ? 14 : 12 } public var pillNavigation: Bool { theme != .evergreen } /// Almanac separates with rules instead of filled cards. public var ruled: Bool { theme == .almanac } private func pick(_ ahL: UInt32, _ ahD: UInt32, _ evL: UInt32, _ evD: UInt32, _ alL: UInt32, _ alD: UInt32) -> Color { switch theme { case .afterhours: Color(hex: dark ? ahD : ahL) case .evergreen: Color(hex: dark ? evD : evL) case .almanac: Color(hex: dark ? alD : alL) } } } public extension Color { init(hex rgb: UInt32) { self.init(.sRGB, red: Double((rgb >> 16) & 255) / 255, green: Double((rgb >> 8) & 255) / 255, blue: Double(rgb & 255) / 255) } } private struct BucksleyPaletteKey: EnvironmentKey { static let defaultValue = BucksleyPalette(theme: .afterhours, dark: true) } public extension EnvironmentValues { var palette: BucksleyPalette { get { self[BucksleyPaletteKey.self] } set { self[BucksleyPaletteKey.self] = newValue } } } /// Reads the saved theme and appearance, resolves the palette for the current color scheme, and /// hands it to every view below. Changing either preference restyles the app live. public struct ThemedRoot: View { @AppStorage(BucksleyTheme.preferenceKey) private var themeRaw = BucksleyTheme.defaultPreference.rawValue @AppStorage(BucksleyAppearance.preferenceKey) private var appearanceRaw = BucksleyAppearance.system.rawValue @Environment(\.colorScheme) private var systemScheme let content: Content public init(@ViewBuilder content: () -> Content) { self.content = content() } public var body: some View { let appearance = BucksleyAppearance(rawValue: appearanceRaw) ?? .system let scheme = appearance.colorScheme ?? systemScheme let palette = BucksleyPalette(theme: BucksleyTheme(preference: themeRaw), dark: scheme == .dark) content .environment(\.palette, palette) .preferredColorScheme(appearance.colorScheme) .tint(palette.accent) .foregroundStyle(palette.text) } }