Remove width from backlog and last region

This commit is contained in:
Laurenz 2022-02-02 15:25:43 +01:00
parent bdb1c008f2
commit 0a1916c1e4
4 changed files with 68 additions and 78 deletions

View File

@ -200,6 +200,7 @@ impl PackedNode {
} }
impl Layout for PackedNode { impl Layout for PackedNode {
#[track_caller]
fn layout( fn layout(
&self, &self,
ctx: &mut LayoutContext, ctx: &mut LayoutContext,
@ -219,8 +220,12 @@ impl Layout for PackedNode {
state.finish() state.finish()
}; };
// This is not written with `unwrap_or_else`, because then the
// #[track_caller] annotation doesn't work.
#[cfg(feature = "layout-cache")] #[cfg(feature = "layout-cache")]
ctx.layout_cache.get(hash, regions).unwrap_or_else(|| { if let Some(frames) = ctx.layout_cache.get(hash, regions) {
frames
} else {
ctx.level += 1; ctx.level += 1;
let frames = self.node.layout(ctx, regions, styles); let frames = self.node.layout(ctx, regions, styles);
ctx.level -= 1; ctx.level -= 1;
@ -240,7 +245,7 @@ impl Layout for PackedNode {
ctx.layout_cache.insert(hash, entry); ctx.layout_cache.insert(hash, entry);
frames frames
}) }
} }
fn pack(self) -> PackedNode { fn pack(self) -> PackedNode {

View File

@ -7,10 +7,11 @@ pub struct Regions {
pub current: Size, pub current: Size,
/// The base size for relative sizing. /// The base size for relative sizing.
pub base: Size, pub base: Size,
/// An iterator of followup regions. /// The height of followup regions. The width is the same for all regions.
pub backlog: std::vec::IntoIter<Size>, pub backlog: std::vec::IntoIter<Length>,
/// The final region that is repeated once the backlog is drained. /// The height of the final region that is repeated once the backlog is
pub last: Option<Size>, /// drained. The width is the same for all regions.
pub last: Option<Length>,
/// Whether nodes should expand to fill the regions instead of shrinking to /// Whether nodes should expand to fill the regions instead of shrinking to
/// fit the content. /// fit the content.
pub expand: Spec<bool>, pub expand: Spec<bool>,
@ -34,19 +35,33 @@ impl Regions {
current: size, current: size,
base, base,
backlog: vec![].into_iter(), backlog: vec![].into_iter(),
last: Some(size), last: Some(size.y),
expand, expand,
} }
} }
/// Create new regions where all sizes are mapped with `f`. /// Create new regions where all sizes are mapped with `f`.
///
/// Note that since all regions must have the same width, the width returned
/// by `f` is ignored for the backlog and the final region.
pub fn map<F>(&self, mut f: F) -> Self pub fn map<F>(&self, mut f: F) -> Self
where where
F: FnMut(Size) -> Size, F: FnMut(Size) -> Size,
{ {
let mut regions = self.clone(); let x = self.current.x;
regions.mutate(|s| *s = f(*s)); Self {
regions current: f(self.current),
base: f(self.base),
backlog: self
.backlog
.as_slice()
.iter()
.map(|&y| f(Size::new(x, y)).y)
.collect::<Vec<_>>()
.into_iter(),
last: self.last.map(|y| f(Size::new(x, y)).y),
expand: self.expand,
}
} }
/// Whether the current region is full and a region break is called for. /// Whether the current region is full and a region break is called for.
@ -58,14 +73,15 @@ impl Regions {
/// ///
/// If this is true, calling `next()` will have no effect. /// If this is true, calling `next()` will have no effect.
pub fn in_last(&self) -> bool { pub fn in_last(&self) -> bool {
self.backlog.len() == 0 && self.last.map_or(true, |size| self.current == size) self.backlog.len() == 0
&& self.last.map_or(true, |height| self.current.y == height)
} }
/// Advance to the next region if there is any. /// Advance to the next region if there is any.
pub fn next(&mut self) { pub fn next(&mut self) {
if let Some(size) = self.backlog.next().or(self.last) { if let Some(height) = self.backlog.next().or(self.last) {
self.current = size; self.current.y = height;
self.base = size; self.base.y = height;
} }
} }
@ -76,17 +92,11 @@ impl Regions {
let first = std::iter::once((self.current, self.base)); let first = std::iter::once((self.current, self.base));
let backlog = self.backlog.as_slice().iter(); let backlog = self.backlog.as_slice().iter();
let last = self.last.iter().cycle(); let last = self.last.iter().cycle();
first.chain(backlog.chain(last).map(|&s| (s, s))) first.chain(backlog.chain(last).map(|&height| {
} (
Size::new(self.current.x, height),
/// Mutate all contained sizes in place. Size::new(self.base.x, height),
pub fn mutate<F>(&mut self, mut f: F) )
where }))
F: FnMut(&mut Size),
{
f(&mut self.current);
f(&mut self.base);
self.last.as_mut().map(|x| f(x));
self.backlog.as_mut_slice().iter_mut().for_each(f);
} }
} }

View File

@ -38,63 +38,40 @@ impl Layout for ColumnsNode {
regions: &Regions, regions: &Regions,
styles: StyleChain, styles: StyleChain,
) -> Vec<Constrained<Arc<Frame>>> { ) -> Vec<Constrained<Arc<Frame>>> {
let columns = self.columns.get();
// Separating the infinite space into infinite columns does not make // Separating the infinite space into infinite columns does not make
// much sense. Note that this line assumes that no infinitely wide // much sense.
// region will follow if the first region's width is finite.
if regions.current.x.is_infinite() { if regions.current.x.is_infinite() {
return self.child.layout(ctx, regions, styles); return self.child.layout(ctx, regions, styles);
} }
// Gutter width for each region. (Can be different because the relative // Determine the width of the gutter and each column.
// component is calculated seperately for each region.) let columns = self.columns.get();
let mut gutters = vec![]; let gutter = styles.get(Self::GUTTER).resolve(regions.base.x);
let width = (regions.current.x - gutter * (columns - 1) as f64) / columns as f64;
// Sizes of all columns resulting from `region.current`, // Create the pod regions.
// `region.backlog` and `regions.last`. let pod = Regions {
let mut sizes = vec![]; current: Size::new(width, regions.current.y),
base: Size::new(width, regions.base.y),
backlog: std::iter::once(&regions.current.y)
.chain(regions.backlog.as_slice())
.flat_map(|&height| std::iter::repeat(height).take(columns))
.skip(1)
.collect::<Vec<_>>()
.into_iter(),
last: regions.last,
expand: Spec::new(true, regions.expand.y),
};
for (current, base) in regions // Layout the children.
.iter()
.take(1 + regions.backlog.len() + regions.last.iter().len())
{
let gutter = styles.get(Self::GUTTER).resolve(base.x);
let width = (current.x - gutter * (columns - 1) as f64) / columns as f64;
let size = Size::new(width, current.y);
gutters.push(gutter);
sizes.extend(std::iter::repeat(size).take(columns));
}
let first = sizes.remove(0);
let mut pod = Regions::one(
first,
Size::new(first.x, regions.base.y),
Spec::new(true, regions.expand.y),
);
// Retrieve elements for the last region from the vectors.
let last_gutter = regions.last.map(|_| {
let gutter = gutters.pop().unwrap();
let size = sizes.drain(sizes.len() - columns ..).next().unwrap();
pod.last = Some(size);
gutter
});
pod.backlog = sizes.into_iter();
let mut finished = vec![];
let mut frames = self.child.layout(ctx, &pod, styles).into_iter(); let mut frames = self.child.layout(ctx, &pod, styles).into_iter();
let dir = styles.get(ParNode::DIR); let dir = styles.get(ParNode::DIR);
let total_regions = (frames.len() as f32 / columns as f32).ceil() as usize; let total_regions = (frames.len() as f32 / columns as f32).ceil() as usize;
let mut finished = vec![];
// Stitch together the columns for each region. // Stitch together the columns for each region.
for ((current, base), gutter) in regions for (current, base) in regions.iter().take(total_regions) {
.iter()
.take(total_regions)
.zip(gutters.into_iter().chain(last_gutter.into_iter().cycle()))
{
// The height should be the parent height if the node shall expand. // The height should be the parent height if the node shall expand.
// Otherwise its the maximum column height for the frame. In that // Otherwise its the maximum column height for the frame. In that
// case, the frame is first created with zero height and then // case, the frame is first created with zero height and then

View File

@ -386,9 +386,10 @@ impl<'a> GridLayouter<'a> {
// Determine the size for each region of the row. // Determine the size for each region of the row.
for (x, &rcol) in self.rcols.iter().enumerate() { for (x, &rcol) in self.rcols.iter().enumerate() {
if let Some(node) = self.cell(x, y) { if let Some(node) = self.cell(x, y) {
// All widths should be `rcol` except the base for auto columns.
let mut pod = self.regions.clone(); let mut pod = self.regions.clone();
pod.mutate(|size| size.x = rcol); pod.current.x = rcol;
// All widths should be `rcol` except the base for auto columns.
if self.cols[x] == TrackSizing::Auto { if self.cols[x] == TrackSizing::Auto {
pod.base.x = self.regions.base.x; pod.base.x = self.regions.base.x;
} }
@ -513,18 +514,15 @@ impl<'a> GridLayouter<'a> {
// Prepare regions. // Prepare regions.
let size = Size::new(self.used.x, heights[0]); let size = Size::new(self.used.x, heights[0]);
let mut pod = Regions::one(size, self.regions.base, Spec::splat(true)); let mut pod = Regions::one(size, self.regions.base, Spec::splat(true));
pod.backlog = heights[1 ..] pod.backlog = heights[1 ..].to_vec().into_iter();
.iter()
.map(|&h| Size::new(self.used.x, h))
.collect::<Vec<_>>()
.into_iter();
// Layout the row. // Layout the row.
let mut pos = Point::zero(); let mut pos = Point::zero();
for (x, &rcol) in self.rcols.iter().enumerate() { for (x, &rcol) in self.rcols.iter().enumerate() {
if let Some(node) = self.cell(x, y) { if let Some(node) = self.cell(x, y) {
pod.current.x = rcol;
// All widths should be `rcol` except the base for auto columns. // All widths should be `rcol` except the base for auto columns.
pod.mutate(|size| size.x = rcol);
if self.cols[x] == TrackSizing::Auto { if self.cols[x] == TrackSizing::Auto {
pod.base.x = self.regions.base.x; pod.base.x = self.regions.base.x;
} }