fix(panes): focus change when closing and between tabs (#1966)

* fix(panes): do not forget pane focus when switching tabs or closing panes

* style(fmt): rustfmt

* fix(tests): e2e snapshots
This commit is contained in:
Aram Drevekenin 2022-11-23 18:14:24 +01:00 committed by GitHub
parent 00fedafa38
commit f8fbd8a138
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 52 additions and 18 deletions

View file

@ -1,11 +1,11 @@
---
source: src/tests/e2e/cases.rs
assertion_line: 948
assertion_line: 952
expression: last_snapshot
---
Zellij (e2e-test)  Tab #1 
┌ Pane #1 ─────────────────────────────────────────────────┐┌ Pane #2 ─────────────────────────────────────────────────┐
│$ █ ││$ I am some text
│$ ││$ I am some text█
│ ││ │
│ ││ │
│ ││ │

View file

@ -1,11 +1,11 @@
---
source: src/tests/e2e/cases.rs
assertion_line: 1308
assertion_line: 1312
expression: second_runner_snapshot
---
Zellij (mirrored_sessions)  Tab #1  Tab #2 
┌ Pane #1 ─────────────────────────────────────────────────┐┌ Pane #2 ─────────────────────────────────────────────────┐
│$ █ ││$
│$ ││$ █
│ ││ │
│ ││ │
│ ││ │

View file

@ -1,11 +1,11 @@
---
source: src/tests/e2e/cases.rs
assertion_line: 1342
assertion_line: 1311
expression: first_runner_snapshot
---
Zellij (mirrored_sessions)  Tab #1  Tab #2 
┌ Pane #1 ─────────────────────────────────────────────────┐┌ Pane #2 ─────────────────────────────────────────────────┐
│$ █ ││$
│$ ││$ █
│ ││ │
│ ││ │
│ ││ │

View file

@ -807,14 +807,27 @@ impl FloatingPanes {
.iter()
.map(|(cid, pid)| (*cid, *pid))
.collect();
let next_active_pane = self.panes.keys().next().copied();
// find the most recently active pane
let mut next_active_pane_candidates: Vec<(&PaneId, &Box<dyn Pane>)> = self
.panes
.iter()
.filter(|(_p_id, p)| p.selectable())
.collect();
next_active_pane_candidates.sort_by(|(_pane_id_a, pane_a), (_pane_id_b, pane_b)| {
pane_a.active_at().cmp(&pane_b.active_at())
});
let next_active_pane_id = next_active_pane_candidates
.last()
.map(|(pane_id, _pane)| **pane_id);
for (client_id, active_pane_id) in active_panes {
if active_pane_id == pane_id {
match next_active_pane {
Some(next_active_pane) => {
match next_active_pane_id {
Some(next_active_pane_id) => {
self.active_panes
.insert(client_id, next_active_pane, &mut self.panes);
self.focus_pane(next_active_pane, client_id);
.insert(client_id, next_active_pane_id, &mut self.panes);
self.focus_pane(next_active_pane_id, client_id);
},
None => {
self.defocus_pane(pane_id, client_id);
@ -840,6 +853,11 @@ impl FloatingPanes {
.insert(client_id, pane_id, &mut self.panes);
self.focus_pane_for_all_clients(pane_id);
}
pub fn focus_pane_if_client_not_focused(&mut self, pane_id: PaneId, client_id: ClientId) {
if self.active_panes.get(&client_id).is_none() {
self.focus_pane(pane_id, client_id)
}
}
pub fn defocus_pane(&mut self, pane_id: PaneId, client_id: ClientId) {
self.z_indices.retain(|p_id| *p_id != pane_id);
self.active_panes.remove(&client_id, &mut self.panes);

View file

@ -345,6 +345,13 @@ impl TiledPanes {
}
self.reset_boundaries();
}
pub fn focus_pane_if_client_not_focused(&mut self, pane_id: PaneId, client_id: ClientId) {
log::info!("inside focus_pane_if_client_not_focused");
if self.active_panes.get(&client_id).is_none() {
log::info!("is none");
self.focus_pane(pane_id, client_id)
}
}
pub fn clear_active_panes(&mut self) {
self.active_panes.clear(&mut self.panes);
self.reset_boundaries();
@ -999,12 +1006,20 @@ impl TiledPanes {
.iter()
.map(|(cid, pid)| (*cid, *pid))
.collect();
let next_active_pane_id = self
// find the most recently active pane
let mut next_active_pane_candidates: Vec<(&PaneId, &Box<dyn Pane>)> = self
.panes
.iter()
.filter(|(p_id, _)| !self.panes_to_hide.contains(p_id))
.find(|(p_id, p)| **p_id != pane_id && p.selectable())
.map(|(p_id, _p)| *p_id);
.filter(|(p_id, p)| !self.panes_to_hide.contains(p_id) && p.selectable())
.collect();
next_active_pane_candidates.sort_by(|(_pane_id_a, pane_a), (_pane_id_b, pane_b)| {
pane_a.active_at().cmp(&pane_b.active_at())
});
let next_active_pane_id = next_active_pane_candidates
.last()
.map(|(pane_id, _pane)| **pane_id);
match next_active_pane_id {
Some(next_active_pane) => {
for (client_id, active_pane_id) in active_panes {

View file

@ -681,11 +681,11 @@ impl Tab {
self.floating_panes.first_active_floating_pane_id()
{
self.floating_panes
.focus_pane(first_active_floating_pane_id, client_id);
.focus_pane_if_client_not_focused(first_active_floating_pane_id, client_id);
}
if let Some(first_active_tiled_pane_id) = self.tiled_panes.first_active_pane_id() {
self.tiled_panes
.focus_pane(first_active_tiled_pane_id, client_id);
.focus_pane_if_client_not_focused(first_active_tiled_pane_id, client_id);
}
self.connected_clients.borrow_mut().insert(client_id);
self.mode_info.borrow_mut().insert(
@ -707,7 +707,8 @@ impl Tab {
format!("failed to acquire id of focused pane while adding client {client_id}",)
})?)
};
self.tiled_panes.focus_pane(focus_pane_id, client_id);
self.tiled_panes
.focus_pane_if_client_not_focused(focus_pane_id, client_id);
self.connected_clients.borrow_mut().insert(client_id);
self.mode_info.borrow_mut().insert(
client_id,