Skip to main content

windows_animation/
manager.rs

1use super::*;
2
3/// Coordinates variables, transitions, and storyboards.
4#[derive(Clone)]
5pub struct Manager(pub(crate) IUIAnimationManager2);
6
7impl Manager {
8    /// Creates a new animation manager.
9    pub fn new() -> Result<Self> {
10        unsafe {
11            let mut ptr = core::ptr::null_mut();
12            CoCreateInstance(
13                &UIAnimationManager2,
14                core::ptr::null_mut(),
15                CLSCTX_INPROC_SERVER,
16                &IUIAnimationManager2::IID,
17                &mut ptr,
18            )
19            .ok()?;
20            Ok(Self(windows_core::imp::Type::from_abi(ptr)?))
21        }
22    }
23
24    /// Creates a new animation variable with the given initial value.
25    pub fn create_variable(&self, initial_value: f64) -> Result<Variable> {
26        unsafe { Ok(Variable(self.0.CreateAnimationVariable(initial_value)?)) }
27    }
28
29    /// Creates a new storyboard for sequencing transitions.
30    pub fn create_storyboard(&self) -> Result<Storyboard> {
31        unsafe { Ok(Storyboard(self.0.CreateStoryboard()?)) }
32    }
33
34    /// Advances the animation manager to the specified time in seconds.
35    pub fn update(&self, time: f64) -> Result<()> {
36        unsafe { self.0.Update(time, None) }.ok()?;
37        Ok(())
38    }
39
40    /// Schedules a transition on a variable, starting at the specified time in seconds.
41    pub fn schedule_transition(
42        &self,
43        variable: &Variable,
44        transition: &Transition,
45        time: f64,
46    ) -> Result<()> {
47        unsafe { self.0.ScheduleTransition(&variable.0, &transition.0, time) }.ok()
48    }
49}