Files
komp_ac/client/src/modes/handlers/event.rs
2025-02-28 20:16:31 +01:00

147 lines
5.7 KiB
Rust

// src/modes/handlers/event.rs
use crossterm::event::{Event, 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 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,
app_terminal,
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()));
}
// Check for entering command mode from any other mode
if config.is_enter_command_mode(key.code, key.modifiers) {
self.command_mode = true;
self.command_input.clear();
self.command_message.clear();
return Ok((false, String::new()));
}
// Mode transitions between edit mode and read-only mode
if self.is_edit_mode {
// Check for exiting edit mode
if config.get_edit_action_for_key(key.code, key.modifiers) == Some("exit_edit_mode") {
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();
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((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,
).await?;
self.key_sequence_tracker.reset();
return Ok((false, result));
} else {
// Check for entering edit mode from read-only mode
if config.get_read_only_action_for_key(key.code, key.modifiers) == Some("enter_edit_mode_before") {
if config.get_read_only_action_for_key(key.code, key.modifiers) == Some("enter_edit_mode_after") {
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((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,
app_terminal,
&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()))
}
}