From 748f7738e69e4e17491e3557ab412fc659b05789 Mon Sep 17 00:00:00 2001 From: Jason Rodney Hansen Date: Mon, 27 May 2024 14:10:14 -0600 Subject: [PATCH] Fix for arrow key navigation/selection after click This fixes a bug where using the arrow keys to move around after clicking on an item with the mouse would require the arrow key to be pressed twice before moving. --- src/tab.rs | 45 +++++++++++++++++++++++++++++---------------- 1 file changed, 29 insertions(+), 16 deletions(-) diff --git a/src/tab.rs b/src/tab.rs index 22b7ed2..74eb452 100644 --- a/src/tab.rs +++ b/src/tab.rs @@ -1157,16 +1157,19 @@ impl Tab { } } Message::ItemDown => { - if let Some((row, col)) = self.select_focus_pos_opt() { + if let Some((row, col)) = self.select_focus_pos_opt().or(self.select_last_pos_opt()) + { + if self.select_focus.is_none() { + // Select last item in current selection to focus it. + self.select_position(row, col, mod_shift); + } + //TODO: Shift modifier should select items in between // Try to select item in next row if !self.select_position(row + 1, col, mod_shift) { // Ensure current item is still selected if there are no other items self.select_position(row, col, mod_shift); } - } else if let Some((row, col)) = self.select_last_pos_opt() { - // Select last item in current selection to focus it - self.select_position(row, col, mod_shift); } else { // Select first item //TODO: select first in scroll @@ -1180,7 +1183,14 @@ impl Tab { } } Message::ItemLeft => { - if let Some((row, col)) = self.select_focus_pos_opt() { + if let Some((row, col)) = + self.select_focus_pos_opt().or(self.select_first_pos_opt()) + { + if self.select_focus.is_none() { + // Select first item in current selection to focus it. + self.select_position(row, col, mod_shift); + } + // Try to select previous item in current row if !col .checked_sub(1) @@ -1205,9 +1215,6 @@ impl Tab { self.select_position(row, col, mod_shift); } } - } else if let Some((row, col)) = self.select_first_pos_opt() { - // Select first item in current selection to focus it - self.select_position(row, col, mod_shift); } else { // Select first item //TODO: select first in scroll @@ -1221,7 +1228,12 @@ impl Tab { } } Message::ItemRight => { - if let Some((row, col)) = self.select_focus_pos_opt() { + if let Some((row, col)) = self.select_focus_pos_opt().or(self.select_last_pos_opt()) + { + if self.select_focus.is_none() { + // Select last item in current selection to focus it. + self.select_position(row, col, mod_shift); + } // Try to select next item in current row if !self.select_position(row, col + 1, mod_shift) { // Try to select first item in next row @@ -1230,9 +1242,6 @@ impl Tab { self.select_position(row, col, mod_shift); } } - } else if let Some((row, col)) = self.select_last_pos_opt() { - // Select last item in current selection to focus it - self.select_position(row, col, mod_shift); } else { // Select first item //TODO: select first in scroll @@ -1246,7 +1255,14 @@ impl Tab { } } Message::ItemUp => { - if let Some((row, col)) = self.select_focus_pos_opt() { + if let Some((row, col)) = + self.select_focus_pos_opt().or(self.select_first_pos_opt()) + { + if self.select_focus.is_none() { + // Select first item in current selection to focus it. + self.select_position(row, col, mod_shift); + } + //TODO: Shift modifier should select items in between // Try to select item in last row if !row @@ -1256,9 +1272,6 @@ impl Tab { // Ensure current item is still selected if there are no other items self.select_position(row, col, mod_shift); } - } else if let Some((row, col)) = self.select_first_pos_opt() { - // Select first item in current selection to focus it - self.select_position(row, col, mod_shift); } else { // Select first item //TODO: select first in scroll