Combine diagnostic status w/ diagnostic message indicators

Improve the indicator styling
This commit is contained in:
Max Brunsfeld
2022-04-27 14:25:39 -07:00
parent 9e87be722e
commit a3700e0dd8
12 changed files with 366 additions and 431 deletions
+159 -97
View File
@@ -1,29 +1,33 @@
use editor::Editor;
use gpui::{
elements::*, platform::CursorStyle, serde_json, Entity, ModelHandle, RenderContext, View,
ViewContext,
elements::*, platform::CursorStyle, serde_json, Entity, ModelHandle, RenderContext,
Subscription, View, ViewContext, ViewHandle,
};
use language::Diagnostic;
use project::Project;
use settings::Settings;
use workspace::StatusItemView;
pub struct DiagnosticSummary {
pub struct DiagnosticIndicator {
summary: project::DiagnosticSummary,
in_progress: bool,
current_diagnostic: Option<Diagnostic>,
check_in_progress: bool,
_observe_active_editor: Option<Subscription>,
}
impl DiagnosticSummary {
impl DiagnosticIndicator {
pub fn new(project: &ModelHandle<Project>, cx: &mut ViewContext<Self>) -> Self {
cx.subscribe(project, |this, project, event, cx| match event {
project::Event::DiskBasedDiagnosticsUpdated => {
cx.notify();
}
project::Event::DiskBasedDiagnosticsStarted => {
this.in_progress = true;
this.check_in_progress = true;
cx.notify();
}
project::Event::DiskBasedDiagnosticsFinished => {
this.summary = project.read(cx).diagnostic_summary(cx);
this.in_progress = false;
this.check_in_progress = false;
cx.notify();
}
_ => {}
@@ -31,113 +35,163 @@ impl DiagnosticSummary {
.detach();
Self {
summary: project.read(cx).diagnostic_summary(cx),
in_progress: project.read(cx).is_running_disk_based_diagnostics(),
check_in_progress: project.read(cx).is_running_disk_based_diagnostics(),
current_diagnostic: None,
_observe_active_editor: None,
}
}
fn update(&mut self, editor: ViewHandle<Editor>, cx: &mut ViewContext<Self>) {
let editor = editor.read(cx);
let buffer = editor.buffer().read(cx);
let cursor_position = editor
.newest_selection_with_snapshot::<usize>(&buffer.read(cx))
.head();
let new_diagnostic = buffer
.read(cx)
.diagnostics_in_range::<_, usize>(cursor_position..cursor_position, false)
.filter(|entry| !entry.range.is_empty())
.min_by_key(|entry| (entry.diagnostic.severity, entry.range.len()))
.map(|entry| entry.diagnostic);
if new_diagnostic != self.current_diagnostic {
self.current_diagnostic = new_diagnostic;
cx.notify();
}
}
}
impl Entity for DiagnosticSummary {
impl Entity for DiagnosticIndicator {
type Event = ();
}
impl View for DiagnosticSummary {
impl View for DiagnosticIndicator {
fn ui_name() -> &'static str {
"DiagnosticSummary"
"DiagnosticIndicator"
}
fn render(&mut self, cx: &mut RenderContext<Self>) -> ElementBox {
enum Tag {}
let in_progress = self.in_progress;
MouseEventHandler::new::<Tag, _, _>(0, cx, |state, cx| {
let style = &cx
.global::<Settings>()
.theme
.workspace
.status_bar
.diagnostics;
let summary_style = if self.summary.error_count > 0 {
if state.hovered {
&style.summary_error_hover
let in_progress = self.check_in_progress;
let mut element = Flex::row().with_child(
MouseEventHandler::new::<Tag, _, _>(0, cx, |state, cx| {
let style = &cx
.global::<Settings>()
.theme
.workspace
.status_bar
.diagnostics;
let summary_style = if self.summary.error_count > 0 {
if state.hovered {
&style.summary_error_hover
} else {
&style.summary_error
}
} else if self.summary.warning_count > 0 {
if state.hovered {
&style.summary_warning_hover
} else {
&style.summary_warning
}
} else if state.hovered {
&style.summary_ok_hover
} else {
&style.summary_error
&style.summary_ok
};
let mut summary_row = Flex::row();
if self.summary.error_count > 0 {
summary_row.add_children([
Svg::new("icons/error-solid-14.svg")
.with_color(style.icon_color_error)
.constrained()
.with_width(style.icon_width)
.aligned()
.contained()
.with_margin_right(style.icon_spacing)
.named("error-icon"),
Label::new(
self.summary.error_count.to_string(),
summary_style.text.clone(),
)
.aligned()
.boxed(),
]);
}
} else if self.summary.warning_count > 0 {
if state.hovered {
&style.summary_warning_hover
} else {
&style.summary_warning
if self.summary.warning_count > 0 {
summary_row.add_children([
Svg::new("icons/warning-solid-14.svg")
.with_color(style.icon_color_warning)
.constrained()
.with_width(style.icon_width)
.aligned()
.contained()
.with_margin_right(style.icon_spacing)
.with_margin_left(if self.summary.error_count > 0 {
style.summary_spacing
} else {
0.
})
.named("warning-icon"),
Label::new(
self.summary.warning_count.to_string(),
summary_style.text.clone(),
)
.aligned()
.boxed(),
]);
}
} else if state.hovered {
&style.summary_ok_hover
} else {
&style.summary_ok
};
let mut row = Flex::row();
if self.summary.error_count > 0 {
row.add_children([
Svg::new("icons/error-solid-14.svg")
.with_color(style.icon_color_error)
.constrained()
.with_width(style.icon_width)
.aligned()
.contained()
.with_margin_right(style.icon_spacing)
.named("error-icon"),
Label::new(
self.summary.error_count.to_string(),
summary_style.text.clone(),
)
if self.summary.error_count == 0 && self.summary.warning_count == 0 {
summary_row.add_child(
Svg::new("icons/no-error-solid-14.svg")
.with_color(style.icon_color_ok)
.constrained()
.with_width(style.icon_width)
.aligned()
.named("ok-icon"),
);
}
summary_row
.constrained()
.with_height(style.height)
.contained()
.with_style(summary_style.container)
.boxed()
})
.with_cursor_style(CursorStyle::PointingHand)
.on_click(|cx| cx.dispatch_action(crate::Deploy))
.aligned()
.boxed(),
);
let style = &cx.global::<Settings>().theme.workspace.status_bar;
if in_progress {
element.add_child(
Label::new("checking…".into(), style.diagnostics.message.text.clone())
.aligned()
.contained()
.with_margin_left(style.item_spacing)
.boxed(),
]);
}
if self.summary.warning_count > 0 {
row.add_children([
Svg::new("icons/warning-solid-14.svg")
.with_color(style.icon_color_warning)
.constrained()
.with_width(style.icon_width)
.aligned()
.contained()
.with_margin_right(style.icon_spacing)
.with_margin_left(if self.summary.error_count > 0 {
style.summary_spacing
} else {
0.
})
.named("warning-icon"),
Label::new(
self.summary.warning_count.to_string(),
summary_style.text.clone(),
)
.aligned()
.boxed(),
]);
}
if self.summary.error_count == 0 && self.summary.warning_count == 0 {
row.add_child(
Svg::new("icons/no-error-solid-14.svg")
.with_color(style.icon_color_ok)
.constrained()
.with_width(style.icon_width)
.aligned()
.named("ok-icon"),
);
}
row.constrained()
.with_height(style.height)
);
} else if let Some(diagnostic) = &self.current_diagnostic {
element.add_child(
Label::new(
diagnostic.message.split('\n').next().unwrap().to_string(),
style.diagnostics.message.text.clone(),
)
.aligned()
.contained()
.with_style(summary_style.container)
.boxed()
})
.with_cursor_style(CursorStyle::PointingHand)
.on_click(|cx| cx.dispatch_action(crate::Deploy))
.boxed()
.with_margin_left(style.item_spacing)
.boxed(),
);
}
element.named("diagnostic indicator")
}
fn debug_json(&self, _: &gpui::AppContext) -> serde_json::Value {
@@ -145,11 +199,19 @@ impl View for DiagnosticSummary {
}
}
impl StatusItemView for DiagnosticSummary {
impl StatusItemView for DiagnosticIndicator {
fn set_active_pane_item(
&mut self,
_: Option<&dyn workspace::ItemHandle>,
_: &mut ViewContext<Self>,
active_pane_item: Option<&dyn workspace::ItemHandle>,
cx: &mut ViewContext<Self>,
) {
if let Some(editor) = active_pane_item.and_then(|item| item.downcast::<Editor>()) {
self._observe_active_editor = Some(cx.observe(&editor, Self::update));
self.update(editor, cx);
} else {
self.current_diagnostic = None;
self._observe_active_editor = None;
}
cx.notify();
}
}
+1 -73
View File
@@ -5,7 +5,7 @@ use gpui::{
elements::*, geometry::vector::vec2f, AppContext, Entity, ModelHandle, MutableAppContext,
RenderContext, Subscription, Task, View, ViewContext, ViewHandle,
};
use language::{Bias, Buffer, Diagnostic, File as _, SelectionGoal};
use language::{Bias, Buffer, File as _, SelectionGoal};
use project::{File, Project, ProjectEntryId, ProjectPath};
use rpc::proto::{self, update_view};
use settings::Settings;
@@ -505,75 +505,3 @@ impl StatusItemView for CursorPosition {
cx.notify();
}
}
pub struct DiagnosticMessage {
diagnostic: Option<Diagnostic>,
_observe_active_editor: Option<Subscription>,
}
impl DiagnosticMessage {
pub fn new() -> Self {
Self {
diagnostic: None,
_observe_active_editor: None,
}
}
fn update(&mut self, editor: ViewHandle<Editor>, cx: &mut ViewContext<Self>) {
let editor = editor.read(cx);
let buffer = editor.buffer().read(cx);
let cursor_position = editor
.newest_selection_with_snapshot::<usize>(&buffer.read(cx))
.head();
let new_diagnostic = buffer
.read(cx)
.diagnostics_in_range::<_, usize>(cursor_position..cursor_position, false)
.filter(|entry| !entry.range.is_empty())
.min_by_key(|entry| (entry.diagnostic.severity, entry.range.len()))
.map(|entry| entry.diagnostic);
if new_diagnostic != self.diagnostic {
self.diagnostic = new_diagnostic;
cx.notify();
}
}
}
impl Entity for DiagnosticMessage {
type Event = ();
}
impl View for DiagnosticMessage {
fn ui_name() -> &'static str {
"DiagnosticMessage"
}
fn render(&mut self, cx: &mut RenderContext<Self>) -> ElementBox {
if let Some(diagnostic) = &self.diagnostic {
let theme = &cx.global::<Settings>().theme.workspace.status_bar;
Label::new(
diagnostic.message.split('\n').next().unwrap().to_string(),
theme.diagnostic_message.clone(),
)
.boxed()
} else {
Empty::new().boxed()
}
}
}
impl StatusItemView for DiagnosticMessage {
fn set_active_pane_item(
&mut self,
active_pane_item: Option<&dyn ItemHandle>,
cx: &mut ViewContext<Self>,
) {
if let Some(editor) = active_pane_item.and_then(|item| item.downcast::<Editor>()) {
self._observe_active_editor = Some(cx.observe(&editor, Self::update));
self.update(editor, cx);
} else {
self.diagnostic = Default::default();
self._observe_active_editor = None;
}
cx.notify();
}
}
+1 -3
View File
@@ -180,9 +180,8 @@ pub fn build_workspace(
sidebar.add_item("icons/contacts-solid-14.svg", contact_panel.into(), cx)
});
let diagnostic_message = cx.add_view(|_| editor::items::DiagnosticMessage::new());
let diagnostic_summary =
cx.add_view(|cx| diagnostics::items::DiagnosticSummary::new(workspace.project(), cx));
cx.add_view(|cx| diagnostics::items::DiagnosticIndicator::new(workspace.project(), cx));
let lsp_status = cx.add_view(|cx| {
workspace::lsp_status::LspStatus::new(workspace.project(), app_state.languages.clone(), cx)
});
@@ -190,7 +189,6 @@ pub fn build_workspace(
let auto_update = cx.add_view(|cx| auto_update::AutoUpdateIndicator::new(cx));
workspace.status_bar().update(cx, |status_bar, cx| {
status_bar.add_left_item(diagnostic_summary, cx);
status_bar.add_left_item(diagnostic_message, cx);
status_bar.add_left_item(lsp_status, cx);
status_bar.add_right_item(cursor_position, cx);
status_bar.add_right_item(auto_update, cx);