Skip to content

Commit

Permalink
fix: fix toggle to previous tab when deleting tabs
Browse files Browse the repository at this point in the history
  • Loading branch information
sagittarius-a committed Jul 23, 2021
1 parent 03ccd72 commit 081dbcb
Show file tree
Hide file tree
Showing 3 changed files with 115 additions and 10 deletions.
19 changes: 15 additions & 4 deletions zellij-server/src/screen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,7 @@ impl Screen {
/// Closes this [`Screen`]'s active [`Tab`], exiting the application if it happens
/// to be the last tab.
pub fn close_tab(&mut self) {
let future_previous = self.previous_active_tab_index;
let active_tab_index = self.active_tab_index.unwrap();
if self.tabs.len() > 1 {
self.switch_tab_prev();
Expand Down Expand Up @@ -298,6 +299,7 @@ impl Screen {
}
self.update_tabs();
}
self.previous_active_tab_index = future_previous;
}

pub fn resize_to_screen(&mut self, new_screen_size: PositionAndSize) {
Expand Down Expand Up @@ -336,6 +338,14 @@ impl Screen {
}
}

/// Returns an immutable reference to this [`Screen`]'s previous active [`Tab`].
pub fn get_previous_tab(&self) -> Option<&Tab> {
match self.previous_active_tab_index {
Some(tab) => self.tabs.get(&tab),
None => None,
}
}

/// Returns a mutable reference to this [`Screen`]'s active [`Tab`].
pub fn get_active_tab_mut(&mut self) -> Option<&mut Tab> {
match self.active_tab_index {
Expand Down Expand Up @@ -420,10 +430,11 @@ impl Screen {
self.switch_tab_next();
}
}
pub fn go_to_last_tab(&mut self) {
pub fn toggle_tab(&mut self) {
let active_tab_index = self.active_tab_index.unwrap();
if let Some(i) = self.previous_active_tab_index {
self.go_to_tab(i + 1);
if let Some(_) = self.previous_active_tab_index {
let position = self.get_previous_tab().unwrap().position;
self.go_to_tab(position + 1);
}
self.previous_active_tab_index = Some(active_tab_index);
self.update_tabs();
Expand Down Expand Up @@ -734,7 +745,7 @@ pub(crate) fn screen_thread_main(
break;
}
ScreenInstruction::ToggleTab => {
screen.go_to_last_tab();
screen.toggle_tab();
screen
.bus
.senders
Expand Down
104 changes: 99 additions & 5 deletions zellij-server/src/unit/screen_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ fn move_focus_right_at_right_screen_edge_changes_tab() {
}

#[test]
pub fn switch_to_last_tab() {
pub fn toggle_to_previous_tab_simple() {
let position_and_size = PositionAndSize {
cols: 121,
rows: 20,
Expand All @@ -264,17 +264,111 @@ pub fn switch_to_last_tab() {
screen.go_to_tab(1);
screen.go_to_tab(2);

screen.go_to_last_tab();
screen.toggle_tab();
assert_eq!(
screen.get_active_tab().unwrap().position,
0,
"Active tab switched to last tab"
"Active tab toggler to previous tab"
);

screen.go_to_last_tab();
screen.toggle_tab();
assert_eq!(
screen.get_active_tab().unwrap().position,
1,
"Active tab switched to last tab"
"Active tab toggler to previous tab"
);
}

#[test]
pub fn toggle_to_previous_tab_create_tabs_only() {
let position_and_size = PositionAndSize {
cols: 121,
rows: 20,
x: 0,
y: 0,
..Default::default()
};
let mut screen = create_new_screen(position_and_size);

screen.new_tab(1);
screen.new_tab(2);
screen.new_tab(3);

screen.toggle_tab();
assert_eq!(
screen.get_active_tab().unwrap().position,
1,
"Active tab toggler to previous tab"
);

screen.toggle_tab();
assert_eq!(
screen.get_active_tab().unwrap().position,
2,
"Active tab toggler to previous tab"
);

screen.toggle_tab();
assert_eq!(
screen.get_active_tab().unwrap().position,
1,
"Active tab toggler to previous tab"
);
}

#[test]
pub fn toggle_to_previous_tab_delete() {
let position_and_size = PositionAndSize {
cols: 121,
rows: 20,
x: 0,
y: 0,
..Default::default()
};
let mut screen = create_new_screen(position_and_size);

screen.new_tab(1);
screen.new_tab(2);
screen.new_tab(3);

screen.toggle_tab();
assert_eq!(
screen.get_active_tab().unwrap().position,
1,
"Active tab toggler to previous tab"
);
assert_eq!(
screen.get_previous_tab().unwrap().position,
2,
"Previous active tab invalid"
);

screen.close_tab();
assert_eq!(
screen.get_active_tab().unwrap().position,
0,
"Active tab toggler to previous tab"
);
assert_eq!(
screen.get_previous_tab().unwrap().position,
1,
"Previous active tab invalid"
);
assert_eq!(
screen.get_previous_tab().unwrap().index,
2,
"Previous active tab invalid"
);

screen.toggle_tab();
assert_eq!(
screen.get_active_tab().unwrap().position,
1,
"Active tab toggler to previous tab"
);
assert_eq!(
screen.get_previous_tab().unwrap().position,
0,
"Previous active tab invalid"
);
}
2 changes: 1 addition & 1 deletion zellij-utils/src/input/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ pub fn get_mode_info(
("x".to_string(), "Close".to_string()),
("r".to_string(), "Rename".to_string()),
("s".to_string(), "Sync".to_string()),
("Tab".to_string(), "Last".to_string()),
("Tab".to_string(), "Toggle".to_string()),
],
InputMode::Scroll => vec![
("↓↑".to_string(), "Scroll".to_string()),
Expand Down

0 comments on commit 081dbcb

Please sign in to comment.