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: Option<SharedString>,
20    pub(super) overflow_label: Option<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: None,
34            overflow_label: None,
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
43    /// [`gpui_material_theme::MaterialStrings::breadcrumb`] (`"Breadcrumb"`).
44    pub fn accessible_label(mut self, label: impl Into<SharedString>) -> Self {
45        self.accessible_label = Some(label.into());
46        self
47    }
48
49    /// Sets the accessible name of the collapsed-segments overflow trigger button.
50    /// Defaults to [`gpui_material_theme::MaterialStrings::more_breadcrumbs`] (`"More breadcrumbs"`).
51    pub fn overflow_label(mut self, label: impl Into<SharedString>) -> Self {
52        self.overflow_label = Some(label.into());
53        self
54    }
55
56    /// Invoked when clicking or activating any non-current enabled ancestor segment,
57    /// including collapsed segments selected from the overflow [`crate::Menu`].
58    pub fn on_select(
59        mut self,
60        handler: impl Fn(SharedString, &mut Window, &mut App) + 'static,
61    ) -> Self {
62        self.on_select = Some(Rc::new(handler));
63        self
64    }
65
66    /// Caps visible inline segments before collapsing middle segments into the overflow menu.
67    /// Values below `2` are clamped to `2` when three or more segments exist.
68    pub fn max_items(mut self, count: usize) -> Self {
69        self.max_items = Some(count.max(2));
70        self
71    }
72
73    /// Replaces the decorative separator between segments.
74    /// Defaults to `Icon::new(IconName::ChevronRight).size(px(16.)).mirror_in_rtl(true)`.
75    pub fn separator<E: IntoElement>(mut self, separator: impl Fn() -> E + 'static) -> Self {
76        self.separator = Some(Rc::new(move || separator().into_any_element()));
77        self
78    }
79
80    /// Overrides ancestor, current, separator, and disabled colors.
81    pub fn colors(mut self, colors: BreadcrumbsColors) -> Self {
82        self.colors = colors;
83        self
84    }
85}