Skip to main content

ferroui_base/media/
cache_mode.rs

1use crate::media::BitmapCache;
2use crate::utilities::FormatError;
3use crate::rendering::composition::{CompositionCacheMode, Compositor};
4use crate::{ferro_class, ferro_impl_classes, FerroObjectImpl, Ref, StyledElement, StyledElementImpl};
5use std::rc::Rc;
6
7/// Represents cached content modes for graphics acceleration features.
8#[repr(C)]
9pub struct CacheMode {
10    base: StyledElement,
11}
12
13ferro_class! {
14    CacheMode: StyledElement, virtuals CacheModeImpl: StyledElementImpl {
15        /// The composition cache mode that mirrors this cache mode on
16        /// compositor `c`.
17        // We currently only allow visual to be attached to one compositor at a time, so keep it simple for now
18        fn get_for_compositor(this, c: &Rc<Compositor>) -> Rc<CompositionCacheMode>;
19    }
20}
21ferro_impl_classes!(CacheMode: FerroObjectImpl, StyledElementImpl);
22
23impl CacheModeImpl for CacheMode {
24    fn get_for_compositor(_this: &Self, _c: &Rc<Compositor>) -> Rc<CompositionCacheMode> {
25        panic!("the cache mode class is abstract")
26    }
27}
28
29impl CacheMode {
30    /// Creates the class data; see [`crate::FerroObject::construct`].
31    pub fn construct() -> Self {
32        Self { base: StyledElement::construct() }
33    }
34
35    /// Parses a cache mode: the only known mode is `BitmapCache`.
36    pub fn parse(s: &str) -> Result<Ref<CacheMode>, FormatError> {
37        if s == "BitmapCache" {
38            return Ok(BitmapCache::new().upcast());
39        }
40        Err(FormatError::from_string(format!("Unknown CacheMode: {s}")))
41    }
42}