ailloli_ui_widgets/layout/
panel.rs

1//! Immediate-mode panel surface and horizontal border rules.
2
3use ailloli_ui_core::{Color, Rect};
4use ailloli_ui_runtime::{DrawCmd, DrawRRect, DrawRect};
5
6#[derive(Debug, Clone, Copy)]
7/// Fill, optional border, and corner radius for [`draw_panel`].
8///
9/// Dimensions are logical pixels. A radius greater than zero selects a rounded
10/// command; zero, negative, or `NaN` selects an axis-aligned rectangle.
11///
12/// # Examples
13///
14/// ```
15/// use ailloli_ui_core::Color;
16/// use ailloli_ui_widgets::layout::panel::PanelStyle;
17/// let style = PanelStyle::simple(Color::BLACK);
18/// assert!(style.border.is_none());
19/// assert_eq!(style.radius, 0.0);
20/// ```
21pub struct PanelStyle {
22    /// Panel fill.
23    pub bg: Color,
24    /// Optional color for one-pixel top and bottom rules only.
25    pub border: Option<Color>,
26    /// Corner radius in logical pixels.
27    pub radius: f32,
28}
29
30impl PanelStyle {
31    /// Creates a square, borderless panel style.
32    ///
33    /// # Examples
34    ///
35    /// ```
36    /// use ailloli_ui_core::Color;
37    /// use ailloli_ui_widgets::layout::panel::PanelStyle;
38    /// let style = PanelStyle::simple(Color::WHITE);
39    /// assert_eq!(style.bg, Color::WHITE);
40    /// ```
41    pub fn simple(bg: Color) -> Self {
42        Self {
43            bg,
44            border: None,
45            radius: 0.0,
46        }
47    }
48}
49
50/// Draws one panel fill and optional one-pixel top/bottom border rules.
51///
52/// Returns one command without a border and three with a border. Geometry is
53/// passed through without normalization; vertical side borders are absent.
54///
55/// # Examples
56///
57/// ```
58/// use ailloli_ui_core::{Color, Rect};
59/// use ailloli_ui_widgets::layout::panel::{draw_panel, PanelStyle};
60/// let style = PanelStyle { bg: Color::BLACK, border: Some(Color::WHITE), radius: 4.0 };
61/// assert_eq!(draw_panel(Rect::new(0.0, 0.0, 80.0, 40.0), style).len(), 3);
62/// ```
63pub fn draw_panel(rect: Rect, style: PanelStyle) -> Vec<DrawCmd> {
64    let mut out = Vec::new();
65    if style.radius > 0.0 {
66        out.push(DrawCmd::RRect(DrawRRect {
67            rect,
68            radius: style.radius,
69            color: style.bg,
70        }));
71    } else {
72        out.push(DrawCmd::Rect(DrawRect {
73            rect,
74            color: style.bg,
75        }));
76    }
77
78    if let Some(border) = style.border {
79        out.push(DrawCmd::Rect(DrawRect {
80            rect: Rect::new(rect.x, rect.y, rect.w, 1.0),
81            color: border,
82        }));
83        out.push(DrawCmd::Rect(DrawRect {
84            rect: Rect::new(rect.x, rect.y + rect.h - 1.0, rect.w, 1.0),
85            color: border,
86        }));
87    }
88
89    out
90}