toolbar_controls.rs

 1use crate::ProjectDiagnosticsEditor;
 2use gpui::{EventEmitter, ParentElement, Render, ViewContext, WeakView};
 3use ui::prelude::*;
 4use ui::{IconButton, IconName, Tooltip};
 5use workspace::{item::ItemHandle, ToolbarItemEvent, ToolbarItemLocation, ToolbarItemView};
 6
 7pub struct ToolbarControls {
 8    editor: Option<WeakView<ProjectDiagnosticsEditor>>,
 9}
10
11impl Render for ToolbarControls {
12    fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
13        let mut include_warnings = false;
14        let mut has_stale_excerpts = false;
15        let mut is_updating = false;
16
17        if let Some(editor) = self.editor.as_ref().and_then(|editor| editor.upgrade()) {
18            let editor = editor.read(cx);
19
20            include_warnings = editor.include_warnings;
21            has_stale_excerpts = !editor.paths_to_update.is_empty();
22            is_updating = editor.update_paths_tx.len() > 0
23                || editor
24                    .project
25                    .read(cx)
26                    .language_servers_running_disk_based_diagnostics()
27                    .next()
28                    .is_some();
29        }
30
31        let tooltip = if include_warnings {
32            "Exclude Warnings"
33        } else {
34            "Include Warnings"
35        };
36
37        h_flex()
38            .when(has_stale_excerpts, |div| {
39                div.child(
40                    IconButton::new("update-excerpts", IconName::Update)
41                        .icon_color(Color::Info)
42                        .disabled(is_updating)
43                        .tooltip(move |cx| Tooltip::text("Update excerpts", cx))
44                        .on_click(cx.listener(|this, _, cx| {
45                            if let Some(editor) =
46                                this.editor.as_ref().and_then(|editor| editor.upgrade())
47                            {
48                                editor.update(cx, |editor, _| {
49                                    editor.enqueue_update_stale_excerpts(None);
50                                });
51                            }
52                        })),
53                )
54            })
55            .child(
56                IconButton::new("toggle-warnings", IconName::ExclamationTriangle)
57                    .tooltip(move |cx| Tooltip::text(tooltip, cx))
58                    .on_click(cx.listener(|this, _, cx| {
59                        if let Some(editor) =
60                            this.editor.as_ref().and_then(|editor| editor.upgrade())
61                        {
62                            editor.update(cx, |editor, cx| {
63                                editor.toggle_warnings(&Default::default(), cx);
64                            });
65                        }
66                    })),
67            )
68    }
69}
70
71impl EventEmitter<ToolbarItemEvent> for ToolbarControls {}
72
73impl ToolbarItemView for ToolbarControls {
74    fn set_active_pane_item(
75        &mut self,
76        active_pane_item: Option<&dyn ItemHandle>,
77        _: &mut ViewContext<Self>,
78    ) -> ToolbarItemLocation {
79        if let Some(pane_item) = active_pane_item.as_ref() {
80            if let Some(editor) = pane_item.downcast::<ProjectDiagnosticsEditor>() {
81                self.editor = Some(editor.downgrade());
82                ToolbarItemLocation::PrimaryRight
83            } else {
84                ToolbarItemLocation::Hidden
85            }
86        } else {
87            ToolbarItemLocation::Hidden
88        }
89    }
90}
91
92impl ToolbarControls {
93    pub fn new() -> Self {
94        ToolbarControls { editor: None }
95    }
96}