moved to form_ro

This commit is contained in:
filipriec
2025-04-03 22:12:20 +02:00
parent 6d6fff474f
commit 2e912dd261
4 changed files with 84 additions and 141 deletions

View File

@@ -19,14 +19,92 @@ pub async fn execute_action<S: CanvasState>(
command_message: &mut String,
) -> Result<String, Box<dyn Error>> {
match action {
"previous_entry" | "next_entry" | "move_up" | "move_down" |
"move_first_line" | "move_last_line" => {
"previous_entry" | "next_entry" => {
key_sequence_tracker.reset();
Ok(format!(
"Action '{}' should be handled by context-specific logic",
action
))
}
"move_up" => {
key_sequence_tracker.reset();
let num_fields = state.fields().len();
if num_fields == 0 {
return Ok("No fields to navigate.".to_string());
}
let current_field = state.current_field();
let new_field = if current_field == 0 {
num_fields - 1
} else {
current_field - 1
};
state.set_current_field(new_field);
let current_input = state.get_current_input();
let max_cursor_pos = if current_input.is_empty() {
0
} else {
current_input.len().saturating_sub(1)
};
let new_pos = (*ideal_cursor_column).min(max_cursor_pos);
state.set_current_cursor_pos(new_pos);
Ok("".to_string())
}
"move_down" => {
key_sequence_tracker.reset();
let num_fields = state.fields().len();
if num_fields == 0 {
return Ok("No fields to navigate.".to_string());
}
let current_field = state.current_field();
let new_field = (current_field + 1) % num_fields;
state.set_current_field(new_field);
let current_input = state.get_current_input();
let max_cursor_pos = if current_input.is_empty() {
0
} else {
current_input.len().saturating_sub(1)
};
let new_pos = (*ideal_cursor_column).min(max_cursor_pos);
state.set_current_cursor_pos(new_pos);
Ok("".to_string())
}
"move_first_line" => {
key_sequence_tracker.reset();
let num_fields = state.fields().len();
if num_fields == 0 {
return Ok("No fields to navigate to.".to_string());
}
state.set_current_field(0);
let current_input = state.get_current_input();
let max_cursor_pos = if current_input.is_empty() {
0
} else {
current_input.len().saturating_sub(1)
};
let new_pos = (*ideal_cursor_column).min(max_cursor_pos);
state.set_current_cursor_pos(new_pos);
*ideal_cursor_column = new_pos;
Ok("".to_string())
}
"move_last_line" => {
key_sequence_tracker.reset();
let num_fields = state.fields().len();
if num_fields == 0 {
return Ok("No fields to navigate to.".to_string());
}
let last_field_index = num_fields - 1;
state.set_current_field(last_field_index);
let current_input = state.get_current_input();
let max_cursor_pos = if current_input.is_empty() {
0
} else {
current_input.len().saturating_sub(1)
};
let new_pos = (*ideal_cursor_column).min(max_cursor_pos);
state.set_current_cursor_pos(new_pos);
*ideal_cursor_column = new_pos;
Ok("".to_string())
}
"exit_edit_mode" => {
key_sequence_tracker.reset();
command_message.clear();

View File

@@ -59,10 +59,6 @@ pub async fn handle_read_only_event(
const CONTEXT_ACTIONS_FORM: &[&str] = &[
"previous_entry",
"next_entry",
"move_up",
"move_down",
"move_first_line",
"move_last_line",
];
const CONTEXT_ACTIONS_LOGIN: &[&str] = &[
"move_up",

View File

@@ -75,92 +75,6 @@ pub async fn handle_action(
Ok("Already at last entry".into())
}
}
"move_first_line" => {
// *** GUARD CLAUSE ***
if form_state.fields.is_empty() {
// Or log an error, or do nothing gracefully
return Ok("No fields to navigate to.".to_string());
}
// Set the field index
form_state.set_current_field(0);
// Get input of the *new* current field (index 0)
let current_input = form_state.get_current_input();
// Calculate the maximum valid cursor position for read-only mode
// Cursor should be ON the last char, or at 0 if empty.
let max_cursor_pos = if current_input.is_empty() {
0
} else {
current_input.len().saturating_sub(1)
};
// Set the cursor position, clamped by ideal column and max valid position
form_state.set_current_cursor_pos((*ideal_cursor_column).min(max_cursor_pos));
Ok("".to_string()) // Or a confirmation message
}
"move_last_line" => {
// *** GUARD CLAUSE ***
if form_state.fields.is_empty() {
return Ok("No fields to navigate to.".to_string());
}
let last_field_index = form_state.fields.len() - 1;
form_state.set_current_field(last_field_index);
let current_input = form_state.get_current_input();
let max_cursor_pos = if current_input.is_empty() {
0
} else {
current_input.len().saturating_sub(1)
};
form_state.set_current_cursor_pos((*ideal_cursor_column).min(max_cursor_pos));
Ok("".to_string())
}
"move_up" => {
if form_state.fields.is_empty() {
return Ok("No fields to navigate.".to_string());
}
let current_field = form_state.current_field();
let new_field = if current_field == 0 {
form_state.fields.len() - 1
} else {
current_field - 1
};
form_state.set_current_field(new_field);
let current_input = form_state.get_current_input();
let max_cursor_pos = if current_input.is_empty() {
0
} else {
current_input.len().saturating_sub(1) // Adjust for read-only if needed
};
form_state.set_current_cursor_pos((*ideal_cursor_column).min(max_cursor_pos));
Ok("".to_string())
}
"move_down" => {
if form_state.fields.is_empty() {
return Ok("No fields to navigate.".to_string());
}
let current_field = form_state.current_field();
let new_field = (current_field + 1) % form_state.fields.len();
form_state.set_current_field(new_field);
let current_input = form_state.get_current_input();
let max_cursor_pos = if current_input.is_empty() {
0
} else {
current_input.len().saturating_sub(1) // Adjust for read-only if needed
};
form_state.set_current_cursor_pos((*ideal_cursor_column).min(max_cursor_pos));
Ok("".to_string())
}
_ => Err("Unknown form action".into())
}
}

View File

@@ -8,56 +8,11 @@ pub async fn handle_action(
ideal_cursor_column: &mut usize,
) -> Result<String, Box<dyn std::error::Error>> {
match action {
"move_up" => {
if auth_state.return_selected {
// From Return button to last field (password)
auth_state.return_selected = false;
auth_state.current_field = 1;
} else if auth_state.current_field == 1 {
// Password -> Username
auth_state.current_field = 0;
} else if auth_state.current_field == 0 {
// Username -> Password (wrap around fields only)
auth_state.current_field = 1;
} else if auth_state.current_field == 2 {
// From Login button to Password field
auth_state.current_field = 1;
}
// Update cursor position only when in a field
if auth_state.current_field < 2 {
let current_input = auth_state.get_current_input();
let max_cursor_pos = current_input.len();
auth_state.current_cursor_pos = (*ideal_cursor_column).min(max_cursor_pos);
}
Ok(format!("Navigation 'up' from functions/login"))
"previous_entry" => {
Ok("Previous entry at tui/functions/login.rs not implemented".into())
}
"move_down" => {
if auth_state.return_selected {
// From Return button to first field (username)
auth_state.return_selected = false;
auth_state.current_field = 0;
} else if auth_state.current_field == 0 {
// Username -> Password
auth_state.current_field = 1;
} else if auth_state.current_field == 1 {
// Password -> Login button
auth_state.current_field = 2;
auth_state.return_selected = false;
} else if auth_state.current_field == 2 {
// Login button -> Return button
auth_state.return_selected = true;
}
// Update cursor position only when in a field
if auth_state.current_field < 2 {
let current_input = auth_state.get_current_input();
let max_cursor_pos = current_input.len();
auth_state.current_cursor_pos = (*ideal_cursor_column).min(max_cursor_pos);
}
Ok(format!("Navigation 'down' from functions/login"))
"next_entry" => {
Ok("Next entry at tui/functions/login.rs not implemented".into())
}
_ => Err("Unknown login action".into())
}