From e36b1817bcc4b2abefc51a47013bca3041000d82 Mon Sep 17 00:00:00 2001 From: filipriec Date: Sun, 30 Mar 2025 15:46:49 +0200 Subject: [PATCH] working, split config where functions for each page are defined for this page, if the functions are not general for each page. Huge update, works for tui/functions/form and fui/functions/login. Working, time to move to other things --- client/src/modes/canvas/read_only.rs | 8 +++ client/src/modes/handlers/event.rs | 4 ++ client/src/tui/functions/login.rs | 78 +++++++++++++++++----------- 3 files changed, 61 insertions(+), 29 deletions(-) diff --git a/client/src/modes/canvas/read_only.rs b/client/src/modes/canvas/read_only.rs index eeb40f1..b9e0604 100644 --- a/client/src/modes/canvas/read_only.rs +++ b/client/src/modes/canvas/read_only.rs @@ -4,6 +4,7 @@ use crossterm::event::{KeyEvent}; use crate::config::binds::config::Config; use crate::state::pages::form::FormState; +use crate::state::pages::auth::AuthState; use crate::config::binds::key_sequences::KeySequenceTracker; use crate::tui::terminal::grpc_client::GrpcClient; @@ -19,6 +20,7 @@ pub async fn handle_read_only_event( key: KeyEvent, config: &Config, form_state: &mut FormState, + auth_state: &mut AuthState, key_sequence_tracker: &mut KeySequenceTracker, current_position: &mut u64, total_count: u64, @@ -62,6 +64,12 @@ pub async fn handle_read_only_event( total_count, ideal_cursor_column, ).await? + } else if (action == "move_up" || action == "move_down") && app_state.ui.show_login { + crate::tui::functions::login::handle_action( + action, + auth_state, + ideal_cursor_column, + ).await? } else { execute_action( action, diff --git a/client/src/modes/handlers/event.rs b/client/src/modes/handlers/event.rs index e870df0..7c08d90 100644 --- a/client/src/modes/handlers/event.rs +++ b/client/src/modes/handlers/event.rs @@ -8,6 +8,7 @@ use crate::tui::terminal::{ use crate::tui::controls::commands::CommandHandler; use crate::config::binds::config::Config; use crate::state::pages::form::FormState; +use crate::state::pages::auth::AuthState; use crate::ui::handlers::rat_state::UiStateHandler; use crate::modes::{ common::{command_mode}, @@ -25,6 +26,7 @@ pub struct EventHandler { pub edit_mode_cooldown: bool, pub ideal_cursor_column: usize, pub key_sequence_tracker: KeySequenceTracker, + pub auth_state: AuthState, } impl EventHandler { @@ -37,6 +39,7 @@ impl EventHandler { edit_mode_cooldown: false, ideal_cursor_column: 0, key_sequence_tracker: KeySequenceTracker::new(800), + auth_state: AuthState::new(), } } @@ -145,6 +148,7 @@ impl EventHandler { key, config, form_state, + &mut self.auth_state, &mut self.key_sequence_tracker, current_position, total_count, diff --git a/client/src/tui/functions/login.rs b/client/src/tui/functions/login.rs index c9e9043..23ae6f9 100644 --- a/client/src/tui/functions/login.rs +++ b/client/src/tui/functions/login.rs @@ -2,37 +2,57 @@ use crate::state::pages::auth::AuthState; use crate::state::canvas_state::CanvasState; -pub async fn handle_move_up( +pub async fn handle_action( + action: &str, auth_state: &mut AuthState, + ideal_cursor_column: &mut usize, ) -> Result> { - if auth_state.current_field > 0 { - auth_state.current_field -= 1; - } else { - // If at first field (username), cycle to button selection - auth_state.current_field = 0; - auth_state.return_selected = false; + match action { + "move_up" => { + if auth_state.return_selected { + // Coming from return button to fields + auth_state.return_selected = false; + auth_state.current_field = 1; // Focus on password field + } else if auth_state.current_field == 1 { + // Moving from password to username/email + auth_state.current_field = 0; + } else if auth_state.current_field == 0 { + // Wrap around to buttons + auth_state.return_selected = false; // Select Login button + } + + // Update cursor position when in a field + if !auth_state.return_selected { + 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")) + }, + "move_down" => { + if auth_state.return_selected { + // Coming from return button to fields + auth_state.return_selected = false; + auth_state.current_field = 0; // Focus on username field + } else if auth_state.current_field == 0 { + // Moving from username/email to password + auth_state.current_field = 1; + } else if auth_state.current_field == 1 { + // Moving from password to buttons + auth_state.return_selected = false; // Select Login button + } + + // Update cursor position when in a field + if !auth_state.return_selected { + 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")) + }, + _ => Err("Unknown login action".into()) } - - // Reset cursor position for the field - auth_state.current_cursor_pos = auth_state.get_current_input().len(); - - Ok("".to_string()) -} - -pub async fn handle_move_down( - auth_state: &mut AuthState, -) -> Result> { - if auth_state.current_field < 1 { - // Moving down from username to password - auth_state.current_field += 1; - - // Reset cursor position for the new field - auth_state.current_cursor_pos = auth_state.get_current_input().len(); - } else { - // Moving from password field to button selection - auth_state.return_selected = false; - } - - Ok("".to_string()) }