Skip to main content

ferroui_base/rendering/composition/
composition_cache_mode.rs

1//! `CompositionCacheMode` and `CompositionBitmapCache`.
2//!
3//! The only cache mode is the bitmap cache, so the abstract class is an
4//! alias of it.
5
6use super::animations::{ICompositionAnimation, ICompositionAnimationBase, ImplicitAnimationCollection};
7use super::expressions::ExpressionVariant;
8use super::generated::{
9    CompositionBitmapCacheHooks, CompositionBitmapCacheProps, CompositionCacheModeHooks,
10    ServerCompositionBitmapCacheProps,
11};
12use super::server::{CompositionProperty, ServerCompositionBitmapCache, ServerObjectId};
13use super::transport::{BatchStreamWriter, IRegisterForSerialization};
14use super::{
15    AsCompositionObject, CompositionObject, Compositor, ICompositionObject, ICompositionObjectAnimations,
16    ICompositionObjectHost, ICompositorSerializable, PendingAnimations,
17};
18use std::any::Any;
19use std::rc::{Rc, Weak};
20
21/// The cache mode of a composition visual.
22pub type CompositionCacheMode = CompositionBitmapCache;
23
24/// Caches the subtree of a visual in a bitmap.
25pub struct CompositionBitmapCache {
26    this: Weak<CompositionBitmapCache>,
27    object: CompositionObject,
28    props: CompositionBitmapCacheProps,
29}
30
31impl CompositionBitmapCache {
32    pub fn new(compositor: &Rc<Compositor>) -> Rc<CompositionBitmapCache> {
33        let server = compositor.create_server_object(|compositor, _| ServerCompositionBitmapCache::new(compositor));
34        let cache = Rc::new_cyclic(|this: &Weak<CompositionBitmapCache>| CompositionBitmapCache {
35            this: this.clone(),
36            object: CompositionObject::new(compositor, Some(server)),
37            props: CompositionBitmapCacheProps::new(),
38        });
39        cache.props.initialize_defaults(&*cache);
40        cache
41    }
42
43    pub fn compositor(&self) -> &Rc<Compositor> {
44        self.object.compositor()
45    }
46
47    /// The embedded `CompositionObject`.
48    pub fn object(&self) -> &CompositionObject {
49        &self.object
50    }
51
52    /// The collection of implicit animations attached to this object.
53    pub fn implicit_animations(&self) -> Option<Rc<ImplicitAnimationCollection>> {
54        self.object.implicit_animations()
55    }
56
57    pub fn set_implicit_animations(&self, value: Option<Rc<ImplicitAnimationCollection>>) {
58        self.object.set_implicit_animations(value)
59    }
60
61    pub fn render_at_scale(&self) -> f64 {
62        self.props.render_at_scale()
63    }
64
65    pub fn set_render_at_scale(&self, value: f64) {
66        self.props.set_render_at_scale(self, value)
67    }
68
69    pub fn snaps_to_device_pixels(&self) -> bool {
70        self.props.snaps_to_device_pixels()
71    }
72
73    pub fn set_snaps_to_device_pixels(&self, value: bool) {
74        self.props.set_snaps_to_device_pixels(self, value)
75    }
76
77    pub fn enable_clear_type(&self) -> bool {
78        self.props.enable_clear_type()
79    }
80
81    pub fn set_enable_clear_type(&self, value: bool) {
82        self.props.set_enable_clear_type(self, value)
83    }
84}
85
86impl CompositionCacheModeHooks for CompositionBitmapCache {}
87impl CompositionBitmapCacheHooks for CompositionBitmapCache {}
88
89impl IRegisterForSerialization for CompositionBitmapCache {
90    fn register_for_serialization(&self) {
91        self.object
92            .register_for_serialization(|| self.this.upgrade().map(|this| this as Rc<dyn ICompositorSerializable>));
93    }
94}
95
96impl ICompositionObjectHost for CompositionBitmapCache {
97    fn server(&self) -> ServerObjectId {
98        self.object.required_server()
99    }
100
101    fn pending_animations(&self) -> &PendingAnimations {
102        self.object.pending_animations()
103    }
104
105    fn implicit_animation(&self, property_name: &str) -> Option<Rc<dyn ICompositionAnimationBase>> {
106        self.object.implicit_animation(property_name)
107    }
108
109    fn start_animation_group(
110        &self,
111        grp: &Rc<dyn ICompositionAnimationBase>,
112        target: &str,
113        final_value: ExpressionVariant,
114    ) -> bool {
115        self.start_animation_group_for(&**grp, target, final_value)
116    }
117}
118
119impl ICompositionObjectAnimations for CompositionBitmapCache {
120    fn try_start_animation(
121        &self,
122        property_name: &str,
123        animation: &dyn ICompositionAnimation,
124        final_value: Option<ExpressionVariant>,
125    ) -> bool {
126        self.props.start_animation(self, property_name, animation, final_value)
127    }
128
129    fn get_composition_property(&self, property_name: &str) -> Option<&'static CompositionProperty> {
130        ServerCompositionBitmapCacheProps::get_composition_property(property_name)
131    }
132
133    fn composition_object(&self) -> &CompositionObject {
134        &self.object
135    }
136}
137
138impl AsCompositionObject for CompositionBitmapCache {
139    fn as_composition_object(&self) -> &CompositionObject {
140        &self.object
141    }
142
143    fn composition_type_name(&self) -> &'static str {
144        "CompositionBitmapCache"
145    }
146}
147
148impl ICompositionObject for CompositionBitmapCache {
149    fn server(&self) -> ServerObjectId {
150        self.object.required_server()
151    }
152
153    fn as_any(&self) -> &dyn Any {
154        self
155    }
156
157    fn into_any_rc(self: Rc<Self>) -> Rc<dyn Any> {
158        self
159    }
160}
161
162impl ICompositorSerializable for CompositionBitmapCache {
163    fn try_get_server(&self, c: &Compositor) -> Option<ServerObjectId> {
164        self.object.try_get_server(c)
165    }
166
167    fn serialize_changes(&self, c: &Compositor, writer: &mut BatchStreamWriter<'_>) {
168        self.object.begin_serialize_changes(c);
169        self.props.serialize_changes_core(self, writer);
170    }
171}