|
| 1 | +// |
| 2 | +// TransactionAnimation.swift |
| 3 | +// OpenSwiftUICore |
| 4 | +// |
| 5 | +// Audited for iOS 18.0 |
| 6 | +// Status: Complete |
| 7 | +// ID: 39EC6D46662E6D7A6963F5C611934B0A (SwiftUI) |
| 8 | +// ID: D98E9A1069CEEADA58829ED440E36F30 (SwiftUICore) |
| 9 | + |
| 10 | +extension Transaction { |
| 11 | + /// Creates a transaction and assigns its animation property. |
| 12 | + /// |
| 13 | + /// - Parameter animation: The animation to perform when the current state |
| 14 | + /// changes. |
| 15 | + public init(animation: Animation?) { |
| 16 | + self.init() |
| 17 | + self.animation = animation |
| 18 | + } |
| 19 | + |
| 20 | + /// The animation, if any, associated with the current state change. |
| 21 | + public var animation: Animation? { |
| 22 | + get { self[AnimationKey.self] } |
| 23 | + set { self[AnimationKey.self] = newValue } |
| 24 | + } |
| 25 | + |
| 26 | + package var effectiveAnimation: Animation? { |
| 27 | + animation ?? (tracksVelocity ? .velocityTracking : nil) |
| 28 | + } |
| 29 | + |
| 30 | + package var _animationFrameInterval: Double? { |
| 31 | + get { self[AnimationFrameIntervalKey.self] } |
| 32 | + set { self[AnimationFrameIntervalKey.self] = newValue } |
| 33 | + } |
| 34 | + |
| 35 | + package var _animationReason: UInt32? { |
| 36 | + get { self[AnimationReasonKey.self] } |
| 37 | + set { self[AnimationReasonKey.self] = newValue } |
| 38 | + } |
| 39 | + |
| 40 | + package var isAnimated: Bool { |
| 41 | + guard let animation, |
| 42 | + !disablesAnimations else { |
| 43 | + return false |
| 44 | + } |
| 45 | + return true |
| 46 | + } |
| 47 | + |
| 48 | + /// A Boolean value that indicates whether views should disable animations. |
| 49 | + /// |
| 50 | + /// This value is `true` during the initial phase of a two-part transition |
| 51 | + /// update, to prevent ``View/animation(_:)`` from inserting new animations |
| 52 | + /// into the transaction. |
| 53 | + public var disablesAnimations: Bool { |
| 54 | + get { self[DisablesAnimationsKey.self] } |
| 55 | + set { self[DisablesAnimationsKey.self] = newValue } |
| 56 | + } |
| 57 | + |
| 58 | + package var disablesContentTransitions: Bool { |
| 59 | + get { self[DisablesContentTransactionKey.self] } |
| 60 | + set { self[DisablesContentTransactionKey.self] = newValue } |
| 61 | + } |
| 62 | + |
| 63 | + package mutating func disableAnimations() { |
| 64 | + animation = nil |
| 65 | + disablesAnimations = true |
| 66 | + } |
| 67 | + |
| 68 | + package var animationIgnoringTransitionPhase: Animation? { |
| 69 | + guard disablesAnimations else { |
| 70 | + return animation |
| 71 | + } |
| 72 | + var result: Animation? |
| 73 | + forEach(keyType: AnimationKey.self) { animation, stop in |
| 74 | + guard let animation else { |
| 75 | + return |
| 76 | + } |
| 77 | + result = animation |
| 78 | + stop = true |
| 79 | + } |
| 80 | + return result |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +private struct AnimationKey: TransactionKey { |
| 85 | + static var defaultValue: Animation? { nil } |
| 86 | +} |
| 87 | + |
| 88 | +private struct DisablesAnimationsKey: TransactionKey { |
| 89 | + static var defaultValue: Bool { false } |
| 90 | +} |
| 91 | + |
| 92 | +private struct AnimationFrameIntervalKey: TransactionKey { |
| 93 | + static var defaultValue: Double? { nil } |
| 94 | +} |
| 95 | + |
| 96 | +private struct AnimationReasonKey: TransactionKey { |
| 97 | + static var defaultValue: UInt32? { nil } |
| 98 | +} |
| 99 | + |
| 100 | +private struct DisablesContentTransactionKey: TransactionKey { |
| 101 | + static var defaultValue: Bool { false } |
| 102 | +} |
0 commit comments