249 lines
10 KiB
Rust
249 lines
10 KiB
Rust
// src/modes/handlers/event.rs
|
|
use crossterm::event::{Event, KeyEvent};
|
|
use crossterm::cursor::SetCursorStyle;
|
|
use crate::tui::terminal::{
|
|
core::TerminalCore,
|
|
grpc_client::GrpcClient,
|
|
};
|
|
use crate::tui::controls::commands::CommandHandler;
|
|
use crate::config::binds::config::Config;
|
|
use crate::state::pages::form::FormState;
|
|
use crate::ui::handlers::rat_state::UiStateHandler;
|
|
use crate::modes::{
|
|
common::{command_mode},
|
|
canvas::{edit, read_only, common},
|
|
general::navigation,
|
|
};
|
|
use crate::config::binds::key_sequences::KeySequenceTracker;
|
|
use crate::modes::handlers::mode_manager::{ModeManager, AppMode};
|
|
|
|
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,
|
|
app_state: &mut crate::state::state::AppState,
|
|
total_count: u64,
|
|
current_position: &mut u64,
|
|
) -> Result<(bool, String), Box<dyn std::error::Error>> {
|
|
// Determine current mode based on app state and event handler state
|
|
let current_mode = ModeManager::derive_mode(app_state, self);
|
|
app_state.update_mode(current_mode);
|
|
|
|
if let Event::Key(key) = event {
|
|
let key_code = key.code;
|
|
let modifiers = key.modifiers;
|
|
|
|
// Handle common actions across all modes
|
|
if UiStateHandler::toggle_sidebar(&mut app_state.ui, config, key_code, modifiers) {
|
|
return Ok((false, format!("Sidebar {}",
|
|
if app_state.ui.show_sidebar { "shown" } else { "hidden" }
|
|
)));
|
|
}
|
|
|
|
// Mode-specific handling
|
|
match current_mode {
|
|
AppMode::General => {
|
|
return navigation::handle_navigation_event(
|
|
key,
|
|
config,
|
|
form_state,
|
|
app_state,
|
|
&mut self.command_mode,
|
|
&mut self.command_input,
|
|
&mut self.command_message,
|
|
).await;
|
|
},
|
|
|
|
AppMode::ReadOnly => {
|
|
// Check for mode transitions first
|
|
if config.is_enter_edit_mode_before(key_code, modifiers) &&
|
|
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((false, self.command_message.clone()));
|
|
}
|
|
|
|
if config.is_enter_edit_mode_after(key_code, modifiers) &&
|
|
ModeManager::can_enter_edit_mode(current_mode) {
|
|
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()));
|
|
}
|
|
|
|
// Check for entering command mode
|
|
if let Some(action) = config.get_read_only_action_for_key(key_code, modifiers) {
|
|
if action == "enter_command_mode" && ModeManager::can_enter_command_mode(current_mode) {
|
|
self.command_mode = true;
|
|
self.command_input.clear();
|
|
self.command_message.clear();
|
|
return Ok((false, String::new()));
|
|
}
|
|
}
|
|
|
|
// Check for core application actions (save, quit, etc.)
|
|
// ONLY handle a limited subset of core actions here
|
|
if let Some(action) = config.get_action_for_key_in_mode(
|
|
&config.keybindings.common,
|
|
key_code,
|
|
modifiers
|
|
) {
|
|
match action {
|
|
"save" | "force_quit" | "save_and_quit" | "revert" => {
|
|
return common::handle_core_action(
|
|
action,
|
|
form_state,
|
|
grpc_client,
|
|
command_handler,
|
|
terminal,
|
|
app_state,
|
|
current_position,
|
|
total_count,
|
|
).await;
|
|
},
|
|
_ => {} // For other actions, let the mode-specific handler take care of it
|
|
}
|
|
}
|
|
|
|
// Let read_only mode handle its own actions (including navigation from common bindings)
|
|
return read_only::handle_read_only_event(
|
|
key,
|
|
config,
|
|
form_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;
|
|
},
|
|
|
|
AppMode::Edit => {
|
|
// Check for exiting edit mode
|
|
if config.is_exit_edit_mode(key_code, 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()));
|
|
}
|
|
|
|
// Check for core application actions (save, quit, etc.)
|
|
// ONLY handle a limited subset of core actions here
|
|
if let Some(action) = config.get_action_for_key_in_mode(
|
|
&config.keybindings.common,
|
|
key_code,
|
|
modifiers
|
|
) {
|
|
match action {
|
|
"save" | "force_quit" | "save_and_quit" | "revert" => {
|
|
return common::handle_core_action(
|
|
action,
|
|
form_state,
|
|
grpc_client,
|
|
command_handler,
|
|
terminal,
|
|
app_state,
|
|
current_position,
|
|
total_count,
|
|
).await;
|
|
},
|
|
_ => {} // For other actions, let the mode-specific handler take care of it
|
|
}
|
|
}
|
|
|
|
// Let edit mode handle its own actions (including navigation from common bindings)
|
|
let result = edit::handle_edit_event_internal(
|
|
key,
|
|
config,
|
|
form_state,
|
|
&mut self.ideal_cursor_column,
|
|
&mut self.command_message,
|
|
&mut app_state.ui.is_saved,
|
|
current_position,
|
|
total_count,
|
|
grpc_client,
|
|
).await?;
|
|
|
|
self.key_sequence_tracker.reset();
|
|
return Ok((false, result));
|
|
},
|
|
|
|
AppMode::Command => {
|
|
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,
|
|
command_handler,
|
|
terminal,
|
|
current_position,
|
|
total_count,
|
|
).await?;
|
|
|
|
if exit_command_mode {
|
|
self.command_mode = false;
|
|
}
|
|
|
|
return Ok((should_exit, message));
|
|
}
|
|
}
|
|
}
|
|
|
|
// Non-key events or if no specific handler was matched
|
|
self.edit_mode_cooldown = false;
|
|
Ok((false, self.command_message.clone()))
|
|
}
|
|
|
|
|
|
}
|