Skip to content

Commit ac04fce

Browse files
authored
Update Animatable API and implementation (#309)
1 parent c39a762 commit ac04fce

File tree

7 files changed

+187
-105
lines changed

7 files changed

+187
-105
lines changed

Package.resolved

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
//
2+
// Animatable.swift
3+
// OpenSwiftUICore
4+
//
5+
// Status: Complete
6+
7+
public import Foundation
8+
package import OpenGraphShims
9+
10+
// MARK: - Animatable [6.4.41]
11+
12+
/// A type that describes how to animate a property of a view.
13+
public protocol Animatable {
14+
/// The type defining the data to animate.
15+
associatedtype AnimatableData: VectorArithmetic
16+
17+
/// The data to animate.
18+
var animatableData: AnimatableData { get set }
19+
20+
/// Replaces `value` with an animated version of the value, using
21+
/// `inputs`.
22+
static func _makeAnimatable(value: inout _GraphValue<Self>, inputs: _GraphInputs)
23+
}
24+
25+
// MARK: - Animateble + Extension [6.4.41]
26+
27+
extension Animatable where Self: VectorArithmetic {
28+
public var animatableData: Self {
29+
get { self }
30+
set { self = newValue }
31+
}
32+
}
33+
34+
extension Animatable where AnimatableData == EmptyAnimatableData {
35+
public var animatableData: EmptyAnimatableData {
36+
@inlinable
37+
get { EmptyAnimatableData() }
38+
@inlinable
39+
set {}
40+
}
41+
42+
public static func _makeAnimatable(value _: inout _GraphValue<Self>, inputs _: _GraphInputs) {}
43+
}
44+
45+
extension Animatable {
46+
package static func makeAnimatable(value: inout _GraphValue<Self>, inputs: _GraphInputs) {
47+
_makeAnimatable(value: &value, inputs: inputs)
48+
}
49+
}
50+
51+
extension Attribute where Value: Animatable {
52+
package func animated(inputs: _GraphInputs) -> Attribute<Value> {
53+
var value = _GraphValue(self)
54+
Value._makeAnimatable(value: &value, inputs: inputs)
55+
return value.value
56+
}
57+
}
58+
59+
extension Animatable {
60+
public static func _makeAnimatable(value: inout _GraphValue<Self>, inputs: _GraphInputs) {
61+
guard MemoryLayout<AnimatableData>.size != 0,
62+
!inputs.animationsDisabled else {
63+
return
64+
}
65+
let animatable = AnimatableAttribute(
66+
source: value.value,
67+
phase: inputs.phase,
68+
time: inputs.time,
69+
transaction: inputs.transaction,
70+
environment: inputs.environment
71+
)
72+
let newValue = _GraphValue(animatable)
73+
value = newValue
74+
value.value.setFlags(.active, mask: .mask)
75+
}
76+
}
77+
78+
// MARK: - EmptyAnimatableData [6.4.41]
79+
80+
/// An empty type for animatable data.
81+
///
82+
/// This type is suitable for use as the `animatableData` property of
83+
/// types that do not have any animatable properties.
84+
@frozen
85+
public struct EmptyAnimatableData: VectorArithmetic {
86+
@inlinable
87+
public init() {}
88+
89+
@inlinable
90+
public static var zero: EmptyAnimatableData { .init() }
91+
92+
@inlinable
93+
public static func += (_: inout EmptyAnimatableData, _: EmptyAnimatableData) {}
94+
95+
@inlinable
96+
public static func -= (_: inout EmptyAnimatableData, _: EmptyAnimatableData) {}
97+
98+
@inlinable
99+
public static func + (_: EmptyAnimatableData, _: EmptyAnimatableData) -> EmptyAnimatableData { .zero }
100+
101+
@inlinable
102+
public static func - (_: EmptyAnimatableData, _: EmptyAnimatableData) -> EmptyAnimatableData { .zero }
103+
104+
@inlinable
105+
public mutating func scale(by _: Double) {}
106+
107+
@inlinable
108+
public var magnitudeSquared: Double { 0 }
109+
110+
public static func == (_: EmptyAnimatableData, _: EmptyAnimatableData) -> Bool { true }
111+
}
112+
113+
extension Double: Animatable {
114+
public typealias AnimatableData = Double
115+
}
116+
117+
extension CGFloat: Animatable {
118+
public typealias AnimatableData = CGFloat
119+
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package import OpenGraphShims
2+
3+
@frozen
4+
public struct Animation: Equatable {
5+
// var box: AnimationBoxBase
6+
public static func == (lhs: Animation, rhs: Animation) -> Bool {
7+
// TODO
8+
true
9+
}
10+
11+
public static var `default` = Animation()
12+
}
13+
14+
// MARK: - AnimatableAttribute [6.4.41] [TODO]
15+
16+
package struct AnimatableAttribute<Value>: StatefulRule, AsyncAttribute, ObservedAttribute, CustomStringConvertible where Value: Animatable {
17+
@Attribute var source: Value
18+
@Attribute var environment: EnvironmentValues
19+
var helper: AnimatableAttributeHelper<Value>
20+
21+
package init(
22+
source: Attribute<Value>,
23+
phase: Attribute<_GraphInputs.Phase>,
24+
time: Attribute<Time>,
25+
transaction: Attribute<Transaction>,
26+
environment: Attribute<EnvironmentValues>
27+
) {
28+
_source = source
29+
_environment = environment
30+
helper = .init(phase: phase, time: time, transaction: transaction)
31+
}
32+
33+
package mutating func updateValue() {
34+
// TODO
35+
}
36+
37+
package var description: String { "Animatable<\(Value.self)>" }
38+
39+
package mutating func destroy() {
40+
// helper.animatorState?.removeListeners()
41+
}
42+
}
43+
44+
// MARK: - AnimatableAttributeHelper [FIXME]
45+
46+
package struct AnimatableAttributeHelper<Value> where Value: Animatable {
47+
@Attribute var phase: _GraphInputs.Phase
48+
@Attribute var time: Time
49+
@Attribute var transaction: Transaction
50+
var previousModelData: Value.AnimatableData?
51+
// var animatorState: AnimatorState<Value.AnimatableData>?
52+
var resetSeed: UInt32 = 0
53+
54+
init(
55+
phase: Attribute<_GraphInputs.Phase>,
56+
time: Attribute<Time>,
57+
transaction: Attribute<Transaction>
58+
) {
59+
_phase = phase
60+
_time = time
61+
_transaction = transaction
62+
}
63+
}

Sources/OpenSwiftUICore/View/Animation/Animation/EmptyAnimatableData.swift

Lines changed: 0 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -5,45 +5,3 @@
55
// Audited for iOS 18.0
66
// Status: Complete
77

8-
/// An empty type for animatable data.
9-
///
10-
/// This type is suitable for use as the `animatableData` property of
11-
/// types that do not have any animatable properties.
12-
@frozen
13-
public struct EmptyAnimatableData: VectorArithmetic {
14-
@inlinable
15-
public init() {}
16-
17-
@inlinable
18-
public static var zero: EmptyAnimatableData { .init() }
19-
20-
@inlinable
21-
public static func += (_: inout EmptyAnimatableData, _: EmptyAnimatableData) {}
22-
23-
@inlinable
24-
public static func -= (_: inout EmptyAnimatableData, _: EmptyAnimatableData) {}
25-
26-
@inlinable
27-
public static func + (_: EmptyAnimatableData, _: EmptyAnimatableData) -> EmptyAnimatableData { .zero }
28-
29-
@inlinable
30-
public static func - (_: EmptyAnimatableData, _: EmptyAnimatableData) -> EmptyAnimatableData { .zero }
31-
32-
@inlinable
33-
public mutating func scale(by _: Double) {}
34-
35-
@inlinable
36-
public var magnitudeSquared: Double { 0 }
37-
38-
public static func == (_: EmptyAnimatableData, _: EmptyAnimatableData) -> Bool { true }
39-
}
40-
41-
public import Foundation
42-
43-
extension Double: Animatable {
44-
public typealias AnimatableData = Swift.Double
45-
}
46-
47-
extension CGFloat: Animatable {
48-
public typealias AnimatableData = CGFloat
49-
}

Sources/OpenSwiftUICore/View/Animation/Animation/TODO/Animatable.swift

Lines changed: 0 additions & 49 deletions
This file was deleted.

Sources/OpenSwiftUICore/View/Animation/Animation/TODO/Animation.swift

Lines changed: 0 additions & 10 deletions
This file was deleted.

Sources/OpenSwiftUICore/View/Animation/Animation/_VectorMath.swift renamed to Sources/OpenSwiftUICore/View/Animation/Animation/VectorMath.swift

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
//
2-
// _VectorMath.swift
2+
// VectorMath.swift
33
// OpenSwiftUICore
44
//
5-
// Audited for iOS 18.0
65
// Status: Complete
76

7+
// MARK: - VectorMath [6.4.41]
8+
89
/// Adds the "vector space" numeric operations for any type that
910
/// conforms to Animatable.
1011
public protocol _VectorMath: Animatable {}

0 commit comments

Comments
 (0)