ailloli_ui_widgets/controls/
checkbox.rs

1//! Immediate-mode checkbox drawing primitives.
2
3use ailloli_ui_core::style::{Border, Radius};
4use ailloli_ui_core::{Color, FontId, IconId, Rect, TextStyle, Theme};
5use ailloli_ui_runtime::{DrawBorder, DrawCmd, DrawRRect, DrawText};
6use ailloli_ui_text::{TextLayoutParams, TextSystem, WrapMode};
7
8use crate::primitives::draw_icon;
9
10#[derive(Debug, Clone, Copy)]
11/// Colors, typography, and logical-pixel metrics used by [`draw_checkbox`].
12///
13/// The style is a value-level drawing configuration. It does not store checked,
14/// disabled, hover, or focus state.
15///
16/// # Examples
17///
18/// ```
19/// use ailloli_ui_core::Theme;
20/// use ailloli_ui_widgets::controls::CheckboxStyle;
21/// let style = CheckboxStyle::from_theme(Theme::dark());
22/// assert_eq!(style.box_size, 18.0);
23/// assert_eq!(style.label_px, 13);
24/// ```
25pub struct CheckboxStyle {
26    /// Unchecked box fill color.
27    pub box_bg: Color,
28    /// Checked box fill color.
29    pub checked_bg: Color,
30    /// Unchecked box border color.
31    pub box_border: Color,
32    /// Checked box border color.
33    pub checked_border: Color,
34    /// Box corner radius in logical pixels.
35    pub box_radius: f32,
36    /// Width and height of the square box in logical pixels.
37    pub box_size: f32,
38    /// Horizontal gap between the box and label in logical pixels.
39    pub gap: f32,
40    /// Label text color.
41    pub label_color: Color,
42    /// Label font size in logical pixels.
43    pub label_px: u16,
44    /// Checkmark icon color.
45    pub icon_tint: Color,
46}
47
48impl Default for CheckboxStyle {
49    fn default() -> Self {
50        Self::from_theme(Theme::default())
51    }
52}
53
54impl CheckboxStyle {
55    /// Resolves a checkbox style from `theme`'s current palette.
56    ///
57    /// The method uses fixed logical-pixel defaults for geometry and typography;
58    /// only colors are derived from the theme.
59    ///
60    /// # Examples
61    ///
62    /// ```
63    /// use ailloli_ui_core::Theme;
64    /// use ailloli_ui_widgets::controls::CheckboxStyle;
65    /// let style = CheckboxStyle::from_theme(Theme::dark());
66    /// assert_eq!(style.box_radius, 4.0);
67    /// assert_eq!(style.gap, 8.0);
68    /// ```
69    pub fn from_theme(theme: Theme) -> Self {
70        let palette = theme.palette();
71        Self {
72            box_bg: palette.surface,
73            checked_bg: palette.accent,
74            box_border: palette.border,
75            checked_border: palette.accent,
76            box_radius: 4.0,
77            box_size: 18.0,
78            gap: 8.0,
79            label_color: palette.text,
80            label_px: 13,
81            icon_tint: palette.text,
82        }
83    }
84}
85
86/// Produces draw commands for a checkbox row without retaining interaction state.
87///
88/// The square is vertically centered at `row_rect.x`. A checked and enabled row
89/// includes a checkmark; a checked but disabled row deliberately omits it.
90/// Disabled state multiplies the box fill and border alpha by `0.45`. `None`
91/// draws no label, whereas `Some("")` still emits an empty text command.
92/// Geometry values are not clamped, and content may extend outside `row_rect`.
93///
94/// # Examples
95///
96/// ```
97/// use ailloli_ui_core::Rect;
98/// use ailloli_ui_text::TextSystem;
99/// use ailloli_ui_widgets::controls::{draw_checkbox, CheckboxStyle};
100/// let mut text = TextSystem::new();
101/// let commands = draw_checkbox(
102///     Rect::new(0.0, 0.0, 120.0, 24.0),
103///     true,
104///     Some("Ready"),
105///     false,
106///     CheckboxStyle::default(),
107///     &mut text,
108/// );
109/// assert_eq!(commands.len(), 4); // fill, border, checkmark, and label
110/// ```
111pub fn draw_checkbox(
112    row_rect: Rect,
113    checked: bool,
114    label: Option<&str>,
115    disabled: bool,
116    style: CheckboxStyle,
117    text_system: &mut TextSystem,
118) -> Vec<DrawCmd> {
119    let mut out = Vec::new();
120    let alpha = if disabled { 0.45 } else { 1.0 };
121    let bg_src = if checked {
122        style.checked_bg
123    } else {
124        style.box_bg
125    };
126    let border_src = if checked {
127        style.checked_border
128    } else {
129        style.box_border
130    };
131    let box_bg = Color::new(bg_src.r, bg_src.g, bg_src.b, bg_src.a * alpha);
132    let border = Color::new(
133        border_src.r,
134        border_src.g,
135        border_src.b,
136        border_src.a * alpha,
137    );
138    let box_r = Rect::new(
139        row_rect.x,
140        row_rect.y + (row_rect.h - style.box_size) / 2.0,
141        style.box_size,
142        style.box_size,
143    );
144    out.push(DrawCmd::RRect(DrawRRect {
145        rect: box_r,
146        radius: style.box_radius,
147        color: box_bg,
148    }));
149    out.push(DrawCmd::Border(DrawBorder {
150        rect: box_r,
151        radius: Radius::uniform(style.box_radius),
152        border: Border::new(1.0, border),
153    }));
154
155    if checked && !disabled {
156        let inset = 3.0;
157        out.push(draw_icon(
158            Rect::new(
159                box_r.x + inset,
160                box_r.y + inset,
161                box_r.w - inset * 2.0,
162                box_r.h - inset * 2.0,
163            ),
164            IconId::Check,
165            style.icon_tint,
166        ));
167    }
168
169    if let Some(lbl) = label {
170        let lx = box_r.x + box_r.w + style.gap;
171        let baseline_y = row_rect.y + row_rect.h / 2.0 + 4.0;
172        out.push(DrawCmd::Text(DrawText {
173            pos: [lx, baseline_y],
174            color: style.label_color,
175            decoration: ailloli_ui_core::TextDecoration::None,
176            layout: text_system.layout_cached(TextLayoutParams {
177                text: lbl,
178                style: TextStyle::new(FontId::Ui, style.label_px, style.label_color),
179                max_width: None,
180                wrap_mode: WrapMode::NoWrap,
181            }),
182        }));
183    }
184
185    out
186}