use blit::{Axis, Context, Fit, LayoutCx, Scalar, Scratch};
blit::builder! {
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Layout<T: Scalar> {
new(columns: u32),
column_gap: T = T::ZERO,
row_gap: T = T::ZERO,
}
}
impl<T: Scalar> Layout<T> {
pub fn gap(mut self, gap: T) -> Self {
self.column_gap = gap;
self.row_gap = gap;
self
}
}
blit::builder! {
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Item<T: Scalar> {
new(),
#[into]
width: Option<T> = None,
#[into]
height: Option<T> = None,
row_span: u32 = 1,
column_span: u32 = 1,
}
}
pub fn new<T: Scalar>(columns: u32) -> Layout<T> {
Layout::new(columns)
}
pub fn item<T: Scalar>() -> Item<T> {
Item::new()
}
impl<C: Context<Scalar = T>, T: Scalar> blit::Layout<C> for Layout<T> {
type Item = Item<T>;
fn fit(&self, cx: &mut LayoutCx<'_, C, Self::Item>, axis: Axis) -> Fit<T> {
let columns = self.columns as usize;
let column_gap = self.column_gap.max(T::ZERO);
let row_gap = self.row_gap.max(T::ZERO);
if axis == Axis::Horizontal {
let mut count = 0usize;
let mut required_min = 0.0f32;
let mut required_natural = 0.0f32;
for child in cx.children() {
let item = cx.item(child);
let child_w = item
.width
.map_or_else(|| cx.fit(child, Axis::Horizontal), |w| Fit::exact(w.max(T::ZERO)));
let span = item.column_span as usize;
let span_gaps = column_gap.repeat(span.saturating_sub(1));
let scale = columns as f32 / span as f32;
required_min = required_min.max((child_w.min - span_gaps).max(T::ZERO).to_f32() * scale);
required_natural = required_natural.max((child_w.natural - span_gaps).max(T::ZERO).to_f32() * scale);
count += 1;
}
if count == 0 {
return Fit::ZERO;
}
let snap = |required: f32| {
let mut width = T::from_f32(required);
if width.to_f32() < required {
width = width.endpoint(T::from_f32(1.0));
}
width + column_gap.repeat(columns.saturating_sub(1))
};
return Fit::new(snap(required_min), snap(required_natural));
}
let (count, spanning, row_count, _) = self.grid_cells(cx, false);
if row_count == 0 {
return Fit::ZERO;
}
let height = if spanning {
let (min_track, natural_track) = self.row_track(cx, row_gap);
Fit::new(
T::from_f32(min_track * row_count as f32),
T::from_f32(natural_track * row_count as f32),
)
} else {
let mut children = cx.children();
let mut height = Fit::ZERO;
let mut index = 0;
while index < count {
let group = columns.min(count - index);
let mut row_height = Fit::ZERO;
for child in children.by_ref().take(group) {
row_height = row_height.max(self.child_height(cx, child));
}
height += row_height;
index += group;
}
height
};
height + Fit::exact(row_gap.repeat(row_count.saturating_sub(1)))
}
fn distribute(&self, cx: &mut LayoutCx<'_, C, Self::Item>, axis: Axis, extent: T) {
let columns = self.columns as usize;
let column_gap = self.column_gap.max(T::ZERO);
let row_gap = self.row_gap.max(T::ZERO);
let (count, spanning, row_count, positions) = self.grid_cells(cx, true);
if row_count == 0 {
return;
}
if axis == Axis::Horizontal {
let horizontal_gaps = column_gap.repeat(columns.saturating_sub(1));
let content_width = (extent - horizontal_gaps).max(T::ZERO).to_f32();
let column_edge = |column: usize| T::from_f32(column as f32 * content_width / columns as f32);
for (index, child) in cx.children().enumerate() {
let column = if spanning { positions[index].1 } else { index % columns };
let span = cx.item(child).column_span as usize;
let assigned =
column_edge(column + span) - column_edge(column) + column_gap.repeat(span.saturating_sub(1));
cx.set_extent(child, assigned);
cx.set_offset(child, column_edge(column) + column_gap.repeat(column));
}
return;
}
if spanning {
let row_track = self.row_track(cx, row_gap).1;
for (index, child) in cx.children().enumerate() {
let row = positions[index].0;
let span = cx.item(child).row_span as usize;
let assigned = T::from_f32((row + span) as f32 * row_track) - T::from_f32(row as f32 * row_track)
+ row_gap.repeat(span.saturating_sub(1));
cx.set_extent(child, assigned);
cx.set_offset(child, T::from_f32(row as f32 * row_track) + row_gap.repeat(row));
}
} else {
let mut children = cx.children();
let mut index = 0;
let mut y = T::ZERO;
while index < count {
let group = columns.min(count - index);
let mut row_height = T::ZERO;
for child in children.take(group) {
row_height = row_height.max(self.child_height(cx, child).natural);
}
for child in children.by_ref().take(group) {
cx.set_extent(child, row_height);
cx.set_offset(child, y);
}
y += row_height + row_gap;
index += group;
}
}
}
}
impl<T: Scalar> Layout<T> {
#[inline]
fn child_height<C: Context<Scalar = T>>(&self, cx: &LayoutCx<'_, C, Item<T>>, child: blit::NodeId) -> Fit<T> {
cx.item(child)
.height
.map_or_else(|| cx.fit(child, Axis::Vertical), |h| Fit::exact(h.max(T::ZERO)))
}
fn row_track<C: Context<Scalar = T>>(&self, cx: &LayoutCx<'_, C, Item<T>>, row_gap: T) -> (f32, f32) {
let mut min_track = 0.0f32;
let mut natural_track = 0.0f32;
for child in cx.children() {
let child_h = self.child_height(cx, child);
let span = cx.item(child).row_span as usize;
let span_gaps = row_gap.repeat(span.saturating_sub(1));
min_track = min_track.max((child_h.min - span_gaps).max(T::ZERO).to_f32() / span as f32);
natural_track = natural_track.max((child_h.natural - span_gaps).max(T::ZERO).to_f32() / span as f32);
}
(min_track, natural_track)
}
fn grid_cells<'a, C: Context<Scalar = T>>(
&self,
cx: &LayoutCx<'a, C, Item<T>>,
need_positions: bool,
) -> (usize, bool, usize, Scratch<'a, (usize, usize)>) {
assert!(self.columns != 0, "grid must have at least one column");
let columns = self.columns as usize;
let mut count = 0usize;
let mut spanning = false;
for child in cx.children() {
let item = cx.item(child);
assert!(
item.row_span != 0 && item.column_span != 0,
"grid spans must be nonzero"
);
assert!(
item.column_span <= self.columns,
"grid column span exceeds its column count"
);
spanning |= item.row_span != 1 || item.column_span != 1;
count += 1;
}
let mut positions = cx.scratch().alloc(if spanning && need_positions { count } else { 0 });
let row_count = if spanning {
let mut column_rows = cx.scratch().alloc(columns);
column_rows.resize(columns, 0usize);
let mut cursor = (0usize, 0usize);
let mut rows = 0usize;
for child in cx.children() {
let item = cx.item(child);
let span = item.column_span as usize;
let (cursor_row, cursor_column) = cursor;
let (row, column) = if span <= columns - cursor_column
&& column_rows[cursor_column..cursor_column + span]
.iter()
.all(|row| *row <= cursor_row)
{
cursor
} else {
(0..=columns - span)
.map(|column| {
let row = column_rows[column..column + span]
.iter()
.copied()
.fold(cursor_row + usize::from(column < cursor_column), usize::max);
(row, column)
})
.min()
.unwrap()
};
column_rows[column..column + span]
.fill(row.checked_add(item.row_span as usize).expect("too many grid rows"));
cursor = if column + span == columns {
(row + 1, 0)
} else {
(row, column + span)
};
if need_positions {
positions.push((row, column));
}
rows = rows.max(row + item.row_span as usize);
}
rows
} else {
count.div_ceil(columns)
};
(count, spanning, row_count, positions)
}
}