#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum TabAxis {
Horizontal,
Vertical,
}
pub(crate) type RectBounds = (f32, f32, f32, f32);
pub(crate) type LabeledRect = (String, RectBounds);
pub fn bounds_span(bounds: &[LabeledRect]) -> Option<RectBounds> {
let mut iter = bounds.iter();
let (_, (x, y, w, h)) = iter.next()?;
let mut min_x = *x;
let mut min_y = *y;
let mut max_x = x + w;
let mut max_y = y + h;
for (_, (bx, by, bw, bh)) in iter {
min_x = min_x.min(*bx);
min_y = min_y.min(*by);
max_x = max_x.max(*bx + *bw);
max_y = max_y.max(*by + *bh);
}
Some((min_x, min_y, max_x, max_y))
}
pub fn detect_tab_axis(bounds: &[LabeledRect]) -> Option<TabAxis> {
let (min_x, min_y, max_x, max_y) = bounds_span(bounds)?;
let span_x = max_x - min_x;
let span_y = max_y - min_y;
if span_x >= span_y {
Some(TabAxis::Horizontal)
} else {
Some(TabAxis::Vertical)
}
}
pub fn tab_bounds(tabs: &[LabeledRect], label: &str) -> Option<RectBounds> {
tabs.iter()
.find(|(name, _)| name == label)
.map(|(_, bounds)| *bounds)
}
pub fn last_reachable_tab(
tabs: &[LabeledRect],
root: RectBounds,
axis: TabAxis,
margin: f32,
) -> Option<&LabeledRect> {
tabs.iter().rev().find(|(_, (x, y, w, h))| match axis {
TabAxis::Horizontal => x + w / 2.0 <= root.2 - margin,
TabAxis::Vertical => y + h / 2.0 <= root.3 - margin,
})
}