mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-17 05:14:24 +00:00
286 lines
14 KiB
Rust
286 lines
14 KiB
Rust
//! The `Grid` cursor: columns resolved from last pass's measurements, and the cell each child takes.
|
|
|
|
use crate::columns::ColumnWidth;
|
|
use crate::flow::Flow;
|
|
use emath::{Pos2, Rect, Vec2};
|
|
|
|
/// The width of each column in `room`: `Fixed` is the app's width, `Content` the widest child of last pass
|
|
/// (`content[i]`, zero when unknown), and `Fill` columns share what is left by weight. `Controls` and `Toggle`
|
|
/// widths take no room until their owner resolves them.
|
|
pub fn resolve_columns(columns: &[ColumnWidth], content: &[f32], room: f32, gap: f32) -> Vec<f32> {
|
|
let known = |i: usize| content.get(i).copied().unwrap_or(0.0);
|
|
let mut widths: Vec<f32> = columns
|
|
.iter()
|
|
.enumerate()
|
|
.map(|(i, column)| match column {
|
|
ColumnWidth::Fixed(width) => *width,
|
|
ColumnWidth::Fill { .. } | ColumnWidth::Controls(_) | ColumnWidth::Toggle => 0.0,
|
|
_ => known(i),
|
|
})
|
|
.collect();
|
|
let gaps = gap * columns.len().saturating_sub(1) as f32;
|
|
let weights: f32 = columns.iter().map(|column| if let ColumnWidth::Fill { weight } = column { *weight } else { 0.0 }).sum();
|
|
let rest = (room - widths.iter().sum::<f32>() - gaps).max(0.0);
|
|
if weights > 0.0 {
|
|
for (width, column) in widths.iter_mut().zip(columns) {
|
|
if let ColumnWidth::Fill { weight } = column {
|
|
*width = rest * weight / weights;
|
|
}
|
|
}
|
|
}
|
|
widths
|
|
}
|
|
|
|
/// What a `Grid` pass measured: the widest child per column and the tallest per row, and the least each column
|
|
/// takes when the grid asked its columns for it.
|
|
#[derive(Clone, Debug, Default, PartialEq)]
|
|
#[non_exhaustive]
|
|
pub struct GridMeasure {
|
|
/// The widest child of each column.
|
|
pub columns: Vec<f32>,
|
|
/// The tallest child of each row.
|
|
pub rows: Vec<f32>,
|
|
/// The least width of each column: what its widest cell takes with every leaf at its least. Empty when the grid
|
|
/// did not ask; a column with no entry has no least.
|
|
pub least: Vec<f32>,
|
|
}
|
|
|
|
impl GridMeasure {
|
|
/// A measurement of `columns` and `rows`.
|
|
pub const fn new(columns: Vec<f32>, rows: Vec<f32>) -> Self {
|
|
GridMeasure { columns, rows, least: Vec::new() }
|
|
}
|
|
|
|
/// The measurement with the least width of each column.
|
|
#[must_use]
|
|
pub fn with_least(mut self, least: Vec<f32>) -> Self {
|
|
self.least = least;
|
|
self
|
|
}
|
|
}
|
|
|
|
/// Hands out the cells of one `Grid` pass, row-major. Cells are placed from `last` pass's measurements; this
|
|
/// pass's own are collected in `measured`.
|
|
#[derive(Clone, Debug)]
|
|
pub struct GridCursor {
|
|
flow: Flow,
|
|
columns: Vec<ColumnWidth>,
|
|
/// Which columns share the room left (`Fill` in the columns the app declared): a `Content` column's room does
|
|
/// not shrink by their widths.
|
|
fills: Vec<bool>,
|
|
gap: Vec2,
|
|
widths: Vec<f32>,
|
|
/// The least width of each column, which the room of every other column leaves it.
|
|
least: Vec<f32>,
|
|
/// Whether the cells measure in no bound along x: the sweep that asks the columns for their least.
|
|
unbounded: bool,
|
|
/// Whether each `Content` column measures in the room the others leave at their least, whatever the columns before
|
|
/// it took.
|
|
alone: bool,
|
|
last_rows: Vec<f32>,
|
|
next: usize,
|
|
measured: GridMeasure,
|
|
}
|
|
|
|
impl GridCursor {
|
|
/// A cursor over `columns` in `flow`'s rect, with `gap` between columns (x) and rows (y), placed by `last`. A
|
|
/// column is never narrower than its least in `last`: a `Content` column starts there, and a `Fill` column's share
|
|
/// does not go below it.
|
|
pub fn new(flow: Flow, columns: &[ColumnWidth], gap: Vec2, last: &GridMeasure) -> Self {
|
|
let least = |i: usize| last.least.get(i).copied().unwrap_or(0.0);
|
|
let content: Vec<f32> = (0..columns.len()).map(|i| last.columns.get(i).copied().unwrap_or(0.0).max(least(i))).collect();
|
|
let widths = resolve_columns(columns, &content, flow.rect().width(), gap.x)
|
|
.into_iter()
|
|
.enumerate()
|
|
.map(|(i, width)| if matches!(columns[i], ColumnWidth::Fill { .. }) { width.max(least(i)) } else { width })
|
|
.collect();
|
|
let fills = columns.iter().map(|kind| matches!(kind, ColumnWidth::Fill { .. })).collect();
|
|
GridCursor {
|
|
flow,
|
|
columns: columns.to_vec(),
|
|
fills,
|
|
gap,
|
|
widths,
|
|
least: (0..columns.len()).map(least).collect(),
|
|
unbounded: false,
|
|
alone: false,
|
|
last_rows: last.rows.clone(),
|
|
next: 0,
|
|
measured: GridMeasure::default(),
|
|
}
|
|
}
|
|
|
|
/// A cursor for a measuring run under a body sized from its content: each `Fill` column of `columns` is placed
|
|
/// as a `Content` column, taking the width its cells take, while the room of the cells after it stays what the
|
|
/// fill column leaves them. `last` gives the rows and the columns' least.
|
|
pub fn sized_by_content(flow: Flow, columns: &[ColumnWidth], gap: Vec2, last: &GridMeasure) -> Self {
|
|
let mut cursor = Self::new(flow, &as_content(columns), gap, &GridMeasure::new(Vec::new(), last.rows.clone()).with_least(last.least.clone()));
|
|
cursor.fills = columns.iter().map(|kind| matches!(kind, ColumnWidth::Fill { .. })).collect();
|
|
cursor
|
|
}
|
|
|
|
/// A cursor for the sweep that asks each column for its least: every column but a `Fixed` one is placed as a
|
|
/// `Content` column whose cells measure in no bound along x, so each column measures its widest cell.
|
|
pub fn unbounded(flow: Flow, columns: &[ColumnWidth], gap: Vec2) -> Self {
|
|
let mut cursor = Self::new(flow, &as_content(columns), gap, &GridMeasure::default());
|
|
cursor.unbounded = true;
|
|
cursor
|
|
}
|
|
|
|
/// The cursor with each `Content` column measuring in the room the other columns leave at their least, whatever
|
|
/// the columns before it took: each column asks for what it would take alone, so `short` tells whether the
|
|
/// columns fit beside each other.
|
|
#[must_use]
|
|
pub fn each_alone(mut self) -> Self {
|
|
self.alone = true;
|
|
self
|
|
}
|
|
|
|
/// Whether the columns, as this sweep measured them, do not fit beside each other: the columns that do not share
|
|
/// the room left take more than the room, or a cell of a `Fill` column took more than its share. Such a grid asks
|
|
/// its columns for their least; any other grid keeps what it measured.
|
|
pub fn short(&self) -> bool {
|
|
let room = self.flow.rect().width();
|
|
if self.columns.len() < 2 || !room.is_finite() {
|
|
return false;
|
|
}
|
|
let measured = &self.measured.columns;
|
|
let gaps = self.gap.x * self.columns.len().saturating_sub(1) as f32;
|
|
let kept: f32 =
|
|
(self.columns.iter().zip(measured)).filter(|(kind, _)| !matches!(kind, ColumnWidth::Fill { .. })).map(|(_, width)| *width).sum();
|
|
// Half a point: less than a pixel shows at any scale the box runs.
|
|
if kept + gaps > room + 0.5 {
|
|
return true;
|
|
}
|
|
let shares = resolve_columns(&self.columns, measured, room, self.gap.x);
|
|
(self.fills.iter().zip(measured).zip(shares)).any(|((fill, took), share)| *fill && *took > share + 0.5)
|
|
}
|
|
|
|
/// The cell of the next child, whose measured size is `size`.
|
|
pub fn cell(&mut self, size: Vec2) -> Rect {
|
|
let count = self.columns.len().max(1);
|
|
let (column, row, start, top) = self.next_place();
|
|
self.next += 1;
|
|
self.measured.columns.resize(count, 0.0);
|
|
self.measured.columns[column] = self.measured.columns[column].max(size.x);
|
|
if self.measured.rows.len() <= row {
|
|
self.measured.rows.resize(row + 1, 0.0);
|
|
}
|
|
self.measured.rows[row] = self.measured.rows[row].max(size.y);
|
|
// A `Content` column is at least as wide as the cell placed in it, so a column no pass has measured yet takes its
|
|
// first cell's width and the cells after it are placed past it.
|
|
if let (Some(ColumnWidth::Content), Some(width)) = (self.columns.get(column), self.widths.get_mut(column)) {
|
|
*width = width.max(size.x);
|
|
}
|
|
let width = self.widths.get(column).copied().unwrap_or(size.x);
|
|
let height = self.last_rows.get(row).copied().unwrap_or(size.y);
|
|
let span = self.flow.span(start, width);
|
|
Rect::from_min_size(Pos2::new(span.min.x, self.flow.rect().top() + top), Vec2::new(width, height))
|
|
}
|
|
|
|
/// The room the next child measures in: as wide as its column, or for a `Content` column as wide as the columns
|
|
/// that do not fill leave, and as tall as the grid below its row's top. The cells placed before it do not shorten
|
|
/// it, as they would shorten the rest of a top-down flow.
|
|
pub fn room(&self) -> Vec2 {
|
|
let count = self.columns.len().max(1);
|
|
let (column, _, _, top) = self.next_place();
|
|
let width = match self.columns.get(column) {
|
|
Some(ColumnWidth::Content) if self.unbounded => f32::INFINITY,
|
|
Some(ColumnWidth::Content) => {
|
|
// Every other column keeps at least its least: a column that fills keeps only that, and so does a
|
|
// content column when each column measures alone.
|
|
let kept: f32 = (self.columns.iter().zip(&self.fills).zip(&self.widths).zip(&self.least).enumerate())
|
|
.filter(|(at, _)| *at != column)
|
|
.map(|(_, (((kind, fill), width), least))| match kind {
|
|
_ if *fill => *least,
|
|
ColumnWidth::Content if self.alone => *least,
|
|
_ => *width,
|
|
})
|
|
.sum();
|
|
let gaps = self.gap.x * count.saturating_sub(1) as f32;
|
|
(self.flow.rect().width() - kept - gaps).max(0.0)
|
|
}
|
|
_ => self.widths.get(column).copied().unwrap_or(0.0),
|
|
};
|
|
Vec2::new(width, (self.flow.rect().height() - top).max(0.0))
|
|
}
|
|
|
|
/// The rect the next child measures and is placed in: from its column's start edge and its row's top, as large
|
|
/// as `room()`.
|
|
pub fn region(&self) -> Rect {
|
|
let (_, _, start, top) = self.next_place();
|
|
let room = self.room();
|
|
// An unbounded cell is placed in the flow's rect; only its room has no bound.
|
|
let span = self.flow.span(start, if room.x.is_finite() { room.x } else { self.flow.rect().width() });
|
|
Rect::from_min_size(Pos2::new(span.min.x, self.flow.rect().top() + top), room)
|
|
}
|
|
|
|
/// The column and row of the next cell, how far its column starts from the start edge and how far its row
|
|
/// starts below the grid's top.
|
|
fn next_place(&self) -> (usize, usize, f32, f32) {
|
|
let count = self.columns.len().max(1);
|
|
let (column, row) = (self.next % count, self.next / count);
|
|
let start: f32 = self.widths.iter().take(column).map(|width| width + self.gap.x).sum();
|
|
let top: f32 = self.last_rows.iter().take(row).map(|height| height + self.gap.y).sum();
|
|
(column, row, start, top)
|
|
}
|
|
|
|
/// The room the next child fills when it fills its width: its column's width, which for a `Content` column is the
|
|
/// width its cells have measured, not the room the cell measures in. A column no pass has measured yet offers 0,
|
|
/// so a filling child there takes its content width.
|
|
pub fn fill_room(&self) -> Vec2 {
|
|
let mut room = self.room();
|
|
if self.in_content_column() {
|
|
room.x = self.widths.get(self.next % self.columns.len().max(1)).copied().unwrap_or(0.0);
|
|
}
|
|
room
|
|
}
|
|
|
|
/// Whether the next child goes in a `Content` column.
|
|
pub fn in_content_column(&self) -> bool {
|
|
matches!(self.columns.get(self.next % self.columns.len().max(1)), Some(ColumnWidth::Content))
|
|
}
|
|
|
|
/// The cell last placed measured `size` after all: its column and row record that, and a `Content` column offers
|
|
/// the cells after it in this pass the wider width.
|
|
pub fn widen(&mut self, size: Vec2) {
|
|
let Some(placed) = self.next.checked_sub(1) else { return };
|
|
let count = self.columns.len().max(1);
|
|
let (column, row) = (placed % count, placed / count);
|
|
self.measured.columns[column] = self.measured.columns[column].max(size.x);
|
|
self.measured.rows[row] = self.measured.rows[row].max(size.y);
|
|
if let (Some(ColumnWidth::Content), Some(width)) = (self.columns.get(column), self.widths.get_mut(column)) {
|
|
*width = width.max(size.x);
|
|
}
|
|
}
|
|
|
|
/// What this pass measured.
|
|
pub fn measured(&self) -> &GridMeasure {
|
|
&self.measured
|
|
}
|
|
}
|
|
|
|
/// `columns` with each `Fill` column as a `Content` column.
|
|
fn as_content(columns: &[ColumnWidth]) -> Vec<ColumnWidth> {
|
|
columns.iter().map(|kind| if matches!(kind, ColumnWidth::Fill { .. }) { ColumnWidth::Content } else { *kind }).collect()
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
|
|
fn flow(rtl: bool) -> Flow {
|
|
Flow::new(Rect::from_min_size(Pos2::ZERO, Vec2::new(300.0, 100.0)), rtl)
|
|
}
|
|
|
|
#[test]
|
|
fn a_content_cell_after_a_fill_cell_measures_in_the_whole_height_and_the_width_fixed_columns_leave() {
|
|
let columns = [ColumnWidth::fill(1.0), ColumnWidth::Content, ColumnWidth::Fixed(100.0)];
|
|
let last = GridMeasure::new(vec![0.0, 50.0, 0.0], vec![100.0]);
|
|
let mut cursor = GridCursor::new(flow(false), &columns, Vec2::ZERO, &last);
|
|
assert_eq!(cursor.room(), Vec2::new(150.0, 100.0), "the fill cell measures in its own column");
|
|
cursor.cell(Vec2::new(150.0, 100.0));
|
|
assert_eq!(cursor.room(), Vec2::new(200.0, 100.0), "the content cell may take what the fill column holds");
|
|
}
|
|
}
|