Compare commits

..

19 Commits

Author SHA1 Message Date
filipriec
57f789290d needs improvements but at least it looks like it exists 2025-04-17 11:53:31 +02:00
filipriec
f34317e504 from template to the working page 2025-04-17 11:11:33 +02:00
filipriec
8159a84447 movement 2025-04-16 23:31:28 +02:00
filipriec
f4db0e384c add table page1 2025-04-16 22:23:30 +02:00
filipriec
69953401b1 NEW PAGE ADD TABLE 2025-04-16 22:07:07 +02:00
filipriec
93a3c246c6 buttons are now added in the admin panel 2025-04-16 21:43:21 +02:00
filipriec
6505e18b0b working admin panel, needs to do buttons for navigation next 2025-04-16 21:22:34 +02:00
filipriec
51ab73014f removing debug statement 2025-04-16 19:19:15 +02:00
filipriec
05d9e6e46b working selection in the admin panel for the admin perfectly well 2025-04-16 19:15:05 +02:00
filipriec
8ea9965ee3 temp debug 2025-04-16 19:10:02 +02:00
filipriec
486df65aa3 better admin panel, main problem not fixed, needs fix now 2025-04-16 18:53:09 +02:00
filipriec
8044696d7c needed fix done 2025-04-16 18:33:00 +02:00
filipriec
04a7d86636 better movement 2025-04-16 16:30:11 +02:00
filipriec
d0e2f31ce8 admin panel improvements 2025-04-16 14:11:52 +02:00
filipriec
50fcb09758 admin panel admin width fixed 2025-04-16 13:58:34 +02:00
filipriec
6d3c09d57a CRUCIAL bug fixed in the admin panel non admin user 2025-04-16 13:43:44 +02:00
filipriec
cc994fb940 admin panel for admin 2025-04-16 13:21:59 +02:00
filipriec
eee12513dd admin panel from scratch 2025-04-16 12:56:57 +02:00
filipriec
055b6a0a43 admin panel bombarded like never before 2025-04-16 12:47:33 +02:00
20 changed files with 1254 additions and 44 deletions

View File

@@ -1,4 +1,8 @@
// src/components/admin.rs
pub mod admin_panel;
pub mod admin_panel_admin;
pub mod add_table;
pub use admin_panel::*;
pub use admin_panel_admin::*;
pub use add_table::*;

View File

@@ -0,0 +1,180 @@
// src/components/admin/add_table.rs
use crate::config::colors::themes::Theme;
use ratatui::{
layout::{Alignment, Constraint, Direction, Layout, Rect},
style::{Style, Stylize},
text::{Line, Span},
widgets::{Block, BorderType, Borders, Paragraph},
Frame,
};
use crate::state::app::state::AppState;
use crate::state::app::highlight::HighlightState;
use crate::state::pages::canvas_state::CanvasState;
use crate::components::handlers::canvas::render_canvas;
use crate::state::pages::add_table::AddTableState;
/// Renders a placeholder page for adding tables.
pub fn render_add_table(
f: &mut Frame,
area: Rect,
theme: &Theme,
_app_state: &AppState,
add_table_state: &mut AddTableState,
is_edit_mode: bool,
highlight_state: &HighlightState,
) {
// Main block for the whole page
let main_block = Block::default()
.title(" Add New Table ")
.borders(Borders::ALL)
.border_type(BorderType::Rounded)
.border_style(Style::default().fg(theme.border))
.style(Style::default().bg(theme.bg));
let inner_area = main_block.inner(area);
f.render_widget(main_block, area);
// Split the inner area horizontally: Left info pane | Right input/action pane
let horizontal_chunks = Layout::default()
.direction(Direction::Horizontal)
.constraints([
Constraint::Percentage(50), // Left Pane
Constraint::Percentage(50), // Right Pane
].as_ref())
.split(inner_area);
let left_pane = horizontal_chunks[0];
let right_pane = horizontal_chunks[1];
// --- Left Pane ---
let left_vertical_chunks = Layout::default()
.direction(Direction::Vertical)
.constraints([
Constraint::Length(3), // Profile & Table Name header
Constraint::Min(5), // Columns section (expandable)
Constraint::Length(1), // Separator
Constraint::Min(3), // Indexes section (expandable)
Constraint::Length(1), // Separator
Constraint::Min(3), // Links section (expandable)
].as_ref())
.split(left_pane);
// Profile & Table Name section
let profile_text = Paragraph::new(vec![
Line::from(Span::styled("profile: default", theme.fg)), // Placeholder
Line::from(Span::styled("table name: [tablename]", theme.fg)), // Placeholder
])
.block(
Block::default()
.borders(Borders::BOTTOM)
.border_style(Style::default().fg(theme.secondary)),
);
f.render_widget(profile_text, left_vertical_chunks[0]);
// Columns section
let columns_text = Paragraph::new(vec![
Line::from(Span::styled("Name Type", theme.accent)), // Header
])
.block(Block::default().title(Span::styled(" Columns ", theme.fg)));
f.render_widget(columns_text, left_vertical_chunks[1]);
// Indexes section
let indexes_text = Paragraph::new(vec![
Line::from(Span::styled("Column name", theme.accent)), // Header
])
.block(
Block::default()
.title(Span::styled(" Indexes ", theme.fg))
.borders(Borders::TOP) // Separator from Columns
.border_style(Style::default().fg(theme.secondary)),
);
f.render_widget(indexes_text, left_vertical_chunks[3]);
// Links section
let links_text = Paragraph::new(vec![
Line::from(Span::styled("Linked table Required", theme.accent)), // Header
])
.block(
Block::default()
.title(Span::styled(" Links ", theme.fg))
.borders(Borders::TOP) // Separator from Indexes
.border_style(Style::default().fg(theme.secondary)),
);
f.render_widget(links_text, left_vertical_chunks[5]);
// --- Right Pane ---
let right_vertical_chunks = Layout::default()
.direction(Direction::Vertical)
.constraints([
Constraint::Length(5), // Area for render_canvas (3 fields + 2 border)
Constraint::Length(3), // Add Button Area
Constraint::Min(1), // Spacer
Constraint::Length(3), // Save/Cancel buttons area
].as_ref())
.split(right_pane);
let canvas_area = right_vertical_chunks[0];
let add_button_area = right_vertical_chunks[1];
let bottom_buttons_area = right_vertical_chunks[3];
// --- Use render_canvas for Inputs ---
let _active_field_rect = render_canvas(
f,
canvas_area,
add_table_state,
&[
"Table name",
"Name",
"Type",
],
&add_table_state.current_field(),
&add_table_state.inputs().iter().map(|s| *s).collect::<Vec<&String>>(),
theme,
is_edit_mode,
highlight_state,
);
// Add Button (Placeholder)
let add_button = Paragraph::new(" Add ")
.style(Style::default().fg(theme.secondary))
.alignment(Alignment::Center)
.block(
Block::default()
.borders(Borders::ALL)
.border_type(BorderType::Rounded)
.border_style(Style::default().fg(theme.secondary)),
);
f.render_widget(add_button, add_button_area);
// Bottom Buttons Area (Save, Cancel)
let bottom_button_chunks = Layout::default()
.direction(Direction::Horizontal)
.constraints([
Constraint::Percentage(50), // Save Button
Constraint::Percentage(50), // Cancel Button
].as_ref())
.split(bottom_buttons_area);
// Save Button (Placeholder)
let save_button = Paragraph::new(" Save table ")
.style(Style::default().fg(theme.secondary))
.alignment(Alignment::Center)
.block(
Block::default()
.borders(Borders::ALL)
.border_type(BorderType::Rounded)
.border_style(Style::default().fg(theme.secondary)),
);
f.render_widget(save_button, bottom_button_chunks[0]);
// Cancel Button (Placeholder)
let cancel_button = Paragraph::new(" Cancel ")
.style(Style::default().fg(theme.secondary))
.alignment(Alignment::Center)
.block(
Block::default()
.borders(Borders::ALL)
.border_type(BorderType::Rounded)
.border_style(Style::default().fg(theme.secondary)),
);
f.render_widget(cancel_button, bottom_button_chunks[1]);
}

View File

@@ -2,6 +2,7 @@
use crate::config::colors::themes::Theme;
use crate::state::pages::auth::AuthState;
use crate::state::app::state::AppState;
use crate::state::pages::admin::AdminState;
use common::proto::multieko2::table_definition::ProfileTreeResponse;
use ratatui::{
@@ -11,21 +12,13 @@ use ratatui::{
widgets::{Block, BorderType, Borders, List, ListItem, Paragraph, Wrap},
Frame,
};
/// Renders the view specific to admin users.
fn render_admin_panel_admin(f: &mut Frame, content_chunks: &[Rect], theme: &Theme) {
// Admin-specific view placeholder
let admin_message = Paragraph::new("Admin-specific view. Profile selection not applicable.")
.style(Style::default().fg(theme.fg))
.alignment(Alignment::Center);
// Render only in the right pane for now, leaving left empty
f.render_widget(admin_message, content_chunks[1]);
}
use super::admin_panel_admin::render_admin_panel_admin;
pub fn render_admin_panel(
f: &mut Frame,
app_state: &AppState,
auth_state: &AuthState,
admin_state: &AdminState,
admin_state: &mut AdminState,
area: Rect,
theme: &Theme,
profile_tree: &ProfileTreeResponse,
@@ -45,11 +38,6 @@ pub fn render_admin_panel(
.constraints([Constraint::Length(3), Constraint::Min(1)])
.split(inner_area);
// Title
let title = Line::from(Span::styled("Admin Panel", Style::default().fg(theme.highlight)));
let title_widget = Paragraph::new(title).alignment(Alignment::Center);
f.render_widget(title_widget, chunks[0]);
// Content
let content_chunks = Layout::default()
.direction(Direction::Horizontal)
@@ -66,7 +54,13 @@ pub fn render_admin_panel(
selected_profile,
);
} else {
render_admin_panel_admin(f, &content_chunks, theme);
render_admin_panel_admin(
f,
chunks[1],
app_state,
admin_state,
theme,
);
}
}
@@ -98,8 +92,8 @@ fn render_admin_panel_non_admin(
.block(Block::default().title("Profiles"))
.highlight_style(Style::default().bg(theme.highlight).fg(theme.bg));
let mut list_state_clone = admin_state.list_state.clone();
f.render_stateful_widget(list, content_chunks[0], &mut list_state_clone);
let mut profile_list_state_clone = admin_state.profile_list_state.clone();
f.render_stateful_widget(list, content_chunks[0], &mut profile_list_state_clone);
// Profile details - Use selection info from admin_state
if let Some(profile) = admin_state

View File

@@ -0,0 +1,243 @@
// src/components/admin/admin_panel_admin.rs
use crate::config::colors::themes::Theme;
use crate::state::pages::admin::{AdminFocus, AdminState};
use crate::state::app::state::AppState;
use ratatui::{
layout::{Alignment, Constraint, Direction, Layout, Rect},
style::Style,
text::{Line, Span, Text}, // Added Text
widgets::{Block, BorderType, Borders, List, ListItem, ListState, Paragraph},
Frame,
};
/// Renders the view specific to admin users with a three-pane layout.
pub fn render_admin_panel_admin(
f: &mut Frame,
area: Rect,
app_state: &AppState,
admin_state: &mut AdminState,
theme: &Theme,
) {
// Split vertically: Top for panes, Bottom for buttons
let main_chunks = Layout::default()
.direction(Direction::Vertical)
.constraints([Constraint::Min(0), Constraint::Length(1)].as_ref()) // 1 line for buttons
.split(area);
let panes_area = main_chunks[0];
let buttons_area = main_chunks[1];
// Split the top area into three panes: Profiles | Tables | Dependencies
let pane_chunks = Layout::default()
.direction(Direction::Horizontal)
.constraints([
Constraint::Percentage(25), // Profiles
Constraint::Percentage(40), // Tables
Constraint::Percentage(35), // Dependencies
].as_ref())
.split(panes_area); // Use the whole area directly
let profiles_pane = pane_chunks[0];
let tables_pane = pane_chunks[1];
let deps_pane = pane_chunks[2];
// --- Profiles Pane (Left) ---
let profile_focus = admin_state.current_focus == AdminFocus::Profiles;
let profile_border_style = if profile_focus {
Style::default().fg(theme.highlight)
} else {
Style::default().fg(theme.border)
};
// Block for the profiles pane
let profiles_block = Block::default()
.title(" Profiles ")
.borders(Borders::ALL)
.border_type(BorderType::Rounded)
.border_style(profile_border_style);
let profiles_inner_area = profiles_block.inner(profiles_pane); // Get inner area for list
f.render_widget(profiles_block, profiles_pane); // Render the block itself
// Create profile list items
let profile_list_items: Vec<ListItem> = app_state
.profile_tree
.profiles
.iter()
.enumerate()
.map(|(idx, profile)| {
// Check persistent selection for prefix, navigation state for style/highlight
let is_selected = admin_state.selected_profile_index == Some(idx); // Use persistent state for [*]
let is_navigated = admin_state.profile_list_state.selected() == Some(idx); // Use nav state for highlight/>
let prefix = if is_selected { "[*] " } else { "[ ] " };
let style = if is_selected { // Style based on selection too
Style::default().fg(theme.highlight).add_modifier(ratatui::style::Modifier::BOLD)
} else {
Style::default().fg(theme.fg)
};
ListItem::new(Line::from(vec![
Span::styled(prefix, style),
Span::styled(&profile.name, style)
]))
})
.collect();
// Build and render profile list inside the block's inner area
let profile_list = List::new(profile_list_items)
// Highlight style depends on focus AND navigation state
.highlight_style(if profile_focus { // Use focus state
Style::default().add_modifier(ratatui::style::Modifier::REVERSED)
} else {
Style::default()
})
.highlight_symbol(if profile_focus { "> " } else { " " });
f.render_stateful_widget(profile_list, profiles_inner_area, &mut admin_state.profile_list_state);
// --- Tables Pane (Middle) ---
let table_focus = admin_state.current_focus == AdminFocus::Tables;
let table_border_style = if table_focus {
Style::default().fg(theme.highlight)
} else {
Style::default().fg(theme.border)
};
// Get selected profile information
let navigated_profile_idx = admin_state.profile_list_state.selected(); // Use nav state for display
let selected_profile_name = app_state
.profile_tree
.profiles
.get(navigated_profile_idx.unwrap_or(usize::MAX)) // Use nav state for title
.map_or("None", |p| &p.name);
// Block for the tables pane
let tables_block = Block::default()
.title(format!(" Tables (Profile: {}) ", selected_profile_name))
.borders(Borders::ALL)
.border_type(BorderType::Rounded)
.border_style(table_border_style);
let tables_inner_area = tables_block.inner(tables_pane); // Get inner area for list
f.render_widget(tables_block, tables_pane); // Render the block itself
// Create table list items and get dependencies for the selected table
let (table_list_items, selected_table_deps): (Vec<ListItem>, Vec<String>) = if let Some(
profile, // Get profile based on NAVIGATED profile index
) = navigated_profile_idx.and_then(|idx| app_state.profile_tree.profiles.get(idx)) {
let items: Vec<ListItem> = profile
.tables
.iter()
.enumerate()
.map(|(idx, table)| { // Renamed i to idx for clarity
// Check persistent selection for prefix, navigation state for style/highlight
let is_selected = admin_state.selected_table_index == Some(idx); // Use persistent state for [*]
let is_navigated = admin_state.table_list_state.selected() == Some(idx); // Use nav state for highlight/>
let prefix = if is_selected { "[*] " } else { "[ ] " };
let style = if is_navigated { // Style based on navigation highlight
Style::default().fg(theme.highlight).add_modifier(ratatui::style::Modifier::BOLD)
} else {
Style::default().fg(theme.fg)
};
ListItem::new(Line::from(vec![
Span::styled(prefix, style),
Span::styled(&table.name, style),
]))
})
.collect();
// Get dependencies only for the PERSISTENTLY selected table in the PERSISTENTLY selected profile
let chosen_profile_idx = admin_state.selected_profile_index; // Use persistent profile selection
let deps = chosen_profile_idx // Start with the chosen profile index
.and_then(|p_idx| app_state.profile_tree.profiles.get(p_idx)) // Get the chosen profile
.and_then(|p| admin_state.selected_table_index.and_then(|t_idx| p.tables.get(t_idx))) // Get the chosen table using its index
.map_or(Vec::new(), |t| t.depends_on.clone()); // If found, clone its depends_on, otherwise return empty Vec
(items, deps)
} else {
// Default when no profile is selected
(vec![ListItem::new("Select a profile to see tables")], vec![])
};
// Build and render table list inside the block's inner area
let table_list = List::new(table_list_items)
// Highlight style depends on focus AND navigation state
.highlight_style(if table_focus { // Use focus state
Style::default().add_modifier(ratatui::style::Modifier::REVERSED)
} else {
Style::default()
})
.highlight_symbol(if table_focus { "> " } else { " " }); // Focus indicator
f.render_stateful_widget(table_list, tables_inner_area, &mut admin_state.table_list_state);
// --- Dependencies Pane (Right) ---
// Get name based on PERSISTENT selections
let chosen_profile_idx = admin_state.selected_profile_index; // Use persistent profile selection
let selected_table_name = chosen_profile_idx
.and_then(|p_idx| app_state.profile_tree.profiles.get(p_idx))
.and_then(|p| admin_state.selected_table_index.and_then(|t_idx| p.tables.get(t_idx))) // Use persistent table selection
.map_or("N/A", |t| &t.name); // Get name of the selected table
// Block for the dependencies pane
let deps_block = Block::default()
.title(format!(" Dependencies (Table: {}) ", selected_table_name))
.borders(Borders::ALL)
.border_type(BorderType::Rounded)
.border_style(Style::default().fg(theme.border)); // No focus highlight for deps pane
let deps_inner_area = deps_block.inner(deps_pane); // Get inner area for content
f.render_widget(deps_block, deps_pane); // Render the block itself
// Prepare content for the dependencies paragraph
let mut deps_content = Text::default();
deps_content.lines.push(Line::from(Span::styled(
"Depends On:",
Style::default().fg(theme.accent), // Use accent color for the label
)));
if !selected_table_deps.is_empty() {
for dep in selected_table_deps {
// List each dependency
deps_content.lines.push(Line::from(Span::styled(format!("- {}", dep), theme.fg)));
}
} else {
// Indicate if there are no dependencies
deps_content.lines.push(Line::from(Span::styled(" None", theme.secondary)));
}
// Build and render dependencies paragraph inside the block's inner area
let deps_paragraph = Paragraph::new(deps_content);
f.render_widget(deps_paragraph, deps_inner_area);
// --- Buttons Row ---
let button_chunks = Layout::default()
.direction(Direction::Horizontal)
.constraints([
Constraint::Percentage(33),
Constraint::Percentage(34),
Constraint::Percentage(33),
].as_ref())
.split(buttons_area);
let btn_base_style = Style::default().fg(theme.secondary);
// Define the helper closure to get style based on focus
let get_btn_style = |button_focus: AdminFocus| {
if admin_state.current_focus == button_focus {
// Apply highlight style if this button is focused
btn_base_style.add_modifier(ratatui::style::Modifier::REVERSED)
} else {
btn_base_style // Use base style otherwise
}
};
let btn1 = Paragraph::new("Add Logic")
.style(get_btn_style(AdminFocus::Button1))
.alignment(Alignment::Center);
let btn2 = Paragraph::new("Add Table")
.style(get_btn_style(AdminFocus::Button2))
.alignment(Alignment::Center);
let btn3 = Paragraph::new("Change Table")
.style(get_btn_style(AdminFocus::Button3))
.alignment(Alignment::Center);
f.render_widget(btn1, button_chunks[0]);
f.render_widget(btn2, button_chunks[1]);
f.render_widget(btn3, button_chunks[2]);
}

View File

@@ -81,10 +81,10 @@ pub fn render_autocomplete_dropdown(
let list = List::new(items);
// State for managing selection highlight (still needed for logic)
let mut list_state = ListState::default();
list_state.select(selected_index);
let mut profile_list_state = ListState::default();
profile_list_state.select(selected_index);
// Render the list statefully *over* the background block
f.render_stateful_widget(list, dropdown_area, &mut list_state);
f.render_stateful_widget(list, dropdown_area, &mut profile_list_state);
}

View File

@@ -11,6 +11,7 @@ pub struct Theme {
pub warning: Color,
pub border: Color,
pub highlight_bg: Color,
pub inactive_highlight_bg: Color,// admin panel no idea what it really is
}
impl Theme {
@@ -33,6 +34,7 @@ impl Theme {
warning: Color::Rgb(255, 182, 193), // Pastel pink
border: Color::Rgb(220, 220, 220), // Light gray border
highlight_bg: Color::Rgb(70, 70, 70), // Darker grey for highlight background
inactive_highlight_bg: Color::Rgb(50, 50, 50),
}
}
@@ -47,6 +49,7 @@ impl Theme {
warning: Color::Rgb(255, 99, 71), // Bright red
border: Color::Rgb(100, 100, 100), // Medium gray border
highlight_bg: Color::Rgb(180, 180, 180), // Lighter grey for highlight background
inactive_highlight_bg: Color::Rgb(50, 50, 50),
}
}
@@ -61,6 +64,7 @@ impl Theme {
warning: Color::Rgb(255, 0, 0), // Red
border: Color::Rgb(0, 0, 0), // Black border
highlight_bg: Color::Rgb(180, 180, 180), // Lighter grey for highlight background
inactive_highlight_bg: Color::Rgb(50, 50, 50),
}
}
}

View File

@@ -6,7 +6,7 @@ use crate::state::app::buffer::AppView;
pub fn get_view_layer(view: &AppView) -> u8 {
match view {
AppView::Intro => 1,
AppView::Login | AppView::Register | AppView::Admin => 2,
AppView::Login | AppView::Register | AppView::Admin | AppView::AddTable => 2,
AppView::Form(_) | AppView::Scratch => 3,
}
}

View File

@@ -6,3 +6,4 @@ pub mod navigation;
pub use read_only::*;
pub use edit::*;
pub use navigation::*;

View File

@@ -1,3 +1,4 @@
// src/functions/modes/navigation.rs
// pub mod admin_nav;
pub mod admin_nav;
pub mod add_table_nav;

View File

@@ -0,0 +1,260 @@
// src/functions/modes/navigation/add_table_nav.rs
use crate::config::binds::config::Config;
use crate::state::{
app::state::AppState,
pages::add_table::{AddTableFocus, AddTableState},
};
use crossterm::event::{KeyEvent};
use ratatui::widgets::TableState; // Import 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)
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
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::InputColumnName => new_focus = AddTableFocus::InputTableName,
AddTableFocus::InputColumnType => new_focus = AddTableFocus::InputColumnName,
AddTableFocus::AddColumnButton => new_focus = AddTableFocus::InputColumnType,
AddTableFocus::ColumnsTable => {
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
}
// 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
}
}
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
}
}
AddTableFocus::SaveButton => new_focus = AddTableFocus::LinksTable,
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 => {
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
}
// 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
}
}
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
}
}
AddTableFocus::SaveButton => new_focus = AddTableFocus::CancelButton,
AddTableFocus::CancelButton => new_focus = AddTableFocus::InputTableName, // Wrap bottom
}
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("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);
}
// --- Tab / Shift+Tab Navigation ---
Some("next_field") => { // Tab
add_table_state.current_focus = match current_focus {
AddTableFocus::InputTableName => AddTableFocus::InputColumnName,
AddTableFocus::InputColumnName => AddTableFocus::InputColumnType,
AddTableFocus::InputColumnType => AddTableFocus::AddColumnButton,
AddTableFocus::AddColumnButton => AddTableFocus::ColumnsTable,
AddTableFocus::ColumnsTable => AddTableFocus::IndexesTable,
AddTableFocus::IndexesTable => AddTableFocus::LinksTable,
AddTableFocus::LinksTable => AddTableFocus::SaveButton,
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 {
AddTableFocus::InputTableName => AddTableFocus::CancelButton, // Wrap
AddTableFocus::InputColumnName => AddTableFocus::InputTableName,
AddTableFocus::InputColumnType => AddTableFocus::InputColumnName,
AddTableFocus::AddColumnButton => AddTableFocus::InputColumnType,
AddTableFocus::ColumnsTable => AddTableFocus::AddColumnButton,
AddTableFocus::IndexesTable => AddTableFocus::ColumnsTable,
AddTableFocus::LinksTable => AddTableFocus::IndexesTable,
AddTableFocus::SaveButton => AddTableFocus::LinksTable,
AddTableFocus::CancelButton => AddTableFocus::SaveButton,
};
*command_message = format!("Focus set to {:?}", add_table_state.current_focus);
}
// --- Selection ---
Some("select") => {
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
}
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
}
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
}
// 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();
}
}
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();
}
}
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();
}
}
_ => {
// 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.
*command_message = format!("Select on {:?}", current_focus);
handled = false; // Let main loop handle edit mode toggle maybe
}
}
}
// --- Other General Keys (Ignore for add_table nav) ---
Some("toggle_sidebar") | Some("toggle_buffer_list") => {
handled = false; // Let global handler manage these
}
// --- No matching action ---
_ => handled = false, // Event not handled by add_table navigation
}
// If focus changed TO a table, select the first row if nothing is selected
if handled && current_focus != add_table_state.current_focus {
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));
}
AddTableFocus::IndexesTable if add_table_state.index_table_state.selected().is_none() && !add_table_state.indexes.is_empty() => {
add_table_state.index_table_state.select(Some(0));
}
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
}
}
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 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 => {
// If nothing selected, moving up could select the last item
table_state.select(Some(item_count - 1));
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 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 => {
// If nothing selected, moving down could select the first item
table_state.select(Some(0));
true
}
}
}

View File

@@ -1 +1,183 @@
// 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};
use crate::state::app::buffer::AppView;
use crate::state::app::buffer::BufferState;
/// 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,
admin_state: &mut AdminState,
buffer_state: &mut BufferState,
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 {
// Updates navigation state, resets table state
admin_state.previous_profile(profile_count);
*command_message = "Navigated profiles".to_string();
}
}
AdminFocus::Tables => {
// Updates table navigation state
if let Some(nav_profile_idx) = admin_state.profile_list_state.selected() {
if let Some(profile) = app_state.profile_tree.profiles.get(nav_profile_idx) {
let table_count = profile.tables.len();
if table_count > 0 {
admin_state.previous_table(table_count);
*command_message = "Navigated tables".to_string();
}
}
}
}
AdminFocus::Button1 | AdminFocus::Button2 | AdminFocus::Button3 => {}
}
true // Event handled
}
Some("move_down") => {
match current_focus {
AdminFocus::Profiles => {
if profile_count > 0 {
// Updates navigation state, resets table state
admin_state.next_profile(profile_count);
*command_message = "Navigated profiles".to_string();
}
}
AdminFocus::Tables => {
if let Some(nav_profile_idx) = admin_state.profile_list_state.selected() {
if let Some(profile) = app_state.profile_tree.profiles.get(nav_profile_idx) {
let table_count = profile.tables.len();
if table_count > 0 {
admin_state.next_table(table_count);
*command_message = "Navigated tables".to_string();
}
}
}
}
AdminFocus::Button1 | AdminFocus::Button2 | AdminFocus::Button3 => {}
}
true // Event handled
}
// --- Horizontal Navigation (Focus Change) ---
Some("next_option") | Some("previous_option") => {
let old_focus = admin_state.current_focus;
let is_next = action == Some("next_option"); // Check if 'l' or 'h'
admin_state.current_focus = match old_focus {
AdminFocus::Profiles => if is_next { AdminFocus::Tables } else { AdminFocus::Button3 }, // P -> T (l) or P -> B3 (h)
AdminFocus::Tables => if is_next { AdminFocus::Button1 } else { AdminFocus::Profiles }, // T -> B1 (l) or T -> P (h)
AdminFocus::Button1 => if is_next { AdminFocus::Button2 } else { AdminFocus::Tables }, // B1 -> B2 (l) or B1 -> T (h)
AdminFocus::Button2 => if is_next { AdminFocus::Button3 } else { AdminFocus::Button1 }, // B2 -> B3 (l) or B2 -> B1 (h)
AdminFocus::Button3 => if is_next { AdminFocus::Profiles } else { AdminFocus::Button2 }, // B3 -> P (l) or B3 -> B2 (h)
};
let new_focus = admin_state.current_focus;
*command_message = format!("Focus set to {:?}", new_focus);
// Auto-select first item only when moving from Profiles to Tables via 'l'
if old_focus == AdminFocus::Profiles && new_focus == AdminFocus::Tables && is_next {
if let Some(profile_idx) = admin_state.profile_list_state.selected() {
if let Some(profile) = app_state.profile_tree.profiles.get(profile_idx) {
if !profile.tables.is_empty() {
admin_state.table_list_state.select(Some(0));
} else {
admin_state.table_list_state.select(None);
}
} else {
admin_state.table_list_state.select(None);
}
} else {
admin_state.table_list_state.select(None);
}
}
// Clear table nav selection if moving away from Tables
if old_focus == AdminFocus::Tables && new_focus != AdminFocus::Tables {
admin_state.table_list_state.select(None);
}
// Clear profile nav selection if moving away from Profiles
if old_focus == AdminFocus::Profiles && new_focus != AdminFocus::Profiles {
// Maybe keep profile nav highlight? Let's try clearing it.
// admin_state.profile_list_state.select(None); // Optional: clear profile nav highlight
}
true // Event handled
}
// --- Selection ---
Some("select") => {
match current_focus {
AdminFocus::Profiles => {
// Set the persistent selection to the currently navigated item
if let Some(nav_idx) = admin_state.profile_list_state.selected() {
admin_state.selected_profile_index = Some(nav_idx); // Set persistent selection
// Move focus to Tables (like pressing 'l')
admin_state.current_focus = AdminFocus::Tables;
// Select the first table for navigation highlight
admin_state.table_list_state.select(None); // Clear table nav first
admin_state.selected_table_index = None; // Clear persistent table selection
if let Some(profile) = app_state.profile_tree.profiles.get(nav_idx) {
if !profile.tables.is_empty() {
// Set table nav highlight
admin_state.table_list_state.select(Some(0));
}
}
*command_message = format!("Selected profile idx {}, focus on Tables", nav_idx);
} else {
*command_message = "No profile selected".to_string();
}
}
AdminFocus::Tables => {
// Set the persistent selection to the currently navigated item
if let Some(nav_idx) = admin_state.table_list_state.selected() {
admin_state.selected_table_index = Some(nav_idx); // Set persistent selection
*command_message = format!("Selected table index {}", nav_idx);
} else {
*command_message = "No table highlighted".to_string();
}
// We don't change focus here for now.
}
AdminFocus::Button1 => {
*command_message = "Action: Add Logic (Not Implemented)".to_string();
// TODO: Trigger action for Button 1
}
AdminFocus::Button2 => {
buffer_state.update_history(AppView::AddTable);
*command_message = "Navigating to Add Table page...".to_string();
}
AdminFocus::Button3 => {
*command_message = "Action: Change Table (Not Implemented)".to_string();
// TODO: Trigger action for Button 3
}
}
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
}
}

View File

@@ -39,6 +39,7 @@ use crate::modes::{
highlight::highlight,
general::{navigation, dialog},
};
use crate::functions::modes::navigation::{admin_nav, add_table_nav};
use crate::config::binds::key_sequences::KeySequenceTracker;
#[derive(Debug, Clone, PartialEq, Eq)]
@@ -103,6 +104,7 @@ impl EventHandler {
else if ui.show_login { AppView::Login }
else if ui.show_register { AppView::Register }
else if ui.show_admin { AppView::Admin }
else if ui.show_add_table { AppView::AddTable }
else if ui.show_form {
let form_name = app_state.selected_profile.clone().unwrap_or_else(|| "Data Form".to_string());
AppView::Form(form_name)
@@ -159,6 +161,36 @@ impl EventHandler {
match current_mode {
AppMode::General => {
// Prioritize Admin Panel navigation if it's visible
if app_state.ui.show_admin
&& auth_state.role.as_deref() == Some("admin") {
if admin_nav::handle_admin_navigation(
key,
config,
app_state,
admin_state,
buffer_state,
&mut self.command_message,
) {
return Ok(EventOutcome::Ok(self.command_message.clone()));
}
}
// --- Add Table Page Navigation ---
if app_state.ui.show_add_table {
if let Some(action) = config.get_general_action(key.code, key.modifiers) {
if add_table_nav::handle_add_table_navigation(
key,
config,
app_state,
&mut admin_state.add_table_state,
&mut self.command_message,
) {
return Ok(EventOutcome::Ok(self.command_message.clone()));
}
}
}
let nav_outcome = navigation::handle_navigation_event(
key,
config,
@@ -179,10 +211,9 @@ impl EventHandler {
UiContext::Intro => {
intro::handle_intro_selection(app_state, buffer_state, index);
if app_state.ui.show_admin {
let profile_names = app_state.profile_tree.profiles.iter()
.map(|p| p.name.clone())
.collect();
admin_state.set_profiles(profile_names);
if !app_state.profile_tree.profiles.is_empty() {
admin_state.profile_list_state.select(Some(0));
}
}
message = format!("Intro Option {} selected", index);
}

View File

@@ -25,7 +25,7 @@ impl ModeManager {
return AppMode::Highlight;
}
if app_state.ui.focus_outside_canvas {
if app_state.ui.focus_outside_canvas || app_state.ui.show_add_table{
return AppMode::General;
}

View File

@@ -7,6 +7,7 @@ pub enum AppView {
Login,
Register,
Admin,
AddTable,
Form(String),
Scratch,
}
@@ -18,7 +19,8 @@ impl AppView {
AppView::Intro => "Intro",
AppView::Login => "Login",
AppView::Register => "Register",
AppView::Admin => "Admin Panel",
AppView::Admin => "Admin_Panel",
AppView::AddTable => "Add_Table",
AppView::Form(name) => name.as_str(),
AppView::Scratch => "*scratch*",
}

View File

@@ -19,6 +19,7 @@ pub struct UiState {
pub show_buffer_list: bool,
pub show_intro: bool,
pub show_admin: bool,
pub show_add_table: bool,
pub show_form: bool,
pub show_login: bool,
pub show_register: bool,
@@ -134,6 +135,7 @@ impl Default for UiState {
show_sidebar: false,
show_intro: true,
show_admin: false,
show_add_table: false,
show_form: false,
show_login: false,
show_register: false,

View File

@@ -4,4 +4,5 @@ pub mod form;
pub mod auth;
pub mod admin;
pub mod intro;
pub mod add_table;
pub mod canvas_state;

View File

@@ -0,0 +1,172 @@
// src/state/pages/add_table.rs
use crate::state::pages::canvas_state::CanvasState;
use ratatui::widgets::TableState;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ColumnDefinition {
pub name: String,
pub data_type: String,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LinkDefinition {
pub linked_table_name: String,
pub is_required: bool,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum AddTableFocus {
#[default]
InputTableName, // Field 0 for CanvasState
InputColumnName, // Field 1 for CanvasState
InputColumnType, // Field 2 for CanvasState
AddColumnButton,
// Result Tables
ColumnsTable,
IndexesTable,
LinksTable,
// Buttons
SaveButton,
CancelButton,
}
#[derive(Debug, Clone)]
pub struct AddTableState {
pub profile_name: String,
pub table_name: String,
pub column_name_input: String,
pub column_type_input: String,
pub columns: Vec<ColumnDefinition>,
pub indexes: Vec<String>,
pub links: Vec<LinkDefinition>,
pub current_focus: AddTableFocus,
pub column_table_state: TableState,
pub index_table_state: TableState,
pub link_table_state: TableState,
pub table_name_cursor_pos: usize,
pub column_name_cursor_pos: usize,
pub column_type_cursor_pos: usize,
pub has_unsaved_changes: bool,
}
impl Default for AddTableState {
fn default() -> Self {
// Initialize with some dummy data for demonstration
AddTableState {
profile_name: "default".to_string(), // Should be set dynamically
table_name: String::new(), // Start empty
column_name_input: String::new(),
column_type_input: String::new(),
columns: vec![
ColumnDefinition { name: "id".to_string(), data_type: "INTEGER".to_string() },
ColumnDefinition { name: "name".to_string(), data_type: "TEXT".to_string() },
],
indexes: vec!["id".to_string()],
links: vec![
LinkDefinition { linked_table_name: "related_table".to_string(), is_required: true },
LinkDefinition { linked_table_name: "another_table".to_string(), is_required: false },
],
current_focus: AddTableFocus::InputTableName,
column_table_state: TableState::default().with_selected(0),
index_table_state: TableState::default().with_selected(0),
link_table_state: TableState::default().with_selected(0),
table_name_cursor_pos: 0,
column_name_cursor_pos: 0,
column_type_cursor_pos: 0,
has_unsaved_changes: false,
}
}
}
impl AddTableState {
const INPUT_FIELD_COUNT: usize = 3;
}
// Implement CanvasState for the input fields
impl CanvasState for AddTableState {
fn current_field(&self) -> usize {
match self.current_focus {
AddTableFocus::InputTableName => 0,
AddTableFocus::InputColumnName => 1,
AddTableFocus::InputColumnType => 2,
// If focus is elsewhere, default to the first field for canvas rendering logic
_ => 0,
}
}
fn current_cursor_pos(&self) -> usize {
match self.current_focus {
AddTableFocus::InputTableName => self.table_name_cursor_pos,
AddTableFocus::InputColumnName => self.column_name_cursor_pos,
AddTableFocus::InputColumnType => self.column_type_cursor_pos,
_ => 0, // Default if focus is not on an input field
}
}
fn has_unsaved_changes(&self) -> bool {
self.has_unsaved_changes
}
fn inputs(&self) -> Vec<&String> {
vec![&self.table_name, &self.column_name_input, &self.column_type_input]
}
fn get_current_input(&self) -> &str {
match self.current_focus {
AddTableFocus::InputTableName => &self.table_name,
AddTableFocus::InputColumnName => &self.column_name_input,
AddTableFocus::InputColumnType => &self.column_type_input,
_ => "", // Should not happen if called correctly
}
}
fn get_current_input_mut(&mut self) -> &mut String {
match self.current_focus {
AddTableFocus::InputTableName => &mut self.table_name,
AddTableFocus::InputColumnName => &mut self.column_name_input,
AddTableFocus::InputColumnType => &mut self.column_type_input,
// This case needs careful handling. If focus isn't on an input,
// which mutable string should we return? Returning the first one
// might be unexpected. Consider panicking or returning Option if this state is invalid.
// For now, returning the first field to avoid panics during rendering.
_ => &mut self.table_name,
}
}
fn fields(&self) -> Vec<&str> {
// These must match the order used in render_add_table
vec!["Table name", "Name", "Type"]
}
fn set_current_field(&mut self, index: usize) {
self.current_focus = match index {
0 => AddTableFocus::InputTableName,
1 => AddTableFocus::InputColumnName,
2 => AddTableFocus::InputColumnType,
_ => self.current_focus, // Stay on current focus if index is out of bounds
};
}
fn set_current_cursor_pos(&mut self, pos: usize) {
match self.current_focus {
AddTableFocus::InputTableName => self.table_name_cursor_pos = pos,
AddTableFocus::InputColumnName => self.column_name_cursor_pos = pos,
AddTableFocus::InputColumnType => self.column_type_cursor_pos = pos,
_ => {} // Do nothing if focus is not on an input field
}
}
fn set_has_unsaved_changes(&mut self, changed: bool) {
self.has_unsaved_changes = changed;
}
// --- Autocomplete Support (Not needed for this form yet) ---
fn get_suggestions(&self) -> Option<&[String]> {
None
}
fn get_selected_suggestion_index(&self) -> Option<usize> {
None
}
}

View File

@@ -1,65 +1,178 @@
// src/state/pages/admin.rs
use ratatui::widgets::ListState;
use crate::state::pages::add_table::AddTableState;
// Define the focus states for the admin panel panes
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum AdminFocus {
#[default] // Default focus is on the profiles list
Profiles,
Tables,
Button1,
Button2,
Button3,
}
#[derive(Default, Clone, Debug)]
pub struct AdminState {
pub profiles: Vec<String>,
pub list_state: ListState,
pub profiles: Vec<String>, // Holds profile names (used by non-admin view)
pub profile_list_state: ListState, // Tracks navigation highlight (>) in profiles
pub table_list_state: ListState, // Tracks navigation highlight (>) in tables
pub selected_profile_index: Option<usize>, // Index with [*] in profiles (persistent)
pub selected_table_index: Option<usize>, // Index with [*] in tables (persistent)
pub current_focus: AdminFocus, // Tracks which pane is focused
pub add_table_state: AddTableState,
}
impl AdminState {
/// Gets the index of the currently selected item.
pub fn get_selected_index(&self) -> Option<usize> {
self.list_state.selected()
self.profile_list_state.selected()
}
/// Gets the name of the currently selected profile.
pub fn get_selected_profile_name(&self) -> Option<&String> {
self.list_state.selected().and_then(|i| self.profiles.get(i))
self.profile_list_state.selected().and_then(|i| self.profiles.get(i))
}
/// Populates the profile list and updates/resets the selection.
pub fn set_profiles(&mut self, new_profiles: Vec<String>) {
let current_selection_index = self.list_state.selected();
let current_selection_index = self.profile_list_state.selected();
self.profiles = new_profiles;
if self.profiles.is_empty() {
self.list_state.select(None);
self.profile_list_state.select(None);
} else {
let new_selection = match current_selection_index {
Some(index) => Some(index.min(self.profiles.len() - 1)),
None => Some(0),
};
self.list_state.select(new_selection);
self.profile_list_state.select(new_selection);
}
}
/// Selects the next profile in the list, wrapping around.
pub fn next(&mut self) {
if self.profiles.is_empty() {
self.list_state.select(None);
self.profile_list_state.select(None);
return;
}
let i = match self.list_state.selected() {
let i = match self.profile_list_state.selected() {
Some(i) => if i >= self.profiles.len() - 1 { 0 } else { i + 1 },
None => 0,
};
self.list_state.select(Some(i));
self.profile_list_state.select(Some(i));
}
/// Selects the previous profile in the list, wrapping around.
pub fn previous(&mut self) {
if self.profiles.is_empty() {
self.list_state.select(None);
self.profile_list_state.select(None);
return;
}
let i = match self.list_state.selected() {
let i = match self.profile_list_state.selected() {
Some(i) => if i == 0 { self.profiles.len() - 1 } else { i - 1 },
None => self.profiles.len() - 1,
};
self.list_state.select(Some(i));
self.profile_list_state.select(Some(i));
}
/// Gets the index of the currently selected profile.
pub fn get_selected_profile_index(&self) -> Option<usize> {
self.profile_list_state.selected()
}
/// Gets the index of the currently selected table.
pub fn get_selected_table_index(&self) -> Option<usize> {
self.table_list_state.selected()
}
/// Selects a profile by index and resets table selection.
pub fn select_profile(&mut self, index: Option<usize>) {
self.profile_list_state.select(index);
self.table_list_state.select(None);
}
/// Selects a table by index.
pub fn select_table(&mut self, index: Option<usize>) {
self.table_list_state.select(index);
}
/// Selects the next profile, wrapping around.
/// `profile_count` should be the total number of profiles available.
pub fn next_profile(&mut self, profile_count: usize) {
if profile_count == 0 {
return;
}
let i = match self.get_selected_profile_index() {
Some(i) => {
if i >= profile_count - 1 {
0
} else {
i + 1
}
}
None => 0,
};
self.select_profile(Some(i)); // Use the helper method
}
/// Selects the previous profile, wrapping around.
/// `profile_count` should be the total number of profiles available.
pub fn previous_profile(&mut self, profile_count: usize) {
if profile_count == 0 {
return;
}
let i = match self.get_selected_profile_index() {
Some(i) => {
if i == 0 {
profile_count - 1
} else {
i - 1
}
}
None => 0, // Or profile_count - 1 if you prefer wrapping from None
};
self.select_profile(Some(i)); // Use the helper method
}
/// Selects the next table, wrapping around.
/// `table_count` should be the number of tables in the *currently selected* profile.
pub fn next_table(&mut self, table_count: usize) {
if table_count == 0 {
return;
}
let i = match self.get_selected_table_index() {
Some(i) => {
if i >= table_count - 1 {
0
} else {
i + 1
}
}
None => 0,
};
self.select_table(Some(i));
}
/// Selects the previous table, wrapping around.
/// `table_count` should be the number of tables in the *currently selected* profile.
pub fn previous_table(&mut self, table_count: usize) {
if table_count == 0 {
return;
}
let i = match self.get_selected_table_index() {
Some(i) => {
if i == 0 {
table_count - 1
} else {
i - 1
}
}
None => 0, // Or table_count - 1
};
self.select_table(Some(i));
}
}

View File

@@ -8,6 +8,7 @@ use crate::components::{
intro::intro::render_intro,
handlers::sidebar::{self, calculate_sidebar_layout},
form::form::render_form,
admin::render_add_table,
auth::{login::render_login, register::render_register},
};
use crate::config::colors::themes::Theme;
@@ -96,6 +97,16 @@ pub fn render_ui(
register_state.current_field < 4,
highlight_state,
);
} else if app_state.ui.show_add_table {
render_add_table(
f,
main_content_area,
theme,
app_state,
&mut admin_state.add_table_state,
login_state.current_field < 3,
highlight_state,
);
} else if app_state.ui.show_login {
render_login(
f,
@@ -109,6 +120,7 @@ pub fn render_ui(
} else if app_state.ui.show_admin {
crate::components::admin::admin_panel::render_admin_panel(
f,
app_state,
auth_state,
admin_state,
main_content_area,

View File

@@ -67,12 +67,20 @@ pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
app_state.ui.show_login = false;
app_state.ui.show_register = false;
app_state.ui.show_admin = false;
app_state.ui.show_add_table = false;
app_state.ui.show_form = false;
match active_view {
AppView::Intro => app_state.ui.show_intro = true,
AppView::Login => app_state.ui.show_login = true,
AppView::Register => app_state.ui.show_register = true,
AppView::Admin => app_state.ui.show_admin = true,
AppView::Admin => {
app_state.ui.show_admin = true;
let profile_names = app_state.profile_tree.profiles.iter()
.map(|p| p.name.clone())
.collect();
admin_state.set_profiles(profile_names);
}
AppView::AddTable => app_state.ui.show_add_table = true,
AppView::Form(_) => app_state.ui.show_form = true,
AppView::Scratch => {} // Or show a scratchpad component
}