breadcrumb.rs

 1use std::marker::PhantomData;
 2use std::path::PathBuf;
 3
 4use gpui3::Div;
 5
 6use crate::prelude::*;
 7use crate::{h_stack, HighlightedText};
 8
 9#[derive(Clone)]
10pub struct Symbol(pub Vec<HighlightedText>);
11
12#[derive(Element)]
13pub struct Breadcrumb<S: 'static + Send + Sync + Clone> {
14    state_type: PhantomData<S>,
15    path: PathBuf,
16    symbols: Vec<Symbol>,
17}
18
19impl<S: 'static + Send + Sync + Clone> Breadcrumb<S> {
20    pub fn new(path: PathBuf, symbols: Vec<Symbol>) -> Self {
21        Self {
22            state_type: PhantomData,
23            path,
24            symbols,
25        }
26    }
27
28    fn render_separator(&self, theme: &Theme) -> Div<S> {
29        div()
30            .child("")
31            .text_color(HighlightColor::Default.hsla(theme))
32    }
33
34    fn render(&mut self, cx: &mut ViewContext<S>) -> impl Element<State = S> {
35        let theme = theme(cx);
36
37        let symbols_len = self.symbols.len();
38
39        h_stack()
40            .px_1()
41            // TODO: Read font from theme (or settings?).
42            .font("Zed Mono Extended")
43            .text_sm()
44            .text_color(theme.middle.base.default.foreground)
45            .rounded_md()
46            // .hover()
47            // .fill(theme.highest.base.hovered.background)
48            .child(self.path.clone().to_str().unwrap().to_string())
49            .child(if !self.symbols.is_empty() {
50                self.render_separator(&theme)
51            } else {
52                div()
53            })
54            .child(
55                div().flex().children(
56                    self.symbols
57                        .iter()
58                        .enumerate()
59                        // TODO: Could use something like `intersperse` here instead.
60                        .flat_map(|(ix, symbol)| {
61                            let mut items =
62                                vec![div().flex().children(symbol.0.iter().map(|segment| {
63                                    div().child(segment.text.clone()).text_color(segment.color)
64                                }))];
65
66                            let is_last_segment = ix == symbols_len - 1;
67                            if !is_last_segment {
68                                items.push(self.render_separator(&theme));
69                            }
70
71                            items
72                        })
73                        .collect::<Vec<_>>(),
74                ),
75            )
76    }
77}