1use crate::*;
2
3#[derive(Clone, Debug, Default, PartialEq, Eq)]
5pub struct SearchOverlayState {
6 pub open: bool,
7 pub query: String,
8 pub selected: Option<usize>,
9}
10
11impl Frame<'_> {
12 pub fn search_bar_overlay(
17 &mut self,
18 id: u64,
19 state: &mut SearchOverlayState,
20 placeholder: &str,
21 suggestions: &[&str],
22 ) -> Option<usize> {
23 let bar_bounds = self.allocate(56.0);
24 let t = self.ui.theme;
25 let was_open = state.open;
26 let mut just_opened = false;
27 let sub_id = |slot: u64| {
28 let v = id.wrapping_mul(0x9E37_79B9_7F4A_7C15).wrapping_add(slot);
29 if v == id || v == 0 {
30 v.wrapping_add(slot.max(1))
31 } else {
32 v
33 }
34 };
35
36 let paint_closed_bar = |f: &mut Self, state: &mut SearchOverlayState, interactive: bool| -> bool {
37 let radius = (bar_bounds.height * 0.5).min(28.0);
38 let bar = f.interact(id, bar_bounds, interactive);
39 f.rect(bar_bounds, radius, t.surface_high);
40 f.state_layer(bar, radius, t.text);
41 f.ink(bar, radius, t.text);
42 let icon_rect = Rect {
43 x: bar_bounds.x + 16.0,
44 y: bar_bounds.y + (bar_bounds.height - 24.0) * 0.5,
45 width: 24.0,
46 height: 24.0,
47 };
48 f.paint_material_icon(icon_rect, MaterialIcon::Search, t.text);
49 let text_rect = Rect {
50 x: bar_bounds.x + 56.0,
51 y: bar_bounds.y,
52 width: (bar_bounds.width - 72.0).max(0.0),
53 height: bar_bounds.height,
54 };
55 let show_query = !state.query.is_empty();
56 f.styled_text(
57 text_rect,
58 if show_query {
59 state.query.as_str()
60 } else {
61 placeholder
62 },
63 if show_query {
64 t.text
65 } else {
66 t.on_surface_variant
67 },
68 TextStyle {
69 size: 16.0,
70 ..TextStyle::default()
71 },
72 );
73 f.focus_ring(bar);
74 f.semantic(bar, Role::Button, placeholder, false, None, None, None);
75 bar.clicked
76 };
77
78 if !was_open {
79 if paint_closed_bar(self, state, true) {
80 state.open = true;
81 just_opened = true;
82 self.ui.active = None;
83 self.ui.continuous = true;
84 if let Some(node) = self.ui.semantics.last_mut() {
85 node.enabled = false;
86 }
87 }
88 } else if !self.key_consumed && self.pressed_key(Key::Escape) {
89 state.open = false;
90 self.key_consumed = true;
91 self.ui.active = None;
92 self.ui.continuous = true;
93 } else if !self.key_consumed && self.enabled {
94 if let Some(k) = self.input.key
95 && k.pressed
96 && let Key::Character(ch) = k.key
97 {
98 if ch == '\u{8}' || ch == '\u{7f}' {
99 state.query.pop();
100 self.key_consumed = true;
101 self.ui.continuous = true;
102 } else if !ch.is_control() {
103 state.query.push(ch);
104 self.key_consumed = true;
105 self.ui.continuous = true;
106 }
107 }
108 }
109
110 let mut progress = self
111 .spatial(id, 21, f32::from(state.open), f32::from(was_open))
112 .clamp(0.0, 1.0);
113
114 if was_open && !state.open && progress <= 0.001 {
115 paint_closed_bar(self, state, false);
116 return None;
117 }
118
119 let mut picked = None;
120 if state.open || progress > 0.001 {
121 let needle = state.query.trim().to_ascii_lowercase();
122 let filtered: Vec<(usize, &str)> = suggestions
123 .iter()
124 .enumerate()
125 .filter(|(_, item)| {
126 needle.is_empty() || item.to_ascii_lowercase().contains(&needle)
127 })
128 .map(|(idx, item)| (idx, *item))
129 .collect();
130
131 let vp = self.viewport();
132 let header_h = 56.0;
133 let row_h = 48.0;
134 let avail_h = (vp.y + vp.height - bar_bounds.y).max(bar_bounds.height);
135 let rows_h = (filtered.len().max(1) as f32) * row_h + 8.0;
136 let target_h = (header_h + 1.0 + rows_h)
137 .min(avail_h)
138 .max(bar_bounds.height);
139 let current_rect = Rect {
140 x: bar_bounds.x,
141 y: bar_bounds.y,
142 width: bar_bounds.width,
143 height: bar_bounds.height + (target_h - bar_bounds.height) * progress,
144 };
145
146 if was_open
147 && state.open
148 && self.input.pressed
149 && self.input.pointer.is_some_and(|p| !current_rect.contains(p))
150 {
151 state.open = false;
152 self.ui.active = None;
153 self.ui.continuous = true;
154 progress = self.spatial(id, 21, 0.0, 1.0).clamp(0.0, 1.0);
155 if progress <= 0.001 {
156 paint_closed_bar(self, state, false);
157 return None;
158 }
159 }
160
161 let interactive = state.open && !just_opened;
162 let saved_clip = self.clip;
163 self.clip = vp;
164 let overlay_start = self.commands.len();
165 if progress > 0.001 {
166 self.rect(vp, 0.0, Color(0, 0, 0, 255).opacity(0.32 * progress));
167 }
168 self.elevation(current_rect, 28.0, 3);
169 self.rect(current_rect, 28.0, t.surface_high);
170 self.within(current_rect, |f| {
171 let back_rect = Rect {
172 x: current_rect.x + 8.0,
173 y: current_rect.y + 8.0,
174 width: 40.0,
175 height: 40.0,
176 };
177 let back = f.interact(sub_id(1), back_rect, interactive);
178 f.state_layer(back, 20.0, t.text);
179 f.ink(back, 20.0, t.text);
180 f.paint_material_icon(back_rect.inset(8.0), MaterialIcon::ArrowBack, t.text);
181 f.focus_ring(back);
182 f.semantic(back, Role::Button, "Back", false, None, None, None);
183 if back.clicked {
184 state.open = false;
185 f.ui.active = None;
186 f.ui.continuous = true;
187 }
188
189 let has_query = !state.query.is_empty();
190 let mut cleared = false;
191 if has_query {
192 let clear_rect = Rect {
193 x: (current_rect.x + current_rect.width - 48.0)
194 .max(back_rect.x + back_rect.width),
195 y: current_rect.y + 8.0,
196 width: 40.0,
197 height: 40.0,
198 };
199 let clear = f.interact(sub_id(3), clear_rect, interactive && state.open);
200 f.state_layer(clear, 20.0, t.on_surface_variant);
201 f.ink(clear, 20.0, t.on_surface_variant);
202 f.paint_material_icon(
203 clear_rect.inset(8.0),
204 MaterialIcon::Close,
205 t.on_surface_variant,
206 );
207 f.focus_ring(clear);
208 f.semantic(clear, Role::Button, "Clear", false, None, None, None);
209 if clear.clicked {
210 state.query.clear();
211 state.selected = None;
212 cleared = true;
213 f.ui.continuous = true;
214 }
215 }
216
217 let trailing_w = if has_query && !cleared { 56.0 } else { 16.0 };
218 let input_rect = Rect {
219 x: current_rect.x + 56.0,
220 y: current_rect.y,
221 width: (current_rect.width - 56.0 - trailing_w).max(0.0),
222 height: header_h,
223 };
224 let field = f.interact(sub_id(2), input_rect, interactive && state.open);
225 let show_query = !state.query.is_empty();
226 f.styled_text(
227 input_rect,
228 if show_query {
229 state.query.as_str()
230 } else {
231 placeholder
232 },
233 if show_query {
234 t.text
235 } else {
236 t.on_surface_variant
237 },
238 TextStyle {
239 size: 16.0,
240 ..TextStyle::default()
241 },
242 );
243 f.focus_ring(field);
244 f.semantic(field, Role::TextField, placeholder, false, None, None, None);
245
246 if current_rect.height > header_h + 0.5 {
247 f.divider_at(
248 Rect {
249 x: current_rect.x,
250 y: current_rect.y + header_h,
251 width: current_rect.width,
252 height: 1.0,
253 },
254 DividerOrientation::Horizontal,
255 0.0,
256 0.0,
257 );
258
259 if filtered.is_empty() {
260 let empty_rect = Rect {
261 x: current_rect.x + 16.0,
262 y: current_rect.y + header_h + 1.0,
263 width: (current_rect.width - 32.0).max(0.0),
264 height: row_h,
265 };
266 f.styled_text(
267 empty_rect,
268 "No matching suggestions",
269 t.on_surface_variant,
270 TextStyle {
271 size: 14.0,
272 ..TextStyle::default()
273 },
274 );
275 } else {
276 for (row_idx, &(orig_idx, suggestion)) in filtered.iter().enumerate() {
277 let row_y = current_rect.y + header_h + 1.0 + row_idx as f32 * row_h;
278 if row_y >= current_rect.y + current_rect.height {
279 break;
280 }
281 let row_rect = Rect {
282 x: current_rect.x,
283 y: row_y,
284 width: current_rect.width,
285 height: row_h,
286 };
287 let row = f.interact(
288 sub_id(100 + orig_idx as u64),
289 row_rect,
290 interactive && state.open,
291 );
292 let is_selected = state.selected == Some(orig_idx);
293 if is_selected {
294 f.rect(row_rect, 0.0, t.secondary.opacity(0.35));
295 }
296 f.state_layer(row, 0.0, t.text);
297 f.ink(row, 0.0, t.text);
298 let icon_rect = Rect {
299 x: row_rect.x + 16.0,
300 y: row_rect.y + (row_h - 20.0) * 0.5,
301 width: 20.0,
302 height: 20.0,
303 };
304 f.paint_material_icon(
305 icon_rect,
306 MaterialIcon::Search,
307 t.on_surface_variant,
308 );
309 let label_rect = Rect {
310 x: row_rect.x + 56.0,
311 y: row_rect.y,
312 width: (row_rect.width - 72.0).max(0.0),
313 height: row_h,
314 };
315 f.styled_text(
316 label_rect,
317 suggestion,
318 t.text,
319 TextStyle {
320 size: 16.0,
321 ..TextStyle::default()
322 },
323 );
324 f.focus_ring(row);
325 f.semantic(
326 row,
327 Role::ListItem,
328 suggestion,
329 is_selected,
330 None,
331 None,
332 None,
333 );
334 if row.clicked {
335 picked = Some(orig_idx);
336 state.selected = Some(orig_idx);
337 state.query.clear();
338 state.query.push_str(suggestion);
339 state.open = false;
340 f.ui.active = None;
341 f.ui.continuous = true;
342 }
343 }
344 }
345 }
346 });
347 self.ui.overlays.extend(self.commands.drain(overlay_start..));
348 self.clip = saved_clip;
349 }
350 self.ui.overlay_active |= state.open;
351 picked
352 }
353
354 pub fn inline_search_bar(
358 &mut self,
359 id: u64,
360 bounds: Rect,
361 placeholder: &str,
362 query: &str,
363 show_back: bool,
364 enabled: bool,
365 ) -> (Response, bool, bool) {
366 let t = self.theme();
367 let rad = (bounds.height * 0.5).min(28.0);
368 self.paint_rect(bounds, rad, t.surface_high);
369 let mut back_clicked = false;
370 let mut clear_clicked = false;
371 let btn_sz = 40.0_f32.min(bounds.height);
372 let btn_y = bounds.y + (bounds.height - btn_sz) * 0.5;
373 let lead_x = bounds.x + 8.0;
374 if show_back {
375 let br = self.material_icon_button_at(
376 id.wrapping_add(1),
377 Rect { x: lead_x, y: btn_y, width: btn_sz, height: btn_sz },
378 MaterialIcon::ArrowBack,
379 "Back",
380 Options { enabled, ..Options::default() },
381 );
382 back_clicked = br.clicked;
383 } else {
384 let icon_rect = Rect { x: lead_x + 8.0, y: bounds.y + (bounds.height - 22.0) * 0.5, width: 22.0, height: 22.0 };
385 self.paint_material_icon(icon_rect, MaterialIcon::Search, t.on_surface_variant);
386 }
387 let has_clear = !query.is_empty();
388 let trail_space = if has_clear { btn_sz + 8.0 } else { 16.0 };
389 let field_rect = Rect {
390 x: lead_x + btn_sz + 4.0,
391 y: bounds.y,
392 width: (bounds.width - btn_sz - trail_space - 12.0).max(0.0),
393 height: bounds.height,
394 };
395 let field_r = self.target(id, field_rect, placeholder, enabled, Role::TextField);
396 let text_style = TextStyle { size: 15.0, ..TextStyle::default() };
397 if query.is_empty() {
398 self.styled_text(field_rect, placeholder, t.on_surface_variant, text_style);
399 } else {
400 self.styled_text(field_rect, query, t.text, text_style);
401 }
402 if field_r.focused && enabled {
403 let blink_on = ((self.time() * 2.0) as u64).is_multiple_of(2) || self.reduced_motion();
404 if blink_on {
405 let cx = field_rect.x + self.text_width(query, text_style).min(field_rect.width - 2.0);
406 self.paint_rect(
407 Rect { x: cx, y: field_rect.y + (field_rect.height - 20.0) * 0.5, width: 2.0, height: 20.0 },
408 1.0,
409 t.primary,
410 );
411 }
412 if !self.reduced_motion() {
413 self.wake_at((self.time() * 2.0).floor() * 0.5 + 0.5);
414 }
415 }
416 if has_clear {
417 let cr = self.material_icon_button_at(
418 id.wrapping_add(2),
419 Rect { x: bounds.x + bounds.width - btn_sz - 8.0, y: btn_y, width: btn_sz, height: btn_sz },
420 MaterialIcon::Close,
421 "Clear search",
422 Options { enabled, ..Options::default() },
423 );
424 clear_clicked = cr.clicked;
425 }
426 (field_r, back_clicked, clear_clicked)
427 }
428}
429
430#[cfg(test)]
431#[path = "search.spec.rs"]
432mod tests;