better movement

This commit is contained in:
filipriec
2025-04-16 16:30:11 +02:00
parent d0e2f31ce8
commit 04a7d86636
5 changed files with 224 additions and 66 deletions

View File

@@ -1 +1,125 @@
// src/functions/modes/navigation/admin_nav.rs
use crate::config::binds::config::Config;
use crate::state::{
app::state::AppState,
pages::admin::{AdminFocus, AdminState},
};
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
/// Handles navigation events specifically for the Admin Panel view.
/// Returns true if the event was handled, false otherwise.
pub fn handle_admin_navigation(
key: KeyEvent,
config: &Config,
app_state: &AppState, // Read-only access needed for counts
admin_state: &mut AdminState, // Mutable to change focus/selection
command_message: &mut String,
) -> bool {
let action = config.get_general_action(key.code, key.modifiers);
let current_focus = admin_state.current_focus;
let profile_count = app_state.profile_tree.profiles.len();
match action {
// --- Vertical Navigation (Up/Down) ---
Some("move_up") => {
match current_focus {
AdminFocus::Profiles => {
if profile_count > 0 {
admin_state.previous_profile(profile_count);
// Reset table selection when profile changes
admin_state.table_list_state.select(None);
*command_message = "Navigated profiles".to_string();
}
}
AdminFocus::Tables => {
if let Some(selected_profile_index) = admin_state.profile_list_state.selected() {
if let Some(profile) = app_state.profile_tree.profiles.get(selected_profile_index) {
let table_count = profile.tables.len();
if table_count > 0 {
admin_state.previous_table(table_count);
*command_message = "Navigated tables".to_string();
}
}
}
}
}
true // Event handled
}
Some("move_down") => {
match current_focus {
AdminFocus::Profiles => {
if profile_count > 0 {
admin_state.next_profile(profile_count);
// Reset table selection when profile changes
admin_state.table_list_state.select(None);
*command_message = "Navigated profiles".to_string();
}
}
AdminFocus::Tables => {
if let Some(selected_profile_index) = admin_state.profile_list_state.selected() {
if let Some(profile) = app_state.profile_tree.profiles.get(selected_profile_index) {
let table_count = profile.tables.len();
if table_count > 0 {
admin_state.next_table(table_count);
*command_message = "Navigated tables".to_string();
}
}
}
}
}
true // Event handled
}
// --- Horizontal Navigation (Focus Change) ---
Some("next_option") | Some("previous_option") => {
admin_state.toggle_focus();
*command_message = format!("Focus set to {:?}", admin_state.current_focus);
true // Event handled
}
// --- Selection ---
Some("select") => {
match current_focus {
AdminFocus::Profiles => {
// If a profile is selected, move focus to tables
if admin_state.profile_list_state.selected().is_some() {
admin_state.toggle_focus(); // Move focus to Tables
// Optionally select the first table if available
if let Some(selected_profile_index) = admin_state.profile_list_state.selected() {
if let Some(profile) = app_state.profile_tree.profiles.get(selected_profile_index) {
if !profile.tables.is_empty() {
admin_state.table_list_state.select(Some(0));
}
}
}
*command_message = "Selected profile, focus on Tables".to_string();
} else {
*command_message = "No profile selected".to_string();
}
}
AdminFocus::Tables => {
// Currently, selecting a table just confirms the highlight.
// Future: Could be used for multi-select toggle or other actions.
if let Some(idx) = admin_state.table_list_state.selected() {
*command_message = format!("Table {} highlighted", idx);
} else {
*command_message = "No table highlighted".to_string();
}
// We don't change focus here for now.
}
}
true // Event handled
}
// --- Other General Keys (Ignore for admin nav) ---
Some("toggle_sidebar") | Some("toggle_buffer_list") | Some("next_field") | Some("prev_field") => {
// These are handled globally or not applicable here.
false
}
// --- No matching action ---
_ => false, // Event not handled by admin navigation
}
}