From fe377f4d850ece44c691b4cb48da5f529ec08dd1 Mon Sep 17 00:00:00 2001 From: pablu Date: Fri, 8 May 2026 16:14:36 +0200 Subject: improve code quality --- src/event.rs | 121 ++++++++++++++++++++++++++++++++++++----------------------- 1 file changed, 74 insertions(+), 47 deletions(-) (limited to 'src/event.rs') diff --git a/src/event.rs b/src/event.rs index 7045d6d..b3a0e8f 100644 --- a/src/event.rs +++ b/src/event.rs @@ -1,58 +1,24 @@ use std::io; -use ratatui::crossterm::event::{self, Event, KeyCode, KeyModifiers}; +use ratatui::crossterm::event::{self, Event, KeyCode, KeyEvent, KeyModifiers}; use crate::app::App; pub fn handle_event(app: &mut App) -> io::Result { if let Event::Key(key) = event::read()? { - if !app.showing_search_window { - match key.code { - KeyCode::Char('q') => return Ok(true), - KeyCode::Char('j') => app.list_state.select_next(), - KeyCode::Char('k') => app.list_state.select_previous(), - KeyCode::Char('/') => app.toggle_search_window(), - KeyCode::Char('n') => app.next_search(), - KeyCode::Char('N') => app.prev_search(), - - KeyCode::Tab => app.cycle_current_tab(), - - KeyCode::Char('d') => match key.modifiers { - KeyModifiers::CONTROL => app - .list_state - .select(Some(app.list_state.selected().unwrap_or(0) + 30)), - - _ => return Ok(false), - }, - - KeyCode::Char('u') => match key.modifiers { - KeyModifiers::CONTROL => { - if let Some(selected) = app.list_state.selected() { - if selected < 30 { - app.list_state.select(Some(0)); - } else { - app.list_state.select(Some(selected - 30)) - } - } - } - - _ => return Ok(false), - }, - - _ => return Ok(false), - } - } else { - match key.code { - KeyCode::Esc => app.toggle_search_window(), - KeyCode::Enter => { - app.search(); - app.toggle_search_window(); - () + match app.view { + crate::app::ViewState::Dashboard => { + if handle_dashboard_keybinds(key, app) { + return Ok(true); } - _ => { - app.textarea.input_without_shortcuts(key); - app.search_found = Some(app.search()); - () + } + crate::app::ViewState::InstalledPackages => { + if app.showing_search_window { + handle_search_keybinds(key, app); + } else { + if handle_installed_packages_keybinds(key, app) { + return Ok(true); + } } } } @@ -60,3 +26,64 @@ pub fn handle_event(app: &mut App) -> io::Result { Ok(false) } + +fn handle_search_keybinds(key: KeyEvent, app: &mut App) { + match key.code { + KeyCode::Esc => app.toggle_search_window(), + KeyCode::Enter => { + app.search(); + app.toggle_search_window(); + () + } + _ => { + app.textarea.input_without_shortcuts(key); + app.search_found = Some(app.search()); + () + } + } +} + +fn handle_installed_packages_keybinds(key: KeyEvent, app: &mut App) -> bool { + match key.modifiers { + KeyModifiers::CONTROL => match key.code { + KeyCode::Char('d') => app + .list_state + .select(Some(app.list_state.selected().unwrap_or(0) + 30)), + KeyCode::Char('u') => { + if let Some(selected) = app.list_state.selected() { + if selected < 30 { + app.list_state.select(Some(0)); + } else { + app.list_state.select(Some(selected - 30)) + } + } + } + _ => {} + }, + _ => match key.code { + KeyCode::Char('q') => return true, + KeyCode::Char('j') => app.list_state.select_next(), + KeyCode::Char('k') => app.list_state.select_previous(), + KeyCode::Char('/') => app.toggle_search_window(), + KeyCode::Char('n') => app.next_search(), + KeyCode::Char('N') => app.prev_search(), + + KeyCode::Tab => app.cycle_current_tab(), + + _ => {} + }, + } + + false +} + +fn handle_dashboard_keybinds(key: KeyEvent, app: &mut App) -> bool { + match key.code { + KeyCode::Char('q') => return true, + KeyCode::Tab => app.cycle_current_tab(), + + _ => {} + } + + return false; +} -- cgit 1.3.1