SPB Git

Repository Public

NVSignatureMotions.swift 11.2 KB 292 lines swift

1// SP Notes Core2// File: Sources/SPNotesDesign/NVSignatureMotions.swift3// Description: The seven signature motions of the Note Visual System 2.0 as reusable transitions, modifiers, shapes and4//              containers. Each degrades to a plain cross-fade of at most 120 ms under Reduce Motion.5//6// Author: Simon-Pierre Boucher7// Contact: contact@spboucher.ai8import SwiftUI910// MARK: - Transition set1112private struct NVBlurModifier: ViewModifier {13    let radius: CGFloat14    func body(content: Content) -> some View { content.blur(radius: radius) }15}1617/// The transitions of the signature motions. Pass the environment's `accessibilityReduceMotion`: when true every18/// transition is a plain opacity cross-fade. Pair them with `NVMotion.animation(_:reduceMotion:)`.19public enum NVTransition {20    /// 1 · Living Pages: rise 6 pt and fade.21    public static func blockReveal(reduceMotion: Bool) -> AnyTransition {22        reduceMotion ? .opacity : .opacity.combined(with: .offset(y: NVMotion.revealRise))23    }2425    /// 2 · Morphing Editor: toolbar content swaps with a slight scale and blur.26    public static func morph(reduceMotion: Bool) -> AnyTransition {27        guard !reduceMotion else { return .opacity }28        return .opacity.combined(with: .scale(scale: 0.94))29            .combined(with: .modifier(active: NVBlurModifier(radius: 4), identity: NVBlurModifier(radius: 0)))30    }3132    /// 5 · Intelligent Panels: slide and blur in from `edge`.33    public static func panel(edge: Edge, reduceMotion: Bool) -> AnyTransition {34        guard !reduceMotion else { return .opacity }35        return .move(edge: edge).combined(with: .opacity)36            .combined(with: .modifier(active: NVBlurModifier(radius: 10), identity: NVBlurModifier(radius: 0)))37    }3839    /// 6 · Quick Capture: scale-in (use with `.fast`, 160 ms).40    public static func captureScaleIn(reduceMotion: Bool) -> AnyTransition {41        reduceMotion ? .opacity : .opacity.combined(with: .scale(scale: 0.92))42    }4344    /// 7 · Semantic Zoom: cross-dissolve between levels.45    public static func semanticZoom(reduceMotion: Bool) -> AnyTransition { .opacity }46}4748// MARK: - 1 · Living Pages4950/// Staggered block reveal: the block rises 6 pt and fades in, 18 ms after the previous one; only the first 2451/// blocks are animated. Under Reduce Motion: a fade of at most 120 ms, no movement, no stagger.52public struct NVBlockReveal: ViewModifier {53    let index: Int54    @Environment(\.accessibilityReduceMotion) private var reduceMotion55    @State private var shown = false5657    public init(index: Int) { self.index = index }5859    public func body(content: Content) -> some View {60        content61            .opacity(shown ? 1 : 0)62            .offset(y: shown || reduceMotion ? 0 : NVMotion.revealRise)63            .onAppear {64                guard !shown else { return }65                guard NVMotion.isStaggered(index: index) else { shown = true; return }66                let delay = NVMotion.staggerDelay(index: index, reduceMotion: reduceMotion)67                withAnimation(NVMotion.animation(.base, reduceMotion: reduceMotion).delay(delay)) { shown = true }68            }69    }70}7172extension View {73    /// Living Pages reveal of block number `index` (0-based position in the page).74    public func nvBlockReveal(index: Int) -> some View { modifier(NVBlockReveal(index: index)) }75}7677// MARK: - 2 · Morphing Editor7879/// A container whose content morphs when `id` changes (the selected block type of an editor toolbar).80public struct NVMorphingToolbar<ID: Hashable, Content: View>: View {81    let id: ID82    let content: Content83    @Environment(\.accessibilityReduceMotion) private var reduceMotion8485    public init(id: ID, @ViewBuilder content: () -> Content) {86        self.id = id87        self.content = content()88    }8990    public var body: some View {91        ZStack {92            content93                .id(id)94                .transition(NVTransition.morph(reduceMotion: reduceMotion))95        }96        .animation(NVMotion.animation(.spring, reduceMotion: reduceMotion), value: id)97    }98}99100// MARK: - 3 · Knowledge Connections101102/// A graph edge between two points, drawn up to `progress` (0…1, animatable): a quadratic curve bent by103/// `curvature` (fraction of the length, 0 = straight line).104public struct NVEdgeShape: Shape {105    public var from: CGPoint106    public var to: CGPoint107    public var curvature: CGFloat108    public var progress: CGFloat109110    public init(from: CGPoint, to: CGPoint, curvature: CGFloat = 0, progress: CGFloat = 1) {111        self.from = from112        self.to = to113        self.curvature = curvature114        self.progress = progress115    }116117    public var animatableData: CGFloat {118        get { progress }119        set { progress = newValue }120    }121122    public func path(in rect: CGRect) -> Path {123        var path = Path()124        path.move(to: from)125        if curvature == 0 {126            path.addLine(to: to)127        } else {128            let mid = CGPoint(x: (from.x + to.x) / 2, y: (from.y + to.y) / 2)129            let dx = to.x - from.x, dy = to.y - from.y130            path.addQuadCurve(to: to, control: CGPoint(x: mid.x - dy * curvature, y: mid.y + dx * curvature))131        }132        return path.trimmedPath(from: 0, to: min(1, max(0, progress)))133    }134}135136/// An edge that draws itself when it appears (Lumen stroke by default). Under Reduce Motion it fades in complete.137public struct NVEdgeLine<S: ShapeStyle>: View {138    let from: CGPoint139    let to: CGPoint140    let curvature: CGFloat141    let lineWidth: CGFloat142    let delay: Double143    let style: S144    @Environment(\.accessibilityReduceMotion) private var reduceMotion145    @State private var drawn = false146147    public init(from: CGPoint, to: CGPoint, curvature: CGFloat = 0, lineWidth: CGFloat = 1.5, delay: Double = 0, style: S) {148        self.from = from149        self.to = to150        self.curvature = curvature151        self.lineWidth = lineWidth152        self.delay = delay153        self.style = style154    }155156    public var body: some View {157        NVEdgeShape(from: from, to: to, curvature: curvature, progress: drawn || reduceMotion ? 1 : 0)158            .stroke(style, style: StrokeStyle(lineWidth: lineWidth, lineCap: .round))159            .opacity(drawn ? 1 : 0)160            .accessibilityHidden(true)161            .onAppear {162                guard !drawn else { return }163                withAnimation(NVMotion.animation(.slow, reduceMotion: reduceMotion).delay(reduceMotion ? 0 : delay)) { drawn = true }164            }165    }166}167168extension NVEdgeLine where S == LinearGradient {169    /// An edge stroked with the Lumen gradient.170    public init(from: CGPoint, to: CGPoint, curvature: CGFloat = 0, lineWidth: CGFloat = 1.5, delay: Double = 0) {171        self.init(from: from, to: to, curvature: curvature, lineWidth: lineWidth, delay: delay, style: NVColor.lumen)172    }173}174175// MARK: - 4 · Fluid Navigation176177/// Shared-element title: the same `id` in the same namespace on the list row and on the page title makes the178/// title travel between them. Under Reduce Motion the geometry is not matched and the title cross-fades.179public struct NVSharedTitle<ID: Hashable>: ViewModifier {180    let id: ID181    let namespace: Namespace.ID182    let isSource: Bool183    @Environment(\.accessibilityReduceMotion) private var reduceMotion184185    public func body(content: Content) -> some View {186        if reduceMotion {187            content.transition(.opacity)188        } else {189            content.matchedGeometryEffect(id: id, in: namespace, properties: .frame, anchor: .topLeading, isSource: isSource)190        }191    }192}193194extension View {195    /// Fluid Navigation: marks a title as the shared element `id` of `namespace`.196    public func nvSharedTitle<ID: Hashable>(id: ID, in namespace: Namespace.ID, isSource: Bool = true) -> some View {197        modifier(NVSharedTitle(id: id, namespace: namespace, isSource: isSource))198    }199}200201// MARK: - 5 · Intelligent Panels202203/// Gives a panel its edge slide + blur transition (Reduce Motion aware). Toggle the panel inside204/// `withNVAnimation(.base, reduceMotion:)` or under `.nvAnimation(.base, value:)`.205public struct NVPanelTransition: ViewModifier {206    let edge: Edge207    @Environment(\.accessibilityReduceMotion) private var reduceMotion208209    public func body(content: Content) -> some View {210        content.transition(NVTransition.panel(edge: edge, reduceMotion: reduceMotion))211    }212}213214extension View {215    /// Intelligent Panels: the panel slides and blurs in from `edge`.216    public func nvPanelTransition(edge: Edge) -> some View { modifier(NVPanelTransition(edge: edge)) }217}218219// MARK: - 6 · Quick Capture220221/// Lumen pulse played each time `trigger` changes (a capture was saved): a gradient ring expands and fades.222/// Under Reduce Motion: a brief static ring that fades, no scaling.223public struct NVCapturePulse<Trigger: Equatable>: ViewModifier {224    let trigger: Trigger225    let radius: CGFloat226    @Environment(\.accessibilityReduceMotion) private var reduceMotion227    @State private var pulsing = false228229    public func body(content: Content) -> some View {230        content231            .overlay {232                NVRadius.shape(radius)233                    .strokeBorder(NVColor.lumen, lineWidth: 2)234                    .scaleEffect(pulsing && !reduceMotion ? 1.06 : 1)235                    .opacity(pulsing ? 0 : 1)236                    .opacity(armed ? 1 : 0)237                    .allowsHitTesting(false)238                    .accessibilityHidden(true)239            }240            .onChange(of: trigger) {241                armed = true242                pulsing = false243                withAnimation(NVMotion.animation(.ambient, reduceMotion: reduceMotion)) { pulsing = true }244            }245    }246247    @State private var armed = false248}249250/// Scale-in of the quick capture surface (160 ms, under the 200 ms budget).251public struct NVCaptureScaleIn: ViewModifier {252    @Environment(\.accessibilityReduceMotion) private var reduceMotion253254    public func body(content: Content) -> some View {255        content.transition(NVTransition.captureScaleIn(reduceMotion: reduceMotion))256    }257}258259extension View {260    /// Quick Capture: plays the Lumen pulse when `trigger` changes.261    public func nvCapturePulse<Trigger: Equatable>(trigger: Trigger, radius: CGFloat = NVRadius.lg) -> some View {262        modifier(NVCapturePulse(trigger: trigger, radius: radius))263    }264265    /// Quick Capture: scale-in transition of the capture surface (animate with `.fast`).266    public func nvCaptureScaleIn() -> some View { modifier(NVCaptureScaleIn()) }267}268269// MARK: - 7 · Semantic Zoom270271/// Cross-dissolves its content when `level` changes. The container keeps its frame so that the caller's scroll272/// anchor (a `ScrollPosition` / `scrollPosition(id:)` owned by the app) stays valid across the change.273public struct NVSemanticZoom<Level: Hashable, Content: View>: View {274    let level: Level275    let content: (Level) -> Content276    @Environment(\.accessibilityReduceMotion) private var reduceMotion277278    public init(level: Level, @ViewBuilder content: @escaping (Level) -> Content) {279        self.level = level280        self.content = content281    }282283    public var body: some View {284        ZStack(alignment: .top) {285            content(level)286                .id(level)287                .transition(NVTransition.semanticZoom(reduceMotion: reduceMotion))288        }289        .animation(NVMotion.animation(.base, reduceMotion: reduceMotion), value: level)290    }291}292
Files main
Menu

Theme