1use std::path::PathBuf;
2
3use crate::prelude::*;
4use crate::{h_stack, HighlightedText};
5use gpui2::Div;
6
7#[derive(Clone)]
8pub struct Symbol(pub Vec<HighlightedText>);
9
10#[derive(Component)]
11pub struct Breadcrumb {
12 path: PathBuf,
13 symbols: Vec<Symbol>,
14}
15
16impl Breadcrumb {
17 pub fn new(path: PathBuf, symbols: Vec<Symbol>) -> Self {
18 Self { path, symbols }
19 }
20
21 fn render_separator<V: 'static>(&self, cx: &WindowContext) -> Div<V> {
22 let theme = theme(cx);
23
24 div().child(" › ").text_color(theme.text_muted)
25 }
26
27 fn render<V: 'static>(self, view_state: &mut V, cx: &mut ViewContext<V>) -> impl Component<V> {
28 let theme = theme(cx);
29
30 let symbols_len = self.symbols.len();
31
32 h_stack()
33 .id("breadcrumb")
34 .px_1()
35 .text_sm()
36 .text_color(theme.text_muted)
37 .rounded_md()
38 .hover(|style| style.bg(theme.ghost_element_hover))
39 .active(|style| style.bg(theme.ghost_element_active))
40 .child(self.path.clone().to_str().unwrap().to_string())
41 .child(if !self.symbols.is_empty() {
42 self.render_separator(cx)
43 } else {
44 div()
45 })
46 .child(
47 div().flex().children(
48 self.symbols
49 .iter()
50 .enumerate()
51 // TODO: Could use something like `intersperse` here instead.
52 .flat_map(|(ix, symbol)| {
53 let mut items =
54 vec![div().flex().children(symbol.0.iter().map(|segment| {
55 div().child(segment.text.clone()).text_color(segment.color)
56 }))];
57
58 let is_last_segment = ix == symbols_len - 1;
59 if !is_last_segment {
60 items.push(self.render_separator(cx));
61 }
62
63 items
64 })
65 .collect::<Vec<_>>(),
66 ),
67 )
68 }
69}
70
71#[cfg(feature = "stories")]
72pub use stories::*;
73
74#[cfg(feature = "stories")]
75mod stories {
76 use super::*;
77 use crate::Story;
78 use gpui2::Render;
79 use std::str::FromStr;
80
81 pub struct BreadcrumbStory;
82
83 impl Render for BreadcrumbStory {
84 type Element = Div<Self>;
85
86 fn render(&mut self, cx: &mut ViewContext<Self>) -> Self::Element {
87 let theme = theme(cx);
88
89 Story::container(cx)
90 .child(Story::title_for::<_, Breadcrumb>(cx))
91 .child(Story::label(cx, "Default"))
92 .child(Breadcrumb::new(
93 PathBuf::from_str("crates/ui/src/components/toolbar.rs").unwrap(),
94 vec![
95 Symbol(vec![
96 HighlightedText {
97 text: "impl ".to_string(),
98 color: theme.syntax.color("keyword"),
99 },
100 HighlightedText {
101 text: "BreadcrumbStory".to_string(),
102 color: theme.syntax.color("function"),
103 },
104 ]),
105 Symbol(vec![
106 HighlightedText {
107 text: "fn ".to_string(),
108 color: theme.syntax.color("keyword"),
109 },
110 HighlightedText {
111 text: "render".to_string(),
112 color: theme.syntax.color("function"),
113 },
114 ]),
115 ],
116 ))
117 }
118 }
119}