Skip to main content

ferroui_base/rendering/composition/animations/
composition_animation_group.rs

1use super::{ICompositionAnimation, ICompositionAnimationBase};
2use crate::rendering::composition::{AsCompositionObject, CompositionObject, Compositor};
3use std::cell::RefCell;
4use std::rc::Rc;
5
6/// A group of animations started and stopped together on a composition
7/// object, each on its target property.
8pub struct CompositionAnimationGroup {
9    object: CompositionObject,
10    animations: RefCell<Vec<Rc<dyn ICompositionAnimation>>>,
11}
12
13impl CompositionAnimationGroup {
14    pub fn new(compositor: &Rc<Compositor>) -> Rc<CompositionAnimationGroup> {
15        Rc::new(CompositionAnimationGroup {
16            object: CompositionObject::new(compositor, None),
17            animations: RefCell::new(Vec::new()),
18        })
19    }
20
21    /// The animations of the group, in the order they were added.
22    pub(crate) fn animations(&self) -> Vec<Rc<dyn ICompositionAnimation>> {
23        self.animations.borrow().clone()
24    }
25
26    pub fn add(&self, value: Rc<dyn ICompositionAnimation>) {
27        self.animations.borrow_mut().push(value)
28    }
29
30    /// Removes the first occurrence of `value` (compared by reference).
31    pub fn remove(&self, value: &Rc<dyn ICompositionAnimation>) {
32        let mut animations = self.animations.borrow_mut();
33        if let Some(index) = animations.iter().position(|a| std::ptr::addr_eq(Rc::as_ptr(a), Rc::as_ptr(value))) {
34            animations.remove(index);
35        }
36    }
37
38    pub fn remove_all(&self) {
39        self.animations.borrow_mut().clear()
40    }
41}
42
43impl ICompositionAnimationBase for CompositionAnimationGroup {
44    fn as_composition_animation_group(&self) -> Option<&CompositionAnimationGroup> {
45        Some(self)
46    }
47}
48
49impl AsCompositionObject for CompositionAnimationGroup {
50    fn as_composition_object(&self) -> &CompositionObject {
51        &self.object
52    }
53
54    fn composition_type_name(&self) -> &'static str {
55        "CompositionAnimationGroup"
56    }
57}