SPB Git

Repository Public

MotionTokens.swift 4.5 KB 99 lines swift

1import Foundation23/// AURA motion tokens. Every animation in SP Browser names one of these; durations and spring parameters live here only.4/// This file is UI-free so the policy (Reduce Motion, Low Power) can be unit-tested; `Shared/Aura/AuraMotion.swift` turns specs into SwiftUI animations.5enum MotionToken: String, CaseIterable, Sendable {6    /// State flips that must not be felt as motion (hover tint, selection).7    case instant8    /// Small controls: icon swaps, chips, toasts.9    case quick10    /// Panels, sidebars, suggestion lists.11    case standard12    /// Signature moves: tab morph, address bar transformation, constellation entry.13    case expressive14    /// Rare, large scene changes (entering Canvas or Constellation full-window).15    case cinematic16    /// Default physical spring for things that settle.17    case spring18    /// Tracks a finger or pointer and can be interrupted at any time.19    case interactive20}2122struct MotionSpec: Equatable, Sendable {23    enum Curve: Equatable, Sendable {24        case easeOut25        case easeInOut26        /// A spring described by its response (seconds) and damping fraction (1 = critically damped).27        case spring(response: Double, damping: Double)28    }29    /// Nominal duration in seconds (for springs: the settling time used for sequencing).30    var duration: Double31    var curve: Curve32    /// True when the spec replaces a spatial move by a plain cross-fade (Reduce Motion).33    var isCrossfade = false34}3536/// What the system and the user currently allow.37struct MotionPolicy: Equatable, Sendable {38    var reduceMotion = false39    var lowPower = false40    /// The window or scene is not frontmost: nothing decorative should run.41    var isBackground = false4243    static let standard = MotionPolicy()4445    /// Ambient backgrounds, shimmer and other motion that carries no information.46    var allowsDecorative: Bool { !reduceMotion && !lowPower && !isBackground }47    /// Scale / offset moves. Under Reduce Motion they become cross-fades.48    var allowsSpatial: Bool { !reduceMotion }49    /// Frame interval for decorative timelines (seconds): 20 fps is plenty for slow ambient light.50    var decorativeFrameInterval: Double { 1.0 / 20.0 }5152    func spec(for token: MotionToken) -> MotionSpec? {53        let base = Self.base(token)54        if reduceMotion {55            // No travel, no bounce: a short fade keeps the change legible without movement.56            switch token {57            case .instant: return nil58            case .interactive: return MotionSpec(duration: 0.01, curve: .easeOut, isCrossfade: true)59            default: return MotionSpec(duration: min(base.duration, 0.16), curve: .easeOut, isCrossfade: true)60            }61        }62        if lowPower, token == .cinematic || token == .expressive {63            // Shorter and without overshoot: fewer frames rendered.64            return MotionSpec(duration: 0.24, curve: .easeOut)65        }66        return base67    }6869    static func base(_ token: MotionToken) -> MotionSpec {70        switch token {71        case .instant: return MotionSpec(duration: 0.08, curve: .easeOut)72        case .quick: return MotionSpec(duration: 0.16, curve: .easeOut)73        case .standard: return MotionSpec(duration: 0.26, curve: .spring(response: 0.30, damping: 0.90))74        case .expressive: return MotionSpec(duration: 0.42, curve: .spring(response: 0.42, damping: 0.80))75        case .cinematic: return MotionSpec(duration: 0.62, curve: .spring(response: 0.60, damping: 0.86))76        case .spring: return MotionSpec(duration: 0.36, curve: .spring(response: 0.36, damping: 0.74))77        case .interactive: return MotionSpec(duration: 0.20, curve: .spring(response: 0.22, damping: 0.86))78        }79    }80}8182/// Decides how a drag that was let go should end: commit or fall back. Shared by every interactive gesture.83enum GestureResolution {84    /// - Parameters: `progress` 0…1 along the gesture, `velocity` in progress units per second (signed).85    static func shouldCommit(progress: Double, velocity: Double, threshold: Double = 0.5, flick: Double = 1.6) -> Bool {86        if velocity > flick { return true }87        if velocity < -flick { return false }88        return progress >= threshold89    }9091    /// Rubber-band resistance beyond the allowed range, as on system scroll views.92    static func rubberBand(_ offset: Double, limit: Double, coefficient: Double = 0.55) -> Double {93        guard limit > 0 else { return 0 }94        let sign: Double = offset < 0 ? -1 : 195        let x = abs(offset)96        return sign * (1 - (1 / ((x * coefficient / limit) + 1))) * limit97    }98}99
Files main
Menu

Theme