diff options
| author | Laurenz <laurmaedje@gmail.com> | 2022-02-22 14:31:09 +0100 |
|---|---|---|
| committer | Laurenz <laurmaedje@gmail.com> | 2022-02-23 14:53:55 +0100 |
| commit | e1f29d6cb9437a4afb2e4fc4ee10a5b8717ab9fa (patch) | |
| tree | 1ce5f2bd858f6665d3867a2939d4b474c1b70377 /src/library/grid.rs | |
| parent | 2bf32c51bceb2f3a8b7ebea3d7c7d6d96757591b (diff) | |
Rework the core context
Diffstat (limited to 'src/library/grid.rs')
| -rw-r--r-- | src/library/grid.rs | 61 |
1 files changed, 33 insertions, 28 deletions
diff --git a/src/library/grid.rs b/src/library/grid.rs index fc62f8eb..2d8cb462 100644 --- a/src/library/grid.rs +++ b/src/library/grid.rs @@ -15,7 +15,7 @@ pub struct GridNode { #[class] impl GridNode { - fn construct(_: &mut Vm, args: &mut Args) -> TypResult<Template> { + fn construct(_: &mut Context, args: &mut Args) -> TypResult<Template> { let columns = args.named("columns")?.unwrap_or_default(); let rows = args.named("rows")?.unwrap_or_default(); let base_gutter: Vec<TrackSizing> = args.named("gutter")?.unwrap_or_default(); @@ -35,7 +35,7 @@ impl GridNode { impl Layout for GridNode { fn layout( &self, - vm: &mut Vm, + ctx: &mut Context, regions: &Regions, styles: StyleChain, ) -> TypResult<Vec<Arc<Frame>>> { @@ -49,7 +49,7 @@ impl Layout for GridNode { ); // Measure the columns and layout the grid row-by-row. - layouter.layout(vm) + layouter.layout(ctx) } } @@ -198,19 +198,19 @@ impl<'a> GridLayouter<'a> { } /// Determines the columns sizes and then layouts the grid row-by-row. - pub fn layout(mut self, vm: &mut Vm) -> TypResult<Vec<Arc<Frame>>> { - self.measure_columns(vm)?; + pub fn layout(mut self, ctx: &mut Context) -> TypResult<Vec<Arc<Frame>>> { + self.measure_columns(ctx)?; for y in 0 .. self.rows.len() { // Skip to next region if current one is full, but only for content // rows, not for gutter rows. if y % 2 == 0 && self.regions.is_full() { - self.finish_region(vm)?; + self.finish_region(ctx)?; } match self.rows[y] { - TrackSizing::Auto => self.layout_auto_row(vm, y)?, - TrackSizing::Linear(v) => self.layout_linear_row(vm, v, y)?, + TrackSizing::Auto => self.layout_auto_row(ctx, y)?, + TrackSizing::Linear(v) => self.layout_linear_row(ctx, v, y)?, TrackSizing::Fractional(v) => { self.lrows.push(Row::Fr(v, y)); self.fr += v; @@ -218,12 +218,12 @@ impl<'a> GridLayouter<'a> { } } - self.finish_region(vm)?; + self.finish_region(ctx)?; Ok(self.finished) } /// Determine all column sizes. - fn measure_columns(&mut self, vm: &mut Vm) -> TypResult<()> { + fn measure_columns(&mut self, ctx: &mut Context) -> TypResult<()> { // Sum of sizes of resolved linear tracks. let mut linear = Length::zero(); @@ -248,7 +248,7 @@ impl<'a> GridLayouter<'a> { let available = self.regions.first.x - linear; if available >= Length::zero() { // Determine size of auto columns. - let (auto, count) = self.measure_auto_columns(vm, available)?; + let (auto, count) = self.measure_auto_columns(ctx, available)?; // If there is remaining space, distribute it to fractional columns, // otherwise shrink auto columns. @@ -271,7 +271,7 @@ impl<'a> GridLayouter<'a> { /// Measure the size that is available to auto columns. fn measure_auto_columns( &mut self, - vm: &mut Vm, + ctx: &mut Context, available: Length, ) -> TypResult<(Length, usize)> { let mut auto = Length::zero(); @@ -298,7 +298,7 @@ impl<'a> GridLayouter<'a> { pod.base.y = v.resolve(self.regions.base.y); } - let frame = node.layout(vm, &pod, self.styles)?.remove(0); + let frame = node.layout(ctx, &pod, self.styles)?.remove(0); resolved.set_max(frame.size.x); } } @@ -352,7 +352,7 @@ impl<'a> GridLayouter<'a> { /// Layout a row with automatic height. Such a row may break across multiple /// regions. - fn layout_auto_row(&mut self, vm: &mut Vm, y: usize) -> TypResult<()> { + fn layout_auto_row(&mut self, ctx: &mut Context, y: usize) -> TypResult<()> { let mut resolved: Vec<Length> = vec![]; // Determine the size for each region of the row. @@ -367,7 +367,7 @@ impl<'a> GridLayouter<'a> { } let mut sizes = node - .layout(vm, &pod, self.styles)? + .layout(ctx, &pod, self.styles)? .into_iter() .map(|frame| frame.size.y); @@ -390,7 +390,7 @@ impl<'a> GridLayouter<'a> { // Layout into a single region. if let &[first] = resolved.as_slice() { - let frame = self.layout_single_row(vm, first, y)?; + let frame = self.layout_single_row(ctx, first, y)?; self.push_row(frame); return Ok(()); } @@ -405,12 +405,12 @@ impl<'a> GridLayouter<'a> { } // Layout into multiple regions. - let frames = self.layout_multi_row(vm, &resolved, y)?; + let frames = self.layout_multi_row(ctx, &resolved, y)?; let len = frames.len(); for (i, frame) in frames.into_iter().enumerate() { self.push_row(frame); if i + 1 < len { - self.finish_region(vm)?; + self.finish_region(ctx)?; } } @@ -419,14 +419,19 @@ impl<'a> GridLayouter<'a> { /// Layout a row with linear height. Such a row cannot break across multiple /// regions, but it may force a region break. - fn layout_linear_row(&mut self, vm: &mut Vm, v: Linear, y: usize) -> TypResult<()> { + fn layout_linear_row( + &mut self, + ctx: &mut Context, + v: Linear, + y: usize, + ) -> TypResult<()> { let resolved = v.resolve(self.regions.base.y); - let frame = self.layout_single_row(vm, resolved, y)?; + let frame = self.layout_single_row(ctx, resolved, y)?; // Skip to fitting region. let height = frame.size.y; while !self.regions.first.y.fits(height) && !self.regions.in_last() { - self.finish_region(vm)?; + self.finish_region(ctx)?; // Don't skip multiple regions for gutter and don't push a row. if y % 2 == 1 { @@ -442,7 +447,7 @@ impl<'a> GridLayouter<'a> { /// Layout a row with fixed height and return its frame. fn layout_single_row( &self, - vm: &mut Vm, + ctx: &mut Context, height: Length, y: usize, ) -> TypResult<Frame> { @@ -460,7 +465,7 @@ impl<'a> GridLayouter<'a> { .select(self.regions.base, size); let pod = Regions::one(size, base, Spec::splat(true)); - let frame = node.layout(vm, &pod, self.styles)?.remove(0); + let frame = node.layout(ctx, &pod, self.styles)?.remove(0); output.push_frame(pos, frame); } @@ -473,7 +478,7 @@ impl<'a> GridLayouter<'a> { /// Layout a row spanning multiple regions. fn layout_multi_row( &self, - vm: &mut Vm, + ctx: &mut Context, heights: &[Length], y: usize, ) -> TypResult<Vec<Frame>> { @@ -486,7 +491,7 @@ impl<'a> GridLayouter<'a> { // Prepare regions. let size = Size::new(self.used.x, heights[0]); let mut pod = Regions::one(size, self.regions.base, Spec::splat(true)); - pod.backlog = heights[1 ..].to_vec().into_iter(); + pod.backlog = heights[1 ..].to_vec(); // Layout the row. let mut pos = Point::zero(); @@ -500,7 +505,7 @@ impl<'a> GridLayouter<'a> { } // Push the layouted frames into the individual output frames. - let frames = node.layout(vm, &pod, self.styles)?; + let frames = node.layout(ctx, &pod, self.styles)?; for (output, frame) in outputs.iter_mut().zip(frames) { output.push_frame(pos, frame); } @@ -520,7 +525,7 @@ impl<'a> GridLayouter<'a> { } /// Finish rows for one region. - fn finish_region(&mut self, vm: &mut Vm) -> TypResult<()> { + fn finish_region(&mut self, ctx: &mut Context) -> TypResult<()> { // Determine the size of the grid in this region, expanding fully if // there are fr rows. let mut size = self.used; @@ -539,7 +544,7 @@ impl<'a> GridLayouter<'a> { Row::Fr(v, y) => { let remaining = self.full - self.used.y; let height = v.resolve(self.fr, remaining); - self.layout_single_row(vm, height, y)? + self.layout_single_row(ctx, height, y)? } }; |
