SPB Git

Repository Public

Motion.swift 6.8 KB 180 lines swift

1// =============================================================================2//                  SP BOUCHER | SOFTWARE & RESEARCH3//             ARTIFICIAL INTELLIGENCE • FINANCE • ENGINEERING4// =============================================================================5//6// Project          : QYVORA7// File             : Sources/QyvoraUI/Motion.swift8// Module           : Qyvora UI9// Repository       : https://git.spboucher.ai/qyvora10// Project Website  : Not specified11// Created          : 2026-12-0512// Last Modified    : 2026-12-0513//14// Author           : Simon-Pierre Boucher15// Position         : Professor of Finance16// Institution      : Université du Québec en Outaouais (UQO)17// Organization     : SP Boucher18// Website          : https://spboucher.ai19// Git              : https://git.spboucher.ai20//21// Preferred Contact: contact@spboucher.ai22// Development      : dev@spboucher.ai23// Support          : support@spboucher.ai24// Security/Admin   : admin@spboucher.ai25//26// Copyright (c) 202627// Simon-Pierre Boucher28// All rights reserved where applicable. This file is governed by the29// applicable project license, copyright notices and relevant law.30// Existing third-party rights and notices remain unaffected.31//32// License          : None specified (no license file in this repository)33//34// Do not embed credentials, private keys, access tokens,35// certificates or confidential personal information.36//37// =============================================================================38// END OF SP BOUCHER SOURCE HEADER39// =============================================================================4041import SwiftUI4243/// The three animations of the design system. Motion is short and has a purpose: it follows a press, a value that44/// changes or a surface that appears. Nothing loops except progress, and everything stops under Reduce Motion.45public enum QMotion {46    /// Presses, selection, small state changes.47    public static let standard = Animation.snappy(duration: 0.22)48    /// A surface that appears or leaves (toast, banner, result).49    public static let emphasized = Animation.spring(response: 0.42, dampingFraction: 0.86)50    /// Figures that change (balance, fiat value, percentage).51    public static let numeric = Animation.snappy(duration: 0.3)52    /// What replaces a movement when Reduce Motion is on: a short cross-fade.53    public static let reduced = Animation.easeInOut(duration: 0.12)5455    /// The animation to use given the accessibility setting.56    public static func resolved(_ animation: Animation, reduceMotion: Bool) -> Animation {57        reduceMotion ? reduced : animation58    }59}6061struct QAnimationModifier<V: Equatable>: ViewModifier {62    let animation: Animation63    let value: V64    @Environment(\.accessibilityReduceMotion) private var reduceMotion6566    func body(content: Content) -> some View {67        content.animation(QMotion.resolved(animation, reduceMotion: reduceMotion), value: value)68    }69}7071struct QNumericTransitionModifier: ViewModifier {72    @Environment(\.accessibilityReduceMotion) private var reduceMotion7374    func body(content: Content) -> some View {75        content.contentTransition(reduceMotion ? .opacity : .numericText())76    }77}7879struct QMatchedSourceModifier<ID: Hashable>: ViewModifier {80    let id: ID81    let namespace: Namespace.ID?8283    func body(content: Content) -> some View {84        #if os(iOS)85        if let namespace {86            content.matchedTransitionSource(id: id, in: namespace)87        } else {88            content89        }90        #else91        content92        #endif93    }94}9596struct QMatchedDestinationModifier<ID: Hashable>: ViewModifier {97    let id: ID98    let namespace: Namespace.ID?99    @Environment(\.accessibilityReduceMotion) private var reduceMotion100101    func body(content: Content) -> some View {102        #if os(iOS)103        if let namespace, !reduceMotion {104            content.navigationTransition(.zoom(sourceID: id, in: namespace))105        } else {106            content107        }108        #else109        content110        #endif111    }112}113114public extension View {115    /// `animation(_:value:)` with the shared curves; under Reduce Motion the change becomes a short cross-fade.116    func qAnimation<V: Equatable>(_ animation: Animation = QMotion.standard, value: V) -> some View {117        modifier(QAnimationModifier(animation: animation, value: value))118    }119120    /// Rolling digits for a figure that changes; a plain fade under Reduce Motion.121    func qNumericTransition() -> some View { modifier(QNumericTransitionModifier()) }122123    /// Marks a row (or its glyph) as the origin of the row → detail transition. Pass the same `id` and namespace to124    /// `qMatchedDestination` on the detail screen. A nil namespace disables the transition.125    func qMatchedSource<ID: Hashable>(id: ID, in namespace: Namespace.ID?) -> some View {126        modifier(QMatchedSourceModifier(id: id, namespace: namespace))127    }128129    /// The detail side of the row → detail transition: the screen grows out of its row. Under Reduce Motion the130    /// standard navigation transition is used instead.131    func qMatchedDestination<ID: Hashable>(id: ID, in namespace: Namespace.ID?) -> some View {132        modifier(QMatchedDestinationModifier(id: id, namespace: namespace))133    }134}135136/// Semantic events of the application mapped to system haptics, so that the same event feels the same everywhere.137public enum QHaptic: Sendable, Equatable {138    /// A choice moved (fee tier, chain, range).139    case selection140    /// A value went to the clipboard.141    case copied142    /// A switch or a hide/show control changed.143    case toggled144    /// A key of the PIN pad or a small press.145    case tap146    /// The slide or hold control reached its end: the signature is requested.147    case committed148    /// A transaction was accepted by the network.149    case sent150    /// Something needs attention before continuing.151    case warning152    /// An operation failed.153    case failed154155    public var feedback: SensoryFeedback {156        switch self {157        case .selection: return .selection158        case .copied: return .success159        case .toggled: return .impact(weight: .light)160        case .tap: return .impact(weight: .light, intensity: 0.6)161        case .committed: return .impact(weight: .heavy)162        case .sent: return .success163        case .warning: return .warning164        case .failed: return .error165        }166    }167}168169public extension View {170    /// Plays the haptic of a semantic event each time `trigger` changes.171    func qHaptic<T: Equatable>(_ event: QHaptic, trigger: T) -> some View {172        sensoryFeedback(event.feedback, trigger: trigger)173    }174175    /// Plays a haptic chosen from the new value of `trigger` (nil plays nothing).176    func qHaptic<T: Equatable>(trigger: T, _ event: @escaping (T) -> QHaptic?) -> some View {177        sensoryFeedback(trigger: trigger) { _, new in event(new)?.feedback }178    }179}180
Files main
Menu

Theme