use crate::resolved_style::EdgeInsets;
use crate::{Size, SizeConstraint};
use k2f_core::{FixedSizeHint, LayoutHint, Pt};
pub fn fixed_size_hint(layout: &Option<LayoutHint>) -> FixedSizeHint {
match layout {
Some(LayoutHint::Stack { size, .. }) => *size,
Some(LayoutHint::Overlay { size }) => *size,
Some(LayoutHint::Grid { size, .. }) => *size,
_ => FixedSizeHint::default(),
}
}
pub fn inner_max_for_children(
constraint: SizeConstraint,
padding: EdgeInsets,
fixed: FixedSizeHint,
) -> Size {
let mut inner_max = Size::new(
subtract_if_bounded(constraint.max.width, padding.horizontal()),
subtract_if_bounded(constraint.max.height, padding.vertical()),
);
if let Some(w) = fixed.width {
let fixed_inner_w = subtract_if_bounded(w, padding.horizontal());
inner_max.width = min_pt(inner_max.width, fixed_inner_w);
}
if let Some(h) = fixed.height {
let fixed_inner_h = subtract_if_bounded(h, padding.vertical());
inner_max.height = min_pt(inner_max.height, fixed_inner_h);
}
inner_max
}
pub fn cap_inner_size_by_fixed(inner: Size, padding: EdgeInsets, fixed: FixedSizeHint) -> Size {
let mut capped = inner;
if let Some(w) = fixed.width {
let fixed_inner_w = subtract_if_bounded(w, padding.horizontal());
capped.width = min_pt(capped.width, fixed_inner_w);
}
if let Some(h) = fixed.height {
let fixed_inner_h = subtract_if_bounded(h, padding.vertical());
capped.height = min_pt(capped.height, fixed_inner_h);
}
capped
}
pub fn apply_fixed_min_outer(
measured_outer: Size,
fixed: FixedSizeHint,
constraint: SizeConstraint,
) -> Size {
let mut out = measured_outer;
let mut changed = false;
if let Some(w) = fixed.width {
out.width = Pt(out.width.0.max(w.0));
changed = true;
}
if let Some(h) = fixed.height {
out.height = Pt(out.height.0.max(h.0));
changed = true;
}
if changed {
constraint.constrain(out)
} else {
out
}
}
fn min_pt(a: Pt, b: Pt) -> Pt {
if a.0 <= b.0 {
a
} else {
b
}
}
pub fn subtract_if_bounded(max: Pt, amount: Pt) -> Pt {
if max.0 == i128::MAX {
return max;
}
let v = max.0 - amount.0;
Pt(v.max(0))
}