iced_graphics/geometry/
cache.rs

1use crate::cache::{self, Cached};
2use crate::core::Size;
3use crate::geometry::{self, Frame};
4
5pub use cache::Group;
6
7/// A simple cache that stores generated geometry to avoid recomputation.
8///
9/// A [`Cache`] will not redraw its geometry unless the dimensions of its layer
10/// change or it is explicitly cleared.
11pub struct Cache<Renderer>
12where
13    Renderer: geometry::Renderer,
14{
15    raw: crate::Cache<Data<<Renderer::Geometry as Cached>::Cache>>,
16}
17
18#[derive(Debug, Clone)]
19struct Data<T> {
20    bounds: Size,
21    geometry: T,
22}
23
24impl<Renderer> Cache<Renderer>
25where
26    Renderer: geometry::Renderer,
27{
28    /// Creates a new empty [`Cache`].
29    pub fn new() -> Self {
30        Cache {
31            raw: cache::Cache::new(),
32        }
33    }
34
35    /// Creates a new empty [`Cache`] with the given [`Group`].
36    ///
37    /// Caches within the same group may reuse internal rendering storage.
38    ///
39    /// You should generally group caches that are likely to change
40    /// together.
41    pub fn with_group(group: Group) -> Self {
42        Cache {
43            raw: crate::Cache::with_group(group),
44        }
45    }
46
47    /// Clears the [`Cache`], forcing a redraw the next time it is used.
48    pub fn clear(&self) {
49        self.raw.clear();
50    }
51
52    /// Draws geometry using the provided closure and stores it in the
53    /// [`Cache`].
54    ///
55    /// The closure will only be called when
56    /// - the bounds have changed since the previous draw call.
57    /// - the [`Cache`] is empty or has been explicitly cleared.
58    ///
59    /// Otherwise, the previously stored geometry will be returned. The
60    /// [`Cache`] is not cleared in this case. In other words, it will keep
61    /// returning the stored geometry if needed.
62    pub fn draw(
63        &self,
64        renderer: &Renderer,
65        bounds: Size,
66        draw_fn: impl FnOnce(&mut Frame<Renderer>),
67    ) -> Renderer::Geometry {
68        use std::ops::Deref;
69
70        let state = self.raw.state();
71
72        let previous = match state.borrow().deref() {
73            cache::State::Empty { previous } => {
74                previous.as_ref().map(|data| data.geometry.clone())
75            }
76            cache::State::Filled { current } => {
77                if current.bounds == bounds {
78                    return Cached::load(&current.geometry);
79                }
80
81                Some(current.geometry.clone())
82            }
83        };
84
85        let mut frame = Frame::new(renderer, bounds);
86        draw_fn(&mut frame);
87
88        let geometry = frame.into_geometry().cache(self.raw.group(), previous);
89        let result = Cached::load(&geometry);
90
91        *state.borrow_mut() = cache::State::Filled {
92            current: Data { bounds, geometry },
93        };
94
95        result
96    }
97}
98
99impl<Renderer> std::fmt::Debug for Cache<Renderer>
100where
101    Renderer: geometry::Renderer,
102    <Renderer::Geometry as Cached>::Cache: std::fmt::Debug,
103{
104    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
105        write!(f, "{:?}", &self.raw)
106    }
107}
108
109impl<Renderer> Default for Cache<Renderer>
110where
111    Renderer: geometry::Renderer,
112{
113    fn default() -> Self {
114        Self::new()
115    }
116}