ailloli_ui_widgets/overlay/
popup.rs

1//! Immediate-mode modal scrim and rounded popup-card helpers.
2
3use ailloli_ui_core::{Color, Rect};
4use ailloli_ui_runtime::{DrawCmd, DrawRRect, DrawRect};
5
6#[derive(Debug, Clone, Copy)]
7/// Fill style for [`draw_modal_overlay`].
8///
9/// # Examples
10///
11/// ```
12/// use ailloli_ui_core::Color;
13/// use ailloli_ui_widgets::overlay::popup::OverlayStyle;
14/// let style = OverlayStyle { bg: Color::rgba(0, 0, 0, 0.5) };
15/// assert_eq!(style.bg.a, 0.5);
16/// ```
17pub struct OverlayStyle {
18    /// Full-overlay fill color, including alpha.
19    pub bg: Color,
20}
21
22/// Returns exactly one solid rectangle command covering `full`.
23///
24/// Geometry is passed through unchanged in logical pixels.
25///
26/// # Examples
27///
28/// ```
29/// use ailloli_ui_core::{Color, Rect};
30/// use ailloli_ui_widgets::overlay::popup::{draw_modal_overlay, OverlayStyle};
31/// let commands = draw_modal_overlay(Rect::new(0.0, 0.0, 100.0, 80.0), OverlayStyle { bg: Color::BLACK });
32/// assert_eq!(commands.len(), 1);
33/// ```
34pub fn draw_modal_overlay(full: Rect, style: OverlayStyle) -> Vec<DrawCmd> {
35    vec![DrawCmd::Rect(DrawRect {
36        rect: full,
37        color: style.bg,
38    })]
39}
40
41/// Draws a rounded popup card and optional top/bottom one-pixel rules.
42///
43/// `None` returns only the background command; `Some` returns three commands.
44/// Side borders are intentionally absent. Radius and rect are logical pixels
45/// and are not clamped or normalized.
46///
47/// # Examples
48///
49/// ```
50/// use ailloli_ui_core::{Color, Rect};
51/// use ailloli_ui_widgets::overlay::popup::draw_modal_card;
52/// let commands = draw_modal_card(Rect::new(0.0, 0.0, 100.0, 60.0), Color::BLACK, Some(Color::WHITE), 8.0);
53/// assert_eq!(commands.len(), 3);
54/// assert_eq!(draw_modal_card(Rect::new(0.0, 0.0, 1.0, 1.0), Color::BLACK, None, 0.0).len(), 1);
55/// ```
56pub fn draw_modal_card(rect: Rect, bg: Color, border: Option<Color>, radius: f32) -> Vec<DrawCmd> {
57    let mut out = Vec::new();
58    out.push(DrawCmd::RRect(DrawRRect {
59        rect,
60        radius,
61        color: bg,
62    }));
63    if let Some(b) = border {
64        out.push(DrawCmd::Rect(DrawRect {
65            rect: Rect::new(rect.x, rect.y, rect.w, 1.0),
66            color: b,
67        }));
68        out.push(DrawCmd::Rect(DrawRect {
69            rect: Rect::new(rect.x, rect.y + rect.h - 1.0, rect.w, 1.0),
70            color: b,
71        }));
72    }
73    out
74}