now for common in canvas

This commit is contained in:
filipriec
2025-03-24 14:54:02 +01:00
parent 1fc9a0e1ff
commit aa3ff18f9c
2 changed files with 70 additions and 46 deletions

View File

@@ -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<dyn std::error::Error>> {
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<String> {
// 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,

View File

@@ -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<dyn std::error::Error>> {
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))),
}
}
}