Restructure status bar theme to style interactive elements more concisely
Introduce an `Interactive` wrapper type that allows themes to selectively override properties of items in a hovered or active state.
This commit is contained in:
@@ -35,6 +35,7 @@ const CONTEXT_LINE_COUNT: u32 = 1;
|
||||
|
||||
pub fn init(cx: &mut MutableAppContext) {
|
||||
cx.add_action(ProjectDiagnosticsEditor::deploy);
|
||||
items::init(cx);
|
||||
}
|
||||
|
||||
type Event = editor::Event;
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
use editor::Editor;
|
||||
use editor::{Editor, GoToNextDiagnostic};
|
||||
use gpui::{
|
||||
elements::*, platform::CursorStyle, serde_json, Entity, ModelHandle, RenderContext,
|
||||
Subscription, View, ViewContext, ViewHandle,
|
||||
elements::*, platform::CursorStyle, serde_json, Entity, ModelHandle, MutableAppContext,
|
||||
RenderContext, Subscription, View, ViewContext, ViewHandle, WeakViewHandle,
|
||||
};
|
||||
use language::Diagnostic;
|
||||
use project::Project;
|
||||
@@ -10,11 +10,16 @@ use workspace::StatusItemView;
|
||||
|
||||
pub struct DiagnosticIndicator {
|
||||
summary: project::DiagnosticSummary,
|
||||
active_editor: Option<WeakViewHandle<Editor>>,
|
||||
current_diagnostic: Option<Diagnostic>,
|
||||
check_in_progress: bool,
|
||||
_observe_active_editor: Option<Subscription>,
|
||||
}
|
||||
|
||||
pub fn init(cx: &mut MutableAppContext) {
|
||||
cx.add_action(DiagnosticIndicator::go_to_next_diagnostic);
|
||||
}
|
||||
|
||||
impl DiagnosticIndicator {
|
||||
pub fn new(project: &ModelHandle<Project>, cx: &mut ViewContext<Self>) -> Self {
|
||||
cx.subscribe(project, |this, project, event, cx| match event {
|
||||
@@ -36,11 +41,20 @@ impl DiagnosticIndicator {
|
||||
Self {
|
||||
summary: project.read(cx).diagnostic_summary(cx),
|
||||
check_in_progress: project.read(cx).is_running_disk_based_diagnostics(),
|
||||
active_editor: None,
|
||||
current_diagnostic: None,
|
||||
_observe_active_editor: None,
|
||||
}
|
||||
}
|
||||
|
||||
fn go_to_next_diagnostic(&mut self, _: &GoToNextDiagnostic, cx: &mut ViewContext<Self>) {
|
||||
if let Some(editor) = self.active_editor.as_ref().and_then(|e| e.upgrade(cx)) {
|
||||
editor.update(cx, |editor, cx| {
|
||||
editor.go_to_diagnostic(editor::Direction::Next, cx);
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
fn update(&mut self, editor: ViewHandle<Editor>, cx: &mut ViewContext<Self>) {
|
||||
let editor = editor.read(cx);
|
||||
let buffer = editor.buffer().read(cx);
|
||||
@@ -70,11 +84,12 @@ impl View for DiagnosticIndicator {
|
||||
}
|
||||
|
||||
fn render(&mut self, cx: &mut RenderContext<Self>) -> ElementBox {
|
||||
enum Tag {}
|
||||
enum Summary {}
|
||||
enum Message {}
|
||||
|
||||
let in_progress = self.check_in_progress;
|
||||
let mut element = Flex::row().with_child(
|
||||
MouseEventHandler::new::<Tag, _, _>(0, cx, |state, cx| {
|
||||
MouseEventHandler::new::<Summary, _, _>(0, cx, |state, cx| {
|
||||
let style = &cx
|
||||
.global::<Settings>()
|
||||
.theme
|
||||
@@ -83,23 +98,19 @@ impl View for DiagnosticIndicator {
|
||||
.diagnostics;
|
||||
|
||||
let summary_style = if self.summary.error_count > 0 {
|
||||
if state.hovered {
|
||||
&style.summary_error_hover
|
||||
} else {
|
||||
&style.summary_error
|
||||
}
|
||||
&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
|
||||
&style.summary_warning
|
||||
} else {
|
||||
&style.summary_ok
|
||||
};
|
||||
|
||||
let summary_style = if state.hovered {
|
||||
summary_style.hover()
|
||||
} else {
|
||||
&summary_style.default
|
||||
};
|
||||
|
||||
let mut summary_row = Flex::row();
|
||||
if self.summary.error_count > 0 {
|
||||
summary_row.add_children([
|
||||
@@ -169,24 +180,36 @@ impl View for DiagnosticIndicator {
|
||||
);
|
||||
|
||||
let style = &cx.global::<Settings>().theme.workspace.status_bar;
|
||||
let item_spacing = style.item_spacing;
|
||||
let message_style = &style.diagnostics.message;
|
||||
|
||||
if in_progress {
|
||||
element.add_child(
|
||||
Label::new("checking…".into(), style.diagnostics.message.text.clone())
|
||||
Label::new("checking…".into(), message_style.default.text.clone())
|
||||
.aligned()
|
||||
.contained()
|
||||
.with_margin_left(style.item_spacing)
|
||||
.with_margin_left(item_spacing)
|
||||
.boxed(),
|
||||
);
|
||||
} else if let Some(diagnostic) = &self.current_diagnostic {
|
||||
let message_style = message_style.clone();
|
||||
element.add_child(
|
||||
Label::new(
|
||||
diagnostic.message.split('\n').next().unwrap().to_string(),
|
||||
style.diagnostics.message.text.clone(),
|
||||
)
|
||||
.aligned()
|
||||
.contained()
|
||||
.with_margin_left(style.item_spacing)
|
||||
MouseEventHandler::new::<Message, _, _>(0, cx, |state, _| {
|
||||
Label::new(
|
||||
diagnostic.message.split('\n').next().unwrap().to_string(),
|
||||
if state.hovered {
|
||||
message_style.hover().text.clone()
|
||||
} else {
|
||||
message_style.default.text.clone()
|
||||
},
|
||||
)
|
||||
.aligned()
|
||||
.contained()
|
||||
.with_margin_left(item_spacing)
|
||||
.boxed()
|
||||
})
|
||||
.with_cursor_style(CursorStyle::PointingHand)
|
||||
.on_click(|cx| cx.dispatch_action(GoToNextDiagnostic))
|
||||
.boxed(),
|
||||
);
|
||||
}
|
||||
@@ -206,9 +229,11 @@ impl StatusItemView for DiagnosticIndicator {
|
||||
cx: &mut ViewContext<Self>,
|
||||
) {
|
||||
if let Some(editor) = active_pane_item.and_then(|item| item.downcast::<Editor>()) {
|
||||
self.active_editor = Some(editor.downgrade());
|
||||
self._observe_active_editor = Some(cx.observe(&editor, Self::update));
|
||||
self.update(editor, cx);
|
||||
} else {
|
||||
self.active_editor = None;
|
||||
self.current_diagnostic = None;
|
||||
self._observe_active_editor = None;
|
||||
}
|
||||
|
||||
+80
-15
@@ -6,7 +6,8 @@ use gpui::{
|
||||
fonts::{HighlightStyle, TextStyle},
|
||||
Border,
|
||||
};
|
||||
use serde::Deserialize;
|
||||
use serde::{de::DeserializeOwned, Deserialize};
|
||||
use serde_json::Value;
|
||||
use std::{collections::HashMap, sync::Arc};
|
||||
|
||||
pub use theme_registry::*;
|
||||
@@ -145,7 +146,7 @@ pub struct StatusBar {
|
||||
pub cursor_position: TextStyle,
|
||||
pub auto_update_progress_message: TextStyle,
|
||||
pub auto_update_done_message: TextStyle,
|
||||
pub lsp_status: StatusBarLspStatus,
|
||||
pub lsp_status: Interactive<StatusBarLspStatus>,
|
||||
pub sidebar_buttons: StatusBarSidebarButtons,
|
||||
pub diagnostics: StatusBarDiagnostics,
|
||||
}
|
||||
@@ -154,20 +155,15 @@ pub struct StatusBar {
|
||||
pub struct StatusBarSidebarButtons {
|
||||
pub group_left: ContainerStyle,
|
||||
pub group_right: ContainerStyle,
|
||||
pub item: SidebarItem,
|
||||
pub item_active: SidebarItem,
|
||||
pub item_hover: SidebarItem,
|
||||
pub item: Interactive<SidebarItem>,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Default)]
|
||||
pub struct StatusBarDiagnostics {
|
||||
pub message: ContainedText,
|
||||
pub summary_ok: ContainedText,
|
||||
pub summary_ok_hover: ContainedText,
|
||||
pub summary_warning: ContainedText,
|
||||
pub summary_warning_hover: ContainedText,
|
||||
pub summary_error: ContainedText,
|
||||
pub summary_error_hover: ContainedText,
|
||||
pub message: Interactive<ContainedText>,
|
||||
pub summary_ok: Interactive<ContainedText>,
|
||||
pub summary_warning: Interactive<ContainedText>,
|
||||
pub summary_error: Interactive<ContainedText>,
|
||||
pub icon_color_ok: Color,
|
||||
pub icon_color_error: Color,
|
||||
pub icon_color_warning: Color,
|
||||
@@ -182,13 +178,10 @@ pub struct StatusBarLspStatus {
|
||||
#[serde(flatten)]
|
||||
pub container: ContainerStyle,
|
||||
pub height: f32,
|
||||
pub container_hover: ContainerStyle,
|
||||
pub icon_spacing: f32,
|
||||
pub icon_color: Color,
|
||||
pub icon_color_hover: Color,
|
||||
pub icon_width: f32,
|
||||
pub message: TextStyle,
|
||||
pub message_hover: TextStyle,
|
||||
}
|
||||
|
||||
#[derive(Deserialize, Default)]
|
||||
@@ -421,6 +414,78 @@ pub struct FieldEditor {
|
||||
pub selection: SelectionStyle,
|
||||
}
|
||||
|
||||
#[derive(Default, Clone, Copy)]
|
||||
pub struct Interactive<T> {
|
||||
pub default: T,
|
||||
pub hover: Option<T>,
|
||||
pub active: Option<T>,
|
||||
}
|
||||
|
||||
impl<T> Interactive<T> {
|
||||
pub fn active(&self) -> &T {
|
||||
self.active.as_ref().unwrap_or(&self.default)
|
||||
}
|
||||
|
||||
pub fn hover(&self) -> &T {
|
||||
self.hover.as_ref().unwrap_or(&self.default)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de, T: DeserializeOwned> Deserialize<'de> for Interactive<T> {
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
where
|
||||
D: serde::Deserializer<'de>,
|
||||
{
|
||||
#[derive(Deserialize)]
|
||||
struct Helper {
|
||||
#[serde(flatten)]
|
||||
default: Value,
|
||||
hover: Option<Value>,
|
||||
active: Option<Value>,
|
||||
}
|
||||
|
||||
let json = Helper::deserialize(deserializer)?;
|
||||
|
||||
let hover = if let Some(mut hovered) = json.hover {
|
||||
if let Value::Object(hovered) = &mut hovered {
|
||||
if let Value::Object(default) = &json.default {
|
||||
for (key, value) in default {
|
||||
if !hovered.contains_key(key) {
|
||||
hovered.insert(key.clone(), value.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Some(serde_json::from_value::<T>(hovered).map_err(serde::de::Error::custom)?)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
let active = if let Some(mut active) = json.active {
|
||||
if let Value::Object(active) = &mut active {
|
||||
if let Value::Object(default) = &json.default {
|
||||
for (key, value) in default {
|
||||
if !active.contains_key(key) {
|
||||
active.insert(key.clone(), value.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Some(serde_json::from_value::<T>(active).map_err(serde::de::Error::custom)?)
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
let default = serde_json::from_value(json.default).map_err(serde::de::Error::custom)?;
|
||||
|
||||
Ok(Interactive {
|
||||
default,
|
||||
hover,
|
||||
active,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl Editor {
|
||||
pub fn replica_selection_style(&self, replica_id: u16) -> &SelectionStyle {
|
||||
let style_ix = replica_id as usize % (self.guest_selections.len() + 1);
|
||||
|
||||
@@ -175,17 +175,21 @@ impl View for LspStatus {
|
||||
}
|
||||
|
||||
let mut element = MouseEventHandler::new::<Self, _, _>(0, cx, |state, cx| {
|
||||
let hovered = state.hovered && handler.is_some();
|
||||
let theme = &cx.global::<Settings>().theme;
|
||||
let style = &theme.workspace.status_bar.lsp_status;
|
||||
let theme = &cx
|
||||
.global::<Settings>()
|
||||
.theme
|
||||
.workspace
|
||||
.status_bar
|
||||
.lsp_status;
|
||||
let style = if state.hovered && handler.is_some() {
|
||||
theme.hover.as_ref().unwrap_or(&theme.default)
|
||||
} else {
|
||||
&theme.default
|
||||
};
|
||||
Flex::row()
|
||||
.with_children(icon.map(|path| {
|
||||
Svg::new(path)
|
||||
.with_color(if hovered {
|
||||
style.icon_color_hover
|
||||
} else {
|
||||
style.icon_color
|
||||
})
|
||||
.with_color(style.icon_color)
|
||||
.constrained()
|
||||
.with_width(style.icon_width)
|
||||
.contained()
|
||||
@@ -193,26 +197,11 @@ impl View for LspStatus {
|
||||
.aligned()
|
||||
.named("warning-icon")
|
||||
}))
|
||||
.with_child(
|
||||
Label::new(
|
||||
message,
|
||||
if hovered {
|
||||
style.message_hover.clone()
|
||||
} else {
|
||||
style.message.clone()
|
||||
},
|
||||
)
|
||||
.aligned()
|
||||
.boxed(),
|
||||
)
|
||||
.with_child(Label::new(message, style.message.clone()).aligned().boxed())
|
||||
.constrained()
|
||||
.with_height(style.height)
|
||||
.contained()
|
||||
.with_style(if hovered {
|
||||
style.container_hover
|
||||
} else {
|
||||
style.container
|
||||
})
|
||||
.with_style(style.container)
|
||||
.aligned()
|
||||
.boxed()
|
||||
});
|
||||
|
||||
@@ -183,8 +183,6 @@ impl View for SidebarButtons {
|
||||
.sidebar_buttons;
|
||||
let sidebar = self.sidebar.read(cx);
|
||||
let item_style = theme.item;
|
||||
let hover_item_style = theme.item_hover;
|
||||
let active_item_style = theme.item_active;
|
||||
let active_ix = sidebar.active_item_ix;
|
||||
let side = sidebar.side;
|
||||
let group_style = match side {
|
||||
@@ -196,11 +194,11 @@ impl View for SidebarButtons {
|
||||
.with_children(items.iter().enumerate().map(|(ix, item)| {
|
||||
MouseEventHandler::new::<Self, _, _>(ix, cx, move |state, _| {
|
||||
let style = if Some(ix) == active_ix {
|
||||
active_item_style
|
||||
item_style.active()
|
||||
} else if state.hovered {
|
||||
hover_item_style
|
||||
item_style.hover()
|
||||
} else {
|
||||
item_style
|
||||
&item_style.default
|
||||
};
|
||||
Svg::new(item.icon_path)
|
||||
.with_color(style.icon_color)
|
||||
|
||||
Reference in New Issue
Block a user