diff options
| author | Martin <mhaug@live.de> | 2021-06-18 13:00:36 +0200 |
|---|---|---|
| committer | Martin <mhaug@live.de> | 2021-06-18 13:00:36 +0200 |
| commit | 7db78d83bedf62adea0d715c9a2a179ce23a1a48 (patch) | |
| tree | 71c1db7fe3fae60390e46e18148c5b252b811c48 /src/layout | |
| parent | 2a30c20f0e2c3b51d3b2746a60e15008f1aa2619 (diff) | |
Levels
Diffstat (limited to 'src/layout')
| -rw-r--r-- | src/layout/incremental.rs | 15 | ||||
| -rw-r--r-- | src/layout/mod.rs | 20 |
2 files changed, 28 insertions, 7 deletions
diff --git a/src/layout/incremental.rs b/src/layout/incremental.rs index 427df079..7857f33c 100644 --- a/src/layout/incremental.rs +++ b/src/layout/incremental.rs @@ -20,6 +20,19 @@ impl LayoutCache { pub fn clear(&mut self) { self.frames.clear(); } + + /// Retains all elements for which the closure on the level returns `true`. + pub fn retain<F>(&mut self, mut f: F) + where + F: FnMut(usize) -> bool, + { + self.frames.retain(|_, b| f(b.level)); + } + + /// Amount of items in the cache. + pub fn len(&self) -> usize { + self.frames.len() + } } #[derive(Debug, Clone)] @@ -27,6 +40,8 @@ impl LayoutCache { pub struct FramesEntry { /// The cached frames for a node. pub frames: Vec<Constrained<Frame>>, + /// How nested the frame was in the context is was originally appearing in. + pub level: usize, } impl FramesEntry { diff --git a/src/layout/mod.rs b/src/layout/mod.rs index 15c80017..28e83da9 100644 --- a/src/layout/mod.rs +++ b/src/layout/mod.rs @@ -32,7 +32,7 @@ use crate::loading::Loader; /// Layout a tree into a collection of frames. pub fn layout(loader: &mut dyn Loader, cache: &mut Cache, tree: &Tree) -> Vec<Frame> { - tree.layout(&mut LayoutContext { loader, cache }) + tree.layout(&mut LayoutContext { loader, cache, level: 0 }) } /// A tree of layout nodes. @@ -98,19 +98,23 @@ impl Layout for AnyNode { ctx: &mut LayoutContext, regions: &Regions, ) -> Vec<Constrained<Frame>> { - ctx.cache + ctx.level += 1; + let frames = ctx + .cache .layout .frames .get(&self.hash) .and_then(|x| x.check(regions.clone())) .unwrap_or_else(|| { let frames = self.node.layout(ctx, regions); - ctx.cache - .layout - .frames - .insert(self.hash, FramesEntry { frames: frames.clone() }); + ctx.cache.layout.frames.insert(self.hash, FramesEntry { + frames: frames.clone(), + level: ctx.level - 1, + }); frames - }) + }); + ctx.level -= 1; + frames } } @@ -184,6 +188,8 @@ pub struct LayoutContext<'a> { pub loader: &'a mut dyn Loader, /// A cache for loaded fonts and artifacts from past layouting. pub cache: &'a mut Cache, + /// How deeply nested is the current layout tree position. + pub level: usize, } /// A sequence of regions to layout into. |
