1use editor::Editor;
2use gpui::{
3 Context, Element, EventEmitter, Focusable, FontWeight, IntoElement, ParentElement, Render,
4 StyledText, Subscription, Window,
5};
6use itertools::Itertools;
7use settings::Settings;
8use std::cmp;
9use theme::ActiveTheme;
10use ui::{ButtonLike, ButtonStyle, Label, Tooltip, prelude::*};
11use workspace::{
12 TabBarSettings, ToolbarItemEvent, ToolbarItemLocation, ToolbarItemView,
13 item::{BreadcrumbText, ItemEvent, ItemHandle},
14};
15
16pub struct Breadcrumbs {
17 pane_focused: bool,
18 active_item: Option<Box<dyn ItemHandle>>,
19 subscription: Option<Subscription>,
20}
21
22impl Default for Breadcrumbs {
23 fn default() -> Self {
24 Self::new()
25 }
26}
27
28impl Breadcrumbs {
29 pub fn new() -> Self {
30 Self {
31 pane_focused: false,
32 active_item: Default::default(),
33 subscription: Default::default(),
34 }
35 }
36}
37
38impl EventEmitter<ToolbarItemEvent> for Breadcrumbs {}
39
40// Potential idea:
41// - Rename this to "BreadcrumbToolbar" or something
42// - Create a wrapping "Breadcrumb" struct for Vec<BreadcrumbText>
43// - Implement render for _that_ breadcrumb struct.
44// - Call that from here to eliminate much of the logic.
45// - This will change the Item interface, so do it only after you're happy with the features thus far
46impl Render for Breadcrumbs {
47 fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
48 const MAX_SEGMENTS: usize = 12;
49
50 let element = h_flex()
51 .id("breadcrumb-container")
52 .flex_grow()
53 .overflow_x_scroll()
54 .text_ui(cx);
55
56 let Some(active_item) = self.active_item.as_ref() else {
57 return element;
58 };
59
60 // Begin - logic we should copy/move
61 let Some(mut segments) = active_item.breadcrumbs(cx.theme(), cx) else {
62 return element;
63 };
64
65 let prefix_end_ix = cmp::min(segments.len(), MAX_SEGMENTS / 2);
66 let suffix_start_ix = cmp::max(
67 prefix_end_ix,
68 segments.len().saturating_sub(MAX_SEGMENTS / 2),
69 );
70
71 if suffix_start_ix > prefix_end_ix {
72 segments.splice(
73 prefix_end_ix..suffix_start_ix,
74 Some(BreadcrumbText {
75 text: "⋯".into(),
76 highlights: None,
77 font: None,
78 }),
79 );
80 }
81
82 let highlighted_segments = segments.into_iter().enumerate().map(|(index, segment)| {
83 let mut text_style = window.text_style();
84 if let Some(ref font) = segment.font {
85 text_style.font_family = font.family.clone();
86 text_style.font_features = font.features.clone();
87 text_style.font_style = font.style;
88 text_style.font_weight = font.weight;
89 }
90 text_style.color = Color::Muted.color(cx);
91
92 if index == 0
93 && !TabBarSettings::get_global(cx).show
94 && active_item.is_dirty(cx)
95 && let Some(styled_element) = apply_dirty_filename_style(&segment, &text_style, cx)
96 {
97 return styled_element;
98 }
99
100 StyledText::new(segment.text.replace('\n', "⏎"))
101 .with_default_highlights(&text_style, segment.highlights.unwrap_or_default())
102 .into_any()
103 });
104 let breadcrumbs = Itertools::intersperse_with(highlighted_segments, || {
105 Label::new("›").color(Color::Placeholder).into_any_element()
106 });
107
108 let breadcrumbs_stack = h_flex().gap_1().children(breadcrumbs);
109
110 let prefix_element = active_item.breadcrumb_prefix(window, cx);
111
112 let breadcrumbs = if let Some(prefix) = prefix_element {
113 h_flex().gap_1p5().child(prefix).child(breadcrumbs_stack)
114 } else {
115 breadcrumbs_stack
116 };
117
118 match active_item
119 .downcast::<Editor>()
120 .map(|editor| editor.downgrade())
121 {
122 Some(editor) => element.child(
123 ButtonLike::new("toggle outline view")
124 .child(breadcrumbs)
125 .style(ButtonStyle::Transparent)
126 .on_click({
127 let editor = editor.clone();
128 move |_, window, cx| {
129 if let Some((editor, callback)) = editor
130 .upgrade()
131 .zip(zed_actions::outline::TOGGLE_OUTLINE.get())
132 {
133 callback(editor.to_any_view(), window, cx);
134 }
135 }
136 })
137 .tooltip(move |_window, cx| {
138 if let Some(editor) = editor.upgrade() {
139 let focus_handle = editor.read(cx).focus_handle(cx);
140 Tooltip::for_action_in(
141 "Show Symbol Outline",
142 &zed_actions::outline::ToggleOutline,
143 &focus_handle,
144 cx,
145 )
146 } else {
147 Tooltip::for_action(
148 "Show Symbol Outline",
149 &zed_actions::outline::ToggleOutline,
150 cx,
151 )
152 }
153 }),
154 ),
155 None => element
156 // Match the height and padding of the `ButtonLike` in the other arm.
157 .h(rems_from_px(22.))
158 .pl_1()
159 .child(breadcrumbs),
160 }
161 // End
162 }
163}
164
165impl ToolbarItemView for Breadcrumbs {
166 fn set_active_pane_item(
167 &mut self,
168 active_pane_item: Option<&dyn ItemHandle>,
169 window: &mut Window,
170 cx: &mut Context<Self>,
171 ) -> ToolbarItemLocation {
172 cx.notify();
173 self.active_item = None;
174
175 let Some(item) = active_pane_item else {
176 return ToolbarItemLocation::Hidden;
177 };
178
179 let this = cx.entity().downgrade();
180 self.subscription = Some(item.subscribe_to_item_events(
181 window,
182 cx,
183 Box::new(move |event, _, cx| {
184 if let ItemEvent::UpdateBreadcrumbs = event {
185 this.update(cx, |this, cx| {
186 cx.notify();
187 if let Some(active_item) = this.active_item.as_ref() {
188 cx.emit(ToolbarItemEvent::ChangeLocation(
189 active_item.breadcrumb_location(cx),
190 ))
191 }
192 })
193 .ok();
194 }
195 }),
196 ));
197 self.active_item = Some(item.boxed_clone());
198 item.breadcrumb_location(cx)
199 }
200
201 fn pane_focus_update(
202 &mut self,
203 pane_focused: bool,
204 _window: &mut Window,
205 _: &mut Context<Self>,
206 ) {
207 self.pane_focused = pane_focused;
208 }
209}
210
211fn apply_dirty_filename_style(
212 segment: &BreadcrumbText,
213 text_style: &gpui::TextStyle,
214 cx: &mut Context<Breadcrumbs>,
215) -> Option<gpui::AnyElement> {
216 let text = segment.text.replace('\n', "⏎");
217
218 let filename_position = std::path::Path::new(&segment.text)
219 .file_name()
220 .and_then(|f| {
221 let filename_str = f.to_string_lossy();
222 segment.text.rfind(filename_str.as_ref())
223 })?;
224
225 let bold_weight = FontWeight::BOLD;
226 let default_color = Color::Default.color(cx);
227
228 if filename_position == 0 {
229 let mut filename_style = text_style.clone();
230 filename_style.font_weight = bold_weight;
231 filename_style.color = default_color;
232
233 return Some(
234 StyledText::new(text)
235 .with_default_highlights(&filename_style, [])
236 .into_any(),
237 );
238 }
239
240 let highlight_style = gpui::HighlightStyle {
241 font_weight: Some(bold_weight),
242 color: Some(default_color),
243 ..Default::default()
244 };
245
246 let highlight = vec![(filename_position..text.len(), highlight_style)];
247 Some(
248 StyledText::new(text)
249 .with_default_highlights(text_style, highlight)
250 .into_any(),
251 )
252}