router implementation

This commit is contained in:
Priec
2025-08-22 22:19:59 +02:00
parent 6833ac5fad
commit 957f5bf9f0
9 changed files with 225 additions and 180 deletions

View File

@@ -4,10 +4,7 @@ use crossterm::event::KeyEvent;
use crate::config::binds::config::Config;
use crate::state::app::state::AppState;
use crate::state::pages::form::FormState;
use crate::state::pages::auth::LoginState;
use crate::state::pages::auth::RegisterState;
use crate::state::pages::intro::IntroState;
use crate::state::pages::admin::AdminState;
use crate::pages::routing::{Router, Page};
use crate::ui::handlers::context::UiContext;
use crate::modes::handlers::event::EventOutcome;
use crate::modes::general::command_navigation::{handle_command_navigation_event, NavigationState};
@@ -18,10 +15,7 @@ pub async fn handle_navigation_event(
key: KeyEvent,
config: &Config,
app_state: &mut AppState,
login_state: &mut LoginState,
register_state: &mut RegisterState,
intro_state: &mut IntroState,
admin_state: &mut AdminState,
router: &mut Router,
command_mode: &mut bool,
command_input: &mut String,
command_message: &mut String,
@@ -35,19 +29,19 @@ pub async fn handle_navigation_event(
if let Some(action) = config.get_general_action(key.code, key.modifiers) {
match action {
"move_up" => {
move_up(app_state, login_state, register_state, intro_state, admin_state);
move_up(app_state, router);
return Ok(EventOutcome::Ok(String::new()));
}
"move_down" => {
move_down(app_state, intro_state, admin_state);
move_down(app_state, router);
return Ok(EventOutcome::Ok(String::new()));
}
"next_option" => {
next_option(app_state, intro_state);
next_option(app_state, router);
return Ok(EventOutcome::Ok(String::new()));
}
"previous_option" => {
previous_option(app_state, intro_state);
previous_option(app_state, router);
return Ok(EventOutcome::Ok(String::new()));
}
"next_field" => {
@@ -67,18 +61,21 @@ pub async fn handle_navigation_event(
return Ok(EventOutcome::Ok(String::new()));
}
"select" => {
let (context, index) = if app_state.ui.show_intro {
(UiContext::Intro, intro_state.selected_option)
} else if app_state.ui.show_login && app_state.ui.focus_outside_canvas {
(UiContext::Login, app_state.focused_button_index)
} else if app_state.ui.show_register && app_state.ui.focus_outside_canvas {
(UiContext::Register, app_state.focused_button_index)
} else if app_state.ui.show_admin {
(UiContext::Admin, admin_state.get_selected_index().unwrap_or(0))
} else if app_state.ui.dialog.dialog_show {
(UiContext::Dialog, app_state.ui.dialog.dialog_active_button_index)
} else {
return Ok(EventOutcome::Ok("Select (No Action)".to_string()));
let (context, index) = match &router.current {
Page::Intro(state) => (UiContext::Intro, state.selected_option),
Page::Login(_) if app_state.ui.focus_outside_canvas => {
(UiContext::Login, app_state.focused_button_index)
}
Page::Register(_) if app_state.ui.focus_outside_canvas => {
(UiContext::Register, app_state.focused_button_index)
}
Page::Admin(state) => {
(UiContext::Admin, state.get_selected_index().unwrap_or(0))
}
_ if app_state.ui.dialog.dialog_show => {
(UiContext::Dialog, app_state.ui.dialog.dialog_active_button_index)
}
_ => return Ok(EventOutcome::Ok("Select (No Action)".to_string())),
};
return Ok(EventOutcome::ButtonSelected { context, index });
}
@@ -88,60 +85,76 @@ pub async fn handle_navigation_event(
Ok(EventOutcome::Ok(String::new()))
}
pub fn move_up(app_state: &mut AppState, login_state: &mut LoginState, register_state: &mut RegisterState, intro_state: &mut IntroState, admin_state: &mut AdminState) {
if app_state.ui.focus_outside_canvas && app_state.ui.show_login || app_state.ui.show_register{
if app_state.focused_button_index == 0 {
app_state.ui.focus_outside_canvas = false;
if app_state.ui.show_login {
let last_field_index = login_state.field_count().saturating_sub(1);
login_state.set_current_field(last_field_index);
pub fn move_up(app_state: &mut AppState, router: &mut Router) {
match &mut router.current {
Page::Login(state) if app_state.ui.focus_outside_canvas => {
if app_state.focused_button_index == 0 {
app_state.ui.focus_outside_canvas = false;
let last_field_index = state.field_count().saturating_sub(1);
state.set_current_field(last_field_index);
} else {
let last_field_index = register_state.field_count().saturating_sub(1);
register_state.set_current_field(last_field_index);
app_state.focused_button_index =
app_state.focused_button_index.saturating_sub(1);
}
} else {
app_state.focused_button_index = app_state.focused_button_index.saturating_sub(1);
}
} else if app_state.ui.show_intro {
intro_state.previous_option();
} else if app_state.ui.show_admin {
admin_state.previous();
}
}
pub fn move_down(app_state: &mut AppState, intro_state: &mut IntroState, admin_state: &mut AdminState) {
if app_state.ui.focus_outside_canvas && app_state.ui.show_login || app_state.ui.show_register {
let num_general_elements = 2;
if app_state.focused_button_index < num_general_elements - 1 {
app_state.focused_button_index += 1;
Page::Register(state) if app_state.ui.focus_outside_canvas => {
if app_state.focused_button_index == 0 {
app_state.ui.focus_outside_canvas = false;
let last_field_index = state.field_count().saturating_sub(1);
state.set_current_field(last_field_index);
} else {
app_state.focused_button_index =
app_state.focused_button_index.saturating_sub(1);
}
}
} else if app_state.ui.show_intro {
intro_state.next_option();
} else if app_state.ui.show_admin {
admin_state.next();
Page::Intro(state) => state.previous_option(),
Page::Admin(state) => state.previous(),
_ => {}
}
}
pub fn next_option(app_state: &mut AppState, intro_state: &mut IntroState) {
if app_state.ui.show_intro {
intro_state.next_option();
} else {
// Get option count from state instead of parameter
let option_count = app_state.profile_tree.profiles.len();
app_state.focused_button_index = (app_state.focused_button_index + 1) % option_count;
pub fn move_down(app_state: &mut AppState, router: &mut Router) {
match &mut router.current {
Page::Login(_) | Page::Register(_) if app_state.ui.focus_outside_canvas => {
let num_general_elements = 2;
if app_state.focused_button_index < num_general_elements - 1 {
app_state.focused_button_index += 1;
}
}
Page::Intro(state) => state.next_option(),
Page::Admin(state) => state.next(),
_ => {}
}
}
pub fn previous_option(app_state: &mut AppState, intro_state: &mut IntroState) {
if app_state.ui.show_intro {
intro_state.previous_option();
} else {
let option_count = app_state.profile_tree.profiles.len();
app_state.focused_button_index = if app_state.focused_button_index == 0 {
option_count.saturating_sub(1)
} else {
app_state.focused_button_index - 1
};
pub fn next_option(app_state: &mut AppState, router: &mut Router) {
match &mut router.current {
Page::Intro(state) => state.next_option(),
Page::Admin(_) => {
let option_count = app_state.profile_tree.profiles.len();
if option_count > 0 {
app_state.focused_button_index =
(app_state.focused_button_index + 1) % option_count;
}
}
_ => {}
}
}
pub fn previous_option(app_state: &mut AppState, router: &mut Router) {
match &mut router.current {
Page::Intro(state) => state.previous_option(),
Page::Admin(_) => {
let option_count = app_state.profile_tree.profiles.len();
if option_count > 0 {
app_state.focused_button_index = if app_state.focused_button_index == 0 {
option_count.saturating_sub(1)
} else {
app_state.focused_button_index - 1
};
}
}
_ => {}
}
}
@@ -164,7 +177,7 @@ pub fn prev_field(form_state: &mut FormState) {
pub fn handle_enter_command_mode(
command_mode: &mut bool,
command_input: &mut String,
command_message: &mut String
command_message: &mut String,
) {
*command_mode = true;
command_input.clear();