readonly and edit functionality to add table
This commit is contained in:
@@ -5,101 +5,120 @@ use crate::state::{
|
||||
pages::add_table::{AddTableFocus, AddTableState},
|
||||
};
|
||||
use crossterm::event::{KeyEvent};
|
||||
use ratatui::widgets::TableState; // Import TableState
|
||||
use ratatui::widgets::TableState;
|
||||
|
||||
/// Handles navigation events specifically for the Add Table view.
|
||||
/// Returns true if the event was handled, false otherwise.
|
||||
pub fn handle_add_table_navigation(
|
||||
key: KeyEvent,
|
||||
config: &Config,
|
||||
_app_state: &AppState, // Keep for potential future use (e.g., checking permissions)
|
||||
_app_state: &AppState,
|
||||
add_table_state: &mut AddTableState,
|
||||
command_message: &mut String,
|
||||
) -> bool {
|
||||
let action = config.get_general_action(key.code, key.modifiers);
|
||||
let current_focus = add_table_state.current_focus;
|
||||
let mut handled = true; // Assume handled unless logic determines otherwise
|
||||
let mut new_focus = current_focus; // Initialize new_focus
|
||||
|
||||
// Define focus groups for horizontal navigation
|
||||
let is_left_pane_focus = matches!(current_focus,
|
||||
AddTableFocus::ColumnsTable | AddTableFocus::IndexesTable | AddTableFocus::LinksTable
|
||||
);
|
||||
let is_right_pane_focus = matches!(current_focus,
|
||||
AddTableFocus::InputTableName | AddTableFocus::InputColumnName | AddTableFocus::InputColumnType |
|
||||
AddTableFocus::AddColumnButton | AddTableFocus::SaveButton | AddTableFocus::CancelButton
|
||||
);
|
||||
|
||||
match action.as_deref() {
|
||||
// --- Vertical Navigation (Up/Down) ---
|
||||
Some("move_up") => {
|
||||
let mut new_focus = current_focus; // Start with current focus
|
||||
match current_focus {
|
||||
AddTableFocus::InputTableName => new_focus = AddTableFocus::CancelButton, // Wrap top
|
||||
AddTableFocus::InputTableName => new_focus = AddTableFocus::CancelButton, // Wrap top (right pane)
|
||||
AddTableFocus::InputColumnName => new_focus = AddTableFocus::InputTableName,
|
||||
AddTableFocus::InputColumnType => new_focus = AddTableFocus::InputColumnName,
|
||||
AddTableFocus::AddColumnButton => new_focus = AddTableFocus::InputColumnType,
|
||||
AddTableFocus::ColumnsTable => {
|
||||
AddTableFocus::ColumnsTable => { // Left pane navigation
|
||||
if !navigate_table_up(&mut add_table_state.column_table_state, add_table_state.columns.len()) {
|
||||
new_focus = AddTableFocus::AddColumnButton; // Move focus up if at table top
|
||||
// If at top of columns, potentially wrap to bottom of left pane (LinksTable) or stay? Let's stay for now.
|
||||
// Or maybe move to AddColumnButton? Let's try moving up from right pane instead.
|
||||
new_focus = AddTableFocus::AddColumnButton; // Tentative: move focus up from right pane
|
||||
}
|
||||
// Keep focus on table while navigating within it
|
||||
}
|
||||
AddTableFocus::IndexesTable => {
|
||||
if !navigate_table_up(&mut add_table_state.index_table_state, add_table_state.indexes.len()) {
|
||||
new_focus = AddTableFocus::ColumnsTable; // Move focus up
|
||||
new_focus = AddTableFocus::ColumnsTable;
|
||||
}
|
||||
}
|
||||
AddTableFocus::LinksTable => {
|
||||
if !navigate_table_up(&mut add_table_state.link_table_state, add_table_state.links.len()) {
|
||||
new_focus = AddTableFocus::IndexesTable; // Move focus up
|
||||
new_focus = AddTableFocus::IndexesTable;
|
||||
}
|
||||
}
|
||||
AddTableFocus::SaveButton => new_focus = AddTableFocus::LinksTable,
|
||||
AddTableFocus::SaveButton => new_focus = AddTableFocus::LinksTable, // Move up to left pane bottom
|
||||
AddTableFocus::CancelButton => new_focus = AddTableFocus::SaveButton,
|
||||
}
|
||||
add_table_state.current_focus = new_focus;
|
||||
*command_message = format!("Focus set to {:?}", add_table_state.current_focus);
|
||||
}
|
||||
Some("move_down") => {
|
||||
let mut new_focus = current_focus; // Start with current focus
|
||||
match current_focus {
|
||||
AddTableFocus::InputTableName => new_focus = AddTableFocus::InputColumnName,
|
||||
AddTableFocus::InputColumnName => new_focus = AddTableFocus::InputColumnType,
|
||||
AddTableFocus::InputColumnType => new_focus = AddTableFocus::AddColumnButton,
|
||||
AddTableFocus::AddColumnButton => new_focus = AddTableFocus::ColumnsTable,
|
||||
AddTableFocus::ColumnsTable => {
|
||||
AddTableFocus::AddColumnButton => new_focus = AddTableFocus::ColumnsTable, // Move down to left pane top
|
||||
AddTableFocus::ColumnsTable => { // Left pane navigation
|
||||
if !navigate_table_down(&mut add_table_state.column_table_state, add_table_state.columns.len()) {
|
||||
new_focus = AddTableFocus::IndexesTable; // Move focus down if at table bottom
|
||||
new_focus = AddTableFocus::IndexesTable; // Move to next left pane item
|
||||
}
|
||||
// Keep focus on table while navigating within it
|
||||
}
|
||||
AddTableFocus::IndexesTable => {
|
||||
if !navigate_table_down(&mut add_table_state.index_table_state, add_table_state.indexes.len()) {
|
||||
new_focus = AddTableFocus::LinksTable; // Move focus down
|
||||
new_focus = AddTableFocus::LinksTable;
|
||||
}
|
||||
}
|
||||
AddTableFocus::LinksTable => {
|
||||
if !navigate_table_down(&mut add_table_state.link_table_state, add_table_state.links.len()) {
|
||||
new_focus = AddTableFocus::SaveButton; // Move focus down
|
||||
new_focus = AddTableFocus::SaveButton; // Move down to right pane bottom
|
||||
}
|
||||
}
|
||||
AddTableFocus::SaveButton => new_focus = AddTableFocus::CancelButton,
|
||||
AddTableFocus::CancelButton => new_focus = AddTableFocus::InputTableName, // Wrap bottom
|
||||
AddTableFocus::CancelButton => new_focus = AddTableFocus::InputTableName, // Wrap bottom (right pane)
|
||||
}
|
||||
add_table_state.current_focus = new_focus;
|
||||
*command_message = format!("Focus set to {:?}", add_table_state.current_focus);
|
||||
}
|
||||
|
||||
// --- Horizontal Navigation (Left/Right) ---
|
||||
Some("next_option") => { // 'l' or Right
|
||||
add_table_state.current_focus = match current_focus {
|
||||
AddTableFocus::SaveButton => AddTableFocus::CancelButton,
|
||||
_ => current_focus, // No change for others yet
|
||||
};
|
||||
*command_message = format!("Focus set to {:?}", add_table_state.current_focus);
|
||||
Some("next_option") => { // 'l' or Right: Move from Left Pane to Right Pane
|
||||
if is_left_pane_focus {
|
||||
new_focus = match current_focus {
|
||||
// Map left pane items to corresponding right pane items (approximate vertical alignment)
|
||||
AddTableFocus::ColumnsTable => AddTableFocus::InputTableName,
|
||||
AddTableFocus::IndexesTable => AddTableFocus::InputColumnName, // Or AddColumnButton?
|
||||
AddTableFocus::LinksTable => AddTableFocus::SaveButton,
|
||||
_ => current_focus, // Should not happen based on is_left_pane_focus
|
||||
};
|
||||
} else if is_right_pane_focus {
|
||||
// If already in right pane, maybe wrap Save -> Cancel or stay? Let's handle Save->Cancel only.
|
||||
if current_focus == AddTableFocus::SaveButton {
|
||||
new_focus = AddTableFocus::CancelButton;
|
||||
}
|
||||
}
|
||||
}
|
||||
Some("previous_option") => { // 'h' or Left
|
||||
add_table_state.current_focus = match current_focus {
|
||||
AddTableFocus::CancelButton => AddTableFocus::SaveButton,
|
||||
_ => current_focus, // No change for others yet
|
||||
};
|
||||
*command_message = format!("Focus set to {:?}", add_table_state.current_focus);
|
||||
Some("previous_option") => { // 'h' or Left: Move from Right Pane to Left Pane
|
||||
if is_right_pane_focus {
|
||||
new_focus = match current_focus {
|
||||
// Map right pane items back to left pane items (approximate vertical alignment)
|
||||
AddTableFocus::InputTableName | AddTableFocus::InputColumnName | AddTableFocus::InputColumnType | AddTableFocus::AddColumnButton => AddTableFocus::ColumnsTable, // Go to top of left pane
|
||||
AddTableFocus::SaveButton | AddTableFocus::CancelButton => AddTableFocus::LinksTable, // Go to bottom of left pane
|
||||
_ => current_focus, // Should not happen
|
||||
};
|
||||
} else if is_left_pane_focus {
|
||||
// If already in left pane, pressing 'h' could wrap to Cancel button?
|
||||
new_focus = AddTableFocus::CancelButton; // Wrap left-to-right bottom
|
||||
}
|
||||
}
|
||||
|
||||
// --- Tab / Shift+Tab Navigation ---
|
||||
// --- Tab / Shift+Tab Navigation (Keep as vertical cycle) ---
|
||||
Some("next_field") => { // Tab
|
||||
add_table_state.current_focus = match current_focus {
|
||||
new_focus = match current_focus {
|
||||
AddTableFocus::InputTableName => AddTableFocus::InputColumnName,
|
||||
AddTableFocus::InputColumnName => AddTableFocus::InputColumnType,
|
||||
AddTableFocus::InputColumnType => AddTableFocus::AddColumnButton,
|
||||
@@ -110,10 +129,9 @@ pub fn handle_add_table_navigation(
|
||||
AddTableFocus::SaveButton => AddTableFocus::CancelButton,
|
||||
AddTableFocus::CancelButton => AddTableFocus::InputTableName, // Wrap
|
||||
};
|
||||
*command_message = format!("Focus set to {:?}", add_table_state.current_focus);
|
||||
}
|
||||
Some("prev_field") => { // Shift+Tab
|
||||
add_table_state.current_focus = match current_focus {
|
||||
new_focus = match current_focus {
|
||||
AddTableFocus::InputTableName => AddTableFocus::CancelButton, // Wrap
|
||||
AddTableFocus::InputColumnName => AddTableFocus::InputTableName,
|
||||
AddTableFocus::InputColumnType => AddTableFocus::InputColumnName,
|
||||
@@ -124,7 +142,6 @@ pub fn handle_add_table_navigation(
|
||||
AddTableFocus::SaveButton => AddTableFocus::LinksTable,
|
||||
AddTableFocus::CancelButton => AddTableFocus::SaveButton,
|
||||
};
|
||||
*command_message = format!("Focus set to {:?}", add_table_state.current_focus);
|
||||
}
|
||||
|
||||
// --- Selection ---
|
||||
@@ -132,70 +149,54 @@ pub fn handle_add_table_navigation(
|
||||
match current_focus {
|
||||
AddTableFocus::AddColumnButton => {
|
||||
*command_message = "Action: Add Column (Not Implemented)".to_string();
|
||||
// TODO: Implement logic to add column based on inputs
|
||||
// Clear input fields, add to columns list, mark unsaved changes
|
||||
// add_table_state.add_column(); // Example method call
|
||||
// TODO: Implement logic
|
||||
}
|
||||
AddTableFocus::SaveButton => {
|
||||
*command_message = "Action: Save Table (Not Implemented)".to_string();
|
||||
// TODO: Implement logic to save table (e.g., call API)
|
||||
// Mark changes as saved
|
||||
// add_table_state.save_table(); // Example method call
|
||||
// TODO: Implement logic
|
||||
}
|
||||
AddTableFocus::CancelButton => {
|
||||
*command_message = "Action: Cancel Add Table".to_string();
|
||||
// TODO: Implement logic to navigate back (e.g., update AppView history)
|
||||
// Maybe show a confirmation dialog if there are unsaved changes
|
||||
// buffer_state.go_back(); // Example call
|
||||
// TODO: Implement logic
|
||||
}
|
||||
// Selecting input fields usually means entering Edit mode (handled elsewhere)
|
||||
// Selecting tables might mean focusing on them for editing/deletion (TODO)
|
||||
AddTableFocus::ColumnsTable => {
|
||||
if let Some(index) = add_table_state.column_table_state.selected() {
|
||||
*command_message = format!("Selected column index {}", index);
|
||||
// TODO: Add logic for editing/deleting selected column
|
||||
} else {
|
||||
*command_message = "No column selected".to_string();
|
||||
}
|
||||
} else { *command_message = "No column selected".to_string(); }
|
||||
}
|
||||
AddTableFocus::IndexesTable => {
|
||||
if let Some(index) = add_table_state.index_table_state.selected() {
|
||||
*command_message = format!("Selected index index {}", index);
|
||||
// TODO: Add logic for editing/deleting selected index
|
||||
} else {
|
||||
*command_message = "No index selected".to_string();
|
||||
}
|
||||
} else { *command_message = "No index selected".to_string(); }
|
||||
}
|
||||
AddTableFocus::LinksTable => {
|
||||
if let Some(index) = add_table_state.link_table_state.selected() {
|
||||
*command_message = format!("Selected link index {}", index);
|
||||
// TODO: Add logic for editing/deleting selected link
|
||||
} else {
|
||||
*command_message = "No link selected".to_string();
|
||||
}
|
||||
} else { *command_message = "No link selected".to_string(); }
|
||||
}
|
||||
_ => {
|
||||
// For InputTableName, InputColumnName, InputColumnType,
|
||||
// the main event loop should handle 'select' by potentially
|
||||
// switching to Edit mode if not already in it.
|
||||
// We don't need specific logic here for that.
|
||||
_ => { // Input fields
|
||||
*command_message = format!("Select on {:?}", current_focus);
|
||||
handled = false; // Let main loop handle edit mode toggle maybe
|
||||
}
|
||||
}
|
||||
// Keep handled = true for select actions unless specifically set to false
|
||||
}
|
||||
|
||||
// --- Other General Keys (Ignore for add_table nav) ---
|
||||
// --- Other General Keys ---
|
||||
Some("toggle_sidebar") | Some("toggle_buffer_list") => {
|
||||
handled = false; // Let global handler manage these
|
||||
handled = false;
|
||||
}
|
||||
|
||||
// --- No matching action ---
|
||||
_ => handled = false, // Event not handled by add_table navigation
|
||||
_ => handled = false,
|
||||
}
|
||||
|
||||
// If focus changed TO a table, select the first row if nothing is selected
|
||||
if handled && current_focus != add_table_state.current_focus {
|
||||
// Update focus state if it changed and was handled
|
||||
if handled && current_focus != new_focus {
|
||||
add_table_state.current_focus = new_focus;
|
||||
*command_message = format!("Focus set to {:?}", add_table_state.current_focus);
|
||||
|
||||
// Select first item when focusing a table
|
||||
match add_table_state.current_focus {
|
||||
AddTableFocus::ColumnsTable if add_table_state.column_table_state.selected().is_none() && !add_table_state.columns.is_empty() => {
|
||||
add_table_state.column_table_state.select(Some(0));
|
||||
@@ -206,10 +207,14 @@ pub fn handle_add_table_navigation(
|
||||
AddTableFocus::LinksTable if add_table_state.link_table_state.selected().is_none() && !add_table_state.links.is_empty() => {
|
||||
add_table_state.link_table_state.select(Some(0));
|
||||
}
|
||||
_ => {} // No action needed for other focus states
|
||||
_ => {}
|
||||
}
|
||||
} else if !handled {
|
||||
// If not handled by this specific navigation, clear the message potentially set by get_general_action
|
||||
// command_message.clear(); // Optional: depends if you want default messages
|
||||
}
|
||||
|
||||
|
||||
handled
|
||||
}
|
||||
|
||||
@@ -217,20 +222,19 @@ pub fn handle_add_table_navigation(
|
||||
// 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 TableState, item_count: usize) -> bool {
|
||||
if item_count == 0 { return false; } // Cannot navigate empty table
|
||||
if item_count == 0 { return false; }
|
||||
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
|
||||
true
|
||||
} else {
|
||||
false // Was at the top
|
||||
}
|
||||
}
|
||||
None => {
|
||||
// If nothing selected, moving up could select the last item
|
||||
table_state.select(Some(item_count - 1));
|
||||
table_state.select(Some(item_count - 1)); // Select last item
|
||||
true
|
||||
}
|
||||
}
|
||||
@@ -239,20 +243,19 @@ fn navigate_table_up(table_state: &mut TableState, item_count: usize) -> bool {
|
||||
// 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 TableState, item_count: usize) -> bool {
|
||||
if item_count == 0 { return false; } // Cannot navigate empty table
|
||||
if item_count == 0 { return false; }
|
||||
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
|
||||
true
|
||||
} else {
|
||||
false // Was at the bottom
|
||||
}
|
||||
}
|
||||
None => {
|
||||
// If nothing selected, moving down could select the first item
|
||||
table_state.select(Some(0));
|
||||
table_state.select(Some(0)); // Select first item
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user