summaryrefslogtreecommitdiffstats
path: root/renderer/src/geometry/cache.rs
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--renderer/src/geometry/cache.rs (renamed from graphics/src/widget/canvas/cache.rs)59
1 files changed, 23 insertions, 36 deletions
diff --git a/graphics/src/widget/canvas/cache.rs b/renderer/src/geometry/cache.rs
index 52217bbb..2a3534d0 100644
--- a/graphics/src/widget/canvas/cache.rs
+++ b/renderer/src/geometry/cache.rs
@@ -1,22 +1,11 @@
-use crate::widget::canvas::{Frame, Geometry};
-use crate::Primitive;
+use crate::core::Size;
+use crate::geometry::{Frame, Geometry};
+use crate::graphics::Primitive;
+use crate::Renderer;
-use iced_native::Size;
-use std::{cell::RefCell, sync::Arc};
+use std::cell::RefCell;
+use std::sync::Arc;
-enum State {
- Empty,
- Filled {
- bounds: Size,
- primitive: Arc<Primitive>,
- },
-}
-
-impl Default for State {
- fn default() -> Self {
- State::Empty
- }
-}
/// A simple cache that stores generated [`Geometry`] to avoid recomputation.
///
/// A [`Cache`] will not redraw its geometry unless the dimensions of its layer
@@ -26,6 +15,16 @@ pub struct Cache {
state: RefCell<State>,
}
+#[derive(Debug, Default)]
+enum State {
+ #[default]
+ Empty,
+ Filled {
+ bounds: Size,
+ primitive: Arc<Primitive>,
+ },
+}
+
impl Cache {
/// Creates a new empty [`Cache`].
pub fn new() -> Self {
@@ -49,8 +48,9 @@ impl Cache {
/// Otherwise, the previously stored [`Geometry`] will be returned. The
/// [`Cache`] is not cleared in this case. In other words, it will keep
/// returning the stored [`Geometry`] if needed.
- pub fn draw(
+ pub fn draw<Theme>(
&self,
+ renderer: &Renderer<Theme>,
bounds: Size,
draw_fn: impl FnOnce(&mut Frame),
) -> Geometry {
@@ -62,19 +62,19 @@ impl Cache {
} = self.state.borrow().deref()
{
if *cached_bounds == bounds {
- return Geometry::from_primitive(Primitive::Cached {
- cache: primitive.clone(),
+ return Geometry(Primitive::Cache {
+ content: primitive.clone(),
});
}
}
- let mut frame = Frame::new(bounds);
+ let mut frame = Frame::new(renderer, bounds);
draw_fn(&mut frame);
let primitive = {
let geometry = frame.into_geometry();
- Arc::new(geometry.into_primitive())
+ Arc::new(geometry.0)
};
*self.state.borrow_mut() = State::Filled {
@@ -82,19 +82,6 @@ impl Cache {
primitive: primitive.clone(),
};
- Geometry::from_primitive(Primitive::Cached { cache: primitive })
- }
-}
-
-impl std::fmt::Debug for State {
- fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
- match self {
- State::Empty => write!(f, "Empty"),
- State::Filled { primitive, bounds } => f
- .debug_struct("Filled")
- .field("primitive", primitive)
- .field("bounds", bounds)
- .finish(),
- }
+ Geometry(Primitive::Cache { content: primitive })
}
}