Show terminal count in panel button

This commit is contained in:
Antonio Scandurra 2023-05-09 12:21:35 +02:00
parent 02066afb0e
commit e6be35c9a5
4 changed files with 33 additions and 25 deletions

View file

@ -1327,11 +1327,7 @@ impl Entity for ProjectPanel {
type Event = Event; type Event = Event;
} }
impl workspace::dock::Panel for ProjectPanel { impl workspace::dock::Panel for ProjectPanel {}
fn should_show_badge(&self, _: &AppContext) -> bool {
false
}
}
impl ClipboardEntry { impl ClipboardEntry {
fn is_cut(&self) -> bool { fn is_cut(&self) -> bool {

View file

@ -16,7 +16,7 @@ pub struct TerminalPanel {
project: ModelHandle<Project>, project: ModelHandle<Project>,
pane: ViewHandle<Pane>, pane: ViewHandle<Pane>,
workspace: WeakViewHandle<Workspace>, workspace: WeakViewHandle<Workspace>,
_subscription: Subscription, _subscriptions: Vec<Subscription>,
} }
impl TerminalPanel { impl TerminalPanel {
@ -37,12 +37,15 @@ impl TerminalPanel {
}); });
pane 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 { Self {
project: workspace.project().clone(), project: workspace.project().clone(),
pane, pane,
workspace: workspace.weak_handle(), 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 { fn should_close_on_event(&self, event: &Event, _: &AppContext) -> bool {
matches!(event, Event::Close) matches!(event, Event::Close)
} }
fn label(&self, cx: &AppContext) -> Option<String> {
let count = self.pane.read(cx).items_len();
if count == 0 {
None
} else {
Some(count.to_string())
}
}
} }

View file

@ -346,7 +346,6 @@ pub struct StatusBarPanelButtons {
pub group_bottom: ContainerStyle, pub group_bottom: ContainerStyle,
pub group_right: ContainerStyle, pub group_right: ContainerStyle,
pub button: Interactive<PanelButton>, pub button: Interactive<PanelButton>,
pub badge: ContainerStyle,
} }
#[derive(Deserialize, Default)] #[derive(Deserialize, Default)]

View file

@ -14,8 +14,8 @@ pub trait Panel: View {
fn should_close_on_event(&self, _: &Self::Event, _: &AppContext) -> bool { fn should_close_on_event(&self, _: &Self::Event, _: &AppContext) -> bool {
false false
} }
fn should_show_badge(&self, _: &AppContext) -> bool { fn label(&self, _: &AppContext) -> Option<String> {
false None
} }
fn contains_focused_view(&self, _: &AppContext) -> bool { fn contains_focused_view(&self, _: &AppContext) -> bool {
false false
@ -24,7 +24,7 @@ pub trait Panel: View {
pub trait PanelHandle { pub trait PanelHandle {
fn id(&self) -> usize; fn id(&self) -> usize;
fn should_show_badge(&self, cx: &WindowContext) -> bool; fn label(&self, cx: &WindowContext) -> Option<String>;
fn is_focused(&self, cx: &WindowContext) -> bool; fn is_focused(&self, cx: &WindowContext) -> bool;
fn as_any(&self) -> &AnyViewHandle; fn as_any(&self) -> &AnyViewHandle;
} }
@ -37,8 +37,8 @@ where
self.id() self.id()
} }
fn should_show_badge(&self, cx: &WindowContext) -> bool { fn label(&self, cx: &WindowContext) -> Option<String> {
self.read(cx).should_show_badge(cx) self.read(cx).label(cx)
} }
fn is_focused(&self, cx: &WindowContext) -> bool { fn is_focused(&self, cx: &WindowContext) -> bool {
@ -247,7 +247,6 @@ impl View for PanelButtons {
let theme = &theme.workspace.status_bar.panel_buttons; let theme = &theme.workspace.status_bar.panel_buttons;
let dock = self.dock.read(cx); let dock = self.dock.read(cx);
let item_style = theme.button.clone(); let item_style = theme.button.clone();
let badge_style = theme.badge;
let active_ix = dock.active_item_ix; let active_ix = dock.active_item_ix;
let is_open = dock.is_open; let is_open = dock.is_open;
let dock_position = dock.position; let dock_position = dock.position;
@ -274,23 +273,25 @@ impl View for PanelButtons {
MouseEventHandler::<Self, _>::new(ix, cx, |state, cx| { MouseEventHandler::<Self, _>::new(ix, cx, |state, cx| {
let is_active = is_open && ix == active_ix; let is_active = is_open && ix == active_ix;
let style = item_style.style_for(state, is_active); let style = item_style.style_for(state, is_active);
Stack::new() Flex::row()
.with_child(Svg::new(icon_path).with_color(style.icon_color)) .with_child(
.with_children(if !is_active && item_view.should_show_badge(cx) { 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( Some(
Empty::new() Label::new(label, style.label.text.clone())
.collapsed()
.contained() .contained()
.with_style(badge_style) .with_style(style.label.container)
.aligned() .aligned(),
.bottom()
.right(),
) )
} else { } else {
None None
}) })
.constrained() .constrained()
.with_width(style.icon_size)
.with_height(style.icon_size) .with_height(style.icon_size)
.contained() .contained()
.with_style(style.container) .with_style(style.container)