toolbar_controls.rs

 1use crate::ProjectDiagnosticsEditor;
 2use gpui::{div, 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 include_warnings = self
14            .editor
15            .as_ref()
16            .and_then(|editor| editor.upgrade())
17            .map(|editor| editor.read(cx).include_warnings)
18            .unwrap_or(false);
19
20        let tooltip = if include_warnings {
21            "Exclude Warnings"
22        } else {
23            "Include Warnings"
24        };
25
26        div().child(
27            IconButton::new("toggle-warnings", IconName::ExclamationTriangle)
28                .tooltip(move |cx| Tooltip::text(tooltip, cx))
29                .on_click(cx.listener(|this, _, cx| {
30                    if let Some(editor) = this.editor.as_ref().and_then(|editor| editor.upgrade()) {
31                        editor.update(cx, |editor, cx| {
32                            editor.toggle_warnings(&Default::default(), cx);
33                        });
34                    }
35                })),
36        )
37    }
38}
39
40impl EventEmitter<ToolbarItemEvent> for ToolbarControls {}
41
42impl ToolbarItemView for ToolbarControls {
43    fn set_active_pane_item(
44        &mut self,
45        active_pane_item: Option<&dyn ItemHandle>,
46        _: &mut ViewContext<Self>,
47    ) -> ToolbarItemLocation {
48        if let Some(pane_item) = active_pane_item.as_ref() {
49            if let Some(editor) = pane_item.downcast::<ProjectDiagnosticsEditor>() {
50                self.editor = Some(editor.downgrade());
51                ToolbarItemLocation::PrimaryRight
52            } else {
53                ToolbarItemLocation::Hidden
54            }
55        } else {
56            ToolbarItemLocation::Hidden
57        }
58    }
59}
60
61impl ToolbarControls {
62    pub fn new() -> Self {
63        ToolbarControls { editor: None }
64    }
65}