use crate::LayoutItem;
use serde::{Deserialize, Serialize};
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct BreakpointSpec {
pub name: String,
pub min_width_px: i32,
pub cols: i32,
}
pub fn select_breakpoint(
breakpoints: &[BreakpointSpec],
container_width_px: i32,
) -> Option<&BreakpointSpec> {
let mut best: Option<&BreakpointSpec> = None;
for bp in breakpoints {
if container_width_px >= bp.min_width_px
&& best.is_none_or(|b| bp.min_width_px >= b.min_width_px)
{
best = Some(bp);
}
}
best
}
pub fn scale_layout_cols(items: &[LayoutItem], from_cols: i32, to_cols: i32) -> Vec<LayoutItem> {
if from_cols < 1 || to_cols < 1 || from_cols == to_cols {
return items.to_vec();
}
let ratio = to_cols as f64 / from_cols as f64;
items
.iter()
.map(|it| {
let mut n = it.clone();
n.x = (it.x as f64 * ratio).round() as i32;
n.w = ((it.w as f64 * ratio).round() as i32).max(1);
n.x = n.x.max(0).min((to_cols - n.w).max(0));
n
})
.collect()
}