Files
komp_ac/client/src/modes/handlers/event.rs
2025-02-28 14:44:47 +01:00

222 lines
7.2 KiB
Rust

// src/modes/handlers/event.rs
use crossterm::event::{Event, KeyCode, KeyEvent};
use crossterm::cursor::SetCursorStyle;
use crate::tui::terminal::AppTerminal;
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;
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,
app_terminal: &mut AppTerminal,
form_state: &mut FormState,
is_saved: &mut bool,
total_count: u64,
current_position: &mut u64,
) -> Result<(bool, String), Box<dyn std::error::Error>> {
if let Event::Key(key) = event {
// Handle command mode first
if self.command_mode {
return self.handle_command_mode(
key, config, app_terminal, form_state,
is_saved, current_position, total_count
).await;
}
// Handle mode transitions
if self.handle_mode_transitions(key, config, app_terminal, form_state)? {
return Ok((false, self.command_message.clone()));
}
// Delegate to appropriate mode handler
if self.is_edit_mode {
return self.handle_edit_mode(
key, config, app_terminal, form_state,
is_saved, current_position, total_count
).await;
} else {
return self.handle_read_only_mode(
key, config, app_terminal, form_state,
current_position, total_count
).await;
}
}
self.edit_mode_cooldown = false;
Ok((false, self.command_message.clone()))
}
async fn handle_command_mode(
&mut self,
key: KeyEvent,
config: &Config,
app_terminal: &mut AppTerminal,
form_state: &mut FormState,
is_saved: &mut bool,
current_position: &mut u64,
total_count: u64,
) -> Result<(bool, String), Box<dyn std::error::Error>> {
// Check for exit_command_mode binding first
if config.is_exit_command_mode(key.code, key.modifiers) {
self.command_mode = false;
self.command_input.clear();
return Ok((false, String::new()));
}
// Existing command mode handling
let (should_exit, message, exit_command_mode) = command_mode::handle_command_event(
key,
config,
form_state,
&mut self.command_input,
&mut self.command_message,
app_terminal,
is_saved,
current_position,
total_count,
).await?;
if exit_command_mode {
self.command_mode = false;
}
if !message.is_empty() {
return Ok((should_exit, message));
}
Ok((false, "".to_string()))
}
fn handle_mode_transitions(
&mut self,
key: KeyEvent,
config: &Config,
app_terminal: &mut AppTerminal,
form_state: &mut FormState,
) -> Result<bool, Box<dyn std::error::Error>> {
// Enter command mode
if !self.is_edit_mode && config.is_enter_command_mode(key.code, key.modifiers) {
self.command_mode = true;
self.command_input.clear();
self.command_message.clear();
return Ok(true);
}
// Enter edit mode
if !self.is_edit_mode && config.is_enter_edit_mode(key.code, key.modifiers) {
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".to_string();
app_terminal.set_cursor_style(SetCursorStyle::BlinkingBar)?;
return Ok(true);
}
// Exit edit mode
if self.is_edit_mode && 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(true);
}
self.is_edit_mode = false;
self.edit_mode_cooldown = true;
self.command_message = "Read-only mode".to_string();
app_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(true);
}
Ok(false)
}
async fn handle_edit_mode(
&mut self,
key: KeyEvent,
config: &Config,
app_terminal: &mut AppTerminal,
form_state: &mut FormState,
is_saved: &mut bool,
current_position: &mut u64,
total_count: u64,
) -> Result<(bool, String), Box<dyn std::error::Error>> {
let result = edit::handle_edit_event(
key,
config,
form_state,
&mut self.is_edit_mode,
&mut self.edit_mode_cooldown,
&mut self.ideal_cursor_column,
&mut self.command_message,
&mut self.command_mode,
&mut self.command_input,
app_terminal,
is_saved,
current_position,
total_count,
).await;
self.key_sequence_tracker.reset();
result
}
async fn handle_read_only_mode(
&mut self,
key: KeyEvent,
config: &Config,
app_terminal: &mut AppTerminal,
form_state: &mut FormState,
current_position: &mut u64,
total_count: u64,
) -> Result<(bool, String), Box<dyn std::error::Error>> {
read_only::handle_read_only_event(
key,
config,
form_state,
&mut self.key_sequence_tracker,
current_position,
total_count,
app_terminal,
&mut self.command_message,
&mut self.edit_mode_cooldown,
&mut self.ideal_cursor_column,
).await
}
}