Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(tabs): moving tabs after one was deleted #3299

Merged
merged 2 commits into from
Apr 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 49 additions & 21 deletions zellij-server/src/screen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1593,25 +1593,30 @@ impl Screen {
}

pub fn move_active_tab_to_left(&mut self, client_id: ClientId) -> Result<()> {
let err_context = || "Failed to move active tab left";
if self.tabs.len() < 2 {
debug!("cannot move tab to left: only one tab exists");
return Ok(());
}
let Some(client_id) = self.client_id(client_id) else {
return Ok(());
};
let Some(&active_tab_idx) = self.active_tab_indices.get(&client_id) else {
return Ok(());
};

// wraps around: [tab1, tab2, tab3] => [tab1, tab2, tab3]
// ^ ^
// active_tab_idx left_tab_idx
let left_tab_idx = (active_tab_idx + self.tabs.len() - 1) % self.tabs.len();
match self.get_active_tab(client_id) {
Ok(active_tab) => {
let active_tab_pos = active_tab.position;
let left_tab_pos = if active_tab_pos == 0 {
self.tabs.len() - 1
} else {
active_tab_pos - 1
};

self.switch_tabs(active_tab_idx, left_tab_idx, client_id);
self.log_and_report_session_state()
.context("failed to move tab to left")?;
self.switch_tabs(active_tab_pos, left_tab_pos, client_id);
self.log_and_report_session_state()
.context("failed to move tab to left")?;
},
Err(err) => Err::<(), _>(err).with_context(err_context).non_fatal(),
}
Ok(())
}

Expand All @@ -1623,7 +1628,29 @@ impl Screen {
}
}

fn switch_tabs(&mut self, active_tab_idx: usize, other_tab_idx: usize, client_id: u16) {
fn switch_tabs(&mut self, active_tab_pos: usize, other_tab_pos: usize, client_id: u16) {
let Some(active_tab_idx) = self
.tabs
.values()
.find(|t| t.position == active_tab_pos)
.map(|t| t.index)
else {
log::error!("Failed to find active tab at position: {}", active_tab_pos);
return;
};
let Some(other_tab_idx) = self
.tabs
.values()
.find(|t| t.position == other_tab_pos)
.map(|t| t.index)
else {
log::error!(
"Failed to find tab to switch to at position: {}",
other_tab_pos
);
return;
};

if !self.tabs.contains_key(&active_tab_idx) || !self.tabs.contains_key(&other_tab_idx) {
warn!(
"failed to switch tabs: index {} or {} not found in {:?}",
Expand Down Expand Up @@ -1655,25 +1682,26 @@ impl Screen {
}

pub fn move_active_tab_to_right(&mut self, client_id: ClientId) -> Result<()> {
let err_context = || "Failed to move active tab right ";
if self.tabs.len() < 2 {
debug!("cannot move tab to right: only one tab exists");
return Ok(());
}
let Some(client_id) = self.client_id(client_id) else {
return Ok(());
};
let Some(&active_tab_idx) = self.active_tab_indices.get(&client_id) else {
return Ok(());
};

// wraps around: [tab1, tab2, tab3] => [tab1, tab2, tab3]
// ^ ^
// active_tab_idx right_tab_idx
let right_tab_idx = (active_tab_idx + 1) % self.tabs.len();
match self.get_active_tab(client_id) {
Ok(active_tab) => {
let active_tab_pos = active_tab.position;
let right_tab_pos = (active_tab_pos + 1) % self.tabs.len();

self.switch_tabs(active_tab_idx, right_tab_idx, client_id);
self.log_and_report_session_state()
.context("failed to move active tab to right")?;
self.switch_tabs(active_tab_pos, right_tab_pos, client_id);
self.log_and_report_session_state()
.context("failed to move tab to the right")?;
},
Err(err) => Err::<(), _>(err).with_context(err_context).non_fatal(),
}
Ok(())
}

Expand Down
Loading