From 1eaa716f422a561a089b284f63e82a9623574971 Mon Sep 17 00:00:00 2001 From: filipriec Date: Sat, 5 Apr 2025 00:42:56 +0200 Subject: [PATCH] working now, able to switch between form_e and auth_e --- client/src/modes/canvas/edit.rs | 130 +++++++++++++++++++---------- client/src/modes/handlers/event.rs | 40 ++++----- 2 files changed, 99 insertions(+), 71 deletions(-) diff --git a/client/src/modes/canvas/edit.rs b/client/src/modes/canvas/edit.rs index fb712e2..ba3bcae 100644 --- a/client/src/modes/canvas/edit.rs +++ b/client/src/modes/canvas/edit.rs @@ -2,16 +2,17 @@ use crate::config::binds::config::Config; use crate::services::grpc_client::GrpcClient; -use crate::state::canvas_state::CanvasState; -use crate::state::pages::form::FormState; -use crate::functions::modes::edit::form_e; +use crate::state::pages::{auth::AuthState, form::FormState}; +use crate::state::state::AppState; +use crate::functions::modes::edit::{auth_e, form_e}; use crossterm::event::{KeyCode, KeyEvent}; -use std::any::Any; -pub async fn handle_edit_event_internal( +pub async fn handle_edit_event( + is_auth_context: bool, key: KeyEvent, config: &Config, - state: &mut S, + form_state: &mut FormState, + auth_state: &mut AuthState, ideal_cursor_column: &mut usize, command_message: &mut String, is_saved: &mut bool, @@ -19,61 +20,98 @@ pub async fn handle_edit_event_internal( total_count: u64, grpc_client: &mut GrpcClient, ) -> Result> { - if let Some(action) = config.get_action_for_key_in_mode( + + // Global command mode check + if let Some("enter_command_mode") = config.get_action_for_key_in_mode( &config.keybindings.global, key.code, - key.modifiers, + key.modifiers ) { - if action == "enter_command_mode" { - *command_message = "Switching to Command Mode...".to_string(); - return Ok(command_message.clone()); - } + *command_message = "Switching to Command Mode...".to_string(); + return Ok(command_message.clone()); } + // Common actions (save/revert) if let Some(action) = config.get_action_for_key_in_mode( &config.keybindings.common, key.code, - key.modifiers, + key.modifiers ) { - if action == "save" || action == "revert" { - return form_e::execute_common_action( - action, - state, - grpc_client, - is_saved, - current_position, - total_count, - ) - .await; + if matches!(action, "save" | "revert") { + return if is_auth_context { + auth_e::execute_common_action( + action, + auth_state, // Concrete AuthState + grpc_client, + is_saved, + current_position, + total_count + ).await + } else { + form_e::execute_common_action( + action, + form_state, // Concrete FormState + grpc_client, + is_saved, + current_position, + total_count + ).await + }; } } + // Edit-specific actions if let Some(action) = config.get_edit_action_for_key(key.code, key.modifiers) { - return form_e::execute_edit_action( - action, - key, - state, - ideal_cursor_column, - grpc_client, - is_saved, - current_position, - total_count, - ) - .await; + return if is_auth_context { + auth_e::execute_edit_action( + action, + key, + auth_state, // Full access to AuthState fields + ideal_cursor_column, + grpc_client, + is_saved, + current_position, + total_count + ).await + } else { + form_e::execute_edit_action( + action, + key, + form_state, // Full access to FormState fields + ideal_cursor_column, + grpc_client, + is_saved, + current_position, + total_count + ).await + }; } - if let KeyCode::Char(c) = key.code { - return form_e::execute_edit_action( - "insert_char", - key, - state, - ideal_cursor_column, - grpc_client, - is_saved, - current_position, - total_count, - ) - .await; + // Character insertion + if let KeyCode::Char(_) = key.code { + return if is_auth_context { + auth_e::execute_edit_action( + "insert_char", + key, + auth_state, + ideal_cursor_column, + grpc_client, + is_saved, + current_position, + total_count + ).await + } else { + form_e::execute_edit_action( + "insert_char", + key, + form_state, + ideal_cursor_column, + grpc_client, + is_saved, + current_position, + total_count + ).await + }; } Ok(command_message.clone()) diff --git a/client/src/modes/handlers/event.rs b/client/src/modes/handlers/event.rs index f246ab6..9e9ee4c 100644 --- a/client/src/modes/handlers/event.rs +++ b/client/src/modes/handlers/event.rs @@ -210,31 +210,21 @@ impl EventHandler { } // Let edit mode handle its own actions (including navigation from common bindings) - let result = if app_state.ui.show_login { - edit::handle_edit_event_internal( - key, - config, - &mut self.auth_state, // Use auth_state instead of form_state - &mut self.ideal_cursor_column, - &mut self.command_message, - &mut app_state.ui.is_saved, - current_position, - total_count, - grpc_client, - ).await? - } else { - 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? - }; + let result = edit::handle_edit_event( + app_state.ui.show_login, + key, + config, + form_state, + &mut self.auth_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));