Automatically close terminal dock when the last terminal was closed

This commit is contained in:
Antonio Scandurra 2023-05-09 10:17:35 +02:00
parent 916612caf1
commit 506f978c41
3 changed files with 85 additions and 27 deletions

View file

@ -8,7 +8,10 @@ use settings::Settings;
use std::rc::Rc;
pub trait Panel: View {
fn should_activate_item_on_event(&self, _: &Self::Event, _: &AppContext) -> bool {
fn should_activate_on_event(&self, _: &Self::Event, _: &AppContext) -> bool {
false
}
fn should_close_on_event(&self, _: &Self::Event, _: &AppContext) -> bool {
false
}
fn should_show_badge(&self, _: &AppContext) -> bool {
@ -53,6 +56,10 @@ impl From<&dyn PanelHandle> for AnyViewHandle {
}
}
pub enum Event {
Close,
}
pub struct Dock {
position: DockPosition,
items: Vec<Item>,
@ -138,7 +145,7 @@ impl Dock {
let subscriptions = [
cx.observe(&view, |_, _, cx| cx.notify()),
cx.subscribe(&view, |this, view, event, cx| {
if view.read(cx).should_activate_item_on_event(event, cx) {
if view.read(cx).should_activate_on_event(event, cx) {
if let Some(ix) = this
.items
.iter()
@ -146,6 +153,8 @@ impl Dock {
{
this.activate_item(ix, cx);
}
} else if view.read(cx).should_close_on_event(event, cx) {
cx.emit(Event::Close);
}
}),
];
@ -183,7 +192,7 @@ impl Dock {
}
impl Entity for Dock {
type Event = ();
type Event = Event;
}
impl View for Dock {

View file

@ -461,7 +461,7 @@ pub struct Workspace {
leader_updates_tx: mpsc::UnboundedSender<(PeerId, proto::UpdateFollowers)>,
database_id: WorkspaceId,
app_state: Arc<AppState>,
_window_subscriptions: [Subscription; 3],
_subscriptions: Vec<Subscription>,
_apply_leader_updates: Task<Result<()>>,
_observe_current_user: Task<Result<()>>,
}
@ -592,7 +592,7 @@ impl Workspace {
active_call = Some((call, subscriptions));
}
let subscriptions = [
let subscriptions = vec![
cx.observe_fullscreen(|_, _, cx| cx.notify()),
cx.observe_window_activation(Self::on_window_activation_changed),
cx.observe_window_bounds(move |_, mut bounds, display, cx| {
@ -612,6 +612,9 @@ impl Workspace {
.spawn(DB.set_window_bounds(workspace_id, bounds, display))
.detach_and_log_err(cx);
}),
Self::register_dock(&left_dock, cx),
Self::register_dock(&bottom_dock, cx),
Self::register_dock(&right_dock, cx),
];
let mut this = Workspace {
@ -640,7 +643,7 @@ impl Workspace {
_observe_current_user,
_apply_leader_updates,
leader_updates_tx,
_window_subscriptions: subscriptions,
_subscriptions: subscriptions,
};
this.project_remote_id_changed(project.read(cx).remote_id(), cx);
cx.defer(|this, cx| this.update_window_title(cx));
@ -1316,6 +1319,26 @@ impl Workspace {
}
}
fn register_dock(dock: &ViewHandle<Dock>, cx: &mut ViewContext<Self>) -> Subscription {
cx.subscribe(dock, Self::handle_dock_event)
}
fn handle_dock_event(
&mut self,
dock: ViewHandle<Dock>,
event: &dock::Event,
cx: &mut ViewContext<Self>,
) {
match event {
dock::Event::Close => {
dock.update(cx, |dock, cx| dock.set_open(false, cx));
self.serialize_workspace(cx);
cx.focus_self();
cx.notify();
}
}
}
pub fn toggle_dock(&mut self, dock_side: DockPosition, cx: &mut ViewContext<Self>) {
let dock = match dock_side {
DockPosition::Left => &mut self.left_dock,