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