From e4bbbe6b98b896fcf7c2ed359bed5825fa68a1b1 Mon Sep 17 00:00:00 2001 From: JEEVITHA KANNAN K S Date: Thu, 12 Sep 2024 11:34:02 +0530 Subject: [PATCH] Refactor tab --- src/hint.rs | 9 ++++++--- src/state.rs | 32 +++++++++++++++++++++++++++----- 2 files changed, 33 insertions(+), 8 deletions(-) diff --git a/src/hint.rs b/src/hint.rs index 27474ea99..b557a7d40 100644 --- a/src/hint.rs +++ b/src/hint.rs @@ -118,7 +118,7 @@ pub fn draw_shortcuts(state: &AppState, frame: &mut Frame, area: Rect) { let mut hints = Vec::new(); hints.push(Shortcut::new(vec!["q", "CTRL-c"], "Exit linutil")); if state.at_root() { - hints.push(Shortcut::new(vec!["h", "Left", "Tab"], "Focus tab list")); + hints.push(Shortcut::new(vec!["h", "Left"], "Focus tab list")); hints.push(get_list_item_shortcut(state)); } else { if state.selected_item_is_up_dir() { @@ -133,12 +133,13 @@ pub fn draw_shortcuts(state: &AppState, frame: &mut Frame, area: Rect) { hints.push(Shortcut::new(vec!["p"], "Enable preview")); } } - hints.push(Shortcut::new(vec!["Tab"], "Focus tab list")); }; hints.push(Shortcut::new(vec!["k", "Up"], "Select item above")); hints.push(Shortcut::new(vec!["j", "Down"], "Select item below")); hints.push(Shortcut::new(vec!["t"], "Next theme")); hints.push(Shortcut::new(vec!["T"], "Previous theme")); + hints.push(Shortcut::new(vec!["Tab"], "Next tab")); + hints.push(Shortcut::new(vec!["Shift-Tab"], "Previous tab")); ShortcutList { scope_name: "Item list", hints, @@ -148,11 +149,13 @@ pub fn draw_shortcuts(state: &AppState, frame: &mut Frame, area: Rect) { scope_name: "Tab list", hints: vec![ Shortcut::new(vec!["q", "CTRL-c"], "Exit linutil"), - Shortcut::new(vec!["l", "Right", "Tab", "Enter"], "Focus action list"), + Shortcut::new(vec!["l", "Right", "Enter"], "Focus action list"), Shortcut::new(vec!["k", "Up"], "Select item above"), Shortcut::new(vec!["j", "Down"], "Select item below"), Shortcut::new(vec!["t"], "Next theme"), Shortcut::new(vec!["T"], "Previous theme"), + Shortcut::new(vec!["Tab"], "Next tab"), + Shortcut::new(vec!["Shift-Tab"], "Previous tab"), ], }, Focus::FloatingWindow(ref float) => float.get_shortcut_list(), diff --git a/src/state.rs b/src/state.rs index 6bb4f5f33..677fad58c 100644 --- a/src/state.rs +++ b/src/state.rs @@ -187,6 +187,32 @@ impl AppState { draw_shortcuts(self, frame, vertical[1]); } pub fn handle_key(&mut self, key: &KeyEvent) -> bool { + // Handle key only when Tablist or List is focused + // Prevents exiting the application even when a command is running + // Add keys here which should work on both TabList and List + if matches!(self.focus, Focus::TabList | Focus::List) { + match key.code { + KeyCode::Char('q') => return false, + KeyCode::Tab => { + if self.current_tab.selected().unwrap() == self.tabs.len() - 1 { + self.current_tab.select_first(); // Select first tab when it is at last + } else { + self.current_tab.select_next(); + } + self.refresh_tab(); + } + KeyCode::BackTab => { + if self.current_tab.selected().unwrap() == 0 { + self.current_tab.select(Some(self.tabs.len() - 1)); // Select last tab when it is at first + } else { + self.current_tab.select_previous(); + } + self.refresh_tab(); + } + _ => {} + } + } + match &mut self.focus { Focus::FloatingWindow(command) => { if command.handle_key_event(key) { @@ -198,11 +224,8 @@ impl AppState { SearchAction::Update => self.update_items(), _ => {} }, - _ if key.code == KeyCode::Char('q') => return false, Focus::TabList => match key.code { - KeyCode::Enter | KeyCode::Char('l') | KeyCode::Right | KeyCode::Tab => { - self.focus = Focus::List - } + KeyCode::Enter | KeyCode::Char('l') | KeyCode::Right => self.focus = Focus::List, KeyCode::Char('j') | KeyCode::Down if self.current_tab.selected().unwrap() + 1 < self.tabs.len() => { @@ -231,7 +254,6 @@ impl AppState { } } KeyCode::Char('/') => self.enter_search(), - KeyCode::Tab => self.focus = Focus::TabList, KeyCode::Char('t') => self.theme.next(), KeyCode::Char('T') => self.theme.prev(), _ => {}