windows_composition/
animation.rs1use super::*;
2use std::time::Duration;
3use windows_time::TimeSpan;
4
5fn to_time_span(duration: Duration) -> TimeSpan {
6 TimeSpan::try_from(duration).unwrap_or(TimeSpan::MAX)
8}
9
10#[derive(Clone)]
14pub struct CompositionAnimation(pub(crate) bindings::CompositionAnimation);
15
16pub trait Animation: Sealed {
20 fn as_animation(&self) -> CompositionAnimation;
22}
23
24#[derive(Clone)]
29pub struct CompositionEasingFunction(pub(crate) bindings::CompositionEasingFunction);
30
31#[derive(Clone)]
34pub struct ScalarKeyFrameAnimation(pub(crate) bindings::ScalarKeyFrameAnimation);
35
36impl ScalarKeyFrameAnimation {
37 pub fn insert_key_frame_with_easing(
40 &self,
41 progress: f32,
42 value: f32,
43 easing: &CompositionEasingFunction,
44 ) {
45 let animation: bindings::IScalarKeyFrameAnimation = self.0.cast().unwrap();
46 animation
47 .InsertKeyFrameWithEasingFunction(progress, value, &easing.0)
48 .unwrap();
49 }
50
51 pub fn insert_expression_key_frame_with_easing(
54 &self,
55 progress: f32,
56 expression: &str,
57 easing: &CompositionEasingFunction,
58 ) {
59 let animation: bindings::IKeyFrameAnimation = self.0.cast().unwrap();
60 animation
61 .InsertExpressionKeyFrameWithEasingFunction(progress, expression, &easing.0)
62 .unwrap();
63 }
64
65 pub fn set_duration(&self, duration: Duration) {
67 let animation: bindings::IKeyFrameAnimation = self.0.cast().unwrap();
68 animation.SetDuration(to_time_span(duration)).unwrap();
69 }
70
71 pub fn set_target(&self, target: &str) {
74 let animation: bindings::ICompositionAnimation2 = self.0.cast().unwrap();
75 animation.SetTarget(target).unwrap();
76 }
77}
78
79impl Sealed for ScalarKeyFrameAnimation {}
80
81impl Animation for ScalarKeyFrameAnimation {
82 fn as_animation(&self) -> CompositionAnimation {
83 CompositionAnimation(self.0.cast().unwrap())
84 }
85}
86
87#[derive(Clone)]
90pub struct Vector3KeyFrameAnimation(pub(crate) bindings::Vector3KeyFrameAnimation);
91
92impl Vector3KeyFrameAnimation {
93 pub fn insert_key_frame(&self, progress: f32, value: Vector3) {
95 self.0.InsertKeyFrame(progress, value).unwrap();
96 }
97
98 pub fn insert_key_frame_with_easing(
100 &self,
101 progress: f32,
102 value: Vector3,
103 easing: &CompositionEasingFunction,
104 ) {
105 let animation: bindings::IVector3KeyFrameAnimation = self.0.cast().unwrap();
106 animation
107 .InsertKeyFrameWithEasingFunction(progress, value, &easing.0)
108 .unwrap();
109 }
110
111 pub fn insert_expression_key_frame_with_easing(
114 &self,
115 progress: f32,
116 expression: &str,
117 easing: &CompositionEasingFunction,
118 ) {
119 let animation: bindings::IKeyFrameAnimation = self.0.cast().unwrap();
120 animation
121 .InsertExpressionKeyFrameWithEasingFunction(progress, expression, &easing.0)
122 .unwrap();
123 }
124
125 pub fn set_duration(&self, duration: Duration) {
127 let animation: bindings::IKeyFrameAnimation = self.0.cast().unwrap();
128 animation.SetDuration(to_time_span(duration)).unwrap();
129 }
130
131 pub fn set_delay(&self, delay: Duration) {
133 let animation: bindings::IKeyFrameAnimation = self.0.cast().unwrap();
134 animation.SetDelayTime(to_time_span(delay)).unwrap();
135 }
136
137 pub fn set_target(&self, target: &str) {
140 let animation: bindings::ICompositionAnimation2 = self.0.cast().unwrap();
141 animation.SetTarget(target).unwrap();
142 }
143
144 pub fn set_iteration_count(&self, count: i32) {
146 let animation: bindings::IKeyFrameAnimation = self.0.cast().unwrap();
147 animation
148 .SetIterationBehavior(bindings::AnimationIterationBehavior::Count)
149 .unwrap();
150 animation.SetIterationCount(count).unwrap();
151 }
152
153 pub fn set_iterate_forever(&self) {
155 let animation: bindings::IKeyFrameAnimation = self.0.cast().unwrap();
156 animation
157 .SetIterationBehavior(bindings::AnimationIterationBehavior::Forever)
158 .unwrap();
159 }
160}
161
162impl Sealed for Vector3KeyFrameAnimation {}
163
164impl Animation for Vector3KeyFrameAnimation {
165 fn as_animation(&self) -> CompositionAnimation {
166 CompositionAnimation(self.0.cast().unwrap())
167 }
168}
169
170#[derive(Clone)]
172pub struct CompositionAnimationGroup(pub(crate) bindings::CompositionAnimationGroup);
173
174impl CompositionAnimationGroup {
175 pub fn add(&self, animation: &impl Animation) {
177 self.0.Add(&animation.as_animation().0).unwrap();
178 }
179
180 #[cfg(feature = "reactor")]
182 pub fn as_host(&self) -> windows_core::IInspectable {
183 self.0.cast().unwrap()
184 }
185}
186
187#[derive(Clone)]
194pub struct ImplicitAnimationCollection(pub(crate) bindings::ImplicitAnimationCollection);
195
196impl ImplicitAnimationCollection {
197 pub fn insert(&self, target: &str, animation: &impl Animation) {
201 let map: windows_collections::IMap<
202 windows_core::HSTRING,
203 bindings::ICompositionAnimationBase,
204 > = self.0.cast().unwrap();
205 let base: bindings::ICompositionAnimationBase = animation.as_animation().0.cast().unwrap();
206 map.Insert(&windows_core::HSTRING::from(target), &base)
207 .unwrap();
208 }
209}