general mode

This commit is contained in:
filipriec
2025-03-23 21:00:53 +01:00
parent 9917195fc4
commit 7caa4d8c3c
2 changed files with 83 additions and 36 deletions

View File

@@ -0,0 +1,68 @@
// src/modes/general/navigation.rs
use crate::state::state::AppState;
use crate::ui::handlers::form::FormState;
pub fn move_up(app_state: &mut AppState) {
app_state.general.selected_item = app_state.general.selected_item.saturating_sub(1);
}
pub fn move_down(app_state: &mut AppState, item_count: usize) {
app_state.general.selected_item = app_state.general.selected_item.saturating_add(1);
if item_count > 0 {
app_state.general.selected_item = app_state.general.selected_item.min(item_count - 1);
}
}
pub fn next_option(app_state: &mut AppState, option_count: usize) {
app_state.general.current_option = app_state.general.current_option.saturating_add(1);
if option_count > 0 {
app_state.general.current_option = app_state.general.current_option.min(option_count - 1);
}
}
pub fn previous_option(app_state: &mut AppState) {
app_state.general.current_option = app_state.general.current_option.saturating_sub(1);
}
pub fn select(app_state: &mut AppState) {
if app_state.ui.show_intro {
app_state.ui.show_intro = false;
} else if app_state.ui.show_admin {
app_state.ui.show_admin = false;
}
}
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() {
form_state.current_field = if form_state.current_field == 0 {
form_state.fields.len() - 1
} else {
form_state.current_field - 1
};
}
}
pub fn handle_enter_command_mode(event_handler: &mut crate::modes::handlers::event::EventHandler) {
event_handler.command_mode = true;
event_handler.command_input.clear();
event_handler.command_message.clear();
}
// Helper function for bounds checking in lists
pub fn clamp_index(selected: usize, item_count: usize) -> usize {
if item_count == 0 {
0
} else {
selected.min(item_count - 1)
}
}

View File

@@ -13,6 +13,7 @@ use crate::modes::{
handlers::{command_mode},
canvas::{edit, read_only, common},
};
use crate::modes::navigation;
use crate::config::binds::key_sequences::KeySequenceTracker;
pub struct EventHandler {
@@ -59,73 +60,51 @@ impl EventHandler {
if let Some(action) = config.get_general_action(key_code, modifiers) {
match action {
"move_up" => {
app_state.general.selected_item = app_state.general.selected_item.saturating_sub(1);
navigation::move_up(app_state);
return Ok((false, String::new()));
}
"move_down" => {
app_state.general.selected_item = app_state.general.selected_item.saturating_add(1);
let item_count = if app_state.ui.show_intro {
2 // Intro options count
} else {
app_state.profile_tree.profiles.len() // Admin panel items
};
navigation::move_down(app_state, item_count);
return Ok((false, String::new()));
}
"next_option" => {
app_state.general.current_option = app_state.general.current_option.saturating_add(1);
navigation::next_option(app_state, 2); // Intro has 2 options
return Ok((false, String::new()));
}
"previous_option" => {
app_state.general.current_option = app_state.general.current_option.saturating_sub(1);
navigation::previous_option(app_state);
return Ok((false, String::new()));
}
"select" => {
if app_state.ui.show_intro {
app_state.ui.show_intro = false;
} else if app_state.ui.show_admin {
app_state.ui.show_admin = false;
}
navigation::select(app_state);
return Ok((false, "Selected".to_string()));
}
"toggle_sidebar" => {
app_state.ui.show_sidebar = !app_state.ui.show_sidebar;
navigation::toggle_sidebar(app_state);
return Ok((false, format!("Sidebar {}",
if app_state.ui.show_sidebar { "shown" } else { "hidden" }
)));
}
"next_field" => {
// Increment field navigation
if form_state.fields.len() > 0 {
form_state.current_field = (form_state.current_field + 1) % form_state.fields.len();
}
navigation::next_field(form_state);
return Ok((false, String::new()));
}
"prev_field" => {
// Decrement field navigation
if form_state.fields.len() > 0 {
form_state.current_field = if form_state.current_field == 0 {
form_state.fields.len() - 1
} else {
form_state.current_field - 1
};
}
navigation::prev_field(form_state);
return Ok((false, String::new()));
}
"enter_command_mode" => {
self.command_mode = true;
self.command_input.clear();
self.command_message.clear();
navigation::handle_enter_command_mode(self);
return Ok((false, String::new()));
}
_ => {}
}
}
if let Some("enter_command_mode") = config.get_action_for_key_in_mode(
&config.keybindings.global,
key_code,
modifiers
) {
self.command_mode = true;
return Ok((false, String::new()));
}
// If no general action matched, return to stay in general mode
return Ok((false, String::new()));
}