Skip to main content

windows_animation/
transition.rs

1use super::*;
2
3/// A transition that interpolates a variable's value over time.
4#[derive(Clone)]
5pub struct Transition(pub(crate) IUIAnimationTransition2);
6
7/// Provides factory methods for creating common transition types.
8#[derive(Clone)]
9pub struct TransitionLibrary(IUIAnimationTransitionLibrary2);
10
11impl TransitionLibrary {
12    /// Creates a new transition library.
13    pub fn new() -> Result<Self> {
14        unsafe {
15            let mut ptr = core::ptr::null_mut();
16            CoCreateInstance(
17                &UIAnimationTransitionLibrary2,
18                core::ptr::null_mut(),
19                CLSCTX_INPROC_SERVER,
20                &IUIAnimationTransitionLibrary2::IID,
21                &mut ptr,
22            )
23            .ok()?;
24            Ok(Self(windows_core::imp::Type::from_abi(ptr)?))
25        }
26    }
27
28    /// Creates a transition that accelerates then decelerates.
29    ///
30    /// `duration` is measured in seconds.
31    ///
32    /// The sum of `acceleration_ratio` and `deceleration_ratio` must be <= 1.0.
33    pub fn accelerate_decelerate(
34        &self,
35        duration: f64,
36        final_value: f64,
37        acceleration_ratio: f64,
38        deceleration_ratio: f64,
39    ) -> Result<Transition> {
40        unsafe {
41            Ok(Transition(self.0.CreateAccelerateDecelerateTransition(
42                duration,
43                final_value,
44                acceleration_ratio,
45                deceleration_ratio,
46            )?))
47        }
48    }
49
50    /// Creates a linear transition to the final value over `duration` seconds.
51    pub fn linear(&self, duration: f64, final_value: f64) -> Result<Transition> {
52        unsafe {
53            Ok(Transition(
54                self.0.CreateLinearTransition(duration, final_value)?,
55            ))
56        }
57    }
58
59    /// Creates an instantaneous transition that jumps to the final value.
60    pub fn instantaneous(&self, final_value: f64) -> Result<Transition> {
61        unsafe {
62            Ok(Transition(
63                self.0.CreateInstantaneousTransition(final_value)?,
64            ))
65        }
66    }
67}