Skip to main content

ferroui_base/rendering/composition/server/
server_composition_visual_collection.rs

1use super::{impl_animated_server_object, IAnimatedServerObject, IServerObject, ServerCompositionVisual, ServerCompositor, ServerObject};
2use crate::rendering::composition::generated::{
3    ServerCompositionVisualCollectionHooks, ServerCompositionVisualCollectionProps,
4};
5use crate::rendering::composition::transport::BatchStreamReader;
6use std::any::{Any, TypeId};
7use std::cell::RefCell;
8use std::rc::{Rc, Weak};
9use std::time::Duration;
10
11/// Server-side counterpart of the children collection of a visual.
12pub struct ServerCompositionVisualCollection {
13    object: ServerObject,
14    props: ServerCompositionVisualCollectionProps,
15    /// The items as visuals: what the tree walks iterate.
16    typed: RefCell<Rc<Vec<Rc<ServerCompositionVisual>>>>,
17}
18
19impl ServerCompositionVisualCollection {
20    pub fn new(compositor: &Rc<ServerCompositor>) -> Rc<ServerCompositionVisualCollection> {
21        Rc::new_cyclic(|this: &Weak<ServerCompositionVisualCollection>| {
22            let owner: Weak<dyn IAnimatedServerObject> = this.clone();
23            ServerCompositionVisualCollection {
24                object: ServerObject::new(compositor, owner),
25                props: ServerCompositionVisualCollectionProps::new(),
26                typed: RefCell::new(Rc::new(Vec::new())),
27            }
28        })
29    }
30
31    /// A snapshot of the items.
32    pub fn list(&self) -> Rc<Vec<Rc<ServerCompositionVisual>>> {
33        self.typed.borrow().clone()
34    }
35
36    pub fn count(&self) -> usize {
37        self.typed.borrow().len()
38    }
39}
40
41impl ServerCompositionVisualCollectionHooks for ServerCompositionVisualCollection {}
42
43impl_animated_server_object!(ServerCompositionVisualCollection, object);
44
45impl IServerObject for ServerCompositionVisualCollection {
46    fn deserialize_changes_core(&self, reader: &mut BatchStreamReader<'_>, committed_at: Duration) {
47        self.props.deserialize_changes_core(self, reader, committed_at);
48        *self.typed.borrow_mut() = Rc::new(self.props.list().items::<ServerCompositionVisual>());
49    }
50
51    fn dispose(&self) {
52        self.props.list().clear();
53        *self.typed.borrow_mut() = Rc::new(Vec::new());
54    }
55
56    fn get_props(&self, type_id: TypeId) -> Option<&dyn Any> {
57        self.props.find_props(type_id)
58    }
59
60    fn as_animated(self: Rc<Self>) -> Option<Rc<dyn IAnimatedServerObject>> {
61        Some(self)
62    }
63
64    fn as_any(&self) -> &dyn Any {
65        self
66    }
67
68    fn into_any_rc(self: Rc<Self>) -> Rc<dyn Any> {
69        self
70    }
71}