Graphley / app · graphley.ai / Sources / AnalyticsUI / Theme.swift
| 1 | import SwiftUI |
| 2 | import CoreText |
| 3 | import AnalyticsKit |
| 4 | |
| 5 | /// Direction A, "Tide table" (picked Nov 24 2026): sea-glass ground, harbor-blue ink, one color per site. |
| 6 | /// Fonts: Bricolage Grotesque for headings, Instrument Sans for text, IBM Plex Mono for figures, bundled in |
| 7 | /// `Resources/Fonts` and registered by `Tide.registerFonts()` at launch. Full spec: design/DESIGN.md. |
| 8 | public enum Tide { |
| 9 | public static let ground = Color(light: 0xE6EDEB, dark: 0x0B1B21) |
| 10 | public static let surface = Color(light: 0xF7FAF9, dark: 0x11252C) |
| 11 | public static let sunk = Color(light: 0xDAE4E1, dark: 0x0A171C) |
| 12 | public static let ink = Color(light: 0x0E2830, dark: 0xE2EEEC) |
| 13 | public static let muted = Color(light: 0x557070, dark: 0x88A2A2) |
| 14 | public static let line = Color(light: 0xC3D2CE, dark: 0x21404A) |
| 15 | // Brand: the four colors of the Stacked tide icon. Every other token is tuned from these. |
| 16 | public static let brandTide = Color(light: 0x1D6E8A, dark: 0x1D6E8A) |
| 17 | public static let brandFoam = Color(light: 0xE6EDEB, dark: 0xE6EDEB) |
| 18 | public static let brandLagoon = Color(light: 0x3BA49C, dark: 0x3BA49C) |
| 19 | public static let brandAmber = Color(light: 0xC9853A, dark: 0xC9853A) |
| 20 | |
| 21 | /// Text, icons, focus rings and outlines. Tide blue in light mode; Lagoon in dark, where Tide is too dark to read. |
| 22 | public static let accent = Color(light: 0x1D6E8A, dark: 0x5EC4BA) |
| 23 | /// Filled controls (primary buttons, the selected sidebar row, switches): Tide blue in both modes, like the icon. |
| 24 | public static let fill = Color(light: 0x1D6E8A, dark: 0x1D6E8A) |
| 25 | public static let up = Color(light: 0x247A72, dark: 0x5EC4BA) // Lagoon |
| 26 | public static let down = Color(light: 0xB0503A, dark: 0xE58A72) |
| 27 | |
| 28 | public static let surface2 = Color(light: 0xEDF3F1, dark: 0x163039) |
| 29 | public static let ink2 = Color(light: 0x2F4C52, dark: 0xB4C8C7) |
| 30 | public static let line2 = Color(light: 0xA9BDB8, dark: 0x2F5561) |
| 31 | public static let control = Color(light: 0x7B9390, dark: 0x53747B) |
| 32 | public static let accentStrong = Color(light: 0x175A71, dark: 0x86D6CD) |
| 33 | public static let fillHover = Color(light: 0x175A71, dark: 0x247E9D) |
| 34 | public static let fillPressed = Color(light: 0x18596F, dark: 0x195F77) |
| 35 | public static let accentInk = Color(light: 0xFFFFFF, dark: 0xF2F7F6) // text on `fill` |
| 36 | public static let accentTint = Color(light: 0xE1ECEE, dark: 0x173A3D) |
| 37 | public static let accentTint2 = Color(light: 0xD0E1E5, dark: 0x1D4749) |
| 38 | public static let onDown = Color(light: 0xFFFFFF, dark: 0x1A0B07) |
| 39 | public static let downHover = Color(light: 0x934938, dark: 0xEA9F8B) |
| 40 | public static let downPressed = Color(light: 0x7C4337, dark: 0xC47966) |
| 41 | public static let downText = Color(light: 0x984A38, dark: 0xE58A72) |
| 42 | public static let attention = Color(light: 0xC9853A, dark: 0xE3A45A) |
| 43 | public static let attentionText = Color(light: 0x755B36, dark: 0xE3A45A) |
| 44 | /// Quiet text inside the top bar pills: `muted` reaches only 4.1:1 on `sunk` in light mode. |
| 45 | public static let pillText = Color(light: 0x485C5E, dark: 0x88A2A2) |
| 46 | /// The selected range segment: `surface` disappears on `sunk` in dark mode. |
| 47 | public static let pillSelected = Color(light: 0xF7FAF9, dark: 0x2A4651) |
| 48 | public static let knob = Color(light: 0xFFFFFF, dark: 0xE2EEEC) |
| 49 | |
| 50 | public static let series: [Color] = [ |
| 51 | Color(light: 0x1D6E8A, dark: 0x5DB6D2), Color(light: 0x3BA49C, dark: 0x59C9BF), Color(light: 0xC9853A, dark: 0xE3A45A), |
| 52 | Color(light: 0x7A6CB0, dark: 0xA79AE0), Color(light: 0xC2566B, dark: 0xE57F93), Color(light: 0x6C8F3A, dark: 0x9DC266), |
| 53 | Color(light: 0x8E9DA6, dark: 0x9FB0BA) |
| 54 | ] |
| 55 | /// The gray used for "Other sites" when a chart groups the long tail. |
| 56 | public static let other = series[6] |
| 57 | public static func color(for site: Site) -> Color { series[site.colorIndex % series.count] } |
| 58 | |
| 59 | // MARK: Type |
| 60 | |
| 61 | /// Registers the bundled fonts for this process. Call once at launch, before the first view draws. |
| 62 | public static func registerFonts() { |
| 63 | guard let urls = Bundle.module.urls(forResourcesWithExtension: "ttf", subdirectory: "Fonts"), !urls.isEmpty else { return } |
| 64 | CTFontManagerRegisterFontURLs(urls as CFArray, .process, true, nil) |
| 65 | } |
| 66 | |
| 67 | public enum Sans: String { case regular = "InstrumentSans-Regular", medium = "InstrumentSans-Medium", semibold = "InstrumentSans-SemiBold" } |
| 68 | public static func sans(_ size: CGFloat, _ weight: Sans = .regular, relativeTo style: Font.TextStyle = .body) -> Font { |
| 69 | .custom(weight.rawValue, size: size, relativeTo: style) |
| 70 | } |
| 71 | public static func display(_ size: CGFloat) -> Font { .custom("BricolageGrotesque-Bold", size: size, relativeTo: .largeTitle) } |
| 72 | public static func figure(_ size: CGFloat, medium: Bool = false) -> Font { |
| 73 | .custom(medium ? "IBMPlexMono-Medium" : "IBMPlexMono-Regular", size: size, relativeTo: .body).monospacedDigit() |
| 74 | } |
| 75 | public static let body = sans(14) |
| 76 | |
| 77 | #if os(iOS) |
| 78 | static let phone = true |
| 79 | #else |
| 80 | static let phone = false |
| 81 | #endif |
| 82 | public static var screenTitle: Font { display(phone ? 32 : 28) } |
| 83 | public static var kpi: Font { display(30) } |
| 84 | public static var hero: Font { display(phone ? 44 : 34) } |
| 85 | public static var cardTitle: Font { sans(phone ? 17 : 14, .semibold, relativeTo: .headline) } |
| 86 | public static var text: Font { sans(phone ? 16 : 14) } |
| 87 | public static var rowTitle: Font { sans(phone ? 16 : 13.5, .medium) } |
| 88 | public static var secondary: Font { sans(phone ? 14 : 12.5, relativeTo: .subheadline) } |
| 89 | public static var caption: Font { sans(phone ? 12 : 11.5, relativeTo: .caption) } |
| 90 | public static var fieldLabel: Font { sans(phone ? 14 : 12, .semibold, relativeTo: .subheadline) } |
| 91 | public static var sectionLabel: Font { sans(phone ? 12 : 10.5, .semibold, relativeTo: .caption) } |
| 92 | public static var code: Font { figure(phone ? 13 : 12) } |
| 93 | |
| 94 | /// Full numbers below 100,000 ("6,284"), compact above ("128K"). |
| 95 | public static func number(_ v: Double) -> String { |
| 96 | abs(v) < 100_000 ? v.formatted(.number.precision(.fractionLength(0))) : v.formatted(.number.notation(.compactName)) |
| 97 | } |
| 98 | } |
| 99 | |
| 100 | public extension Color { |
| 101 | init(light: UInt32, dark: UInt32) { |
| 102 | #if os(macOS) |
| 103 | self.init(nsColor: NSColor(name: nil) { $0.bestMatch(from: [.darkAqua, .aqua]) == .darkAqua ? NSColor(hex: dark) : NSColor(hex: light) }) |
| 104 | #else |
| 105 | self.init(uiColor: UIColor { $0.userInterfaceStyle == .dark ? UIColor(hex: dark) : UIColor(hex: light) }) |
| 106 | #endif |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | #if os(macOS) |
| 111 | extension NSColor { convenience init(hex: UInt32) { self.init(srgbRed: CGFloat(hex >> 16 & 0xff) / 255, green: CGFloat(hex >> 8 & 0xff) / 255, blue: CGFloat(hex & 0xff) / 255, alpha: 1) } } |
| 112 | #else |
| 113 | extension UIColor { convenience init(hex: UInt32) { self.init(red: CGFloat(hex >> 16 & 0xff) / 255, green: CGFloat(hex >> 8 & 0xff) / 255, blue: CGFloat(hex & 0xff) / 255, alpha: 1) } } |
| 114 | #endif |
| 115 | |
| 116 | /// "+60%" in green, "−12%" in red, "new" when there is nothing to compare with. |
| 117 | public struct DeltaLabel: View { |
| 118 | let current: Double, previous: Double |
| 119 | public init(current: Double, previous: Double) { self.current = current; self.previous = previous } |
| 120 | public var body: some View { |
| 121 | if previous <= 0 { |
| 122 | Text(current > 0 ? "new" : "–").font(Tide.figure(12, medium: true)).foregroundStyle(Tide.muted) |
| 123 | } else { |
| 124 | let p = Int(((current - previous) / previous * 100).rounded()) |
| 125 | // Ten times or more reads better as a multiple than as "+4000%". |
| 126 | let text = current >= previous * 10 ? "×\(Int(current / previous))" : (p >= 0 ? "+\(p)%" : "−\(abs(p))%") |
| 127 | Text(text).font(Tide.figure(12, medium: true)).foregroundStyle(p >= 0 ? Tide.up : Tide.down).lineLimit(1).fixedSize() |
| 128 | } |
| 129 | } |
| 130 | } |
| 131 | |
| 132 | /// A number tile: label, big value, change vs the period before. With `selected`, it doubles as a tab. |
| 133 | public struct KPITile: View { |
| 134 | let label: String, value: Double, previous: Double, footer: String |
| 135 | var selected: Bool? |
| 136 | @State private var hover = false |
| 137 | public init(_ label: String, value: Double, previous: Double, footer: String = "vs previous period", selected: Bool? = nil) { |
| 138 | self.label = label; self.value = value; self.previous = previous; self.footer = footer; self.selected = selected |
| 139 | } |
| 140 | public var body: some View { |
| 141 | let isSelected = selected == true |
| 142 | VStack(alignment: .leading, spacing: 3) { |
| 143 | Text(label).font(Tide.caption).foregroundStyle(isSelected ? Tide.accent : Tide.muted) |
| 144 | Text(Tide.number(value)).font(Tide.kpi).foregroundStyle(Tide.ink).lineLimit(1).minimumScaleFactor(0.6) |
| 145 | ViewThatFits(in: .horizontal) { |
| 146 | HStack(spacing: 5) { DeltaLabel(current: value, previous: previous); Text(footer).font(Tide.caption).foregroundStyle(Tide.muted).lineLimit(1).fixedSize() } |
| 147 | VStack(alignment: .leading, spacing: 1) { DeltaLabel(current: value, previous: previous); Text(footer).font(Tide.caption).foregroundStyle(Tide.muted).lineLimit(1).minimumScaleFactor(0.8) } |
| 148 | } |
| 149 | } |
| 150 | .frame(maxWidth: .infinity, maxHeight: .infinity, alignment: .topLeading) |
| 151 | .padding(.vertical, 12).padding(.horizontal, 14) |
| 152 | .background(isSelected ? Tide.surface : Tide.sunk, in: RoundedRectangle(cornerRadius: 10)) |
| 153 | .overlay { |
| 154 | RoundedRectangle(cornerRadius: 10) |
| 155 | .strokeBorder(isSelected ? Tide.accent : (hover && selected != nil ? Tide.line2 : .clear), lineWidth: isSelected ? 1.5 : 1) |
| 156 | } |
| 157 | .onHover { hover = $0 } |
| 158 | .accessibilityElement(children: .combine) |
| 159 | .accessibilityAddTraits(isSelected ? .isSelected : []) |
| 160 | } |
| 161 | } |