返回 CodeWhale
key_actions.rs
根目录 / crates / tui / src / tui / key_actions.rs
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 pub(crate) fn toggle_file_tree(app: &mut App) {
13 if app.file_tree.take().is_some() {
14 app.show_input_hint("File tree closed");
15 } else {
16 app.file_tree = Some(super::file_tree::FileTreeState::new(&app.workspace));
17 app.show_input_hint("File tree: ↑/↓ navigate Enter select Esc close");
18 }
19 app.needs_redraw = true;
20 }
21
22 /// Handle keyboard input when the file-tree pane is visible.
23 ///
24 /// Returns `true` when the key was consumed (caller should `continue`).
25 pub fn handle_file_tree_key(app: &mut App, key: &KeyEvent) -> bool {
26 // Guard: do not intercept keys when the file-tree pane is not visible.
27 if !app.file_tree_visible {
28 return false;
29 }
30
31 // Esc closes the tree even when entries are still loading.
32 if key.code == KeyCode::Esc && app.file_tree.is_some() {
33 toggle_file_tree(app);
34 return true;
35 }
36
37 let Some(file_tree) = app.file_tree.as_mut() else {
38 return false;
39 };
40
41 match key.code {
42 KeyCode::Up => {
43 file_tree.cursor_up();
44 app.needs_redraw = true;
45 true
46 }
47 KeyCode::Down => {
48 file_tree.cursor_down();
49 app.needs_redraw = true;
50 true
51 }
52 KeyCode::Enter => {
53 if let Some(rel_path) = file_tree.activate() {
54 let path_str = rel_path.to_string_lossy().to_string();
55 app.show_input_hint(format!("Attached @{path_str}"));
56 app.insert_str(&format!(
57 "@{} ",
58 crate::tui::file_mention::file_mention_body(&path_str)
59 ));
60 } else {
61 app.needs_redraw = true;
62 }
63 true
64 }
65 _ => false,
66 }
67 }
68
68 lines RUST