use crate::{CornerRadius, Padding, Rect, Transform, Vec2};
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum ModalAnchor {
#[default]
Center,
Left,
Right,
Top,
Bottom,
}
impl ModalAnchor {
fn edge(self) -> Option<Vec2> {
match self {
Self::Center => None,
Self::Left => Some(Vec2::new(-1.0, 0.0)),
Self::Right => Some(Vec2::new(1.0, 0.0)),
Self::Top => Some(Vec2::new(0.0, -1.0)),
Self::Bottom => Some(Vec2::new(0.0, 1.0)),
}
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct Measure {
pub width: f32,
pub header: f32,
pub body: f32,
pub footer: f32,
pub parts: u8,
pub last_frame: u64,
}
impl Measure {
pub(crate) fn changed_from(&self, previous: &Measure) -> bool {
(self.width - previous.width).abs() > 0.5
|| (self.header - previous.header).abs() > 0.5
|| (self.body - previous.body).abs() > 0.5
|| (self.footer - previous.footer).abs() > 0.5
|| previous.last_frame == 0
}
}
pub(crate) struct Metrics {
pub anchor: ModalAnchor,
pub width: Option<f32>,
pub max_height: Option<f32>,
pub min_width: f32,
pub max_width: f32,
pub margin: f32,
pub padding: Padding,
pub gap: f32,
}
pub(crate) struct Geometry {
pub surface: Rect,
pub body_height: f32,
pub rounding: CornerRadius,
}
pub(crate) fn geometry(
viewport: Rect,
m: &Metrics,
measure: &Measure,
rounding: CornerRadius,
) -> Geometry {
let edge = m.anchor.edge();
let margin = if edge.is_some() { 0.0 } else { m.margin };
let avail = (viewport.size() - Vec2::splat(2.0 * margin)).max(Vec2::splat(1.0));
let pad = m.padding.size();
let natural = if measure.width > 0.0 {
measure.width + pad.x
} else {
m.max_width
};
let width = m
.width
.unwrap_or(natural.clamp(m.min_width, m.max_width.max(m.min_width)))
.min(avail.x)
.max(1.0);
let height_cap = m.max_height.unwrap_or(f32::INFINITY).min(avail.y);
let gaps = f32::from(measure.parts.saturating_sub(1)) * m.gap;
let fixed = pad.y + measure.header + measure.footer + gaps;
let body_height = measure.body.min((height_cap - fixed).max(0.0));
let height = match edge {
Some(e) if e.x != 0.0 => avail.y,
_ => (fixed + body_height).max(1.0),
};
let width = match edge {
Some(e) if e.y != 0.0 => avail.x,
_ => width,
};
let center = viewport.center();
let mut min = Vec2::new(center.x - width * 0.5, center.y - height * 0.5);
match edge {
Some(e) => {
if e.x < 0.0 {
min.x = viewport.min.x;
} else if e.x > 0.0 {
min.x = viewport.max.x - width;
}
if e.y < 0.0 {
min.y = viewport.min.y;
} else if e.y > 0.0 {
min.y = viewport.max.y - height;
}
}
None => {
min.x = min.x.clamp(
viewport.min.x + margin,
(viewport.max.x - margin - width).max(viewport.min.x),
);
let low = viewport.min.y + margin;
let high = viewport.max.y - margin - height;
min.y = if high >= low {
min.y.clamp(low, high)
} else {
viewport.max.y - margin - height
};
}
}
let mut rounding_corners = rounding;
if let Some(e) = edge {
if e.x < 0.0 {
rounding_corners.top_left = 0.0;
rounding_corners.bottom_left = 0.0;
} else if e.x > 0.0 {
rounding_corners.top_right = 0.0;
rounding_corners.bottom_right = 0.0;
} else if e.y < 0.0 {
rounding_corners.top_left = 0.0;
rounding_corners.top_right = 0.0;
} else {
rounding_corners.bottom_left = 0.0;
rounding_corners.bottom_right = 0.0;
}
}
Geometry {
surface: Rect::from_min_size(min, Vec2::new(width, height)),
body_height,
rounding: rounding_corners,
}
}
pub(crate) fn pose(
anchor: ModalAnchor,
surface: Rect,
t: f32,
scale: f32,
offset: f32,
) -> Transform {
let hidden = 1.0 - t.clamp(0.0, 1.0);
match anchor.edge() {
Some(e) => Transform::translation(e * surface.size() * hidden),
None => Transform::around(
surface.center(),
1.0 + (scale - 1.0) * hidden,
Vec2::new(0.0, offset * hidden),
),
}
}