Files
komp_ac/client/src/modes/general/navigation.rs

157 lines
5.0 KiB
Rust

// 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::tui::functions::{intro, admin};
pub async fn handle_navigation_event(
key: KeyEvent,
config: &Config,
form_state: &mut FormState,
app_state: &mut AppState,
command_mode: &mut bool,
command_input: &mut String,
command_message: &mut String,
) -> Result<(bool, String), Box<dyn std::error::Error>> {
if let Some(action) = config.get_general_action(key.code, key.modifiers) {
match action {
"move_up" => {
move_up(app_state);
return Ok((false, String::new()));
}
"move_down" => {
move_down(app_state);
return Ok((false, String::new()));
}
"next_option" => {
next_option(app_state); // Intro has 2 options
return Ok((false, String::new()));
}
"previous_option" => {
previous_option(app_state);
return Ok((false, String::new()));
}
"select" => {
select(app_state);
return Ok((false, "Selected".to_string()));
}
"toggle_sidebar" => {
toggle_sidebar(app_state);
return Ok((false, format!("Sidebar {}",
if app_state.ui.show_sidebar { "shown" } else { "hidden" }
)));
}
"next_field" => {
next_field(form_state);
return Ok((false, String::new()));
}
"prev_field" => {
prev_field(form_state);
return Ok((false, String::new()));
}
"enter_command_mode" => {
handle_enter_command_mode(command_mode, command_input, command_message);
return Ok((false, String::new()));
}
_ => {}
}
}
Ok((false, String::new()))
}
pub fn move_up(app_state: &mut AppState) {
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.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 select(app_state: &mut AppState) {
if app_state.ui.show_intro {
intro::handle_intro_selection(app_state);
} else if app_state.ui.show_admin {
admin::handle_admin_selection(app_state);
}
}
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();
}