toolbar_controls.rs

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