use super::*;
use std::time::Duration;
use windows_time::TimeSpan;
fn to_time_span(duration: Duration) -> TimeSpan {
TimeSpan::try_from(duration).unwrap_or(TimeSpan::MAX)
}
#[derive(Clone)]
pub struct CompositionAnimation(pub(crate) bindings::CompositionAnimation);
pub trait Animation: Sealed {
fn as_animation(&self) -> CompositionAnimation;
}
#[derive(Clone)]
pub struct CompositionEasingFunction(pub(crate) bindings::CompositionEasingFunction);
#[derive(Clone)]
pub struct ScalarKeyFrameAnimation(pub(crate) bindings::ScalarKeyFrameAnimation);
impl ScalarKeyFrameAnimation {
pub fn insert_key_frame_with_easing(
&self,
progress: f32,
value: f32,
easing: &CompositionEasingFunction,
) {
let animation: bindings::IScalarKeyFrameAnimation = self.0.cast().unwrap();
animation
.InsertKeyFrameWithEasingFunction(progress, value, &easing.0)
.unwrap();
}
pub fn insert_expression_key_frame_with_easing(
&self,
progress: f32,
expression: &str,
easing: &CompositionEasingFunction,
) {
let animation: bindings::IKeyFrameAnimation = self.0.cast().unwrap();
animation
.InsertExpressionKeyFrameWithEasingFunction(progress, expression, &easing.0)
.unwrap();
}
pub fn set_duration(&self, duration: Duration) {
let animation: bindings::IKeyFrameAnimation = self.0.cast().unwrap();
animation.SetDuration(to_time_span(duration)).unwrap();
}
pub fn set_target(&self, target: &str) {
let animation: bindings::ICompositionAnimation2 = self.0.cast().unwrap();
animation.SetTarget(target).unwrap();
}
}
impl Sealed for ScalarKeyFrameAnimation {}
impl Animation for ScalarKeyFrameAnimation {
fn as_animation(&self) -> CompositionAnimation {
CompositionAnimation(self.0.cast().unwrap())
}
}
#[derive(Clone)]
pub struct Vector3KeyFrameAnimation(pub(crate) bindings::Vector3KeyFrameAnimation);
impl Vector3KeyFrameAnimation {
pub fn insert_key_frame(&self, progress: f32, value: Vector3) {
self.0.InsertKeyFrame(progress, value).unwrap();
}
pub fn insert_key_frame_with_easing(
&self,
progress: f32,
value: Vector3,
easing: &CompositionEasingFunction,
) {
let animation: bindings::IVector3KeyFrameAnimation = self.0.cast().unwrap();
animation
.InsertKeyFrameWithEasingFunction(progress, value, &easing.0)
.unwrap();
}
pub fn insert_expression_key_frame_with_easing(
&self,
progress: f32,
expression: &str,
easing: &CompositionEasingFunction,
) {
let animation: bindings::IKeyFrameAnimation = self.0.cast().unwrap();
animation
.InsertExpressionKeyFrameWithEasingFunction(progress, expression, &easing.0)
.unwrap();
}
pub fn set_duration(&self, duration: Duration) {
let animation: bindings::IKeyFrameAnimation = self.0.cast().unwrap();
animation.SetDuration(to_time_span(duration)).unwrap();
}
pub fn set_delay(&self, delay: Duration) {
let animation: bindings::IKeyFrameAnimation = self.0.cast().unwrap();
animation.SetDelayTime(to_time_span(delay)).unwrap();
}
pub fn set_target(&self, target: &str) {
let animation: bindings::ICompositionAnimation2 = self.0.cast().unwrap();
animation.SetTarget(target).unwrap();
}
pub fn set_iteration_count(&self, count: i32) {
let animation: bindings::IKeyFrameAnimation = self.0.cast().unwrap();
animation
.SetIterationBehavior(bindings::AnimationIterationBehavior::Count)
.unwrap();
animation.SetIterationCount(count).unwrap();
}
pub fn set_iterate_forever(&self) {
let animation: bindings::IKeyFrameAnimation = self.0.cast().unwrap();
animation
.SetIterationBehavior(bindings::AnimationIterationBehavior::Forever)
.unwrap();
}
}
impl Sealed for Vector3KeyFrameAnimation {}
impl Animation for Vector3KeyFrameAnimation {
fn as_animation(&self) -> CompositionAnimation {
CompositionAnimation(self.0.cast().unwrap())
}
}
#[derive(Clone)]
pub struct CompositionAnimationGroup(pub(crate) bindings::CompositionAnimationGroup);
impl CompositionAnimationGroup {
pub fn add(&self, animation: &impl Animation) {
self.0.Add(&animation.as_animation().0).unwrap();
}
#[cfg(feature = "reactor")]
pub fn as_host(&self) -> windows_core::IInspectable {
self.0.cast().unwrap()
}
}
#[derive(Clone)]
pub struct ImplicitAnimationCollection(pub(crate) bindings::ImplicitAnimationCollection);
impl ImplicitAnimationCollection {
pub fn insert(&self, target: &str, animation: &impl Animation) {
let map: windows_collections::IMap<
windows_core::HSTRING,
bindings::ICompositionAnimationBase,
> = self.0.cast().unwrap();
let base: bindings::ICompositionAnimationBase = animation.as_animation().0.cast().unwrap();
map.Insert(&windows_core::HSTRING::from(target), &base)
.unwrap();
}
}