movement
This commit is contained in:
169
client/src/functions/modes/navigation/add_table_nav.rs
Normal file
169
client/src/functions/modes/navigation/add_table_nav.rs
Normal file
@@ -0,0 +1,169 @@
|
||||
// src/functions/modes/navigation/add_table_nav.rs
|
||||
use crate::state::pages::add_table::{AddTableFocus, AddTableState};
|
||||
use crate::state::app::state::AppState; // If needed for list lengths later
|
||||
|
||||
/// Handles navigation events specifically for the Add Table page.
|
||||
/// Returns true if the event was handled, false otherwise.
|
||||
pub fn handle_navigation(
|
||||
state: &mut AddTableState,
|
||||
action: &str,
|
||||
// app_state: &AppState, // Add if needed for list lengths
|
||||
) -> bool {
|
||||
let current_focus = state.current_focus;
|
||||
let mut handled = true; // Assume handled unless proven otherwise
|
||||
|
||||
match action {
|
||||
"move_up" => {
|
||||
state.current_focus = match current_focus {
|
||||
AddTableFocus::TableName => AddTableFocus::CancelButton, // Wrap around top
|
||||
AddTableFocus::ColumnInput => AddTableFocus::TableName,
|
||||
AddTableFocus::ColumnsTable => {
|
||||
// Navigate within table or move focus up
|
||||
if !navigate_table_up(&mut state.column_table_state, state.columns.len()) {
|
||||
state.current_focus = AddTableFocus::ColumnInput;
|
||||
}
|
||||
AddTableFocus::ColumnsTable // Keep focus here while navigating table
|
||||
}
|
||||
AddTableFocus::IndexInput => AddTableFocus::ColumnsTable,
|
||||
AddTableFocus::IndexesTable => {
|
||||
if !navigate_table_up(&mut state.index_table_state, state.indexes.len()) {
|
||||
state.current_focus = AddTableFocus::IndexInput;
|
||||
}
|
||||
AddTableFocus::IndexesTable
|
||||
}
|
||||
AddTableFocus::LinkInput => AddTableFocus::IndexesTable,
|
||||
AddTableFocus::LinksTable => {
|
||||
if !navigate_table_up(&mut state.link_table_state, state.links.len()) {
|
||||
state.current_focus = AddTableFocus::LinkInput;
|
||||
}
|
||||
AddTableFocus::LinksTable
|
||||
}
|
||||
AddTableFocus::SaveButton | AddTableFocus::CancelButton => AddTableFocus::LinksTable,
|
||||
}
|
||||
}
|
||||
"move_down" => {
|
||||
state.current_focus = match current_focus {
|
||||
AddTableFocus::TableName => AddTableFocus::ColumnInput,
|
||||
AddTableFocus::ColumnInput => AddTableFocus::ColumnsTable,
|
||||
AddTableFocus::ColumnsTable => {
|
||||
if !navigate_table_down(&mut state.column_table_state, state.columns.len()) {
|
||||
state.current_focus = AddTableFocus::IndexInput;
|
||||
}
|
||||
AddTableFocus::ColumnsTable
|
||||
}
|
||||
AddTableFocus::IndexInput => AddTableFocus::IndexesTable,
|
||||
AddTableFocus::IndexesTable => {
|
||||
if !navigate_table_down(&mut state.index_table_state, state.indexes.len()) {
|
||||
state.current_focus = AddTableFocus::LinkInput;
|
||||
}
|
||||
AddTableFocus::IndexesTable
|
||||
}
|
||||
AddTableFocus::LinkInput => AddTableFocus::LinksTable,
|
||||
AddTableFocus::LinksTable => {
|
||||
if !navigate_table_down(&mut state.link_table_state, state.links.len()) {
|
||||
// Move to buttons after table
|
||||
state.current_focus = AddTableFocus::SaveButton;
|
||||
}
|
||||
AddTableFocus::LinksTable
|
||||
}
|
||||
AddTableFocus::SaveButton | AddTableFocus::CancelButton => AddTableFocus::TableName, // Wrap around bottom
|
||||
}
|
||||
}
|
||||
"next_option" => { // Typically Tab or Right arrow
|
||||
state.current_focus = match current_focus {
|
||||
// Simple vertical flow for now, like move_down
|
||||
AddTableFocus::TableName => AddTableFocus::ColumnInput,
|
||||
AddTableFocus::ColumnInput => AddTableFocus::ColumnsTable,
|
||||
AddTableFocus::ColumnsTable => AddTableFocus::IndexInput,
|
||||
AddTableFocus::IndexInput => AddTableFocus::IndexesTable,
|
||||
AddTableFocus::IndexesTable => AddTableFocus::LinkInput,
|
||||
AddTableFocus::LinkInput => AddTableFocus::LinksTable,
|
||||
AddTableFocus::LinksTable => AddTableFocus::SaveButton,
|
||||
AddTableFocus::SaveButton => AddTableFocus::CancelButton,
|
||||
AddTableFocus::CancelButton => AddTableFocus::TableName, // Wrap
|
||||
}
|
||||
}
|
||||
"previous_option" => { // Typically Shift+Tab or Left arrow
|
||||
state.current_focus = match current_focus {
|
||||
// Simple vertical flow upwards
|
||||
AddTableFocus::TableName => AddTableFocus::CancelButton, // Wrap
|
||||
AddTableFocus::ColumnInput => AddTableFocus::TableName,
|
||||
AddTableFocus::ColumnsTable => AddTableFocus::ColumnInput,
|
||||
AddTableFocus::IndexInput => AddTableFocus::ColumnsTable,
|
||||
AddTableFocus::IndexesTable => AddTableFocus::IndexInput,
|
||||
AddTableFocus::LinkInput => AddTableFocus::IndexesTable,
|
||||
AddTableFocus::LinksTable => AddTableFocus::LinkInput,
|
||||
AddTableFocus::SaveButton => AddTableFocus::LinksTable,
|
||||
AddTableFocus::CancelButton => AddTableFocus::SaveButton,
|
||||
}
|
||||
}
|
||||
"select" => {
|
||||
// TODO: Implement select action based on current_focus
|
||||
// e.g., Enter edit mode for TableName/Inputs, toggle link required, trigger save/cancel
|
||||
handled = false; // Mark as not handled for now
|
||||
}
|
||||
_ => handled = false, // Action not relevant for this navigation
|
||||
}
|
||||
|
||||
// If focus changed to a table, select the first row if nothing is selected
|
||||
if handled && current_focus != state.current_focus {
|
||||
match state.current_focus {
|
||||
AddTableFocus::ColumnsTable if state.column_table_state.selected().is_none() && !state.columns.is_empty() => {
|
||||
state.column_table_state.select(Some(0));
|
||||
}
|
||||
AddTableFocus::IndexesTable if state.index_table_state.selected().is_none() && !state.indexes.is_empty() => {
|
||||
state.index_table_state.select(Some(0));
|
||||
}
|
||||
AddTableFocus::LinksTable if state.link_table_state.selected().is_none() && !state.links.is_empty() => {
|
||||
state.link_table_state.select(Some(0));
|
||||
}
|
||||
_ => {} // No action needed for other focus states
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
handled
|
||||
}
|
||||
|
||||
|
||||
// Helper function for navigating up within a table state
|
||||
// Returns true if navigation happened within the table, false if it reached the top
|
||||
fn navigate_table_up(table_state: &mut ratatui::widgets::TableState, item_count: usize) -> bool {
|
||||
if item_count == 0 { return false; } // Cannot navigate empty table
|
||||
let current_selection = table_state.selected();
|
||||
match current_selection {
|
||||
Some(index) => {
|
||||
if index > 0 {
|
||||
table_state.select(Some(index - 1));
|
||||
true // Moved up within table
|
||||
} else {
|
||||
false // Was at the top
|
||||
}
|
||||
}
|
||||
None => {
|
||||
table_state.select(Some(item_count - 1)); // Select last item if nothing selected
|
||||
true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Helper function for navigating down within a table state
|
||||
// Returns true if navigation happened within the table, false if it reached the bottom
|
||||
fn navigate_table_down(table_state: &mut ratatui::widgets::TableState, item_count: usize) -> bool {
|
||||
if item_count == 0 { return false; } // Cannot navigate empty table
|
||||
let current_selection = table_state.selected();
|
||||
match current_selection {
|
||||
Some(index) => {
|
||||
if index < item_count - 1 {
|
||||
table_state.select(Some(index + 1));
|
||||
true // Moved down within table
|
||||
} else {
|
||||
false // Was at the bottom
|
||||
}
|
||||
}
|
||||
None => {
|
||||
table_state.select(Some(0)); // Select first item if nothing selected
|
||||
true
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user