login and register are now havving own handlers and loaders, moving logic out of event.rs and ui.rs

This commit is contained in:
filipriec
2025-08-30 19:13:12 +02:00
parent 46149c09db
commit 43f5c1a764
13 changed files with 317 additions and 104 deletions

View File

@@ -305,94 +305,33 @@ impl EventHandler {
if !overlay_active {
if let Page::Login(login_page) = &mut router.current {
use crossterm::event::{KeyCode, KeyModifiers};
// Inside canvas: at the last field, 'j' or Down moves focus to buttons
if !app_state.ui.focus_outside_canvas {
let last_idx = login_page
.editor
.data_provider()
.field_count()
.saturating_sub(1);
let at_last = login_page.editor.current_field() >= last_idx;
if at_last
&& matches!(
(key_code, modifiers),
(KeyCode::Char('j'), KeyModifiers::NONE) | (KeyCode::Down, _)
)
{
app_state.ui.focus_outside_canvas = true;
app_state.focused_button_index = 0; // focus "Login" button
// Ensure canvas mode is ReadOnly when leaving
login_page.editor.set_mode(CanvasMode::ReadOnly);
return Ok(EventOutcome::Ok("Focus moved to buttons".to_string()));
}
}
// Only forward to the canvas while focus is inside it
if !app_state.ui.focus_outside_canvas {
match login_page.handle_key_event(key_event) {
KeyEventOutcome::Consumed(Some(msg)) => {
self.command_message = msg;
return Ok(EventOutcome::Ok("Login input updated".to_string()));
}
KeyEventOutcome::Consumed(None) => {
return Ok(EventOutcome::Ok("Login input updated".to_string()));
}
KeyEventOutcome::Pending => {
return Ok(EventOutcome::Ok("Waiting for next key...".to_string()));
}
KeyEventOutcome::NotMatched => {
// fall through to other handlers (buttons, etc.)
}
}
let outcome =
login::event::handle_login_event(event, app_state, login_page)?;
// Only return if the login page actually consumed the key
if !outcome.get_message_if_ok().is_empty() {
return Ok(outcome);
}
} else if let Page::Register(register_page) = &mut router.current {
use crossterm::event::{KeyCode, KeyModifiers};
// Inside canvas: at the last field, 'j' or Down moves focus to buttons
if !app_state.ui.focus_outside_canvas {
let last_idx = register_page.editor.data_provider().field_count().saturating_sub(1);
let at_last = register_page.editor.current_field() >= last_idx;
if at_last
&& matches!(
(key_code, modifiers),
(KeyCode::Char('j'), KeyModifiers::NONE) | (KeyCode::Down, _)
)
{
app_state.ui.focus_outside_canvas = true;
app_state.focused_button_index = 0; // focus "Register" button
register_page.editor.set_mode(CanvasMode::ReadOnly);
return Ok(EventOutcome::Ok("Focus moved to buttons".to_string()));
}
}
// Only forward to the canvas while focus is inside it
if !app_state.ui.focus_outside_canvas {
match register_page.handle_key_event(key_event) {
KeyEventOutcome::Consumed(Some(msg)) => {
self.command_message = msg;
return Ok(EventOutcome::Ok("Register input updated".to_string()));
}
KeyEventOutcome::Consumed(None) => {
return Ok(EventOutcome::Ok("Register input updated".to_string()));
}
KeyEventOutcome::Pending => {
return Ok(EventOutcome::Ok("Waiting for next key...".to_string()));
}
KeyEventOutcome::NotMatched => {
// fall through
}
}
let outcome = crate::pages::register::event::handle_register_event(
event,
app_state,
register_page,
)?;
// Only stop if page actually consumed the key; else fall through to global handling
if !outcome.get_message_if_ok().is_empty() {
return Ok(outcome);
}
} else if let Page::Form(path) = &router.current {
// NEW: Delegate Form event handling
return forms::event::handle_form_event(
event,
app_state,
path,
&mut self.ideal_cursor_column
);
let outcome = forms::event::handle_form_event(
event,
app_state,
path,
&mut self.ideal_cursor_column,
)?;
// Only return if the form page actually consumed the key
if !outcome.get_message_if_ok().is_empty() {
return Ok(outcome);
}
}
}
if toggle_sidebar(
@@ -508,19 +447,6 @@ impl EventHandler {
match &mut router.current {
// LOGIN: From buttons (general) back into the canvas with 'k' (Up),
// but ONLY from the left-most "Login" button.
Page::Login(page) => {
if app_state.ui.focus_outside_canvas {
if app_state.focused_button_index == 0
&& matches!(ma, crate::movement::MovementAction::Up)
{
app_state.ui.focus_outside_canvas = false;
// Enter canvas in ReadOnly mode (never jump straight to Edit)
page.editor.set_mode(CanvasMode::ReadOnly);
// Optional: keep current field (usually 0 initially)
return Ok(EventOutcome::Ok(String::new()));
}
}
}
Page::AddTable(state) => {
if state.handle_movement(ma) {
// Keep UI focus consistent with inputs vs. outer elements