hyprforge_ui/widgets/scrim.rs
1//! A card over the dimmed window: the shape a modal glance takes.
2//!
3//! Files' Quick Look was the first written here; Files' own dialogs drew
4//! the same dim layer in the app before it, and a lightbox in Media or a
5//! sheet in Settings would draw it again. Nothing about it is about
6//! files — it is "this, over everything, until you look away".
7
8use crate::theme::surface;
9use iced::widget::{container, mouse_area, opaque, stack, Space};
10use iced::{Background, Color, Element, Length};
11
12/// How much of the window behind a scrim hides. Enough that the card is
13/// plainly what the keyboard and pointer are on, little enough that the
14/// window still reads as there.
15pub const SCRIM_ALPHA: f32 = 0.6;
16
17/// The dim layer's colour: the window's own root step, see-through —
18/// never an invented black, which would be a colour constant no theme
19/// can reach, and never the accent, which means selected.
20pub fn scrim_color() -> Color {
21 Color { a: SCRIM_ALPHA, ..surface::root() }
22}
23
24/// `content`, centred over a dim layer that fills whatever it is stacked
25/// over.
26///
27/// A press on the dim layer — either button — sends `on_dismiss`; with
28/// `None` the layer only swallows the press, for a question the window
29/// is waiting on. Both layers are `opaque`, so nothing beneath is
30/// clickable or scrollable while a scrim is up: iced's `stack` gives the
31/// layers under one a levitated cursor exactly when the one above
32/// reports an interaction, and `opaque` always does.
33///
34/// The caller sizes `content`. The scrim only centres it — a card that
35/// wants a margin from the window's edges gives itself one.
36pub fn scrim<'a, Message: Clone + 'a>(
37 content: impl Into<Element<'a, Message>>,
38 on_dismiss: Option<Message>,
39) -> Element<'a, Message> {
40 let dim = container(Space::new().width(Length::Fill).height(Length::Fill))
41 .width(Length::Fill)
42 .height(Length::Fill)
43 .style(|_t: &iced::Theme| container::Style {
44 background: Some(Background::Color(scrim_color())),
45 ..container::Style::default()
46 });
47 let away: Element<'a, Message> = match on_dismiss {
48 Some(message) => opaque(mouse_area(dim).on_press(message.clone()).on_right_press(message)),
49 None => opaque(dim),
50 };
51 let placed = container(opaque(content))
52 .center_x(Length::Fill)
53 .center_y(Length::Fill);
54 stack![away, placed].width(Length::Fill).height(Length::Fill).into()
55}
56
57#[cfg(test)]
58mod tests {
59 use super::*;
60
61 /// The dim layer is the window's own surface, see-through — the rule
62 /// that no app adds a colour of its own, held for the one layer that
63 /// would be easiest to fill with plain black.
64 #[test]
65 fn a_scrim_dims_with_the_windows_own_root_colour() {
66 let dim = scrim_color();
67 let root = surface::root();
68 assert_eq!((dim.r, dim.g, dim.b), (root.r, root.g, root.b));
69 assert!(dim.a > 0.0 && dim.a < 1.0, "see-through, not a wall: {}", dim.a);
70 }
71}