working now, able to switch between form_e and auth_e

This commit is contained in:
filipriec
2025-04-05 00:42:56 +02:00
parent 704bb7401a
commit 1eaa716f42
2 changed files with 99 additions and 71 deletions

View File

@@ -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<S: CanvasState + Any>(
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<S: CanvasState + Any>(
total_count: u64,
grpc_client: &mut GrpcClient,
) -> Result<String, Box<dyn std::error::Error>> {
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())

View File

@@ -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));