compiled if statement in the read_only mode

This commit is contained in:
filipriec
2025-03-30 13:57:22 +02:00
parent 0a4f59cf8e
commit ed99ebf541
4 changed files with 99 additions and 32 deletions

View File

@@ -1,4 +1,5 @@
// src/modes/handlers/read_only.rs
// src/modes/canvas/read_only.rs
use crossterm::event::{KeyEvent}; use crossterm::event::{KeyEvent};
use crate::config::binds::config::Config; use crate::config::binds::config::Config;
@@ -14,6 +15,7 @@ enum CharType {
} }
pub async fn handle_read_only_event( pub async fn handle_read_only_event(
app_state: &crate::state::state::AppState,
key: KeyEvent, key: KeyEvent,
config: &Config, config: &Config,
form_state: &mut FormState, form_state: &mut FormState,
@@ -50,7 +52,17 @@ pub async fn handle_read_only_event(
// Try to match the current sequence against Read-Only mode bindings // Try to match the current sequence against Read-Only mode bindings
if let Some(action) = config.matches_key_sequence_generalized(&sequence) { if let Some(action) = config.matches_key_sequence_generalized(&sequence) {
let result = execute_action( let result = if action == "previous_entry" && app_state.ui.show_form {
crate::tui::functions::form::handle_action(
action,
form_state,
grpc_client,
current_position,
total_count,
ideal_cursor_column,
).await?
} else {
execute_action(
action, action,
form_state, form_state,
ideal_cursor_column, ideal_cursor_column,
@@ -59,7 +71,8 @@ pub async fn handle_read_only_event(
current_position, current_position,
total_count, total_count,
grpc_client, grpc_client,
).await?; ).await?
};
key_sequence_tracker.reset(); key_sequence_tracker.reset();
return Ok((false, result)); return Ok((false, result));
} }
@@ -72,7 +85,17 @@ pub async fn handle_read_only_event(
// Since it's not part of a multi-key sequence, check for a direct action // Since it's not part of a multi-key sequence, check for a direct action
if sequence.len() == 1 && !config.is_key_sequence_prefix(&sequence) { if sequence.len() == 1 && !config.is_key_sequence_prefix(&sequence) {
if let Some(action) = config.get_read_only_action_for_key(key.code, key.modifiers) { if let Some(action) = config.get_read_only_action_for_key(key.code, key.modifiers) {
let result = execute_action( let result = if action == "previous_entry" && app_state.ui.show_form {
crate::tui::functions::form::handle_action(
action,
form_state,
grpc_client,
current_position,
total_count,
ideal_cursor_column,
).await?
} else {
execute_action(
action, action,
form_state, form_state,
ideal_cursor_column, ideal_cursor_column,
@@ -81,7 +104,8 @@ pub async fn handle_read_only_event(
current_position, current_position,
total_count, total_count,
grpc_client, grpc_client,
).await?; ).await?
};
key_sequence_tracker.reset(); key_sequence_tracker.reset();
return Ok((false, result)); return Ok((false, result));
} }
@@ -91,7 +115,17 @@ pub async fn handle_read_only_event(
key_sequence_tracker.reset(); key_sequence_tracker.reset();
if let Some(action) = config.get_read_only_action_for_key(key.code, key.modifiers) { if let Some(action) = config.get_read_only_action_for_key(key.code, key.modifiers) {
let result = execute_action( let result = if action == "previous_entry" && app_state.ui.show_form {
crate::tui::functions::form::handle_action(
action,
form_state,
grpc_client,
current_position,
total_count,
ideal_cursor_column,
).await?
} else {
execute_action(
action, action,
form_state, form_state,
ideal_cursor_column, ideal_cursor_column,
@@ -100,7 +134,8 @@ pub async fn handle_read_only_event(
current_position, current_position,
total_count, total_count,
grpc_client, grpc_client,
).await?; ).await?
};
return Ok((false, result)); return Ok((false, result));
} }
} }
@@ -134,6 +169,7 @@ async fn execute_action(
*current_position = new_position; *current_position = new_position;
match grpc_client.get_adresar_by_position(*current_position).await { match grpc_client.get_adresar_by_position(*current_position).await {
Ok(response) => { Ok(response) => {
// Replace update_from_response with direct field assignments
form_state.id = response.id; form_state.id = response.id;
form_state.values = vec![ form_state.values = vec![
response.firma, response.kz, response.drc, response.firma, response.kz, response.drc,

View File

@@ -141,6 +141,7 @@ impl EventHandler {
// Let read_only mode handle its own actions (including navigation from common bindings) // Let read_only mode handle its own actions (including navigation from common bindings)
return read_only::handle_read_only_event( return read_only::handle_read_only_event(
&app_state,
key, key,
config, config,
form_state, form_state,

View File

@@ -73,7 +73,7 @@ impl FormState {
.expect("Invalid current_field index") .expect("Invalid current_field index")
} }
fn update_from_response(&mut self, response: common::proto::multieko2::adresar::AdresarResponse) { pub fn update_from_response(&mut self, response: common::proto::multieko2::adresar::AdresarResponse) {
self.id = response.id; self.id = response.id;
self.values = vec![ self.values = vec![
response.firma, response.kz, response.drc, response.firma, response.kz, response.drc,

View File

@@ -0,0 +1,30 @@
// src/tui/functions/form.rs
use crate::state::pages::form::FormState;
use crate::tui::terminal::GrpcClient;
pub async fn handle_action(
action: &str,
form_state: &mut FormState,
grpc_client: &mut GrpcClient,
current_position: &mut u64,
total_count: u64,
ideal_cursor_column: &mut usize,
) -> Result<String, Box<dyn std::error::Error>> {
match action {
"previous_entry" => {
// Form-specific previous entry logic
let new_position = current_position.saturating_sub(1);
if new_position >= 1 {
*current_position = new_position;
let response = grpc_client.get_adresar_by_position(*current_position).await?;
form_state.update_from_response(response);
Ok(format!("Loaded form entry {}", *current_position))
} else {
Ok("Already at first form entry".into())
}
}
// Other form-specific actions...
_ => Err("Unknown form action".into())
}
}