From aa3ff18f9c6d5c3c00f1f7f4416a9b022566c411 Mon Sep 17 00:00:00 2001 From: filipriec Date: Mon, 24 Mar 2025 14:54:02 +0100 Subject: [PATCH] now for common in canvas --- client/src/modes/canvas/common.rs | 67 ++++++++++++++++++++++++++++++ client/src/modes/handlers/event.rs | 49 ++-------------------- 2 files changed, 70 insertions(+), 46 deletions(-) diff --git a/client/src/modes/canvas/common.rs b/client/src/modes/canvas/common.rs index 984695f..74a1ba2 100644 --- a/client/src/modes/canvas/common.rs +++ b/client/src/modes/canvas/common.rs @@ -1,9 +1,76 @@ // src/modes/canvas/common.rs +use crossterm::event::{KeyEvent}; +use crate::config::binds::config::Config; use crate::tui::terminal::grpc_client::GrpcClient; +use crate::tui::terminal::core::TerminalCore; +use crate::tui::terminal::commands::CommandHandler; use crate::ui::handlers::form::FormState; +use crate::state::state::AppState; use common::proto::multieko2::adresar::{PostAdresarRequest, PutAdresarRequest}; +/// Main handler for common core actions +pub async fn handle_core_action( + action: &str, + form_state: &mut FormState, + grpc_client: &mut GrpcClient, + command_handler: &mut CommandHandler, + terminal: &mut TerminalCore, + app_state: &mut AppState, + current_position: &mut u64, + total_count: u64, +) -> Result<(bool, String), Box> { + match action { + "save" => { + let message = save( + form_state, + grpc_client, + &mut app_state.ui.is_saved, + current_position, + total_count, + ).await?; + Ok((false, message)) + }, + "force_quit" => { + let (should_exit, message) = command_handler.handle_command("force_quit", terminal).await?; + Ok((should_exit, message)) + }, + "save_and_quit" => { + let (should_exit, message) = command_handler.handle_command("save_and_quit", terminal).await?; + Ok((should_exit, message)) + }, + "revert" => { + let message = revert( + form_state, + grpc_client, + current_position, + total_count, + ).await?; + Ok((false, message)) + }, + // We should never hit this case with proper filtering + _ => Ok((false, format!("Core action not handled: {}", action))), + } +} + +/// Helper function to check if a key event should trigger a core action +pub fn is_core_action(config: &Config, key_code: crossterm::event::KeyCode, modifiers: crossterm::event::KeyModifiers) -> Option { + // Check for core application actions (save, quit, etc.) + 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 Some(action.to_string()) + }, + _ => {} // Other actions are handled by their respective mode handlers + } + } + None +} + /// Shared logic for saving the current form state pub async fn save( form_state: &mut FormState, diff --git a/client/src/modes/handlers/event.rs b/client/src/modes/handlers/event.rs index 9150501..581919d 100644 --- a/client/src/modes/handlers/event.rs +++ b/client/src/modes/handlers/event.rs @@ -125,7 +125,7 @@ impl EventHandler { ) { match action { "save" | "force_quit" | "save_and_quit" | "revert" => { - return self.handle_core_action( + return common::handle_core_action( action, form_state, grpc_client, @@ -184,7 +184,7 @@ impl EventHandler { ) { match action { "save" | "force_quit" | "save_and_quit" | "revert" => { - return self.handle_core_action( + return common::handle_core_action( action, form_state, grpc_client, @@ -243,48 +243,5 @@ impl EventHandler { Ok((false, self.command_message.clone())) } - // Helper method for handling core application actions (not navigation) - async fn handle_core_action( - &mut self, - action: &str, - form_state: &mut FormState, - grpc_client: &mut GrpcClient, - command_handler: &mut CommandHandler, - terminal: &mut TerminalCore, - app_state: &mut crate::state::state::AppState, - current_position: &mut u64, - total_count: u64, - ) -> Result<(bool, String), Box> { - match action { - "save" => { - let message = common::save( - form_state, - grpc_client, - &mut app_state.ui.is_saved, - current_position, - total_count, - ).await?; - Ok((false, message)) - }, - "force_quit" => { - let (should_exit, message) = command_handler.handle_command("force_quit", terminal).await?; - Ok((should_exit, message)) - }, - "save_and_quit" => { - let (should_exit, message) = command_handler.handle_command("save_and_quit", terminal).await?; - Ok((should_exit, message)) - }, - "revert" => { - let message = common::revert( - form_state, - grpc_client, - current_position, - total_count, - ).await?; - Ok((false, message)) - }, - // We should never hit this case given our filtering above - _ => Ok((false, format!("Core action not handled: {}", action))), - } - } + }