diff --git a/Package.resolved b/Package.resolved index f7bd5a0f..2b15206c 100644 --- a/Package.resolved +++ b/Package.resolved @@ -7,7 +7,7 @@ "location" : "https://github.com/OpenSwiftUIProject/DarwinPrivateFrameworks.git", "state" : { "branch" : "main", - "revision" : "81e34b38e1bd8b2729cdc201c4c0f06746dd87b0" + "revision" : "88f975a6b211a5c9b3f2c26e55079b7d8f40275b" } }, { @@ -25,7 +25,7 @@ "location" : "https://github.com/OpenSwiftUIProject/OpenGraph", "state" : { "branch" : "main", - "revision" : "433aa3a58e5c871461328971cacb7bd09f519d9a" + "revision" : "fed93356e5876f1fc1ae452a9a799e481c8ea170" } }, { diff --git a/Sources/OpenSwiftUICore/View/Animation/Animation/Animatable.swift b/Sources/OpenSwiftUICore/View/Animation/Animation/Animatable.swift new file mode 100644 index 00000000..3cec021a --- /dev/null +++ b/Sources/OpenSwiftUICore/View/Animation/Animation/Animatable.swift @@ -0,0 +1,119 @@ +// +// Animatable.swift +// OpenSwiftUICore +// +// Status: Complete + +public import Foundation +package import OpenGraphShims + +// MARK: - Animatable [6.4.41] + +/// A type that describes how to animate a property of a view. +public protocol Animatable { + /// The type defining the data to animate. + associatedtype AnimatableData: VectorArithmetic + + /// The data to animate. + var animatableData: AnimatableData { get set } + + /// Replaces `value` with an animated version of the value, using + /// `inputs`. + static func _makeAnimatable(value: inout _GraphValue, inputs: _GraphInputs) +} + +// MARK: - Animateble + Extension [6.4.41] + +extension Animatable where Self: VectorArithmetic { + public var animatableData: Self { + get { self } + set { self = newValue } + } +} + +extension Animatable where AnimatableData == EmptyAnimatableData { + public var animatableData: EmptyAnimatableData { + @inlinable + get { EmptyAnimatableData() } + @inlinable + set {} + } + + public static func _makeAnimatable(value _: inout _GraphValue, inputs _: _GraphInputs) {} +} + +extension Animatable { + package static func makeAnimatable(value: inout _GraphValue, inputs: _GraphInputs) { + _makeAnimatable(value: &value, inputs: inputs) + } +} + +extension Attribute where Value: Animatable { + package func animated(inputs: _GraphInputs) -> Attribute { + var value = _GraphValue(self) + Value._makeAnimatable(value: &value, inputs: inputs) + return value.value + } +} + +extension Animatable { + public static func _makeAnimatable(value: inout _GraphValue, inputs: _GraphInputs) { + guard MemoryLayout.size != 0, + !inputs.animationsDisabled else { + return + } + let animatable = AnimatableAttribute( + source: value.value, + phase: inputs.phase, + time: inputs.time, + transaction: inputs.transaction, + environment: inputs.environment + ) + let newValue = _GraphValue(animatable) + value = newValue + value.value.setFlags(.active, mask: .mask) + } +} + +// MARK: - EmptyAnimatableData [6.4.41] + +/// An empty type for animatable data. +/// +/// This type is suitable for use as the `animatableData` property of +/// types that do not have any animatable properties. +@frozen +public struct EmptyAnimatableData: VectorArithmetic { + @inlinable + public init() {} + + @inlinable + public static var zero: EmptyAnimatableData { .init() } + + @inlinable + public static func += (_: inout EmptyAnimatableData, _: EmptyAnimatableData) {} + + @inlinable + public static func -= (_: inout EmptyAnimatableData, _: EmptyAnimatableData) {} + + @inlinable + public static func + (_: EmptyAnimatableData, _: EmptyAnimatableData) -> EmptyAnimatableData { .zero } + + @inlinable + public static func - (_: EmptyAnimatableData, _: EmptyAnimatableData) -> EmptyAnimatableData { .zero } + + @inlinable + public mutating func scale(by _: Double) {} + + @inlinable + public var magnitudeSquared: Double { 0 } + + public static func == (_: EmptyAnimatableData, _: EmptyAnimatableData) -> Bool { true } +} + +extension Double: Animatable { + public typealias AnimatableData = Double +} + +extension CGFloat: Animatable { + public typealias AnimatableData = CGFloat +} diff --git a/Sources/OpenSwiftUICore/View/Animation/Animation/Animation.swift b/Sources/OpenSwiftUICore/View/Animation/Animation/Animation.swift new file mode 100644 index 00000000..95e280e8 --- /dev/null +++ b/Sources/OpenSwiftUICore/View/Animation/Animation/Animation.swift @@ -0,0 +1,63 @@ +package import OpenGraphShims + +@frozen +public struct Animation: Equatable { +// var box: AnimationBoxBase + public static func == (lhs: Animation, rhs: Animation) -> Bool { + // TODO + true + } + + public static var `default` = Animation() +} + +// MARK: - AnimatableAttribute [6.4.41] [TODO] + +package struct AnimatableAttribute: StatefulRule, AsyncAttribute, ObservedAttribute, CustomStringConvertible where Value: Animatable { + @Attribute var source: Value + @Attribute var environment: EnvironmentValues + var helper: AnimatableAttributeHelper + + package init( + source: Attribute, + phase: Attribute<_GraphInputs.Phase>, + time: Attribute