// src/modes/handlers/event.rs use crossterm::event::Event; use crossterm::cursor::SetCursorStyle; use crate::tui::terminal::{ core::TerminalCore, grpc_client::GrpcClient, commands::CommandHandler, }; use crate::config::config::Config; use crate::ui::handlers::form::FormState; use crate::modes::handlers::{edit, command_mode, read_only}; use crate::config::key_sequences::KeySequenceTracker; use super::common; pub struct EventHandler { pub command_mode: bool, pub command_input: String, pub command_message: String, pub is_edit_mode: bool, pub edit_mode_cooldown: bool, pub ideal_cursor_column: usize, pub key_sequence_tracker: KeySequenceTracker, } impl EventHandler { pub fn new() -> Self { EventHandler { command_mode: false, command_input: String::new(), command_message: String::new(), is_edit_mode: false, edit_mode_cooldown: false, ideal_cursor_column: 0, key_sequence_tracker: KeySequenceTracker::new(800), } } 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, is_saved: &mut bool, total_count: u64, current_position: &mut u64, ) -> Result<(bool, String), Box> { if let Event::Key(key) = event { if let Some(action) = config.get_action_for_key_in_mode( &config.keybindings.common, key.code, key.modifiers ) { match action { "save" => { let message = common::save( form_state, grpc_client, is_saved, current_position, total_count, ).await?; return Ok((false, message)); }, "force_quit" => { let (should_exit, message) = command_handler.handle_command("force_quit", terminal).await?; return Ok((should_exit, message)); }, "save_and_quit" => { let (should_exit, message) = command_handler.handle_command("save_and_quit", terminal).await?; return Ok((should_exit, message)); }, "revert" => { let message = common::revert( form_state, grpc_client, current_position, total_count, ).await?; return Ok((false, message)); }, _ => {} } } // Handle command mode with highest priority if self.command_mode { let (should_exit, message, exit_command_mode) = command_mode::handle_command_event( key, config, form_state, &mut self.command_input, &mut self.command_message, grpc_client, is_saved, current_position, total_count, ).await?; if exit_command_mode { self.command_mode = false; } if !message.is_empty() || should_exit { return Ok((should_exit, message)); } return Ok((false, String::new())); } // Mode transitions and mode-specific handling if self.is_edit_mode { // When in EDIT mode, we DON'T want to check for entering command mode // as ':' should just be a normal character input // Check for exiting edit mode if config.is_exit_edit_mode(key.code, key.modifiers) { if form_state.has_unsaved_changes { self.command_message = "Unsaved changes! Use :w to save or :q! to discard".to_string(); return Ok((false, self.command_message.clone())); } self.is_edit_mode = false; self.edit_mode_cooldown = true; self.command_message = "Read-only mode".to_string(); terminal.set_cursor_style(SetCursorStyle::SteadyBlock)?; let current_input = form_state.get_current_input(); if !current_input.is_empty() && form_state.current_cursor_pos >= current_input.len() { form_state.current_cursor_pos = current_input.len() - 1; self.ideal_cursor_column = form_state.current_cursor_pos; } return Ok((false, self.command_message.clone())); } // Handle edit mode events let result = edit::handle_edit_event_internal( key, config, form_state, &mut self.ideal_cursor_column, &mut self.command_message, terminal, grpc_client, is_saved, current_position, total_count, ).await?; self.key_sequence_tracker.reset(); return Ok((false, result)); } else { // In READ-ONLY mode, we DO want to check for entering command mode // Check for entering command mode (only in read-only mode) if let Some(action) = config.get_read_only_action_for_key(key.code, key.modifiers) { if action == "enter_command_mode" { self.command_mode = true; self.command_input.clear(); self.command_message.clear(); return Ok((false, String::new())); } } // Check for entering edit mode from read-only mode if config.is_enter_edit_mode_before(key.code, key.modifiers) { 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((false, self.command_message.clone())); } if config.is_enter_edit_mode_after(key.code, key.modifiers) { let current_input = form_state.get_current_input(); if !current_input.is_empty() && form_state.current_cursor_pos < current_input.len() { form_state.current_cursor_pos += 1; self.ideal_cursor_column = form_state.current_cursor_pos; } self.is_edit_mode = true; self.edit_mode_cooldown = true; self.command_message = "Edit mode (after cursor)".to_string(); terminal.set_cursor_style(SetCursorStyle::BlinkingBar)?; return Ok((false, self.command_message.clone())); } // Handle read-only mode events return read_only::handle_read_only_event( key, config, form_state, &mut self.key_sequence_tracker, current_position, total_count, terminal, grpc_client, &mut self.command_message, &mut self.edit_mode_cooldown, &mut self.ideal_cursor_column, ).await; } } self.edit_mode_cooldown = false; Ok((false, self.command_message.clone())) } }