diff options
| author | Martin <mhaug@live.de> | 2021-06-12 18:19:18 +0200 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-06-12 18:19:18 +0200 |
| commit | fec1f41106862617797abf65d4eba8061cf4497b (patch) | |
| tree | 06bc5418a7fc8db6ef091a235099aba38268f2ea /src/layout/mod.rs | |
| parent | 4017b5a9f67e06145129d75de452c8a42e2d2f5a (diff) | |
Allow grid cells to span multiple regions. (#30)
Diffstat (limited to 'src/layout/mod.rs')
| -rw-r--r-- | src/layout/mod.rs | 28 |
1 files changed, 28 insertions, 0 deletions
diff --git a/src/layout/mod.rs b/src/layout/mod.rs index 4153fc3c..6f536e20 100644 --- a/src/layout/mod.rs +++ b/src/layout/mod.rs @@ -280,4 +280,32 @@ impl Regions { let height = width / aspect.into_inner(); self.current = Size::new(width, height); } + + /// Appends new elements to the backlog such that the behavior of `next` + /// does not change. Panics when used with finite regions. + pub fn backlogify(&mut self, count: usize) { + for _ in 0 .. count { + self.backlog.push(self.last.unwrap()) + } + } + + /// Ensures that enough unique regions are present, including the current + /// and last ones. Panics when used with finite regions. + pub fn unique_regions(&mut self, count: usize) { + if self.backlog.len() < count.max(2) - 2 { + self.backlogify((count - 2) - self.backlog.len()); + } + } + + /// Returns a mutable reference to the size of the `n`th region. + pub fn nth_mut(&mut self, n: usize) -> Option<&mut Size> { + let backlog_len = self.backlog.len(); + if n == 0 { + Some(&mut self.current) + } else if n <= backlog_len { + self.backlog.get_mut(backlog_len - n) + } else { + self.last.as_mut() + } + } } |
