From e6be35c9a54d498e0c21cc2a54fc9f7076cc1e20 Mon Sep 17 00:00:00 2001 From: Antonio Scandurra Date: Tue, 9 May 2023 12:21:35 +0200 Subject: [PATCH] Show terminal count in panel button --- crates/project_panel/src/project_panel.rs | 6 +--- crates/terminal_view/src/terminal_panel.rs | 18 ++++++++++-- crates/theme/src/theme.rs | 1 - crates/workspace/src/dock.rs | 33 +++++++++++----------- 4 files changed, 33 insertions(+), 25 deletions(-) diff --git a/crates/project_panel/src/project_panel.rs b/crates/project_panel/src/project_panel.rs index 35ab314f3a..cecf6c114f 100644 --- a/crates/project_panel/src/project_panel.rs +++ b/crates/project_panel/src/project_panel.rs @@ -1327,11 +1327,7 @@ impl Entity for ProjectPanel { type Event = Event; } -impl workspace::dock::Panel for ProjectPanel { - fn should_show_badge(&self, _: &AppContext) -> bool { - false - } -} +impl workspace::dock::Panel for ProjectPanel {} impl ClipboardEntry { fn is_cut(&self) -> bool { diff --git a/crates/terminal_view/src/terminal_panel.rs b/crates/terminal_view/src/terminal_panel.rs index 7d715019c3..ce70f08391 100644 --- a/crates/terminal_view/src/terminal_panel.rs +++ b/crates/terminal_view/src/terminal_panel.rs @@ -16,7 +16,7 @@ pub struct TerminalPanel { project: ModelHandle, pane: ViewHandle, workspace: WeakViewHandle, - _subscription: Subscription, + _subscriptions: Vec, } impl TerminalPanel { @@ -37,12 +37,15 @@ impl TerminalPanel { }); pane }); - let subscription = cx.subscribe(&pane, Self::handle_pane_event); + let subscriptions = vec![ + cx.observe(&pane, |_, _, cx| cx.notify()), + cx.subscribe(&pane, Self::handle_pane_event), + ]; Self { project: workspace.project().clone(), pane, workspace: workspace.weak_handle(), - _subscription: subscription, + _subscriptions: subscriptions, } } @@ -108,4 +111,13 @@ impl Panel for TerminalPanel { fn should_close_on_event(&self, event: &Event, _: &AppContext) -> bool { matches!(event, Event::Close) } + + fn label(&self, cx: &AppContext) -> Option { + let count = self.pane.read(cx).items_len(); + if count == 0 { + None + } else { + Some(count.to_string()) + } + } } diff --git a/crates/theme/src/theme.rs b/crates/theme/src/theme.rs index 67d07a3998..2d4c520127 100644 --- a/crates/theme/src/theme.rs +++ b/crates/theme/src/theme.rs @@ -346,7 +346,6 @@ pub struct StatusBarPanelButtons { pub group_bottom: ContainerStyle, pub group_right: ContainerStyle, pub button: Interactive, - pub badge: ContainerStyle, } #[derive(Deserialize, Default)] diff --git a/crates/workspace/src/dock.rs b/crates/workspace/src/dock.rs index 3acda44297..57d18fd368 100644 --- a/crates/workspace/src/dock.rs +++ b/crates/workspace/src/dock.rs @@ -14,8 +14,8 @@ pub trait Panel: View { fn should_close_on_event(&self, _: &Self::Event, _: &AppContext) -> bool { false } - fn should_show_badge(&self, _: &AppContext) -> bool { - false + fn label(&self, _: &AppContext) -> Option { + None } fn contains_focused_view(&self, _: &AppContext) -> bool { false @@ -24,7 +24,7 @@ pub trait Panel: View { pub trait PanelHandle { fn id(&self) -> usize; - fn should_show_badge(&self, cx: &WindowContext) -> bool; + fn label(&self, cx: &WindowContext) -> Option; fn is_focused(&self, cx: &WindowContext) -> bool; fn as_any(&self) -> &AnyViewHandle; } @@ -37,8 +37,8 @@ where self.id() } - fn should_show_badge(&self, cx: &WindowContext) -> bool { - self.read(cx).should_show_badge(cx) + fn label(&self, cx: &WindowContext) -> Option { + self.read(cx).label(cx) } fn is_focused(&self, cx: &WindowContext) -> bool { @@ -247,7 +247,6 @@ impl View for PanelButtons { let theme = &theme.workspace.status_bar.panel_buttons; let dock = self.dock.read(cx); let item_style = theme.button.clone(); - let badge_style = theme.badge; let active_ix = dock.active_item_ix; let is_open = dock.is_open; let dock_position = dock.position; @@ -274,23 +273,25 @@ impl View for PanelButtons { MouseEventHandler::::new(ix, cx, |state, cx| { let is_active = is_open && ix == active_ix; let style = item_style.style_for(state, is_active); - Stack::new() - .with_child(Svg::new(icon_path).with_color(style.icon_color)) - .with_children(if !is_active && item_view.should_show_badge(cx) { + Flex::row() + .with_child( + Svg::new(icon_path) + .with_color(style.icon_color) + .constrained() + .with_width(style.icon_size) + .aligned(), + ) + .with_children(if let Some(label) = item_view.label(cx) { Some( - Empty::new() - .collapsed() + Label::new(label, style.label.text.clone()) .contained() - .with_style(badge_style) - .aligned() - .bottom() - .right(), + .with_style(style.label.container) + .aligned(), ) } else { None }) .constrained() - .with_width(style.icon_size) .with_height(style.icon_size) .contained() .with_style(style.container)