use std::cmp::Ordering;
use std::iter::IntoIterator;
use orrery_core::{
draw,
geometry::{Bounds, Size},
};
pub trait LayoutBounds {
fn layout_size(&self) -> Size {
self.layout_bounds().to_size()
}
fn layout_bounds(&self) -> Bounds;
}
pub fn calculate_label_spacing<'a, I>(texts: I, padding: f32) -> f32
where
I: IntoIterator<Item = Option<draw::Text<'a>>>,
{
texts
.into_iter()
.flatten()
.map(|text| text.calculate_size().width() + padding)
.max_by(|a, b| a.partial_cmp(b).unwrap_or(Ordering::Equal))
.unwrap_or(0.0)
}
pub fn distribute_horizontally(
sizes: &[Size],
min_spacing: f32,
extra_spacings: Option<&[f32]>,
) -> Vec<f32> {
let mut positions = Vec::with_capacity(sizes.len());
let mut x_position: f32 = 0.0;
for (i, size) in sizes.iter().enumerate() {
if i == 0 {
x_position += size.width() / 2.0;
} else {
let prev_width = sizes[i - 1].width();
let additional_spacing = extra_spacings
.and_then(|spacings| spacings.get(i - 1).copied())
.unwrap_or(0.0);
let effective_spacing = min_spacing.max(additional_spacing);
x_position += (prev_width / 2.0) + effective_spacing + (size.width() / 2.0);
}
positions.push(x_position);
}
positions
}