| 1 | //! Keyboard event action handlers extracted from `ui.rs`. |
| 2 | //! |
| 3 | //! Each function handles a focused subset of keyboard input so the |
| 4 | //! main event loop stays lean. |
| 5 | |
| 6 | use crossterm::event::{KeyCode, KeyEvent}; |
| 7 | |
| 8 | use super::app::App; |
| 9 | |
| 10 | // ── File-tree key handling ─────────────────────────────────────── |
| 11 | |
| 12 | /// Handle keyboard input when the file-tree pane is visible. |
| 13 | /// |
| 14 | /// Returns `true` when the key was consumed (caller should `continue`). |
| 15 | pub fn handle_file_tree_key(app: &mut App, key: &KeyEvent) -> bool { |
| 16 | // Guard: do not intercept keys when the file-tree pane is not visible. |
| 17 | if !app.file_tree_visible { |
| 18 | return false; |
| 19 | } |
| 20 | |
| 21 | // Esc closes the tree even when entries are still loading. |
| 22 | if key.code == KeyCode::Esc && app.file_tree.is_some() { |
| 23 | app.file_tree = None; |
| 24 | app.status_message = Some("File tree closed".to_string()); |
| 25 | app.needs_redraw = true; |
| 26 | return true; |
| 27 | } |
| 28 | |
| 29 | let Some(file_tree) = app.file_tree.as_mut() else { |
| 30 | return false; |
| 31 | }; |
| 32 | |
| 33 | match key.code { |
| 34 | KeyCode::Up => { |
| 35 | file_tree.cursor_up(); |
| 36 | app.needs_redraw = true; |
| 37 | true |
| 38 | } |
| 39 | KeyCode::Down => { |
| 40 | file_tree.cursor_down(); |
| 41 | app.needs_redraw = true; |
| 42 | true |
| 43 | } |
| 44 | KeyCode::Enter => { |
| 45 | if let Some(rel_path) = file_tree.activate() { |
| 46 | let path_str = rel_path.to_string_lossy().to_string(); |
| 47 | app.status_message = Some(format!("Attached @{path_str}")); |
| 48 | app.insert_str(&format!("@{path_str} ")); |
| 49 | } else { |
| 50 | app.needs_redraw = true; |
| 51 | } |
| 52 | true |
| 53 | } |
| 54 | _ => false, |
| 55 | } |
| 56 | } |
| 57 |