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