Simplify constructing tab content that is purely textual (#14695)

This PR adds a streamlined way to consistently construct tab content for
items that only have textual content in the tabs.

The `Item` trait now has a new `tab_content_text` method that can be
used to return the textual content for the tab.

The `tab_content` method now has a default implementation that—unless
overridden—will construct a `Label` out of the text. This default
implementation also takes care of setting the label color based on the
active state of the tab, something that previously had to be repeated in
each `tab_content` implementation.

The majority of our tabs are now using `tab_content_text`.

Release Notes:

- N/A
This commit is contained in:
Marshall Bowers
2024-07-17 20:11:05 -04:00
committed by GitHub
parent ca2976559e
commit 2c8ead4423
11 changed files with 77 additions and 124 deletions
+25 -3
View File
@@ -31,7 +31,7 @@ use std::{
time::Duration,
};
use theme::Theme;
use ui::{Element as _, Icon};
use ui::{Color, Element as _, Icon, IntoElement, Label, LabelCommon};
use util::ResultExt;
pub const LEADER_UPDATE_THROTTLE: Duration = Duration::from_millis(200);
@@ -144,8 +144,30 @@ pub struct TabContentParams {
pub trait Item: FocusableView + EventEmitter<Self::Event> {
type Event;
fn tab_content(&self, _params: TabContentParams, _cx: &WindowContext) -> AnyElement {
gpui::Empty.into_any()
/// Returns the tab contents.
///
/// By default this returns a [`Label`] that displays that text from
/// `tab_content_text`.
fn tab_content(&self, params: TabContentParams, cx: &WindowContext) -> AnyElement {
let Some(text) = self.tab_content_text(cx) else {
return gpui::Empty.into_any();
};
let color = if params.selected {
Color::Default
} else {
Color::Muted
};
Label::new(text).color(color).into_any_element()
}
/// Returns the textual contents of the tab.
///
/// Use this if you don't need to customize the tab contents.
fn tab_content_text(&self, _cx: &WindowContext) -> Option<SharedString> {
None
}
fn tab_icon(&self, _cx: &WindowContext) -> Option<Icon> {
+4 -10
View File
@@ -1,5 +1,5 @@
use crate::{
item::{Item, ItemEvent, TabContentParams},
item::{Item, ItemEvent},
ItemNavHistory, WorkspaceId,
};
use anyhow::Result;
@@ -12,7 +12,7 @@ use gpui::{
WindowContext,
};
use std::sync::{Arc, Weak};
use ui::{prelude::*, Icon, IconName, Label};
use ui::{prelude::*, Icon, IconName};
pub enum Event {
Close,
@@ -97,14 +97,8 @@ impl Item for SharedScreen {
Some(Icon::new(IconName::Screen))
}
fn tab_content(&self, params: TabContentParams, _: &WindowContext<'_>) -> gpui::AnyElement {
Label::new(format!("{}'s screen", self.user.github_login))
.color(if params.selected {
Color::Default
} else {
Color::Muted
})
.into_any_element()
fn tab_content_text(&self, _cx: &WindowContext) -> Option<SharedString> {
Some(format!("{}'s screen", self.user.github_login).into())
}
fn telemetry_event_text(&self) -> Option<&'static str> {