bhoo/systems/material/
breadcrumb.rs1use crate::*;
3
4impl Frame<'_> {
5 pub fn breadcrumb(&mut self, id: u64, segments: &[&str], active: Option<usize>) -> Option<usize> {
8 if segments.is_empty() {
9 return None;
10 }
11 let t = self.theme();
12 let bar = self.reserve(36.0);
13 let active_idx = active.unwrap_or(segments.len() - 1).min(segments.len() - 1);
14 let mut x = bar.x;
15 let max_x = bar.x + bar.width;
16 let mut clicked = None;
17
18 for (i, &seg) in segments.iter().enumerate() {
19 if i > 0 {
20 let sep_w = 18.0;
21 if x + sep_w > max_x {
22 break;
23 }
24 self.styled_text(
25 Rect { x, y: bar.y, width: sep_w, height: bar.height },
26 "›",
27 t.on_surface_variant,
28 TextStyle { size: 14.0, align: 1, ..TextStyle::default() },
29 );
30 x += sep_w;
31 }
32 let is_active = i == active_idx;
33 let style = TextStyle {
34 size: 14.0,
35 weight: if is_active { 600 } else { 500 },
36 align: 1,
37 ..TextStyle::default()
38 };
39 let w = (self.text_width(seg, style) + 16.0).min((max_x - x).max(0.0));
40 if w <= 1.0 {
41 break;
42 }
43 let rect = Rect { x, y: bar.y, width: w, height: bar.height };
44 let r = self.target(id.wrapping_add(i as u64), rect, seg, true, Role::Link);
45 self.state_layer(r, 8.0, t.primary);
46 self.focus_ring(r);
47 self.styled_text(rect, seg, if is_active { t.text } else { t.primary }, style);
48 if r.clicked {
49 clicked = Some(i);
50 }
51 x += w;
52 }
53 clicked
54 }
55}
56
57#[cfg(test)]
58#[path = "breadcrumb.spec.rs"]
59mod tests;