Compare commits

...

2 Commits

Author SHA1 Message Date
filipriec
d577ff6715 edit mode is like a readonly mode 2025-03-31 23:56:58 +02:00
filipriec
ca75c90ee9 changed read_only mode also 2025-03-31 23:37:02 +02:00
4 changed files with 316 additions and 173 deletions

View File

@@ -6,8 +6,8 @@ use crate::state::state::AppState;
use crate::services::grpc_client::GrpcClient; use crate::services::grpc_client::GrpcClient;
use crate::services::auth::AuthClient; use crate::services::auth::AuthClient;
use crate::tui::functions::common::{ use crate::tui::functions::common::{
form::{save as form_save, revert}, form::{save as form_save, revert as form_revert},
login::{save as login_save, cancel} login::{save as login_save, revert as login_revert}
}; };
pub async fn handle_core_action( pub async fn handle_core_action(
@@ -58,10 +58,10 @@ pub async fn handle_core_action(
}, },
"revert" => { "revert" => {
if app_state.ui.show_login { if app_state.ui.show_login {
let message = cancel(auth_state, app_state).await; let message = login_revert(auth_state, app_state).await;
Ok((false, message)) Ok((false, message))
} else { } else {
let message = revert( let message = form_revert(
form_state, form_state,
grpc_client, grpc_client,
current_position, current_position,

View File

@@ -1,11 +1,12 @@
// src/modes/canvas/read_only.rs // src/modes/canvas/read_only.rs
use crossterm::event::{KeyEvent};
use crate::config::binds::config::Config; use crate::config::binds::config::Config;
use crate::state::pages::form::FormState;
use crate::state::pages::auth::AuthState;
use crate::services::grpc_client::GrpcClient;
use crate::config::binds::key_sequences::KeySequenceTracker; use crate::config::binds::key_sequences::KeySequenceTracker;
use crate::services::grpc_client::GrpcClient;
use crate::state::canvas_state::CanvasState; // Import the trait
use crate::state::pages::auth::AuthState;
use crate::state::pages::form::FormState;
use crossterm::event::KeyEvent;
#[derive(PartialEq)] #[derive(PartialEq)]
enum CharType { enum CharType {
@@ -18,12 +19,12 @@ pub async fn handle_read_only_event(
app_state: &crate::state::state::AppState, app_state: &crate::state::state::AppState,
key: KeyEvent, key: KeyEvent,
config: &Config, config: &Config,
form_state: &mut FormState, form_state: &mut FormState, // Keep specific types here for routing
auth_state: &mut AuthState, auth_state: &mut AuthState, // Keep specific types here for routing
key_sequence_tracker: &mut KeySequenceTracker, key_sequence_tracker: &mut KeySequenceTracker,
current_position: &mut u64, current_position: &mut u64, // Needed for form actions
total_count: u64, total_count: u64, // Needed for form actions
grpc_client: &mut GrpcClient, grpc_client: &mut GrpcClient, // Needed for form actions
command_message: &mut String, command_message: &mut String,
edit_mode_cooldown: &mut bool, edit_mode_cooldown: &mut bool,
ideal_cursor_column: &mut usize, ideal_cursor_column: &mut usize,
@@ -32,17 +33,37 @@ pub async fn handle_read_only_event(
if config.is_enter_edit_mode_before(key.code, key.modifiers) { if config.is_enter_edit_mode_before(key.code, key.modifiers) {
*edit_mode_cooldown = true; *edit_mode_cooldown = true;
*command_message = "Entering Edit mode".to_string(); *command_message = "Entering Edit mode".to_string();
// The actual mode switch happens in event.rs based on this return
return Ok((false, command_message.clone())); return Ok((false, command_message.clone()));
} }
if config.is_enter_edit_mode_after(key.code, key.modifiers) { if config.is_enter_edit_mode_after(key.code, key.modifiers) {
let current_input = form_state.get_current_input(); // Use the correct state based on context
if !current_input.is_empty() && form_state.current_cursor_pos < current_input.len() { let (current_input, current_pos) = if app_state.ui.show_login {
form_state.current_cursor_pos += 1; (
*ideal_cursor_column = form_state.current_cursor_pos; auth_state.get_current_input(),
auth_state.current_cursor_pos(),
)
} else {
(
form_state.get_current_input(),
form_state.current_cursor_pos(),
)
};
if !current_input.is_empty() && current_pos < current_input.len() {
// Update the correct state
if app_state.ui.show_login {
auth_state.set_current_cursor_pos(current_pos + 1);
*ideal_cursor_column = auth_state.current_cursor_pos();
} else {
form_state.set_current_cursor_pos(current_pos + 1);
*ideal_cursor_column = form_state.current_cursor_pos();
}
} }
*edit_mode_cooldown = true; *edit_mode_cooldown = true;
*command_message = "Entering Edit mode (after cursor)".to_string(); *command_message = "Entering Edit mode (after cursor)".to_string();
// The actual mode switch happens in event.rs based on this return
return Ok((false, command_message.clone())); return Ok((false, command_message.clone()));
} }
@@ -52,9 +73,13 @@ pub async fn handle_read_only_event(
let sequence = key_sequence_tracker.get_sequence(); let sequence = key_sequence_tracker.get_sequence();
// 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 = if (action == "previous_entry" || action == "next_entry" || {
action == "move_up" || action == "move_down") && app_state.ui.show_form { // Handle context-specific actions first
let result = if app_state.ui.show_form
&& (action == "previous_entry" || action == "next_entry")
{
// Form-specific navigation
crate::tui::functions::form::handle_action( crate::tui::functions::form::handle_action(
action, action,
form_state, form_state,
@@ -62,24 +87,52 @@ pub async fn handle_read_only_event(
current_position, current_position,
total_count, total_count,
ideal_cursor_column, ideal_cursor_column,
).await? )
} else if (action == "move_up" || action == "move_down") && app_state.ui.show_login { .await?
} else if app_state.ui.show_login
&& (action == "move_up" || action == "move_down")
{
// Login-specific field navigation
crate::tui::functions::login::handle_action( crate::tui::functions::login::handle_action(
action, action,
auth_state, auth_state,
ideal_cursor_column, ideal_cursor_column,
).await? )
} else { .await?
execute_action( } else if app_state.ui.show_form
&& (action == "move_up" || action == "move_down")
{
// Form-specific field navigation (can reuse login handler logic if identical)
crate::tui::functions::form::handle_action(
action, action,
form_state, form_state,
grpc_client, // Might not be needed for simple up/down
current_position,
total_count,
ideal_cursor_column,
)
.await?
} else {
// Handle common navigation actions generically
if app_state.ui.show_login {
execute_action(
action,
auth_state, // Pass AuthState
ideal_cursor_column, ideal_cursor_column,
key_sequence_tracker, key_sequence_tracker,
command_message, command_message,
current_position, )
total_count, .await?
grpc_client, } else {
).await? execute_action(
action,
form_state, // Pass FormState
ideal_cursor_column,
key_sequence_tracker,
command_message,
)
.await?
}
}; };
key_sequence_tracker.reset(); key_sequence_tracker.reset();
return Ok((false, result)); return Ok((false, result));
@@ -92,8 +145,14 @@ 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) =
let result = if action == "previous_entry" && app_state.ui.show_form { config.get_read_only_action_for_key(key.code, key.modifiers)
{
// Handle context-specific actions first
let result = if app_state.ui.show_form
&& action == "previous_entry"
{
// Form-specific navigation
crate::tui::functions::form::handle_action( crate::tui::functions::form::handle_action(
action, action,
form_state, form_state,
@@ -101,18 +160,29 @@ pub async fn handle_read_only_event(
current_position, current_position,
total_count, total_count,
ideal_cursor_column, ideal_cursor_column,
).await? )
.await?
} else { } else {
// Handle common navigation actions generically
if app_state.ui.show_login {
execute_action( execute_action(
action, action,
form_state, auth_state, // Pass AuthState
ideal_cursor_column, ideal_cursor_column,
key_sequence_tracker, key_sequence_tracker,
command_message, command_message,
current_position, )
total_count, .await?
grpc_client, } else {
).await? execute_action(
action,
form_state, // Pass FormState
ideal_cursor_column,
key_sequence_tracker,
command_message,
)
.await?
}
}; };
key_sequence_tracker.reset(); key_sequence_tracker.reset();
return Ok((false, result)); return Ok((false, result));
@@ -122,8 +192,14 @@ pub async fn handle_read_only_event(
// If modifiers are pressed, check for direct key bindings // If modifiers are pressed, check for direct key bindings
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) =
let result = if action == "previous_entry" && app_state.ui.show_form { config.get_read_only_action_for_key(key.code, key.modifiers)
{
// Handle context-specific actions first
let result = if app_state.ui.show_form
&& action == "previous_entry"
{
// Form-specific navigation
crate::tui::functions::form::handle_action( crate::tui::functions::form::handle_action(
action, action,
form_state, form_state,
@@ -131,18 +207,29 @@ pub async fn handle_read_only_event(
current_position, current_position,
total_count, total_count,
ideal_cursor_column, ideal_cursor_column,
).await? )
.await?
} else { } else {
// Handle common navigation actions generically
if app_state.ui.show_login {
execute_action( execute_action(
action, action,
form_state, auth_state, // Pass AuthState
ideal_cursor_column, ideal_cursor_column,
key_sequence_tracker, key_sequence_tracker,
command_message, command_message,
current_position, )
total_count, .await?
grpc_client, } else {
).await? execute_action(
action,
form_state, // Pass FormState
ideal_cursor_column,
key_sequence_tracker,
command_message,
)
.await?
}
}; };
return Ok((false, result)); return Ok((false, result));
} }
@@ -151,7 +238,10 @@ pub async fn handle_read_only_event(
// Show a helpful message when no binding was found // Show a helpful message when no binding was found
if !*edit_mode_cooldown { if !*edit_mode_cooldown {
let default_key = "i".to_string(); let default_key = "i".to_string();
let edit_key = config.keybindings.read_only.get("enter_edit_mode_before") let edit_key = config
.keybindings
.read_only
.get("enter_edit_mode_before")
.and_then(|keys| keys.first()) .and_then(|keys| keys.first())
.unwrap_or(&default_key); .unwrap_or(&default_key);
*command_message = format!("Read-only mode - press {} to edit", edit_key); *command_message = format!("Read-only mode - press {} to edit", edit_key);
@@ -160,128 +250,155 @@ pub async fn handle_read_only_event(
Ok((false, command_message.clone())) Ok((false, command_message.clone()))
} }
async fn execute_action( // Make this function generic over CanvasState
async fn execute_action<S: CanvasState>(
action: &str, action: &str,
form_state: &mut FormState, state: &mut S, // Use generic state
ideal_cursor_column: &mut usize, ideal_cursor_column: &mut usize,
key_sequence_tracker: &mut KeySequenceTracker, key_sequence_tracker: &mut KeySequenceTracker, // Keep for resetting
command_message: &mut String, command_message: &mut String, // Keep for clearing
current_position: &mut u64,
total_count: u64,
grpc_client: &mut GrpcClient,
) -> Result<String, Box<dyn std::error::Error>> { ) -> Result<String, Box<dyn std::error::Error>> {
match action { match action {
// These actions are handled outside now based on context
"previous_entry" | "next_entry" => { "previous_entry" | "next_entry" => {
// This will only be called when no component is active
key_sequence_tracker.reset(); key_sequence_tracker.reset();
Ok(format!("Navigation prev/next only available in form mode")) Ok(format!(
"Action '{}' should be handled by context-specific logic",
action
))
} }
// These actions are handled outside now based on context
"move_up" | "move_down" => { "move_up" | "move_down" => {
// This will only be called when no component is active
key_sequence_tracker.reset(); key_sequence_tracker.reset();
Ok(format!("Navigation up/down only available in form mode")) Ok(format!(
"Action '{}' should be handled by context-specific logic",
action
))
} }
"exit_edit_mode" => { "exit_edit_mode" => {
// This action is primarily for Edit mode, but might be bound here too
key_sequence_tracker.reset(); key_sequence_tracker.reset();
command_message.clear(); command_message.clear();
Ok("".to_string()) Ok("".to_string())
} }
"move_left" => { "move_left" => {
let current_pos = form_state.current_cursor_pos; let current_pos = state.current_cursor_pos();
form_state.current_cursor_pos = current_pos.saturating_sub(1); let new_pos = current_pos.saturating_sub(1);
*ideal_cursor_column = form_state.current_cursor_pos; state.set_current_cursor_pos(new_pos); // Use trait setter
*ideal_cursor_column = new_pos;
Ok("".to_string()) Ok("".to_string())
} }
"move_right" => { "move_right" => {
let current_input = form_state.get_current_input(); let current_input = state.get_current_input();
let current_pos = form_state.current_cursor_pos; let current_pos = state.current_cursor_pos();
if !current_input.is_empty() && current_pos < current_input.len() - 1 { // In read-only, cursor stops AT the last character, not after
form_state.current_cursor_pos += 1; if !current_input.is_empty()
*ideal_cursor_column = form_state.current_cursor_pos; && current_pos < current_input.len().saturating_sub(1)
{
let new_pos = current_pos + 1;
state.set_current_cursor_pos(new_pos); // Use trait setter
*ideal_cursor_column = new_pos;
} }
Ok("".to_string()) Ok("".to_string())
} }
"move_word_next" => { "move_word_next" => {
let current_input = form_state.get_current_input(); let current_input = state.get_current_input();
if !current_input.is_empty() { if !current_input.is_empty() {
let new_pos = find_next_word_start(current_input, form_state.current_cursor_pos); let new_pos =
form_state.current_cursor_pos = new_pos.min(current_input.len().saturating_sub(1)); find_next_word_start(current_input, state.current_cursor_pos());
*ideal_cursor_column = form_state.current_cursor_pos; // Clamp to last valid character index in read-only
let final_pos = new_pos.min(current_input.len().saturating_sub(1));
state.set_current_cursor_pos(final_pos); // Use trait setter
*ideal_cursor_column = final_pos;
} }
Ok("".to_string()) Ok("".to_string())
} }
"move_word_end" => { "move_word_end" => {
let current_input = form_state.get_current_input(); let current_input = state.get_current_input();
if !current_input.is_empty() { if !current_input.is_empty() {
let new_pos = find_word_end(current_input, form_state.current_cursor_pos); let new_pos =
form_state.current_cursor_pos = new_pos.min(current_input.len().saturating_sub(1)); find_word_end(current_input, state.current_cursor_pos());
*ideal_cursor_column = form_state.current_cursor_pos; // Clamp to last valid character index in read-only
let final_pos = new_pos.min(current_input.len().saturating_sub(1));
state.set_current_cursor_pos(final_pos); // Use trait setter
*ideal_cursor_column = final_pos;
} }
Ok("".to_string()) Ok("".to_string())
} }
"move_word_prev" => { "move_word_prev" => {
let current_input = form_state.get_current_input(); let current_input = state.get_current_input();
if !current_input.is_empty() { if !current_input.is_empty() {
let new_pos = find_prev_word_start(current_input, form_state.current_cursor_pos); let new_pos = find_prev_word_start(
form_state.current_cursor_pos = new_pos; current_input,
*ideal_cursor_column = form_state.current_cursor_pos; state.current_cursor_pos(),
);
state.set_current_cursor_pos(new_pos); // Use trait setter
*ideal_cursor_column = new_pos;
} }
Ok("".to_string()) Ok("".to_string())
} }
"move_word_end_prev" => { "move_word_end_prev" => {
let current_input = form_state.get_current_input(); let current_input = state.get_current_input();
if !current_input.is_empty() { if !current_input.is_empty() {
let new_pos = find_prev_word_end(current_input, form_state.current_cursor_pos); let new_pos = find_prev_word_end(
form_state.current_cursor_pos = new_pos; current_input,
*ideal_cursor_column = form_state.current_cursor_pos; state.current_cursor_pos(),
);
state.set_current_cursor_pos(new_pos); // Use trait setter
*ideal_cursor_column = new_pos;
} }
Ok("Moved to previous word end".to_string()) Ok("Moved to previous word end".to_string())
} }
"move_line_start" => { "move_line_start" => {
form_state.current_cursor_pos = 0; state.set_current_cursor_pos(0); // Use trait setter
*ideal_cursor_column = form_state.current_cursor_pos; *ideal_cursor_column = 0;
Ok("".to_string()) Ok("".to_string())
} }
"move_line_end" => { "move_line_end" => {
let current_input = form_state.get_current_input(); let current_input = state.get_current_input();
if !current_input.is_empty() { if !current_input.is_empty() {
form_state.current_cursor_pos = current_input.len() - 1; let new_pos = current_input.len().saturating_sub(1);
*ideal_cursor_column = form_state.current_cursor_pos; state.set_current_cursor_pos(new_pos); // Use trait setter
*ideal_cursor_column = new_pos;
} else {
state.set_current_cursor_pos(0); // Handle empty input case
*ideal_cursor_column = 0;
} }
Ok("".to_string()) Ok("".to_string())
} }
"move_first_line" => { "move_first_line" => {
// Change field first // Field change is handled outside based on context
form_state.current_field = 0; // Just set cursor position based on ideal column
let current_input = state.get_current_input();
// Get current input AFTER changing field
let current_input = form_state.get_current_input();
let max_cursor_pos = if !current_input.is_empty() { let max_cursor_pos = if !current_input.is_empty() {
current_input.len() - 1 current_input.len().saturating_sub(1)
} else { } else {
current_input.len() 0
}; };
form_state.current_cursor_pos = (*ideal_cursor_column).min(max_cursor_pos); state.set_current_cursor_pos(
Ok("Moved to first line".to_string()) (*ideal_cursor_column).min(max_cursor_pos),
);
Ok("Moved to first line".to_string()) // Message might be inaccurate now
} }
"move_last_line" => { "move_last_line" => {
// Change field first // Field change is handled outside based on context
form_state.current_field = form_state.fields.len() - 1; // Just set cursor position based on ideal column
let current_input = state.get_current_input();
// Get current input AFTER changing field
let current_input = form_state.get_current_input();
let max_cursor_pos = if !current_input.is_empty() { let max_cursor_pos = if !current_input.is_empty() {
current_input.len() - 1 current_input.len().saturating_sub(1)
} else { } else {
current_input.len() 0
}; };
form_state.current_cursor_pos = (*ideal_cursor_column).min(max_cursor_pos); state.set_current_cursor_pos(
Ok("Moved to last line".to_string()) (*ideal_cursor_column).min(max_cursor_pos),
);
Ok("Moved to last line".to_string()) // Message might be inaccurate now
} }
_ => Ok(format!("Unknown action: {}", action)), _ => Ok(format!("Unknown read-only action: {}", action)),
} }
} }
// Helper functions remain unchanged as they operate on &str
fn get_char_type(c: char) -> CharType { fn get_char_type(c: char) -> CharType {
if c.is_whitespace() { if c.is_whitespace() {
CharType::Whitespace CharType::Whitespace
@@ -299,11 +416,18 @@ fn find_next_word_start(text: &str, current_pos: usize) -> usize {
} }
let mut pos = current_pos; let mut pos = current_pos;
// Handle edge case where current_pos might be out of bounds after edits
if pos >= chars.len() {
return chars.len();
}
let initial_type = get_char_type(chars[pos]); let initial_type = get_char_type(chars[pos]);
// Move past characters of the same type
while pos < chars.len() && get_char_type(chars[pos]) == initial_type { while pos < chars.len() && get_char_type(chars[pos]) == initial_type {
pos += 1; pos += 1;
} }
// Move past whitespace
while pos < chars.len() && get_char_type(chars[pos]) == CharType::Whitespace { while pos < chars.len() && get_char_type(chars[pos]) == CharType::Whitespace {
pos += 1; pos += 1;
} }
@@ -311,36 +435,29 @@ fn find_next_word_start(text: &str, current_pos: usize) -> usize {
pos pos
} }
fn find_word_end(text: &str, current_pos: usize) -> usize { fn find_word_end(text: &str, current_pos: usize) -> usize {
let chars: Vec<char> = text.chars().collect(); let chars: Vec<char> = text.chars().collect();
if chars.is_empty() { if chars.is_empty() {
return 0; return 0;
} }
// Handle edge case where current_pos might be out of bounds
let mut pos = current_pos.min(chars.len().saturating_sub(1));
if current_pos >= chars.len() - 1 { // If starting on whitespace, move to the next non-whitespace
return chars.len() - 1;
}
let mut pos = current_pos;
if get_char_type(chars[pos]) == CharType::Whitespace { if get_char_type(chars[pos]) == CharType::Whitespace {
while pos < chars.len() && get_char_type(chars[pos]) == CharType::Whitespace { while pos + 1 < chars.len() && get_char_type(chars[pos + 1]) == CharType::Whitespace {
pos += 1;
}
} else {
let current_type = get_char_type(chars[pos]);
if pos + 1 < chars.len() && get_char_type(chars[pos + 1]) != current_type {
pos += 1;
while pos < chars.len() && get_char_type(chars[pos]) == CharType::Whitespace {
pos += 1; pos += 1;
} }
// If we are still on whitespace (meaning end of string or only whitespace left), return current pos
if pos + 1 >= chars.len() || get_char_type(chars[pos + 1]) == CharType::Whitespace {
return pos;
} }
// Move to the start of the next word
pos += 1;
} }
if pos >= chars.len() { // Now we are on a non-whitespace character
return chars.len() - 1;
}
let word_type = get_char_type(chars[pos]); let word_type = get_char_type(chars[pos]);
while pos + 1 < chars.len() && get_char_type(chars[pos + 1]) == word_type { while pos + 1 < chars.len() && get_char_type(chars[pos + 1]) == word_type {
pos += 1; pos += 1;
@@ -349,6 +466,7 @@ fn find_word_end(text: &str, current_pos: usize) -> usize {
pos pos
} }
fn find_prev_word_start(text: &str, current_pos: usize) -> usize { fn find_prev_word_start(text: &str, current_pos: usize) -> usize {
let chars: Vec<char> = text.chars().collect(); let chars: Vec<char> = text.chars().collect();
if chars.is_empty() || current_pos == 0 { if chars.is_empty() || current_pos == 0 {
@@ -357,56 +475,64 @@ fn find_prev_word_start(text: &str, current_pos: usize) -> usize {
let mut pos = current_pos.saturating_sub(1); let mut pos = current_pos.saturating_sub(1);
// Move past whitespace
while pos > 0 && get_char_type(chars[pos]) == CharType::Whitespace { while pos > 0 && get_char_type(chars[pos]) == CharType::Whitespace {
pos -= 1; pos -= 1;
} }
// Now on a non-whitespace or at the beginning
if get_char_type(chars[pos]) != CharType::Whitespace { if get_char_type(chars[pos]) != CharType::Whitespace {
let word_type = get_char_type(chars[pos]); let word_type = get_char_type(chars[pos]);
// Move to the beginning of the word
while pos > 0 && get_char_type(chars[pos - 1]) == word_type { while pos > 0 && get_char_type(chars[pos - 1]) == word_type {
pos -= 1; pos -= 1;
} }
} }
// If pos is 0 and it's whitespace, keep it at 0. Otherwise, it's the start of the word.
if pos == 0 && get_char_type(chars[pos]) == CharType::Whitespace {
0
} else {
pos pos
}
} }
fn find_prev_word_end(text: &str, current_pos: usize) -> usize { fn find_prev_word_end(text: &str, current_pos: usize) -> usize {
let chars: Vec<char> = text.chars().collect(); let chars: Vec<char> = text.chars().collect();
if chars.is_empty() || current_pos <= 1 { if chars.is_empty() || current_pos <= 1 { // Need at least 2 chars to find a *previous* word end
return 0; return 0;
} }
let mut pos = current_pos.saturating_sub(1); let mut pos = current_pos.saturating_sub(1); // Start looking one char back
// Skip trailing whitespace from the current position
while pos > 0 && get_char_type(chars[pos]) == CharType::Whitespace { while pos > 0 && get_char_type(chars[pos]) == CharType::Whitespace {
pos -= 1; pos -= 1;
} }
if pos > 0 && get_char_type(chars[pos]) != CharType::Whitespace { // Now we are at the end of the word the cursor was in/after, or at the start
let word_type = get_char_type(chars[pos]); if pos == 0 {
return 0; // Reached the beginning
}
// Skip the word itself
let word_type = get_char_type(chars[pos]);
while pos > 0 && get_char_type(chars[pos - 1]) == word_type { while pos > 0 && get_char_type(chars[pos - 1]) == word_type {
pos -= 1; pos -= 1;
} }
// Skip whitespace before that word
while pos > 0 && get_char_type(chars[pos - 1]) == CharType::Whitespace { while pos > 0 && get_char_type(chars[pos - 1]) == CharType::Whitespace {
pos -= 1; pos -= 1;
} }
// Now pos is at the beginning of the word *or* the end of the *previous* word.
// If we moved back, pos-1 is the index we want.
if pos > 0 { if pos > 0 {
pos -= 1; pos - 1
let prev_word_type = get_char_type(chars[pos]); } else {
while pos > 0 && get_char_type(chars[pos - 1]) == prev_word_type { 0 // We were at the first word
pos -= 1;
} }
while pos < chars.len() - 1 &&
get_char_type(chars[pos + 1]) == prev_word_type {
pos += 1;
}
}
}
pos
} }

View File

@@ -4,5 +4,5 @@ pub mod form;
pub mod login; pub mod login;
pub use commands::*; pub use commands::*;
pub use form::{revert, save as form_save}; pub use form::{revert as form_revert, save as form_save};
pub use login::{cancel, save as login_save}; pub use login::{revert as login_revert, save as login_save};

View File

@@ -2,7 +2,10 @@
use crate::services::auth::AuthClient; use crate::services::auth::AuthClient;
use crate::state::pages::auth::AuthState; use crate::state::pages::auth::AuthState;
use crate::state::state::AppState; use crate::state::state::AppState;
use crate::state::canvas_state::CanvasState;
/// Attempts to log the user in using the provided credentials via gRPC.
/// Updates AuthState and AppState on success or failure.
pub async fn save( pub async fn save(
auth_state: &mut AuthState, auth_state: &mut AuthState,
auth_client: &mut AuthClient, auth_client: &mut AuthClient,
@@ -11,35 +14,49 @@ pub async fn save(
let identifier = auth_state.username.clone(); let identifier = auth_state.username.clone();
let password = auth_state.password.clone(); let password = auth_state.password.clone();
// Call the gRPC login method
match auth_client.login(identifier, password).await { match auth_client.login(identifier, password).await {
Ok(response) => { Ok(response) => {
// Store authentication details on success
auth_state.auth_token = Some(response.access_token); auth_state.auth_token = Some(response.access_token);
auth_state.user_id = Some(response.user_id); auth_state.user_id = Some(response.user_id);
auth_state.role = Some(response.role); auth_state.role = Some(response.role);
auth_state.error_message = None; auth_state.error_message = None;
auth_state.set_has_unsaved_changes(false); // Mark as "saved"
// Update app state to show main interface // Update app state to transition from login to the main form view
app_state.ui.show_login = false; app_state.ui.show_login = false;
app_state.ui.show_form = true; app_state.ui.show_form = true; // Assuming form is the next view
Ok("Login successful!".to_string()) Ok("Login successful!".to_string())
} }
Err(e) => { Err(e) => {
// Store error message on failure
let error_message = format!("Login failed: {}", e); let error_message = format!("Login failed: {}", e);
auth_state.error_message = Some(error_message.clone()); auth_state.error_message = Some(error_message.clone());
Ok(error_message) // Keep unsaved changes true if login fails, allowing retry/revert
auth_state.set_has_unsaved_changes(true);
Ok(error_message) // Return error message to display
} }
} }
} }
pub async fn cancel( /// Reverts the login form fields to empty and returns to the previous screen (Intro).
/// This function is now named 'revert' to match the 'form' counterpart.
pub async fn revert(
auth_state: &mut AuthState, auth_state: &mut AuthState,
app_state: &mut AppState, app_state: &mut AppState,
) -> String { ) -> String {
// Clear the input fields
auth_state.username.clear(); auth_state.username.clear();
auth_state.password.clear(); auth_state.password.clear();
auth_state.error_message = None; auth_state.error_message = None; // Clear any previous error
auth_state.set_has_unsaved_changes(false); // Fields are cleared, no unsaved changes
// Update app state to hide login and show the previous screen (Intro)
app_state.ui.show_login = false; app_state.ui.show_login = false;
app_state.ui.show_intro = true; app_state.ui.show_intro = true; // Assuming Intro is the screen before login
"Login canceled".to_string()
"Login reverted".to_string()
} }