From 24d937474444f391961724dbae07ce7d8ff887b6 Mon Sep 17 00:00:00 2001 From: Marshall Bowers Date: Thu, 18 Jul 2024 09:27:05 -0400 Subject: [PATCH] Add `text_color` helper for tab contents (#14737) This PR adds a `text_color` method to `TabContentParams` to more easily compute the text color to be used for tab contents. This consolidates a number of conditionals that were scattered all over the place to give us a singular source of truth for these colors. Release Notes: - N/A --- crates/collab_ui/src/channel_view.rs | 6 +--- crates/diagnostics/src/diagnostics.rs | 28 ++++++------------- crates/diagnostics/src/grouped_diagnostics.rs | 28 ++++++------------- crates/image_viewer/src/image_viewer.rs | 6 +--- crates/terminal_view/src/terminal_view.rs | 6 +--- crates/workspace/src/item.rs | 21 +++++++++----- 6 files changed, 35 insertions(+), 60 deletions(-) diff --git a/crates/collab_ui/src/channel_view.rs b/crates/collab_ui/src/channel_view.rs index 251b3ea277..c77d4b5782 100644 --- a/crates/collab_ui/src/channel_view.rs +++ b/crates/collab_ui/src/channel_view.rs @@ -417,11 +417,7 @@ impl Item for ChannelView { .gap_2() .child( Label::new(channel_name) - .color(if params.selected { - Color::Default - } else { - Color::Muted - }) + .color(params.text_color()) .italic(params.preview), ) .when_some(status, |element, status| { diff --git a/crates/diagnostics/src/diagnostics.rs b/crates/diagnostics/src/diagnostics.rs index ecace7c076..377fa50700 100644 --- a/crates/diagnostics/src/diagnostics.rs +++ b/crates/diagnostics/src/diagnostics.rs @@ -649,11 +649,7 @@ impl Item for ProjectDiagnosticsEditor { fn tab_content(&self, params: TabContentParams, _: &WindowContext) -> AnyElement { if self.summary.error_count == 0 && self.summary.warning_count == 0 { Label::new("No problems") - .color(if params.selected { - Color::Default - } else { - Color::Muted - }) + .color(params.text_color()) .into_any_element() } else { h_flex() @@ -663,13 +659,10 @@ impl Item for ProjectDiagnosticsEditor { h_flex() .gap_1() .child(Icon::new(IconName::XCircle).color(Color::Error)) - .child(Label::new(self.summary.error_count.to_string()).color( - if params.selected { - Color::Default - } else { - Color::Muted - }, - )), + .child( + Label::new(self.summary.error_count.to_string()) + .color(params.text_color()), + ), ) }) .when(self.summary.warning_count > 0, |then| { @@ -677,13 +670,10 @@ impl Item for ProjectDiagnosticsEditor { h_flex() .gap_1() .child(Icon::new(IconName::ExclamationTriangle).color(Color::Warning)) - .child(Label::new(self.summary.warning_count.to_string()).color( - if params.selected { - Color::Default - } else { - Color::Muted - }, - )), + .child( + Label::new(self.summary.warning_count.to_string()) + .color(params.text_color()), + ), ) }) .into_any_element() diff --git a/crates/diagnostics/src/grouped_diagnostics.rs b/crates/diagnostics/src/grouped_diagnostics.rs index 043e0f5825..e38298312e 100644 --- a/crates/diagnostics/src/grouped_diagnostics.rs +++ b/crates/diagnostics/src/grouped_diagnostics.rs @@ -466,11 +466,7 @@ impl Item for GroupedDiagnosticsEditor { fn tab_content(&self, params: TabContentParams, _: &WindowContext) -> AnyElement { if self.summary.error_count == 0 && self.summary.warning_count == 0 { Label::new("No problems") - .color(if params.selected { - Color::Default - } else { - Color::Muted - }) + .color(params.text_color()) .into_any_element() } else { h_flex() @@ -480,13 +476,10 @@ impl Item for GroupedDiagnosticsEditor { h_flex() .gap_1() .child(Icon::new(IconName::XCircle).color(Color::Error)) - .child(Label::new(self.summary.error_count.to_string()).color( - if params.selected { - Color::Default - } else { - Color::Muted - }, - )), + .child( + Label::new(self.summary.error_count.to_string()) + .color(params.text_color()), + ), ) }) .when(self.summary.warning_count > 0, |then| { @@ -494,13 +487,10 @@ impl Item for GroupedDiagnosticsEditor { h_flex() .gap_1() .child(Icon::new(IconName::ExclamationTriangle).color(Color::Warning)) - .child(Label::new(self.summary.warning_count.to_string()).color( - if params.selected { - Color::Default - } else { - Color::Muted - }, - )), + .child( + Label::new(self.summary.warning_count.to_string()) + .color(params.text_color()), + ), ) }) .into_any_element() diff --git a/crates/image_viewer/src/image_viewer.rs b/crates/image_viewer/src/image_viewer.rs index d654141a11..b43dabf55f 100644 --- a/crates/image_viewer/src/image_viewer.rs +++ b/crates/image_viewer/src/image_viewer.rs @@ -80,11 +80,7 @@ impl Item for ImageView { .to_string(); Label::new(title) .single_line() - .color(if params.selected { - Color::Default - } else { - Color::Muted - }) + .color(params.text_color()) .italic(params.preview) .into_any_element() } diff --git a/crates/terminal_view/src/terminal_view.rs b/crates/terminal_view/src/terminal_view.rs index 2bf633cf2f..fe8cea217e 100644 --- a/crates/terminal_view/src/terminal_view.rs +++ b/crates/terminal_view/src/terminal_view.rs @@ -994,11 +994,7 @@ impl Item for TerminalView { ) }), ) - .child(Label::new(title).color(if params.selected { - Color::Default - } else { - Color::Muted - })) + .child(Label::new(title).color(params.text_color())) .into_any() } diff --git a/crates/workspace/src/item.rs b/crates/workspace/src/item.rs index 149e949568..cfa3b2fe2d 100644 --- a/crates/workspace/src/item.rs +++ b/crates/workspace/src/item.rs @@ -142,6 +142,17 @@ pub struct TabContentParams { pub preview: bool, } +impl TabContentParams { + /// Returns the text color to be used for the tab content. + pub fn text_color(&self) -> Color { + if self.selected { + Color::Default + } else { + Color::Muted + } + } +} + pub trait Item: FocusableView + EventEmitter { type Event; @@ -154,13 +165,9 @@ pub trait Item: FocusableView + EventEmitter { return gpui::Empty.into_any(); }; - let color = if params.selected { - Color::Default - } else { - Color::Muted - }; - - Label::new(text).color(color).into_any_element() + Label::new(text) + .color(params.text_color()) + .into_any_element() } /// Returns the textual contents of the tab.