SPB Git

Repository Public

Tokens.swift 6.3 KB 145 lines swift

1import Foundation23// SP Term Next design language: plain values shared by the chrome. No UI framework is imported4// here so the tokens stay unit-testable; the app turns them into SwiftUI fonts and animations.56/// Corner radii. Concentric shapes use `inner(of:inset:)` so nested corners stay parallel.7public enum Radius {8    public static let xs: Double = 49    public static let s: Double = 610    public static let m: Double = 811    public static let l: Double = 1012    public static let xl: Double = 1413    /// Radius of a shape nested `inset` points inside a shape of radius `outer`.14    public static func inner(of outer: Double, inset: Double) -> Double { max(outer - inset, 2) }15}1617public enum Space {18    public static let xxs: Double = 219    public static let xs: Double = 420    public static let s: Double = 821    public static let m: Double = 1222    public static let l: Double = 1623    public static let xl: Double = 2424}2526/// Type scale of the chrome (points). Terminal text is governed by the user's font setting.27public enum TypeScale {28    public static let micro: Double = 929    public static let caption: Double = 1030    public static let footnote: Double = 1131    public static let body: Double = 1232    public static let callout: Double = 1333    public static let title: Double = 1534}3536/// Parameters of a spring: `response` in seconds, `damping` fraction (1 = critically damped).37public struct SpringSpec: Sendable, Equatable {38    public var response: Double39    public var damping: Double40    public init(response: Double, damping: Double) { self.response = response; self.damping = damping }41}4243/// Motion tokens. Every animation of the chrome uses one of these, so the feel is uniform and44/// "Reduce Motion" can be honoured in a single place.45public enum MotionSpec {46    /// Hover, selection, small state changes.47    public static let snappy = SpringSpec(response: 0.24, damping: 0.86)48    /// Tabs, panes, palette.49    public static let smooth = SpringSpec(response: 0.34, damping: 0.88)50    /// Sidebar, inspector, mode changes.51    public static let gentle = SpringSpec(response: 0.46, damping: 0.92)52    /// Slow drift of the ambient gradient after an event.53    public static let ambientDuration: Double = 1.654    public static let fadeDuration: Double = 0.1655    /// Duration of the plain fade used instead of movement when motion is reduced.56    public static let reducedDuration: Double = 0.1257    public static let tooltipDelay: Double = 0.5558    /// How long the "command finished" feedback stays visible.59    public static let completionHold: Double = 460}6162public enum ChromeDensity: String, Codable, Sendable, CaseIterable { case compact, expanded }6364/// Heights of the chrome for a density.65public struct ChromeMetrics: Sendable, Equatable {66    public var titleBarHeight: Double67    public var tabHeight: Double68    public var statusBarHeight: Double69    public var paneHeaderHeight: Double7071    public static func metrics(for density: ChromeDensity) -> ChromeMetrics {72        switch density {73        case .compact: return ChromeMetrics(titleBarHeight: 38, tabHeight: 26, statusBarHeight: 24, paneHeaderHeight: 24)74        case .expanded: return ChromeMetrics(titleBarHeight: 52, tabHeight: 38, statusBarHeight: 28, paneHeaderHeight: 28)75        }76    }77}7879public extension RGBColor {80    /// Builds a colour from hue (degrees), saturation and lightness (0…1).81    init(hue: Double, saturation: Double, lightness: Double) {82        let h = (hue.truncatingRemainder(dividingBy: 360) + 360).truncatingRemainder(dividingBy: 360) / 6083        let s = min(max(saturation, 0), 1), l = min(max(lightness, 0), 1)84        let chroma = (1 - abs(2 * l - 1)) * s85        let x = chroma * (1 - abs(h.truncatingRemainder(dividingBy: 2) - 1))86        let (r, g, b): (Double, Double, Double)87        switch Int(h) {88        case 0: (r, g, b) = (chroma, x, 0)89        case 1: (r, g, b) = (x, chroma, 0)90        case 2: (r, g, b) = (0, chroma, x)91        case 3: (r, g, b) = (0, x, chroma)92        case 4: (r, g, b) = (x, 0, chroma)93        default: (r, g, b) = (chroma, 0, x)94        }95        let m = l - chroma / 296        func byte(_ v: Double) -> UInt8 { UInt8((min(max(v + m, 0), 1) * 255).rounded()) }97        self.init(byte(r), byte(g), byte(b))98    }99}100101/// Dynamic accent: the chrome takes a colour that says where the focused session runs.102public enum AccentEngine {103    /// Minimum contrast of an accent against the chrome it is drawn on (WCAG non-text: 3).104    public static let minimumContrast: Double = 3105106    /// FNV-1a: stable across launches and machines (Swift's `Hasher` is seeded per process).107    static func stableHash(_ text: String) -> UInt64 {108        var hash: UInt64 = 0xcbf2_9ce4_8422_2325109        for byte in text.lowercased().utf8 { hash = (hash ^ UInt64(byte)) &* 0x0000_0100_0000_01b3 }110        return hash111    }112113    /// Hue for a host name, in 80°…340°: reds and yellows are left to danger and warning states.114    public static func hue(forHost host: String) -> Double {115        80 + Double(stableHash(host) % 2600) / 10116    }117118    /// A stable, readable accent for a remote host: the same host always gets the same colour.119    public static func accent(forHost host: String, theme: TerminalTheme) -> RGBColor {120        readable(RGBColor(hue: hue(forHost: host), saturation: theme.isDark ? 0.72 : 0.78, lightness: theme.isDark ? 0.66 : 0.40), on: theme)121    }122123    /// Moves a colour away from the chrome background until it reaches the minimum contrast.124    public static func readable(_ color: RGBColor, on theme: TerminalTheme) -> RGBColor {125        let target = theme.isHighContrast ? 4.5 : minimumContrast126        let pole = theme.isDark ? RGBColor(255, 255, 255) : RGBColor(0, 0, 0)127        var result = color128        var step = 0129        while result.contrastRatio(with: theme.chromeBackground) < target, step < 20 {130            step += 1131            result = color.mixed(with: pole, amount: Double(step) * 0.05)132        }133        return result134    }135}136137public extension TerminalTheme {138    /// Header of a pane card: one step above the terminal background.139    var elevatedSurface: RGBColor { background.mixed(with: foreground, amount: isHighContrast ? 0.14 : 0.055) }140    /// Chrome tinted by the current accent; the base of the ambient gradient.141    func tintedChrome(accent: RGBColor, amount: Double = 0.07) -> RGBColor {142        chromeBackground.mixed(with: accent, amount: isHighContrast ? 0 : amount)143    }144}145
Files main
Menu

Theme