Skip to main content

gpui_component/
breadcrumb.rs

1use std::rc::Rc;
2
3use gpui::{
4    App, ClickEvent, ElementId, InteractiveElement as _, IntoElement, ParentElement, RenderOnce,
5    Role, SharedString, StatefulInteractiveElement, StyleRefinement, Styled, Window, div,
6    prelude::FluentBuilder as _,
7};
8
9use crate::{ActiveTheme, Icon, IconName, StyledExt, h_flex};
10use gpui_base::{LayoutDirection, LogicalStyledExt as _};
11
12/// A breadcrumb navigation element.
13#[derive(IntoElement)]
14pub struct Breadcrumb {
15    style: StyleRefinement,
16    items: Vec<BreadcrumbItem>,
17}
18
19/// Item for the [`Breadcrumb`].
20#[derive(IntoElement)]
21pub struct BreadcrumbItem {
22    id: ElementId,
23    style: StyleRefinement,
24    label: SharedString,
25    on_click: Option<Rc<dyn Fn(&ClickEvent, &mut Window, &mut App)>>,
26    disabled: bool,
27    is_last: bool,
28}
29
30impl BreadcrumbItem {
31    /// Create a new BreadcrumbItem with the given id and label.
32    pub fn new(label: impl Into<SharedString>) -> Self {
33        Self {
34            id: ElementId::Integer(0),
35            style: StyleRefinement::default(),
36            label: label.into(),
37            on_click: None,
38            disabled: false,
39            is_last: false,
40        }
41    }
42
43    pub fn disabled(mut self, disabled: bool) -> Self {
44        self.disabled = disabled;
45        self
46    }
47
48    pub fn on_click(
49        mut self,
50        on_click: impl Fn(&ClickEvent, &mut Window, &mut App) + 'static,
51    ) -> Self {
52        self.on_click = Some(Rc::new(on_click));
53        self
54    }
55
56    fn id(mut self, id: impl Into<ElementId>) -> Self {
57        self.id = id.into();
58        self
59    }
60
61    /// For internal use only.
62    fn is_last(mut self, is_last: bool) -> Self {
63        self.is_last = is_last;
64        self
65    }
66}
67
68impl Styled for BreadcrumbItem {
69    fn style(&mut self) -> &mut StyleRefinement {
70        &mut self.style
71    }
72}
73
74impl From<&'static str> for BreadcrumbItem {
75    fn from(value: &'static str) -> Self {
76        Self::new(value)
77    }
78}
79
80impl From<String> for BreadcrumbItem {
81    fn from(value: String) -> Self {
82        Self::new(value)
83    }
84}
85
86impl From<SharedString> for BreadcrumbItem {
87    fn from(value: SharedString) -> Self {
88        Self::new(value)
89    }
90}
91
92impl RenderOnce for BreadcrumbItem {
93    fn render(self, _: &mut Window, cx: &mut App) -> impl IntoElement {
94        div()
95            .id(self.id)
96            .role(if self.on_click.is_some() && !self.disabled {
97                Role::Link
98            } else {
99                Role::ListItem
100            })
101            .child(self.label)
102            .text_color(cx.theme().muted_foreground)
103            .when(self.is_last, |this| this.text_color(cx.theme().foreground))
104            .when(self.disabled, |this| {
105                this.text_color(cx.theme().muted_foreground)
106            })
107            .refine_style(&self.style)
108            .when(!self.disabled, |this| {
109                this.when_some(self.on_click, |this, on_click| {
110                    this.cursor_pointer().on_click(move |event, window, cx| {
111                        on_click(event, window, cx);
112                    })
113                })
114            })
115    }
116}
117
118impl Breadcrumb {
119    /// Create a new breadcrumb.
120    pub fn new() -> Self {
121        Self {
122            items: Vec::new(),
123            style: StyleRefinement::default(),
124        }
125    }
126
127    /// Add an [`BreadcrumbItem`] to the breadcrumb.
128    pub fn child(mut self, item: impl Into<BreadcrumbItem>) -> Self {
129        self.items.push(item.into());
130        self
131    }
132
133    /// Add multiple [`BreadcrumbItem`] items to the breadcrumb.
134    pub fn children(mut self, items: impl IntoIterator<Item = impl Into<BreadcrumbItem>>) -> Self {
135        self.items.extend(items.into_iter().map(Into::into));
136        self
137    }
138}
139
140#[derive(IntoElement)]
141struct BreadcrumbSeparator;
142impl RenderOnce for BreadcrumbSeparator {
143    fn render(self, _: &mut Window, cx: &mut App) -> impl IntoElement {
144        // Points from an item to the next, toward the end edge.
145        Icon::new(if LayoutDirection::global(cx).is_rtl() {
146            IconName::ChevronLeft
147        } else {
148            IconName::ChevronRight
149        })
150        .text_color(cx.theme().muted_foreground)
151        .size_3p5()
152        .into_any_element()
153    }
154}
155
156impl Styled for Breadcrumb {
157    fn style(&mut self) -> &mut StyleRefinement {
158        &mut self.style
159    }
160}
161
162impl RenderOnce for Breadcrumb {
163    fn render(self, _: &mut Window, cx: &mut App) -> impl IntoElement {
164        let items_count = self.items.len();
165
166        let mut children = vec![];
167        for (ix, item) in self.items.into_iter().enumerate() {
168            let is_last = ix == items_count - 1;
169
170            let item = item.id(ix);
171            children.push(item.is_last(is_last).into_any_element());
172            if !is_last {
173                children.push(BreadcrumbSeparator.into_any_element());
174            }
175        }
176
177        h_flex()
178            .flex_row_dir(cx)
179            .gap_1p5()
180            .text_sm()
181            .text_color(cx.theme().muted_foreground)
182            .refine_style(&self.style)
183            .children(children)
184    }
185}