ailloli_ui_widgets/controls/
list.rs

1//! Immediate-mode drawing for a compact, non-interactive row list.
2//!
3//! Rows are clipped by count at the bottom edge; this helper does not implement
4//! scrolling, hit testing, or retained virtualization.
5
6use ailloli_ui_core::{Color, FontId, Rect, TextStyle};
7use ailloli_ui_runtime::{DrawCmd, DrawRRect, DrawText};
8use ailloli_ui_text::{TextLayoutParams, TextSystem, WrapMode};
9
10#[derive(Debug, Clone)]
11/// Owned text and optional trailing decoration for one list row.
12///
13/// Empty strings are valid and still produce text commands. `right_icon_bg`
14/// is a color for a trailing rounded square, not an icon identifier; `None`
15/// omits that command.
16///
17/// # Examples
18///
19/// ```
20/// use ailloli_ui_core::Color;
21/// use ailloli_ui_widgets::controls::list::ListRow;
22/// let row = ListRow {
23///     title: "Project".into(),
24///     subtitle: "Updated now".into(),
25///     right_icon_bg: Some(Color::WHITE),
26/// };
27/// assert_eq!(row.title, "Project");
28/// ```
29pub struct ListRow {
30    /// Primary line of text.
31    pub title: String,
32    /// Secondary line of text.
33    pub subtitle: String,
34    /// Optional fill color for the `28 × 28` trailing rounded square.
35    pub right_icon_bg: Option<Color>,
36}
37
38#[derive(Debug, Clone, Copy)]
39/// Logical-pixel geometry, typography, and colors for [`draw_list_rows`].
40///
41/// Values are used as-is. Callers should provide a positive `row_h`; a zero or
42/// negative height cannot make vertical progress through a large slice.
43///
44/// # Examples
45///
46/// ```
47/// use ailloli_ui_core::{Color, FontId};
48/// use ailloli_ui_widgets::controls::list::ListStyle;
49/// let style = ListStyle {
50///     row_h: 44.0,
51///     radius: 8.0,
52///     title_fg: Color::WHITE,
53///     subtitle_fg: Color::WHITE.with_alpha(0.7),
54///     font: FontId::Ui,
55///     title_px: 14,
56///     subtitle_px: 12,
57/// };
58/// assert_eq!(style.row_h, 44.0);
59/// ```
60pub struct ListStyle {
61    /// Height of each row in logical pixels.
62    pub row_h: f32,
63    /// Background corner radius in logical pixels.
64    pub radius: f32,
65    /// Primary text color.
66    pub title_fg: Color,
67    /// Secondary text color.
68    pub subtitle_fg: Color,
69    /// Font used for both text lines.
70    pub font: FontId,
71    /// Primary font size in logical pixels.
72    pub title_px: u16,
73    /// Secondary font size in logical pixels.
74    pub subtitle_px: u16,
75}
76
77/// Draws complete rows from the top of `rect` until the next row would overflow.
78///
79/// A row whose bottom is exactly `rect`'s bottom is included. Each included row
80/// emits a transparent background plus two text commands, and optionally a
81/// trailing rounded square. Horizontal content is not clipped by this helper.
82///
83/// # Examples
84///
85/// ```
86/// use ailloli_ui_core::{Color, FontId, Rect};
87/// use ailloli_ui_text::TextSystem;
88/// use ailloli_ui_widgets::controls::list::{draw_list_rows, ListRow, ListStyle};
89/// let rows = vec![ListRow {
90///     title: "One".into(),
91///     subtitle: "First".into(),
92///     right_icon_bg: None,
93/// }];
94/// let style = ListStyle {
95///     row_h: 44.0,
96///     radius: 8.0,
97///     title_fg: Color::WHITE,
98///     subtitle_fg: Color::WHITE,
99///     font: FontId::Ui,
100///     title_px: 14,
101///     subtitle_px: 12,
102/// };
103/// let mut text = TextSystem::new();
104/// let commands = draw_list_rows(Rect::new(0.0, 0.0, 200.0, 44.0), &rows, style, &mut text);
105/// assert_eq!(commands.len(), 3);
106/// ```
107pub fn draw_list_rows(
108    rect: Rect,
109    rows: &[ListRow],
110    style: ListStyle,
111    text: &mut TextSystem,
112) -> Vec<DrawCmd> {
113    let mut out = Vec::new();
114    let mut y = rect.y;
115    for r in rows {
116        if y + style.row_h > rect.y + rect.h {
117            break;
118        }
119        let row_rect = Rect::new(rect.x, y, rect.w, style.row_h);
120        out.push(DrawCmd::RRect(DrawRRect {
121            rect: row_rect,
122            radius: style.radius,
123            color: Color::new(0.0, 0.0, 0.0, 0.0),
124        }));
125        out.push(DrawCmd::Text(DrawText {
126            pos: [row_rect.x + 14.0, row_rect.y + 18.0],
127            color: style.title_fg,
128            decoration: ailloli_ui_core::TextDecoration::None,
129            layout: text.layout_cached(TextLayoutParams {
130                text: &r.title,
131                style: TextStyle::new(style.font, style.title_px, style.title_fg),
132                max_width: None,
133                wrap_mode: WrapMode::NoWrap,
134            }),
135        }));
136        out.push(DrawCmd::Text(DrawText {
137            pos: [row_rect.x + 14.0, row_rect.y + 34.0],
138            color: style.subtitle_fg,
139            decoration: ailloli_ui_core::TextDecoration::None,
140            layout: text.layout_cached(TextLayoutParams {
141                text: &r.subtitle,
142                style: TextStyle::new(style.font, style.subtitle_px, style.subtitle_fg),
143                max_width: None,
144                wrap_mode: WrapMode::NoWrap,
145            }),
146        }));
147
148        if let Some(bg) = r.right_icon_bg {
149            let icon_rect = Rect::new(row_rect.x + row_rect.w - 34.0, row_rect.y + 8.0, 28.0, 28.0);
150            out.push(DrawCmd::RRect(DrawRRect {
151                rect: icon_rect,
152                radius: 8.0,
153                color: bg,
154            }));
155        }
156
157        y += style.row_h;
158    }
159    out
160}