summaryrefslogtreecommitdiffstats
path: root/wgpu/src/widget/canvas/cache.rs
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón Jiménez <hector0193@gmail.com>2020-04-30 07:38:46 +0200
committerLibravatar Héctor Ramón Jiménez <hector0193@gmail.com>2020-04-30 07:38:46 +0200
commitd4c4198f7242f168de65146e0ca339e0c1cbfe9b (patch)
tree89ba3fb312c65264593720586df7e55726550e21 /wgpu/src/widget/canvas/cache.rs
parent1501a93915b3704a1d57da4c390a8ebca4e35c8b (diff)
downloadiced-d4c4198f7242f168de65146e0ca339e0c1cbfe9b.tar.gz
iced-d4c4198f7242f168de65146e0ca339e0c1cbfe9b.tar.bz2
iced-d4c4198f7242f168de65146e0ca339e0c1cbfe9b.zip
Write documentation for the new `canvas` API
Diffstat (limited to 'wgpu/src/widget/canvas/cache.rs')
-rw-r--r--wgpu/src/widget/canvas/cache.rs36
1 files changed, 24 insertions, 12 deletions
diff --git a/wgpu/src/widget/canvas/cache.rs b/wgpu/src/widget/canvas/cache.rs
index 03643f74..4b28d164 100644
--- a/wgpu/src/widget/canvas/cache.rs
+++ b/wgpu/src/widget/canvas/cache.rs
@@ -19,13 +19,14 @@ impl Default for State {
State::Empty
}
}
-/// A simple cache that stores generated geometry to avoid recomputation.
+/// A simple cache that stores generated [`Geometry`] to avoid recomputation.
///
/// A [`Cache`] will not redraw its geometry unless the dimensions of its layer
/// change or it is explicitly cleared.
///
/// [`Layer`]: ../trait.Layer.html
/// [`Cache`]: struct.Cache.html
+/// [`Geometry`]: struct.Geometry.html
#[derive(Debug, Default)]
pub struct Cache {
state: RefCell<State>,
@@ -41,30 +42,41 @@ impl Cache {
}
}
- /// Clears the cache, forcing a redraw the next time it is used.
+ /// Clears the [`Cache`], forcing a redraw the next time it is used.
///
- /// [`Cached`]: struct.Cached.html
+ /// [`Cache`]: struct.Cache.html
pub fn clear(&mut self) {
*self.state.borrow_mut() = State::Empty;
}
- pub fn draw(
- &self,
- new_bounds: Size,
- draw_fn: impl Fn(&mut Frame),
- ) -> Geometry {
+ /// Draws [`Geometry`] using the provided closure and stores it in the
+ /// [`Cache`].
+ ///
+ /// The closure will only be called when
+ /// - the bounds have changed since the previous draw call.
+ /// - the [`Cache`] is empty or has been explicitly cleared.
+ ///
+ /// 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.
+ ///
+ /// [`Cache`]: struct.Cache.html
+ pub fn draw(&self, bounds: Size, draw_fn: impl Fn(&mut Frame)) -> Geometry {
use std::ops::Deref;
- if let State::Filled { bounds, primitive } = self.state.borrow().deref()
+ if let State::Filled {
+ bounds: cached_bounds,
+ primitive,
+ } = self.state.borrow().deref()
{
- if *bounds == new_bounds {
+ if *cached_bounds == bounds {
return Geometry::from_primitive(Primitive::Cached {
cache: primitive.clone(),
});
}
}
- let mut frame = Frame::new(new_bounds);
+ let mut frame = Frame::new(bounds);
draw_fn(&mut frame);
let primitive = {
@@ -74,7 +86,7 @@ impl Cache {
};
*self.state.borrow_mut() = State::Filled {
- bounds: new_bounds,
+ bounds,
primitive: primitive.clone(),
};