Skip to main content

ferroui_base/animation/
transitions_collection.rs

1use crate::animation::ITransition;
2use crate::collections::{FerroList, ResetBehavior};
3use std::fmt;
4use std::ops::Deref;
5use std::rc::Rc;
6
7/// A collection of [`ITransition`] objects.
8///
9/// The handle is shared: clones refer to the same collection, and handles
10/// compare by identity. Clearing the collection notifies a removal of every
11/// item, and transitions of direct properties are rejected.
12#[derive(Clone)]
13pub struct Transitions(Rc<FerroList<Rc<dyn ITransition>>>);
14
15impl Transitions {
16    /// Creates an empty collection.
17    pub fn new() -> Self {
18        let list = FerroList::new();
19        list.set_reset_behavior(ResetBehavior::Remove);
20        list.set_validate(Some(Rc::new(Self::validate)));
21        Self(Rc::new(list))
22    }
23
24    /// Creates a collection holding `items`.
25    pub fn from_items(items: impl IntoIterator<Item = Rc<dyn ITransition>>) -> Self {
26        let result = Self::new();
27        for item in items {
28            result.add(item);
29        }
30        result
31    }
32
33    /// Whether two handles refer to the same collection.
34    pub fn ptr_eq(&self, other: &Self) -> bool {
35        Rc::ptr_eq(&self.0, &other.0)
36    }
37
38    fn validate(item: &Rc<dyn ITransition>) {
39        let property = item.property();
40        if property.is_direct() {
41            panic!("Cannot animate direct property {} on {}.", property, item.debug_display());
42        }
43    }
44}
45
46impl Default for Transitions {
47    fn default() -> Self {
48        Self::new()
49    }
50}
51
52impl Deref for Transitions {
53    type Target = FerroList<Rc<dyn ITransition>>;
54
55    fn deref(&self) -> &Self::Target {
56        &self.0
57    }
58}
59
60impl PartialEq for Transitions {
61    fn eq(&self, other: &Self) -> bool {
62        self.ptr_eq(other)
63    }
64}
65
66impl fmt::Debug for Transitions {
67    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
68        write!(f, "Transitions({})", self.count())
69    }
70}