// src/modes/general/navigation.rs use crossterm::event::KeyEvent; use crate::config::binds::config::Config; use crate::state::state::AppState; use crate::state::pages::form::FormState; use crate::state::pages::auth::AuthState; use crate::state::canvas_state::CanvasState; use crate::ui::handlers::context::UiContext; use crate::modes::handlers::event::EventOutcome; pub async fn handle_navigation_event( key: KeyEvent, config: &Config, form_state: &mut FormState, app_state: &mut AppState, auth_state: &mut AuthState, command_mode: &mut bool, command_input: &mut String, command_message: &mut String, ) -> Result> { if let Some(action) = config.get_general_action(key.code, key.modifiers) { match action { "move_up" => { move_up(app_state, auth_state); return Ok(EventOutcome::Ok(String::new())); } "move_down" => { move_down(app_state); return Ok(EventOutcome::Ok(String::new())); } "next_option" => { next_option(app_state); return Ok(EventOutcome::Ok(String::new())); } "previous_option" => { previous_option(app_state); return Ok(EventOutcome::Ok(String::new())); } "toggle_sidebar" => { toggle_sidebar(app_state); return Ok(EventOutcome::Ok(format!("Sidebar {}", if app_state.ui.show_sidebar { "shown" } else { "hidden" } ))); } "next_field" => { next_field(form_state); return Ok(EventOutcome::Ok(String::new())); } "prev_field" => { prev_field(form_state); return Ok(EventOutcome::Ok(String::new())); } "enter_command_mode" => { handle_enter_command_mode(command_mode, command_input, command_message); return Ok(EventOutcome::Ok(String::new())); } "select" => { let (context, index) = if app_state.ui.show_intro { (UiContext::Intro, app_state.ui.intro_state.selected_option) } else if app_state.ui.show_login && app_state.ui.focus_outside_canvas { (UiContext::Login, app_state.general.selected_item) } else if app_state.ui.show_register && app_state.ui.focus_outside_canvas { (UiContext::Register, app_state.general.selected_item) } else if app_state.ui.show_admin { (UiContext::Admin, app_state.general.selected_item) } else if app_state.ui.dialog.dialog_show { (UiContext::Dialog, app_state.ui.dialog.dialog_active_button_index) } else { // Handle cases where select is pressed but no button context applies return Ok(EventOutcome::Ok("Select (No Action)".to_string())); }; return Ok(EventOutcome::ButtonSelected { context, index }); } _ => {} } } Ok(EventOutcome::Ok(String::new())) } pub fn move_up(app_state: &mut AppState, auth_state: &mut AuthState) { if app_state.ui.focus_outside_canvas && app_state.ui.show_login { if app_state.general.selected_item == 0 { app_state.ui.focus_outside_canvas = false; let last_field_index = auth_state.fields().len().saturating_sub(1); auth_state.set_current_field(last_field_index); } else { app_state.general.selected_item = app_state.general.selected_item.saturating_sub(1); } } else if app_state.ui.show_intro { app_state.ui.intro_state.previous_option(); } else if app_state.ui.show_admin { // Assuming profile_tree.profiles is the list we're navigating let profile_count = app_state.profile_tree.profiles.len(); if profile_count == 0 { return; } // Use general state for tracking selection in admin panel if app_state.general.selected_item == 0 { app_state.general.selected_item = profile_count - 1; } else { app_state.general.selected_item = app_state.general.selected_item.saturating_sub(1); } } } pub fn move_down(app_state: &mut AppState) { if app_state.ui.focus_outside_canvas && app_state.ui.show_login { let num_general_elements = 2; if app_state.general.selected_item < num_general_elements - 1 { app_state.general.selected_item += 1; } } else if app_state.ui.show_intro { app_state.ui.intro_state.next_option(); } else if app_state.ui.show_admin { // Assuming profile_tree.profiles is the list we're navigating let profile_count = app_state.profile_tree.profiles.len(); if profile_count == 0 { return; } app_state.general.selected_item = (app_state.general.selected_item + 1) % profile_count; } } pub fn next_option(app_state: &mut AppState) { // Remove option_count parameter if app_state.ui.show_intro { app_state.ui.intro_state.next_option(); } else { // Get option count from state instead of parameter let option_count = app_state.profile_tree.profiles.len(); app_state.general.current_option = (app_state.general.current_option + 1) % option_count; } } pub fn previous_option(app_state: &mut AppState) { if app_state.ui.show_intro { app_state.ui.intro_state.previous_option(); } else { let option_count = app_state.profile_tree.profiles.len(); app_state.general.current_option = if app_state.general.current_option == 0 { option_count.saturating_sub(1) // Wrap to last option } else { app_state.general.current_option - 1 }; } } pub fn toggle_sidebar(app_state: &mut AppState) { app_state.ui.show_sidebar = !app_state.ui.show_sidebar; } pub fn next_field(form_state: &mut FormState) { if !form_state.fields.is_empty() { form_state.current_field = (form_state.current_field + 1) % form_state.fields.len(); } } pub fn prev_field(form_state: &mut FormState) { if !form_state.fields.is_empty() { if form_state.current_field == 0 { form_state.current_field = form_state.fields.len() - 1; } else { form_state.current_field -= 1; } } } pub fn handle_enter_command_mode( command_mode: &mut bool, command_input: &mut String, command_message: &mut String ) { *command_mode = true; command_input.clear(); command_message.clear(); }