fix: fix toggle to previous tab when deleting tabs

This commit is contained in:
Sagittarius-a 2021-07-23 22:38:02 +02:00
parent 734636637d
commit 404faf0498
3 changed files with 115 additions and 10 deletions

View file

@ -270,6 +270,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();
@ -300,6 +301,7 @@ impl Screen {
}
self.update_tabs();
}
self.previous_active_tab_index = future_previous;
}
pub fn resize_to_screen(&mut self, new_screen_size: PositionAndSize) {
@ -338,6 +340,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 {
@ -427,10 +437,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();
@ -771,7 +782,7 @@ pub(crate) fn screen_thread_main(
break;
}
ScreenInstruction::ToggleTab => {
screen.go_to_last_tab();
screen.toggle_tab();
screen
.bus
.senders

View file

@ -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,
@ -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"
);
}

View file

@ -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()),