use super::Ui;
use crate::{components::Sense, context::Paint, Color, Context, Id, Rect, Shape};
#[derive(Clone, Copy)]
pub(super) struct Tint {
pub(super) color: Color,
pub(super) blur: f32,
}
impl Tint {
fn at(self, t: f32, dims: bool) -> Self {
let mut color = self.color;
color.0[3] = if dims {
(f32::from(color.0[3]) * t).round() as u8
} else {
0
};
let blur = if dims { self.blur * t } else { 0.0 };
Self { color, blur }
}
}
pub(super) fn show(
root: &mut Ui<'_>,
id: Id,
viewport: Rect,
tint: Tint,
t: f32,
open: bool,
) -> bool {
let tint = tint.at(t, dims(root.context, id));
paint(root.context, id, viewport, tint);
open && root.interact(viewport, "overlay", Sense::CLICK).clicked
}
fn dims(context: &Context, id: Id) -> bool {
context.modals.stack.first().is_none_or(|m| m.id == id)
}
fn paint(context: &mut Context, id: Id, viewport: Rect, tint: Tint) {
context.paint(
id.with("overlay"),
id,
viewport,
vec![Paint::Shape(Shape::rect(viewport, tint.color).into())],
);
context.paint_blur(
id.with("overlay-blur"),
id,
viewport,
crate::Blur::new(viewport).radius(tint.blur),
);
}