toolbar_controls.rs

  1use crate::ProjectDiagnosticsEditor;
  2use gpui::{Context, Entity, EventEmitter, ParentElement, Render, WeakEntity, Window};
  3use ui::prelude::*;
  4use ui::{IconButton, IconButtonShape, IconName, Tooltip};
  5use workspace::{ToolbarItemEvent, ToolbarItemLocation, ToolbarItemView, item::ItemHandle};
  6
  7pub struct ToolbarControls {
  8    editor: Option<WeakEntity<ProjectDiagnosticsEditor>>,
  9}
 10
 11impl Render for ToolbarControls {
 12    fn render(&mut self, _: &mut Window, cx: &mut Context<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.diagnostics() {
 18            let diagnostics = editor.read(cx);
 19            include_warnings = diagnostics.include_warnings;
 20            has_stale_excerpts = !diagnostics.paths_to_update.is_empty();
 21            is_updating = diagnostics.update_excerpts_task.is_some()
 22                || diagnostics
 23                    .project
 24                    .read(cx)
 25                    .language_servers_running_disk_based_diagnostics(cx)
 26                    .next()
 27                    .is_some();
 28        }
 29
 30        let tooltip = if include_warnings {
 31            "Exclude Warnings"
 32        } else {
 33            "Include Warnings"
 34        };
 35
 36        let warning_color = if include_warnings {
 37            Color::Warning
 38        } else {
 39            Color::Muted
 40        };
 41
 42        h_flex()
 43            .gap_1()
 44            .when(has_stale_excerpts, |div| {
 45                div.child(
 46                    IconButton::new("update-excerpts", IconName::Update)
 47                        .icon_color(Color::Info)
 48                        .shape(IconButtonShape::Square)
 49                        .disabled(is_updating)
 50                        .tooltip(Tooltip::text("Update excerpts"))
 51                        .on_click(cx.listener(|this, _, window, cx| {
 52                            if let Some(diagnostics) = this.diagnostics() {
 53                                diagnostics.update(cx, |diagnostics, cx| {
 54                                    diagnostics.update_all_excerpts(window, cx);
 55                                });
 56                            }
 57                        })),
 58                )
 59            })
 60            .child(
 61                IconButton::new("toggle-warnings", IconName::Warning)
 62                    .icon_color(warning_color)
 63                    .shape(IconButtonShape::Square)
 64                    .tooltip(Tooltip::text(tooltip))
 65                    .on_click(cx.listener(|this, _, window, cx| {
 66                        if let Some(editor) = this.diagnostics() {
 67                            editor.update(cx, |editor, cx| {
 68                                editor.toggle_warnings(&Default::default(), window, cx);
 69                            });
 70                        }
 71                    })),
 72            )
 73    }
 74}
 75
 76impl EventEmitter<ToolbarItemEvent> for ToolbarControls {}
 77
 78impl ToolbarItemView for ToolbarControls {
 79    fn set_active_pane_item(
 80        &mut self,
 81        active_pane_item: Option<&dyn ItemHandle>,
 82        _window: &mut Window,
 83        _: &mut Context<Self>,
 84    ) -> ToolbarItemLocation {
 85        if let Some(pane_item) = active_pane_item.as_ref() {
 86            if let Some(editor) = pane_item.downcast::<ProjectDiagnosticsEditor>() {
 87                self.editor = Some(editor.downgrade());
 88                ToolbarItemLocation::PrimaryRight
 89            } else {
 90                ToolbarItemLocation::Hidden
 91            }
 92        } else {
 93            ToolbarItemLocation::Hidden
 94        }
 95    }
 96}
 97
 98impl Default for ToolbarControls {
 99    fn default() -> Self {
100        Self::new()
101    }
102}
103
104impl ToolbarControls {
105    pub fn new() -> Self {
106        ToolbarControls { editor: None }
107    }
108
109    fn diagnostics(&self) -> Option<Entity<ProjectDiagnosticsEditor>> {
110        self.editor.as_ref()?.upgrade()
111    }
112}