// src/modes/handlers/event.rs use crossterm::event::Event; use crossterm::cursor::SetCursorStyle; use crate::services::grpc_client::GrpcClient; use crate::services::auth::AuthClient; use crate::config::binds::config::Config; use crate::ui::handlers::rat_state::UiStateHandler; use crate::ui::handlers::context::UiContext; use crate::functions::common::buffer; use anyhow::Result; use crate::tui::{ terminal::core::TerminalCore, functions::{ common::{form::SaveOutcome, login, register}, }, {intro, admin}, }; use crate::state::{ app::{ highlight::HighlightState, state::AppState, buffer::{AppView, BufferState}, }, pages::{ auth::{AuthState, LoginState, RegisterState}, admin::AdminState, canvas_state::CanvasState, form::FormState, intro::IntroState, }, }; use crate::modes::{ common::{command_mode, commands::CommandHandler}, handlers::mode_manager::{ModeManager, AppMode}, canvas::{edit, read_only, common_mode}, general::{navigation, dialog}, }; use crate::functions::modes::navigation::{admin_nav, add_table_nav}; use crate::config::binds::key_sequences::KeySequenceTracker; use tokio::sync::mpsc; use crate::tui::functions::common::login::LoginResult; use crate::tui::functions::common::register::RegisterResult; use crate::functions::modes::navigation::add_table_nav::SaveTableResultSender; use crate::functions::modes::navigation::add_logic_nav::SaveLogicResultSender; use crate::functions::modes::navigation::add_logic_nav; use crossterm::event::{KeyCode, KeyModifiers}; #[derive(Debug, Clone, PartialEq, Eq)] pub enum EventOutcome { Ok(String), Exit(String), DataSaved(SaveOutcome, String), ButtonSelected { context: UiContext, index: usize }, } pub struct EventHandler { pub command_mode: bool, pub command_input: String, pub command_message: String, pub is_edit_mode: bool, pub highlight_state: HighlightState, pub edit_mode_cooldown: bool, pub ideal_cursor_column: usize, pub key_sequence_tracker: KeySequenceTracker, pub auth_client: AuthClient, pub login_result_sender: mpsc::Sender, pub register_result_sender: mpsc::Sender, pub save_table_result_sender: SaveTableResultSender, pub save_logic_result_sender: SaveLogicResultSender, pub find_file_palette_active: bool, pub find_file_options: Vec, pub find_file_selected_index: Option, pub find_file_input: String, } impl EventHandler { pub async fn new( login_result_sender: mpsc::Sender, register_result_sender: mpsc::Sender, save_table_result_sender: SaveTableResultSender, save_logic_result_sender: SaveLogicResultSender, ) -> Result { Ok(EventHandler { command_mode: false, command_input: String::new(), command_message: String::new(), is_edit_mode: false, highlight_state: HighlightState::Off, edit_mode_cooldown: false, ideal_cursor_column: 0, key_sequence_tracker: KeySequenceTracker::new(400), auth_client: AuthClient::new().await?, login_result_sender, register_result_sender, save_table_result_sender, save_logic_result_sender, find_file_palette_active: false, find_file_options: vec![ "src/main.rs".to_string(), "src/lib.rs".to_string(), "Cargo.toml".to_string(), "README.md".to_string(), "config.toml".to_string(), "src/ui/handlers/ui.rs".to_string(), "src/modes/handlers/event.rs".to_string(), "another_file.txt".to_string(), "yet_another_one.md".to_string(), ], find_file_selected_index: None, find_file_input: String::new(), }) } #[allow(clippy::too_many_arguments)] pub async fn handle_event( &mut self, event: Event, config: &Config, terminal: &mut TerminalCore, grpc_client: &mut GrpcClient, command_handler: &mut CommandHandler, form_state: &mut FormState, auth_state: &mut AuthState, login_state: &mut LoginState, register_state: &mut RegisterState, intro_state: &mut IntroState, admin_state: &mut AdminState, buffer_state: &mut BufferState, app_state: &mut AppState, total_count: u64, current_position: &mut u64, ) -> Result { // Handle find file palette first if self.find_file_palette_active { if let Event::Key(key) = event { if key.code == KeyCode::Esc { self.find_file_palette_active = false; self.find_file_input.clear(); self.find_file_selected_index = None; self.command_message = "Find File palette closed".to_string(); return Ok(EventOutcome::Ok(self.command_message.clone())); } if let KeyCode::Char(c) = key.code { self.find_file_input.push(c); } else if key.code == KeyCode::Backspace { self.find_file_input.pop(); } return Ok(EventOutcome::Ok("Palette event consumed".to_string())); } } let current_mode = ModeManager::derive_mode(app_state, self, admin_state); app_state.update_mode(current_mode); let current_view = { let ui = &app_state.ui; if ui.show_intro { AppView::Intro } else if ui.show_login { AppView::Login } else if ui.show_register { AppView::Register } else if ui.show_admin { AppView::Admin } else if ui.show_add_logic { AppView::AddLogic } else if ui.show_add_table { AppView::AddTable } else if ui.show_form { AppView::Form } else { AppView::Scratch } }; buffer_state.update_history(current_view); if app_state.ui.dialog.dialog_show { if let Some(dialog_result) = dialog::handle_dialog_event( &event, config, app_state, login_state, register_state, buffer_state, admin_state, ).await { return dialog_result; } return Ok(EventOutcome::Ok(String::new())); } if let Event::Key(key) = event { let key_code = key.code; let modifiers = key.modifiers; if UiStateHandler::toggle_sidebar(&mut app_state.ui, config, key_code, modifiers) { let message = format!("Sidebar {}", if app_state.ui.show_sidebar { "shown" } else { "hidden" } ); return Ok(EventOutcome::Ok(message)); } if UiStateHandler::toggle_buffer_list(&mut app_state.ui, config, key_code, modifiers) { let message = format!("Buffer {}", if app_state.ui.show_buffer_list { "shown" } else { "hidden" } ); return Ok(EventOutcome::Ok(message)); } if !matches!(current_mode, AppMode::Edit | AppMode::Command) { if let Some(action) = config.get_action_for_key_in_mode( &config.keybindings.global, key_code, modifiers ) { match action { "next_buffer" => { if buffer::switch_buffer(buffer_state, true) { return Ok(EventOutcome::Ok("Switched to next buffer".to_string())); } } "previous_buffer" => { if buffer::switch_buffer(buffer_state, false) { return Ok(EventOutcome::Ok("Switched to previous buffer".to_string())); } } "close_buffer" => { let current_table_name = Some("2025_customer"); let message = buffer_state.close_buffer_with_intro_fallback(current_table_name); return Ok(EventOutcome::Ok(message)); } _ => {} } } } match current_mode { AppMode::General => { if app_state.ui.show_admin && auth_state.role.as_deref() == Some("admin") { if admin_nav::handle_admin_navigation( key, config, app_state, admin_state, buffer_state, &mut self.command_message, ) { return Ok(EventOutcome::Ok(self.command_message.clone())); } } if app_state.ui.show_add_logic { let client_clone = grpc_client.clone(); let sender_clone = self.save_logic_result_sender.clone(); if add_logic_nav::handle_add_logic_navigation( key, config, app_state, &mut admin_state.add_logic_state, &mut self.is_edit_mode, buffer_state, client_clone, sender_clone, &mut self.command_message, ) { return Ok(EventOutcome::Ok(self.command_message.clone())); } } if app_state.ui.show_add_table { let client_clone = grpc_client.clone(); let sender_clone = self.save_table_result_sender.clone(); if add_table_nav::handle_add_table_navigation( key, config, app_state, &mut admin_state.add_table_state, client_clone, sender_clone, &mut self.command_message, ) { return Ok(EventOutcome::Ok(self.command_message.clone())); } } let nav_outcome = navigation::handle_navigation_event( key, config, form_state, app_state, login_state, register_state, intro_state, admin_state, &mut self.command_mode, &mut self.command_input, &mut self.command_message, ).await; match nav_outcome { Ok(EventOutcome::ButtonSelected { context, index }) => { let message = match context { UiContext::Intro => { intro::handle_intro_selection(app_state, buffer_state, index); if app_state.ui.show_admin { if !app_state.profile_tree.profiles.is_empty() { admin_state.profile_list_state.select(Some(0)); } } format!("Intro Option {} selected", index) } UiContext::Login => { let login_action_message = match index { 0 => { login::initiate_login(login_state, app_state, self.auth_client.clone(), self.login_result_sender.clone()) }, 1 => login::back_to_main(login_state, app_state, buffer_state).await, _ => "Invalid Login Option".to_string(), }; login_action_message } UiContext::Register => { let register_action_message = match index { 0 => { register::initiate_registration(register_state, app_state, self.auth_client.clone(), self.register_result_sender.clone()) }, 1 => register::back_to_login(register_state, app_state, buffer_state).await, _ => "Invalid Login Option".to_string(), }; register_action_message } UiContext::Admin => { admin::handle_admin_selection(app_state, admin_state); format!("Admin Option {} selected", index) } UiContext::Dialog => { "Internal error: Unexpected dialog state".to_string() } }; return Ok(EventOutcome::Ok(message)); } other => return other, } }, AppMode::ReadOnly => { if config.get_read_only_action_for_key(key_code, modifiers) == Some("enter_highlight_mode_linewise") && ModeManager::can_enter_highlight_mode(current_mode) { let current_field_index = if app_state.ui.show_login { login_state.current_field() } else if app_state.ui.show_register { register_state.current_field() } else { form_state.current_field() }; self.highlight_state = HighlightState::Linewise { anchor_line: current_field_index }; self.command_message = "-- LINE HIGHLIGHT --".to_string(); return Ok(EventOutcome::Ok(self.command_message.clone())); } else if config.get_read_only_action_for_key(key_code, modifiers) == Some("enter_highlight_mode") && ModeManager::can_enter_highlight_mode(current_mode) { let current_field_index = if app_state.ui.show_login { login_state.current_field() } else if app_state.ui.show_register { register_state.current_field() } else { form_state.current_field() }; let current_cursor_pos = if app_state.ui.show_login { login_state.current_cursor_pos() } else if app_state.ui.show_register { register_state.current_cursor_pos() } else { form_state.current_cursor_pos() }; let anchor = (current_field_index, current_cursor_pos); self.highlight_state = HighlightState::Characterwise { anchor }; self.command_message = "-- HIGHLIGHT --".to_string(); return Ok(EventOutcome::Ok(self.command_message.clone())); } else if config.get_read_only_action_for_key(key_code, modifiers).as_deref() == Some("enter_edit_mode_before") && ModeManager::can_enter_edit_mode(current_mode) { self.is_edit_mode = true; self.edit_mode_cooldown = true; self.command_message = "Edit mode".to_string(); terminal.set_cursor_style(SetCursorStyle::BlinkingBar)?; return Ok(EventOutcome::Ok(self.command_message.clone())); } else if config.get_read_only_action_for_key(key_code, modifiers).as_deref() == Some("enter_edit_mode_after") && ModeManager::can_enter_edit_mode(current_mode) { let current_input = if app_state.ui.show_login || app_state.ui.show_register{ login_state.get_current_input() } else { form_state.get_current_input() }; let current_cursor_pos = if app_state.ui.show_login || app_state.ui.show_register{ login_state.current_cursor_pos() } else { form_state.current_cursor_pos() }; if !current_input.is_empty() && current_cursor_pos < current_input.len() { if app_state.ui.show_login || app_state.ui.show_register{ login_state.set_current_cursor_pos(current_cursor_pos + 1); self.ideal_cursor_column = login_state.current_cursor_pos(); } else { form_state.set_current_cursor_pos(current_cursor_pos + 1); self.ideal_cursor_column = form_state.current_cursor_pos(); } } self.is_edit_mode = true; self.edit_mode_cooldown = true; app_state.ui.focus_outside_canvas = false; self.command_message = "Edit mode (after cursor)".to_string(); terminal.set_cursor_style(SetCursorStyle::BlinkingBar)?; return Ok(EventOutcome::Ok(self.command_message.clone())); } else if config.get_read_only_action_for_key(key_code, modifiers) == Some("enter_command_mode") && ModeManager::can_enter_command_mode(current_mode) { self.command_mode = true; self.command_input.clear(); self.command_message.clear(); return Ok(EventOutcome::Ok(String::new())); } if let Some(action) = config.get_common_action(key_code, modifiers) { match action { "save" | "force_quit" | "save_and_quit" | "revert" => { return common_mode::handle_core_action( action, form_state, auth_state, login_state, register_state, grpc_client, &mut self.auth_client, terminal, app_state, current_position, total_count, ).await; }, _ => {} } } let (_should_exit, message) = read_only::handle_read_only_event( app_state, key, config, form_state, login_state, register_state, &mut admin_state.add_table_state, &mut admin_state.add_logic_state, &mut self.key_sequence_tracker, current_position, total_count, grpc_client, &mut self.command_message, &mut self.edit_mode_cooldown, &mut self.ideal_cursor_column, ).await?; return Ok(EventOutcome::Ok(message)); }, AppMode::Highlight => { if config.get_highlight_action_for_key(key_code, modifiers) == Some("exit_highlight_mode") { self.highlight_state = HighlightState::Off; self.command_message = "Exited highlight mode".to_string(); terminal.set_cursor_style(SetCursorStyle::SteadyBlock)?; return Ok(EventOutcome::Ok(self.command_message.clone())); } else if config.get_highlight_action_for_key(key_code, modifiers) == Some("enter_highlight_mode_linewise") { if let HighlightState::Characterwise { anchor } = self.highlight_state { self.highlight_state = HighlightState::Linewise { anchor_line: anchor.0 }; self.command_message = "-- LINE HIGHLIGHT --".to_string(); return Ok(EventOutcome::Ok(self.command_message.clone())); } return Ok(EventOutcome::Ok("".to_string())); } let (_should_exit, message) = read_only::handle_read_only_event( app_state, key, config, form_state, login_state, register_state, &mut admin_state.add_table_state, &mut admin_state.add_logic_state, &mut self.key_sequence_tracker, current_position, total_count, grpc_client, &mut self.command_message, &mut self.edit_mode_cooldown, &mut self.ideal_cursor_column, ) .await?; return Ok(EventOutcome::Ok(message)); } AppMode::Edit => { if let Some(action) = config.get_common_action(key_code, modifiers) { match action { "save" | "force_quit" | "save_and_quit" | "revert" => { return common_mode::handle_core_action( action, form_state, auth_state, login_state, register_state, grpc_client, &mut self.auth_client, terminal, app_state, current_position, total_count, ).await; }, _ => {} } } let edit_result = edit::handle_edit_event( key, config, form_state, login_state, register_state, admin_state, &mut self.ideal_cursor_column, current_position, total_count, grpc_client, app_state, ).await; match edit_result { Ok(edit::EditEventOutcome::ExitEditMode) => { self.is_edit_mode = false; self.edit_mode_cooldown = true; let has_changes = if app_state.ui.show_login { login_state.has_unsaved_changes() } else if app_state.ui.show_register { register_state.has_unsaved_changes() } else { form_state.has_unsaved_changes() }; self.command_message = if has_changes { "Exited edit mode (unsaved changes remain)".to_string() } else { "Read-only mode".to_string() }; terminal.set_cursor_style(SetCursorStyle::SteadyBlock)?; let current_input = if app_state.ui.show_login { login_state.get_current_input() } else if app_state.ui.show_register { register_state.get_current_input() } else { form_state.get_current_input() }; let current_cursor_pos = if app_state.ui.show_login { login_state.current_cursor_pos() } else if app_state.ui.show_register { register_state.current_cursor_pos() } else { form_state.current_cursor_pos() }; if !current_input.is_empty() && current_cursor_pos >= current_input.len() { let new_pos = current_input.len() - 1; let target_state: &mut dyn CanvasState = if app_state.ui.show_login { login_state } else if app_state.ui.show_register { register_state } else { form_state }; target_state.set_current_cursor_pos(new_pos); self.ideal_cursor_column = new_pos; } return Ok(EventOutcome::Ok(self.command_message.clone())); } Ok(edit::EditEventOutcome::Message(msg)) => { if !msg.is_empty() { self.command_message = msg; } self.key_sequence_tracker.reset(); return Ok(EventOutcome::Ok(self.command_message.clone())); } Err(e) => { return Err(e.into()); } } }, AppMode::Command => { // Handle immediate command mode actions if config.is_exit_command_mode(key_code, modifiers) { self.command_input.clear(); self.command_message.clear(); self.command_mode = false; self.key_sequence_tracker.reset(); return Ok(EventOutcome::Ok("Exited command mode".to_string())); } if config.is_command_execute(key_code, modifiers) { let outcome = command_mode::handle_command_event( key, config, app_state, login_state, register_state, form_state, &mut self.command_input, &mut self.command_message, grpc_client, command_handler, terminal, current_position, total_count, ).await?; self.command_mode = false; self.key_sequence_tracker.reset(); return Ok(outcome); } // Handle backspace if key_code == KeyCode::Backspace { self.command_input.pop(); self.key_sequence_tracker.reset(); return Ok(EventOutcome::Ok(String::new())); } // Handle character input and sequences if let KeyCode::Char(c) = key_code { if c == 'f' { self.key_sequence_tracker.add_key(key_code); let sequence = self.key_sequence_tracker.get_sequence(); if config.matches_key_sequence_generalized(&sequence) == Some("find_file_palette_toggle") { if app_state.ui.show_form || app_state.ui.show_intro { self.find_file_palette_active = true; self.find_file_input.clear(); self.find_file_selected_index = if self.find_file_options.is_empty() { None } else { Some(0) }; self.command_mode = false; self.command_input.clear(); self.command_message = "Find File:".to_string(); self.key_sequence_tracker.reset(); return Ok(EventOutcome::Ok("Find File palette activated".to_string())); } else { self.key_sequence_tracker.reset(); self.command_input.push('f'); self.command_input.push('f'); self.command_message = "Find File not available in this view.".to_string(); return Ok(EventOutcome::Ok(self.command_message.clone())); } } if config.is_key_sequence_prefix(&sequence) { return Ok(EventOutcome::Ok(String::new())); } } if c != 'f' && !self.key_sequence_tracker.current_sequence.is_empty() { self.key_sequence_tracker.reset(); } self.command_input.push(c); return Ok(EventOutcome::Ok(String::new())); } // Reset tracker for other keys self.key_sequence_tracker.reset(); return Ok(EventOutcome::Ok(String::new())); } } } self.edit_mode_cooldown = false; Ok(EventOutcome::Ok(self.command_message.clone())) } fn is_processed_command(&self, command: &str) -> bool { matches!(command, "w" | "q" | "q!" | "wq" | "r") } }