use std::sync::Arc;
use parking_lot::Mutex;
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum TrackWidth {
Px(f32),
Share(f32),
Content,
}
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct TrackSpec {
pub width: TrackWidth,
pub min: f32,
pub max: f32,
}
impl TrackSpec {
pub fn new(width: TrackWidth) -> Self {
Self {
width,
min: 0.0,
max: f32::INFINITY,
}
}
pub fn px(px: f32) -> Self {
Self::new(TrackWidth::Px(px))
}
pub fn share(share: f32) -> Self {
Self::new(TrackWidth::Share(share))
}
pub fn content() -> Self {
Self::new(TrackWidth::Content)
}
pub fn min(mut self, min: f32) -> Self {
self.min = min.max(0.0);
self
}
pub fn max(mut self, max: f32) -> Self {
self.max = max.max(0.0);
self
}
fn measured(&self) -> bool {
matches!(self.width, TrackWidth::Content)
}
fn clamp(&self, width: f32) -> f32 {
let width = if width.is_finite() { width } else { 0.0 };
width.min(self.max).max(self.min)
}
}
struct Inner {
specs: Vec<TrackSpec>,
content: Vec<f32>,
}
#[derive(Clone)]
pub struct ColumnTracks(Arc<Mutex<Inner>>);
impl PartialEq for ColumnTracks {
fn eq(&self, other: &Self) -> bool {
Arc::ptr_eq(&self.0, &other.0)
}
}
impl std::fmt::Debug for ColumnTracks {
fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let inner = self.0.lock();
formatter
.debug_struct("ColumnTracks")
.field("specs", &inner.specs)
.field("content", &inner.content)
.finish()
}
}
impl ColumnTracks {
pub fn new(specs: impl IntoIterator<Item = TrackSpec>) -> Self {
let specs: Vec<TrackSpec> = specs.into_iter().collect();
let content = vec![0.0; specs.len()];
Self(Arc::new(Mutex::new(Inner { specs, content })))
}
pub fn len(&self) -> usize {
self.0.lock().specs.len()
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
pub fn specs(&self) -> Vec<TrackSpec> {
self.0.lock().specs.clone()
}
pub fn content_widths(&self) -> Vec<f32> {
self.0.lock().content.clone()
}
pub(crate) fn measures(&self, index: usize) -> bool {
self.0.lock().specs.get(index).is_some_and(TrackSpec::measured)
}
pub(crate) fn record(&self, widths: &[f32]) -> bool {
let mut inner = self.0.lock();
let mut widened = false;
for (held, width) in inner.content.iter_mut().zip(widths) {
if width.is_finite() && *width > *held + 0.01 {
*held = *width;
widened = true;
}
}
widened
}
pub fn widths(&self, available: Option<f32>, gap: f32) -> Vec<f32> {
let inner = self.0.lock();
resolve_tracks(&inner.specs, &inner.content, available, gap)
}
}
pub(crate) fn resolve_tracks(specs: &[TrackSpec], content: &[f32], available: Option<f32>, gap: f32) -> Vec<f32> {
let measured = |index: usize| content.get(index).copied().unwrap_or(0.0);
let available = available.filter(|width| width.is_finite());
let shares: Vec<Option<f32>> = specs
.iter()
.map(|spec| match spec.width {
TrackWidth::Share(share) if available.is_some() && share.is_finite() && share > 0.0 => Some(share),
_ => None,
})
.collect();
let mut widths: Vec<f32> = specs
.iter()
.enumerate()
.map(|(index, spec)| match spec.width {
TrackWidth::Px(px) => spec.clamp(px),
TrackWidth::Content => spec.clamp(measured(index)),
TrackWidth::Share(_) => spec.clamp(0.0),
})
.collect();
let Some(available) = available else {
return widths;
};
let gaps = gap.max(0.0) * specs.len().saturating_sub(1) as f32;
let mut free: Vec<usize> = (0..specs.len()).filter(|index| shares[*index].is_some()).collect();
loop {
let taken: f32 = (0..specs.len())
.filter(|index| !free.contains(index))
.map(|index| widths[index])
.sum();
let room = (available - taken - gaps).max(0.0);
let total: f32 = free.iter().filter_map(|index| shares[*index]).sum();
if free.is_empty() || total <= 0.0 {
break;
}
let mut held = None;
for &index in &free {
let wanted = room * shares[index].unwrap_or(0.0) / total;
widths[index] = specs[index].clamp(wanted);
if (widths[index] - wanted).abs() > 0.01 && held.is_none() {
held = Some(index);
}
}
match held {
Some(index) => free.retain(|free| *free != index),
None => break,
}
}
widths
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn shares_split_what_pixels_content_and_gaps_leave() {
let specs = [TrackSpec::share(1.0), TrackSpec::content(), TrackSpec::px(120.0)];
let widths = resolve_tracks(&specs, &[300.0, 64.0, 0.0], Some(608.0), 16.0);
assert_eq!(widths, [392.0, 64.0, 120.0]);
}
#[test]
fn shares_split_in_proportion_and_one_held_by_its_most_gives_the_rest_to_the_others() {
let specs = [TrackSpec::share(1.0), TrackSpec::share(3.0)];
assert_eq!(resolve_tracks(&specs, &[0.0, 0.0], Some(400.0), 0.0), [100.0, 300.0]);
let held = [TrackSpec::share(1.0), TrackSpec::share(3.0).max(200.0)];
assert_eq!(resolve_tracks(&held, &[0.0, 0.0], Some(400.0), 0.0), [200.0, 200.0]);
}
#[test]
fn a_share_keeps_its_least_and_the_row_is_wider_than_its_room() {
let specs = [TrackSpec::share(1.0).min(200.0), TrackSpec::px(300.0)];
let widths = resolve_tracks(&specs, &[0.0, 0.0], Some(320.0), 16.0);
assert_eq!(widths, [200.0, 300.0]);
}
#[test]
fn where_the_room_is_not_known_a_share_is_at_its_least_whatever_is_in_it() {
let specs = [TrackSpec::share(1.0).min(80.0), TrackSpec::share(1.0)];
let widths = resolve_tracks(&specs, &[40.0, 150.0], None, 16.0);
assert_eq!(widths, [80.0, 0.0]);
}
#[test]
fn content_is_held_between_its_least_and_its_most() {
let specs = [TrackSpec::content().max(100.0), TrackSpec::content().min(48.0)];
assert_eq!(resolve_tracks(&specs, &[180.0, 12.0], Some(500.0), 0.0), [100.0, 48.0]);
}
#[test]
fn a_track_keeps_the_widest_cell_it_has_had() {
let tracks = ColumnTracks::new([TrackSpec::content(), TrackSpec::px(40.0), TrackSpec::share(1.0)]);
assert!(tracks.measures(0) && !tracks.measures(1) && !tracks.measures(2));
assert!(tracks.record(&[90.0, 0.0, 10.0]));
assert!(!tracks.record(&[60.0, 0.0, 10.0]), "narrower cells widen nothing");
assert!(tracks.record(&[120.0, 0.0, 0.0]));
assert_eq!(tracks.content_widths(), [120.0, 0.0, 10.0]);
assert_eq!(tracks.widths(Some(400.0), 0.0), [120.0, 40.0, 240.0]);
}
#[test]
fn two_lists_are_equal_only_where_they_are_the_same() {
let tracks = ColumnTracks::new([TrackSpec::content()]);
assert_eq!(tracks, tracks.clone());
assert_ne!(tracks, ColumnTracks::new([TrackSpec::content()]));
}
}