Skip to main content

gpui_material_components/navigation/
breadcrumbs.rs

1use std::rc::Rc;
2
3use gpui::{App, ElementId, IntoElement, SharedString, Window};
4
5mod layout;
6mod render;
7mod types;
8
9pub use types::{BreadcrumbItem, BreadcrumbsColors};
10
11use types::{BreadcrumbSelect, SeparatorFactory};
12
13/// Desktop path navigation with RTL mirroring and automatic middle-segment
14/// overflow collapse into an anchored Material [`crate::Menu`].
15#[derive(IntoElement)]
16pub struct Breadcrumbs {
17    pub(super) id: ElementId,
18    pub(super) items: Vec<BreadcrumbItem>,
19    pub(super) accessible_label: SharedString,
20    pub(super) overflow_label: SharedString,
21    pub(super) on_select: Option<BreadcrumbSelect>,
22    pub(super) max_items: Option<usize>,
23    pub(super) separator: Option<SeparatorFactory>,
24    pub(super) colors: BreadcrumbsColors,
25}
26
27impl Breadcrumbs {
28    /// Creates a breadcrumb navigation strip with the given element ID and ordered path segments.
29    pub fn new(id: impl Into<ElementId>, items: impl IntoIterator<Item = BreadcrumbItem>) -> Self {
30        Self {
31            id: id.into(),
32            items: items.into_iter().collect(),
33            accessible_label: "Breadcrumb".into(),
34            overflow_label: "More breadcrumbs".into(),
35            on_select: None,
36            max_items: None,
37            separator: None,
38            colors: BreadcrumbsColors::default(),
39        }
40    }
41
42    /// Sets the `Role::Navigation` landmark label. Defaults to `"Breadcrumb"`.
43    pub fn accessible_label(mut self, label: impl Into<SharedString>) -> Self {
44        self.accessible_label = label.into();
45        self
46    }
47
48    /// Sets the accessible name of the collapsed-segments overflow trigger button.
49    pub fn overflow_label(mut self, label: impl Into<SharedString>) -> Self {
50        self.overflow_label = label.into();
51        self
52    }
53
54    /// Invoked when clicking or activating any non-current enabled ancestor segment,
55    /// including collapsed segments selected from the overflow [`crate::Menu`].
56    pub fn on_select(
57        mut self,
58        handler: impl Fn(SharedString, &mut Window, &mut App) + 'static,
59    ) -> Self {
60        self.on_select = Some(Rc::new(handler));
61        self
62    }
63
64    /// Caps visible inline segments before collapsing middle segments into the overflow menu.
65    /// Values below `2` are clamped to `2` when three or more segments exist.
66    pub fn max_items(mut self, count: usize) -> Self {
67        self.max_items = Some(count.max(2));
68        self
69    }
70
71    /// Replaces the decorative separator between segments.
72    /// Defaults to `Icon::new(IconName::ChevronRight).size(px(16.)).mirror_in_rtl(true)`.
73    pub fn separator<E: IntoElement>(mut self, separator: impl Fn() -> E + 'static) -> Self {
74        self.separator = Some(Rc::new(move || separator().into_any_element()));
75        self
76    }
77
78    /// Overrides ancestor, current, separator, and disabled colors.
79    pub fn colors(mut self, colors: BreadcrumbsColors) -> Self {
80        self.colors = colors;
81        self
82    }
83}