185 lines
6.2 KiB
Rust
185 lines
6.2 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;
|
|
|
|
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" => {
|
|
let item_count = if app_state.ui.show_intro {
|
|
2 // Intro options count
|
|
} else {
|
|
app_state.profile_tree.profiles.len() // Admin panel items
|
|
};
|
|
move_down(app_state, item_count);
|
|
return Ok((false, String::new()));
|
|
}
|
|
"next_option" => {
|
|
next_option(app_state, 2); // 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, item_count: usize) {
|
|
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, option_count: usize) {
|
|
if app_state.ui.show_intro {
|
|
app_state.ui.intro_state.next_option();
|
|
} else {
|
|
// For other screens that might have options
|
|
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 {
|
|
// For other screens that might have options
|
|
if app_state.general.current_option == 0 {
|
|
// We'd need the option count here, but since it's not passed we can't wrap around correctly
|
|
// For now, just stay at 0
|
|
} else {
|
|
app_state.general.current_option -= 1;
|
|
}
|
|
}
|
|
}
|
|
|
|
pub fn select(app_state: &mut AppState) {
|
|
if app_state.ui.show_intro {
|
|
// Handle selection in intro screen
|
|
match app_state.ui.intro_state.selected_option {
|
|
0 => { // Continue - show form
|
|
app_state.ui.show_form = true;
|
|
app_state.ui.show_admin = false;
|
|
app_state.ui.show_login = false;
|
|
}
|
|
1 => { // Admin
|
|
app_state.ui.show_form = false;
|
|
app_state.ui.show_admin = true;
|
|
app_state.ui.show_login = false;
|
|
}
|
|
2 => { // Login
|
|
app_state.ui.show_form = false;
|
|
app_state.ui.show_admin = false;
|
|
app_state.ui.show_login = true;
|
|
}
|
|
_ => {} // Other options (shouldn't happen)
|
|
}
|
|
app_state.ui.show_intro = false;
|
|
} else if app_state.ui.show_admin {
|
|
// Handle selection in admin panel
|
|
let profiles = &app_state.profile_tree.profiles;
|
|
if !profiles.is_empty() && app_state.general.selected_item < profiles.len() {
|
|
// Set the selected profile
|
|
app_state.selected_profile = Some(profiles[app_state.general.selected_item].name.clone());
|
|
}
|
|
}
|
|
}
|
|
|
|
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();
|
|
}
|