ailloli_ui_widgets/overlay/modal.rs
1//! Immediate-mode fixed-size confirmation modal drawing.
2
3use ailloli_ui_core::{Color, FontId, Rect, TextStyle};
4use ailloli_ui_runtime::{DrawCmd, DrawRRect, DrawRect, DrawText};
5use ailloli_ui_text::{TextLayoutParams, TextSystem, WrapMode};
6
7// For correct z-order: merge returned `DrawCmd` into an overlay layer (see `scene_base_overlay`).
8// (voir `crate::overlay::layered::scene_base_overlay`).
9
10/// Generic confirmation modal dialog style.
11///
12/// Title and button labels are caller-defined; no app-specific copy in this crate.
13///
14/// # Examples
15///
16/// ```
17/// use ailloli_ui_widgets::overlay::modal::ConfirmDialogStyle;
18/// let style = ConfirmDialogStyle::default();
19/// assert!(style.overlay.a > 0.0);
20/// ```
21#[derive(Debug, Clone, Copy)]
22pub struct ConfirmDialogStyle {
23 /// Full-viewport scrim color.
24 pub overlay: Color,
25 /// Dialog panel fill.
26 pub panel_bg: Color,
27 /// One-logical-pixel top and bottom panel rule color.
28 pub panel_border: Color,
29 /// Title glyph color.
30 pub title_fg: Color,
31 /// Body glyph color.
32 pub body_fg: Color,
33 /// Deny-button fill.
34 pub btn_bg: Color,
35 /// Allow-button fill.
36 pub btn_bg_primary: Color,
37 /// Shared button-label glyph color.
38 pub btn_fg: Color,
39}
40
41/// Supplies the dark fixed-confirmation palette.
42impl Default for ConfirmDialogStyle {
43 fn default() -> Self {
44 Self {
45 overlay: Color::rgba(0, 0, 0, 0.66),
46 panel_bg: Color::rgba(17, 24, 40, 1.0),
47 panel_border: Color::rgba(31, 38, 55, 1.0),
48 title_fg: Color::rgba(243, 246, 251, 1.0),
49 body_fg: Color::rgba(243, 246, 251, 1.0),
50 btn_bg: Color::rgba(31, 38, 55, 1.0),
51 btn_bg_primary: Color::rgba(37, 99, 235, 1.0),
52 btn_fg: Color::rgba(243, 246, 251, 1.0),
53 }
54 }
55}
56
57#[derive(Debug, Clone, Copy)]
58/// Logical-pixel hit rectangles returned with confirmation draw commands.
59///
60/// # Examples
61///
62/// ```
63/// use ailloli_ui_core::Rect;
64/// use ailloli_ui_widgets::overlay::modal::ConfirmDialogLayout;
65/// let layout = ConfirmDialogLayout { panel: Rect::new(0.0, 0.0, 520.0, 220.0), allow: Rect::new(0.0, 0.0, 84.0, 34.0), deny: Rect::new(0.0, 0.0, 84.0, 34.0) };
66/// assert_eq!(layout.panel.w, 520.0);
67/// ```
68pub struct ConfirmDialogLayout {
69 /// Centered fixed 520×220 panel rectangle.
70 pub panel: Rect,
71 /// Rightmost 84×34 allow-button hit rectangle.
72 pub allow: Rect,
73 /// 84×34 deny-button hit rectangle immediately left of allow.
74 pub deny: Rect,
75}
76
77#[derive(Debug, Clone, Copy)]
78/// Borrowed caller-owned copy for a confirmation dialog.
79///
80/// Empty strings are valid; labels are drawn without wrapping or truncation.
81///
82/// # Examples
83///
84/// ```
85/// use ailloli_ui_widgets::overlay::modal::ConfirmDialogTexts;
86/// let texts = ConfirmDialogTexts { title: "Delete?", body: "This cannot be undone", deny_label: "Cancel", allow_label: "Delete" };
87/// assert_eq!(texts.allow_label, "Delete");
88/// ```
89pub struct ConfirmDialogTexts<'a> {
90 /// UI-font title.
91 pub title: &'a str,
92 /// Mono-font body line.
93 pub body: &'a str,
94 /// Deny-button label.
95 pub deny_label: &'a str,
96 /// Allow-button label.
97 pub allow_label: &'a str,
98}
99
100/// Draws a fixed confirmation panel centered in `full`.
101///
102/// The panel remains 520×220 logical pixels even when `full` is smaller, so it
103/// may extend outside the viewport. The function returns ten commands in paint
104/// order (scrim, surfaces, then four text commands) plus button hit geometry.
105/// Text is unwrapped and the function performs shaping allocations/cache access.
106///
107/// # Examples
108///
109/// ```
110/// use ailloli_ui_core::Rect;
111/// use ailloli_ui_text::TextSystem;
112/// use ailloli_ui_widgets::overlay::modal::{draw_confirm_dialog, ConfirmDialogStyle, ConfirmDialogTexts};
113/// let mut text_system = TextSystem::new();
114/// let texts = ConfirmDialogTexts { title: "Continue?", body: "Review the action", deny_label: "No", allow_label: "Yes" };
115/// let (commands, layout) = draw_confirm_dialog(Rect::new(0.0, 0.0, 800.0, 600.0), texts, ConfirmDialogStyle::default(), &mut text_system);
116/// assert_eq!(commands.len(), 10);
117/// assert_eq!((layout.panel.w, layout.panel.h), (520.0, 220.0));
118/// ```
119pub fn draw_confirm_dialog(
120 full: Rect,
121 texts: ConfirmDialogTexts<'_>,
122 style: ConfirmDialogStyle,
123 text: &mut TextSystem,
124) -> (Vec<DrawCmd>, ConfirmDialogLayout) {
125 let panel = Rect::new(
126 full.x + (full.w - 520.0) / 2.0,
127 full.y + (full.h - 220.0) / 2.0,
128 520.0,
129 220.0,
130 );
131 let deny = Rect::new(
132 panel.x + panel.w - 190.0,
133 panel.y + panel.h - 54.0,
134 84.0,
135 34.0,
136 );
137 let allow = Rect::new(
138 panel.x + panel.w - 96.0,
139 panel.y + panel.h - 54.0,
140 84.0,
141 34.0,
142 );
143
144 let mut out = Vec::new();
145 /// Shapes one unwrapped label and appends its baseline-positioned command.
146 fn push_text(
147 out: &mut Vec<DrawCmd>,
148 text: &mut TextSystem,
149 pos: [f32; 2],
150 color: Color,
151 font: FontId,
152 px_size: u16,
153 s: &str,
154 ) {
155 out.push(DrawCmd::Text(DrawText {
156 pos,
157 color,
158 decoration: ailloli_ui_core::TextDecoration::None,
159 layout: text.layout_cached(TextLayoutParams {
160 text: s,
161 style: TextStyle::new(font, px_size, color),
162 max_width: None,
163 wrap_mode: WrapMode::NoWrap,
164 }),
165 }));
166 }
167
168 out.extend([
169 DrawCmd::Rect(DrawRect {
170 rect: full,
171 color: style.overlay,
172 }),
173 DrawCmd::RRect(DrawRRect {
174 rect: panel,
175 radius: 10.0,
176 color: style.panel_bg,
177 }),
178 DrawCmd::Rect(DrawRect {
179 rect: Rect::new(panel.x, panel.y, panel.w, 1.0),
180 color: style.panel_border,
181 }),
182 DrawCmd::Rect(DrawRect {
183 rect: Rect::new(panel.x, panel.y + panel.h - 1.0, panel.w, 1.0),
184 color: style.panel_border,
185 }),
186 DrawCmd::RRect(DrawRRect {
187 rect: deny,
188 radius: 8.0,
189 color: style.btn_bg,
190 }),
191 DrawCmd::RRect(DrawRRect {
192 rect: allow,
193 radius: 8.0,
194 color: style.btn_bg_primary,
195 }),
196 ]);
197
198 push_text(
199 &mut out,
200 text,
201 [panel.x + 16.0, panel.y + 30.0],
202 style.title_fg,
203 FontId::Ui,
204 16,
205 texts.title,
206 );
207 push_text(
208 &mut out,
209 text,
210 [panel.x + 16.0, panel.y + 58.0],
211 style.body_fg,
212 FontId::Mono,
213 13,
214 texts.body,
215 );
216 push_text(
217 &mut out,
218 text,
219 [deny.x + 12.0, deny.y + deny.h / 2.0 + 4.0],
220 style.btn_fg,
221 FontId::Ui,
222 13,
223 texts.deny_label,
224 );
225 push_text(
226 &mut out,
227 text,
228 [allow.x + 12.0, allow.y + allow.h / 2.0 + 4.0],
229 style.btn_fg,
230 FontId::Ui,
231 13,
232 texts.allow_label,
233 );
234
235 (out, ConfirmDialogLayout { panel, allow, deny })
236}