| 1 | //! Some code around handling events. |
| 2 | |
| 3 | use std::sync::mpsc::Sender; |
| 4 | |
| 5 | use crossterm::event::{KeyCode, KeyEvent, KeyModifiers, MouseEvent, MouseEventKind}; |
| 6 | |
| 7 | use crate::{ |
| 8 | app::{App, layout_manager::WidgetDirection}, |
| 9 | collection::Data, |
| 10 | }; |
| 11 | |
| 12 | /// Events sent to the main thread. |
| 13 | #[derive(Debug)] |
| 14 | pub enum BottomEvent { |
| 15 | Resize, |
| 16 | KeyInput(KeyEvent), |
| 17 | MouseInput(MouseEvent), |
| 18 | PasteEvent(String), |
| 19 | Update(Box<Data>), |
| 20 | Clean, |
| 21 | Terminate, |
| 22 | } |
| 23 | |
| 24 | /// Events sent to the collection thread. |
| 25 | #[derive(Debug)] |
| 26 | pub enum CollectionThreadEvent { |
| 27 | Reset, |
| 28 | } |
| 29 | |
| 30 | /// Handle a [`MouseEvent`]. |
| 31 | pub fn handle_mouse_event(event: MouseEvent, app: &mut App) { |
| 32 | match event.kind { |
| 33 | MouseEventKind::ScrollUp => app.handle_scroll_up(), |
| 34 | MouseEventKind::ScrollDown => app.handle_scroll_down(), |
| 35 | MouseEventKind::Down(button) => { |
| 36 | let (x, y) = (event.column, event.row); |
| 37 | if !app.app_config_fields.disable_click { |
| 38 | match button { |
| 39 | crossterm::event::MouseButton::Left => { |
| 40 | // Trigger left click widget activity |
| 41 | app.on_left_mouse_up(x, y); |
| 42 | } |
| 43 | crossterm::event::MouseButton::Right => {} |
| 44 | _ => {} |
| 45 | } |
| 46 | } |
| 47 | } |
| 48 | _ => {} |
| 49 | }; |
| 50 | } |
| 51 | |
| 52 | /// Handle a [`KeyEvent`]. |
| 53 | pub fn handle_key_event_or_break( |
| 54 | event: KeyEvent, app: &mut App, reset_sender: &Sender<CollectionThreadEvent>, |
| 55 | ) -> bool { |
| 56 | // c_debug!("KeyEvent: {event:?}"); |
| 57 | |
| 58 | if event.modifiers.is_empty() { |
| 59 | match event.code { |
| 60 | KeyCode::Char('q') | KeyCode::Char('Q') if !app.is_in_any_search() => return true, |
| 61 | KeyCode::End => app.skip_to_last(), |
| 62 | KeyCode::Home => app.skip_to_first(), |
| 63 | KeyCode::Up => app.on_up_key(), |
| 64 | KeyCode::Down => app.on_down_key(), |
| 65 | KeyCode::Left => app.on_left_key(), |
| 66 | KeyCode::Right => app.on_right_key(), |
| 67 | KeyCode::Char(' ') if !app.is_in_search_widget() => app.on_space_key(), |
| 68 | KeyCode::Char(caught_char) => app.on_char_key(caught_char), |
| 69 | KeyCode::Esc => app.on_esc(), |
| 70 | KeyCode::Enter => app.on_enter(), |
| 71 | KeyCode::Tab => app.on_tab(), |
| 72 | KeyCode::Backspace => app.on_backspace(), |
| 73 | KeyCode::Delete => app.on_delete(), |
| 74 | KeyCode::F(1) => app.toggle_ignore_case(), |
| 75 | KeyCode::F(2) => app.toggle_search_whole_word(), |
| 76 | KeyCode::F(3) => app.toggle_search_regex(), |
| 77 | KeyCode::F(5) => app.toggle_tree_mode(), |
| 78 | KeyCode::F(6) => app.toggle_sort_menu(), |
| 79 | KeyCode::F(9) => app.kill_current_process(), |
| 80 | KeyCode::PageDown => app.on_page_down(), |
| 81 | KeyCode::PageUp => app.on_page_up(), |
| 82 | _ => {} |
| 83 | } |
| 84 | } else { |
| 85 | // Otherwise, track the modifier as well... |
| 86 | if let KeyModifiers::ALT = event.modifiers { |
| 87 | match event.code { |
| 88 | KeyCode::Char('c') | KeyCode::Char('C') => app.toggle_ignore_case(), |
| 89 | KeyCode::Char('w') | KeyCode::Char('W') => app.toggle_search_whole_word(), |
| 90 | KeyCode::Char('r') | KeyCode::Char('R') => app.toggle_search_regex(), |
| 91 | KeyCode::Char('h') => app.on_left_key(), |
| 92 | KeyCode::Char('l') => app.on_right_key(), |
| 93 | _ => {} |
| 94 | } |
| 95 | } else if let KeyModifiers::CONTROL = event.modifiers { |
| 96 | if event.code == KeyCode::Char('c') { |
| 97 | return true; |
| 98 | } |
| 99 | |
| 100 | match event.code { |
| 101 | KeyCode::Char('f') => app.on_slash(), |
| 102 | KeyCode::Left => app.move_widget_selection(&WidgetDirection::Left), |
| 103 | KeyCode::Right => app.move_widget_selection(&WidgetDirection::Right), |
| 104 | KeyCode::Up => app.move_widget_selection(&WidgetDirection::Up), |
| 105 | KeyCode::Down => app.move_widget_selection(&WidgetDirection::Down), |
| 106 | KeyCode::Char('r') if reset_sender.send(CollectionThreadEvent::Reset).is_ok() => { |
| 107 | app.reset(); |
| 108 | } |
| 109 | KeyCode::Char('a') => app.skip_cursor_beginning(), |
| 110 | KeyCode::Char('e') => app.skip_cursor_end(), |
| 111 | KeyCode::Char('u') if app.is_in_any_search() => app.clear_search(), |
| 112 | KeyCode::Char('w') => app.clear_previous_word(), |
| 113 | KeyCode::Char('h') => app.on_backspace(), |
| 114 | KeyCode::Char('d') => app.scroll_half_page_down(), |
| 115 | KeyCode::Char('u') => app.scroll_half_page_up(), |
| 116 | // KeyCode::Char('j') => {}, // Move down |
| 117 | // KeyCode::Char('k') => {}, // Move up |
| 118 | // KeyCode::Char('h') => {}, // Move right |
| 119 | // KeyCode::Char('l') => {}, // Move left |
| 120 | // Can't do now, CTRL+BACKSPACE doesn't work and graphemes |
| 121 | // are hard to iter while truncating last (eloquently). |
| 122 | // KeyCode::Backspace => app.skip_word_backspace(), |
| 123 | _ => {} |
| 124 | } |
| 125 | } else if let KeyModifiers::SHIFT = event.modifiers { |
| 126 | match event.code { |
| 127 | KeyCode::Left => app.move_widget_selection(&WidgetDirection::Left), |
| 128 | KeyCode::Right => app.move_widget_selection(&WidgetDirection::Right), |
| 129 | KeyCode::Up => app.move_widget_selection(&WidgetDirection::Up), |
| 130 | KeyCode::Down => app.move_widget_selection(&WidgetDirection::Down), |
| 131 | KeyCode::Char('Q') if !app.is_in_any_search() => return true, |
| 132 | KeyCode::Char(caught_char) => app.on_char_key(caught_char), |
| 133 | _ => {} |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | false |
| 139 | } |