Repository Public License
1import SwiftUI2#if canImport(UIKit)3import UIKit4#endif5#if canImport(AppKit)6import AppKit7#endif89/// Motion tokens. Everything is a spring, so every animation can be interrupted and retargeted mid-flight.10public enum LunarMotion: String, Sendable, CaseIterable {11 /// Hover, press, small state changes.12 case quick13 /// Default for controls: selection, toggles, chips.14 case snappy15 /// Panels, sidebars, sheets, geometric transitions.16 case smooth17 /// Large spatial moves (Orbit view, new-tab entrance).18 case gentle1920 public var response: Double {21 switch self {22 case .quick: 0.1823 case .snappy: 0.2824 case .smooth: 0.4225 case .gentle: 0.6226 }27 }2829 public var dampingFraction: Double {30 switch self {31 case .quick: 1.032 case .snappy: 0.8633 case .smooth: 0.8834 case .gentle: 0.935 }36 }3738 /// The interruptible spring.39 public var animation: Animation {40 .spring(response: response, dampingFraction: dampingFraction, blendDuration: 0)41 }4243 /// The animation to use given the Reduce Motion setting: a short cross-fade instead of spatial movement.44 public func animation(reduceMotion: Bool) -> Animation {45 reduceMotion ? .easeOut(duration: 0.14) : animation46 }47}4849extension View {50 /// Animates changes of `value` with a Lunar spring, falling back to a short fade under Reduce Motion.51 public func lunarAnimation<V: Equatable>(_ motion: LunarMotion = .snappy, value: V) -> some View {52 modifier(LunarAnimationModifier(motion: motion, value: value))53 }5455 /// Geometric appear/disappear transition (scale + fade from an anchor); opacity only under Reduce Motion.56 public func lunarTransition(anchor: UnitPoint = .center) -> some View {57 modifier(LunarTransitionModifier(anchor: anchor))58 }59}6061private struct LunarAnimationModifier<V: Equatable>: ViewModifier {62 @Environment(\.accessibilityReduceMotion) private var reduceMotion63 let motion: LunarMotion64 let value: V6566 func body(content: Content) -> some View {67 content.animation(motion.animation(reduceMotion: reduceMotion), value: value)68 }69}7071private struct LunarTransitionModifier: ViewModifier {72 @Environment(\.accessibilityReduceMotion) private var reduceMotion73 let anchor: UnitPoint7475 func body(content: Content) -> some View {76 content.transition(reduceMotion ? .opacity : .scale(scale: 0.94, anchor: anchor).combined(with: .opacity))77 }78}7980/// Haptic feedback. Real haptics on iPhone; on the Mac the trackpad performer is used for the events that have an81/// equivalent, and the rest is silent.82@MainActor83public enum LunarHaptics {84 public enum Event: Sendable {85 case selection, lightImpact, mediumImpact, success, warning, error86 }8788 public static func play(_ event: Event) {89 #if os(iOS)90 switch event {91 case .selection: UISelectionFeedbackGenerator().selectionChanged()92 case .lightImpact: UIImpactFeedbackGenerator(style: .light).impactOccurred()93 case .mediumImpact: UIImpactFeedbackGenerator(style: .medium).impactOccurred()94 case .success: UINotificationFeedbackGenerator().notificationOccurred(.success)95 case .warning: UINotificationFeedbackGenerator().notificationOccurred(.warning)96 case .error: UINotificationFeedbackGenerator().notificationOccurred(.error)97 }98 #elseif os(macOS)99 switch event {100 case .selection, .lightImpact:101 NSHapticFeedbackManager.defaultPerformer.perform(.alignment, performanceTime: .default)102 case .mediumImpact:103 NSHapticFeedbackManager.defaultPerformer.perform(.levelChange, performanceTime: .default)104 case .success, .warning, .error:105 break106 }107 #endif108 }109}110