active_file_name.rs

 1use gpui::{
 2    Context, Empty, EventEmitter, IntoElement, ParentElement, Render, SharedString, Window,
 3};
 4use settings::Settings;
 5use ui::{Button, Tooltip, prelude::*};
 6use util::paths::PathStyle;
 7
 8use crate::{StatusItemView, item::ItemHandle, workspace_settings::StatusBarSettings};
 9
10pub struct ActiveFileName {
11    project_path: Option<SharedString>,
12    full_path: Option<SharedString>,
13}
14
15impl ActiveFileName {
16    pub fn new() -> Self {
17        Self {
18            project_path: None,
19            full_path: None,
20        }
21    }
22}
23
24impl Render for ActiveFileName {
25    fn render(&mut self, _window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
26        if !StatusBarSettings::get_global(cx).show_active_file {
27            return Empty.into_any_element();
28        }
29
30        let Some(project_path) = self.project_path.clone() else {
31            return Empty.into_any_element();
32        };
33
34        let tooltip_text = self
35            .full_path
36            .clone()
37            .unwrap_or_else(|| project_path.clone());
38
39        div()
40            .child(
41                Button::new("active-file-name-button", project_path)
42                    .label_size(LabelSize::Small)
43                    .tooltip(Tooltip::text(tooltip_text)),
44            )
45            .into_any_element()
46    }
47}
48
49impl EventEmitter<crate::ToolbarItemEvent> for ActiveFileName {}
50
51impl StatusItemView for ActiveFileName {
52    fn set_active_pane_item(
53        &mut self,
54        active_pane_item: Option<&dyn ItemHandle>,
55        _window: &mut Window,
56        cx: &mut Context<Self>,
57    ) {
58        if let Some(item) = active_pane_item {
59            self.project_path = item
60                .project_path(cx)
61                .map(|path| path.path.display(PathStyle::local()).into_owned().into());
62            self.full_path = item.tab_tooltip_text(cx);
63        } else {
64            self.project_path = None;
65            self.full_path = None;
66        }
67        cx.notify();
68    }
69}