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

@@ -6,12 +6,12 @@ use crate::state::app::state::AppState;
use ratatui::{ use ratatui::{
layout::{Alignment, Constraint, Direction, Layout, Rect}, layout::{Alignment, Constraint, Direction, Layout, Rect},
style::Style, style::Style,
text::{Line, Span}, text::{Line, Span, Text}, // Added Text
widgets::{Block, BorderType, Borders, List, ListItem, ListState, Paragraph}, widgets::{Block, BorderType, Borders, List, ListItem, ListState, Paragraph},
Frame, Frame,
}; };
/// Renders the view specific to admin users. /// Renders the view specific to admin users with a three-pane layout.
pub fn render_admin_panel_admin( pub fn render_admin_panel_admin(
f: &mut Frame, f: &mut Frame,
area: Rect, area: Rect,
@@ -19,16 +19,19 @@ pub fn render_admin_panel_admin(
admin_state: &mut AdminState, admin_state: &mut AdminState,
theme: &Theme, theme: &Theme,
) { ) {
let inner_area = area.inner(ratatui::layout::Margin { vertical: 1, horizontal: 1 }); // Split the area into three panes: Profiles | Tables | Dependencies
// Split the inner area into two panes
let chunks = Layout::default() let chunks = Layout::default()
.direction(Direction::Horizontal) .direction(Direction::Horizontal)
.constraints([Constraint::Percentage(30), Constraint::Percentage(70)].as_ref()) .constraints([
.split(inner_area); Constraint::Percentage(25), // Profiles
Constraint::Percentage(40), // Tables
Constraint::Percentage(35), // Dependencies
].as_ref())
.split(area); // Use the whole area directly
let left_pane = chunks[0]; let profiles_pane = chunks[0];
let right_pane = chunks[1]; let tables_pane = chunks[1];
let deps_pane = chunks[2];
// --- Profiles Pane (Left) --- // --- Profiles Pane (Left) ---
let profile_focus = admin_state.current_focus == AdminFocus::Profiles; let profile_focus = admin_state.current_focus == AdminFocus::Profiles;
@@ -38,6 +41,16 @@ pub fn render_admin_panel_admin(
Style::default().fg(theme.border) 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 let profile_list_items: Vec<ListItem> = app_state
.profile_tree .profile_tree
.profiles .profiles
@@ -45,30 +58,27 @@ pub fn render_admin_panel_admin(
.enumerate() .enumerate()
.map(|(i, profile)| { .map(|(i, profile)| {
let is_selected = admin_state.profile_list_state.selected() == Some(i); let is_selected = admin_state.profile_list_state.selected() == Some(i);
let prefix = if is_selected { "[*] " } else { "[ ] " }; let prefix = if is_selected { "[*] " } else { "[ ] " }; // Use [*] for selected
let style = if is_selected { let style = if is_selected {
Style::default().fg(theme.highlight).add_modifier(ratatui::style::Modifier::BOLD) Style::default().fg(theme.highlight).add_modifier(ratatui::style::Modifier::BOLD)
} else { } else {
Style::default().fg(theme.fg) Style::default().fg(theme.fg)
}; };
ListItem::new(Line::from(vec![Span::styled(prefix, style), Span::styled(&profile.name, style)])) ListItem::new(Line::from(vec![
Span::styled(prefix, style),
Span::styled(&profile.name, style)
]))
}) })
.collect(); .collect();
// Build and render profile list inside the block's inner area
let profile_list = List::new(profile_list_items) let profile_list = List::new(profile_list_items)
.block(
Block::default()
.title(" Profiles ")
.borders(Borders::ALL)
.border_type(BorderType::Rounded)
.border_style(profile_border_style),
)
.highlight_style(Style::default().add_modifier(ratatui::style::Modifier::REVERSED)) .highlight_style(Style::default().add_modifier(ratatui::style::Modifier::REVERSED))
.highlight_symbol(if profile_focus { "> " } else { " " }); .highlight_symbol(if profile_focus { "> " } else { " " }); // Focus indicator
f.render_stateful_widget(profile_list, left_pane, &mut admin_state.profile_list_state); f.render_stateful_widget(profile_list, profiles_inner_area, &mut admin_state.profile_list_state);
// --- Tables & Dependencies Pane (Right) --- // --- Tables Pane (Middle) ---
let table_focus = admin_state.current_focus == AdminFocus::Tables; let table_focus = admin_state.current_focus == AdminFocus::Tables;
let table_border_style = if table_focus { let table_border_style = if table_focus {
Style::default().fg(theme.highlight) Style::default().fg(theme.highlight)
@@ -76,21 +86,24 @@ pub fn render_admin_panel_admin(
Style::default().fg(theme.border) Style::default().fg(theme.border)
}; };
let right_chunks = Layout::default() // Get selected profile information
.direction(Direction::Vertical)
.constraints([Constraint::Min(0), Constraint::Length(3)])
.split(right_pane);
let tables_area = right_chunks[0];
let deps_area = right_chunks[1];
let selected_profile_idx = admin_state.profile_list_state.selected(); let selected_profile_idx = admin_state.profile_list_state.selected();
let selected_profile_name = app_state let selected_profile_name = app_state
.profile_tree .profile_tree
.profiles .profiles
.get(selected_profile_idx.unwrap_or(usize::MAX)) .get(selected_profile_idx.unwrap_or(usize::MAX)) // Use index, provide default if None
.map_or("None", |p| &p.name); .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( let (table_list_items, selected_table_deps): (Vec<ListItem>, Vec<String>) = if let Some(
profile, profile,
) = selected_profile_idx.and_then(|idx| app_state.profile_tree.profiles.get(idx)) { ) = selected_profile_idx.and_then(|idx| app_state.profile_tree.profiles.get(idx)) {
@@ -100,12 +113,8 @@ pub fn render_admin_panel_admin(
.enumerate() .enumerate()
.map(|(i, table)| { .map(|(i, table)| {
let is_selected = admin_state.table_list_state.selected() == Some(i); let is_selected = admin_state.table_list_state.selected() == Some(i);
let prefix = if is_selected { "[*] " } else { "[ ] " }; let prefix = if is_selected { "[*] " } else { "[ ] " }; // Use [*] for selected
let deps_str = if !table.depends_on.is_empty() { // Don't show dependencies inline anymore
format!(" -> [{}]", table.depends_on.join(", "))
} else {
String::new()
};
let style = if is_selected { let style = if is_selected {
Style::default().fg(theme.highlight).add_modifier(ratatui::style::Modifier::BOLD) Style::default().fg(theme.highlight).add_modifier(ratatui::style::Modifier::BOLD)
} else { } else {
@@ -114,51 +123,62 @@ pub fn render_admin_panel_admin(
ListItem::new(Line::from(vec![ ListItem::new(Line::from(vec![
Span::styled(prefix, style), Span::styled(prefix, style),
Span::styled(&table.name, style), Span::styled(&table.name, style),
Span::styled(deps_str, style.fg(theme.secondary)),
])) ]))
}) })
.collect(); .collect();
// Get dependencies only for the currently selected/highlighted table
let deps = admin_state.table_list_state.selected() let deps = admin_state.table_list_state.selected()
.and_then(|idx| profile.tables.get(idx)) .and_then(|idx| profile.tables.get(idx))
.map_or(vec![], |t| t.depends_on.clone()); .map_or(vec![], |t| t.depends_on.clone());
(items, deps) (items, deps)
} else { } else {
(vec![ListItem::new("No profile selected or profile has no tables")], vec![]) // 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) let table_list = List::new(table_list_items)
.block(
Block::default()
.title(format!(" Tables & Dependencies (Profile: {}) ", selected_profile_name))
.borders(Borders::ALL)
.border_type(BorderType::Rounded)
.border_style(table_border_style),
)
.highlight_style(Style::default().add_modifier(ratatui::style::Modifier::REVERSED)) .highlight_style(Style::default().add_modifier(ratatui::style::Modifier::REVERSED))
.highlight_symbol(if table_focus { "> " } else { " " }); .highlight_symbol(if table_focus { "> " } else { " " }); // Focus indicator
f.render_stateful_widget(table_list, tables_area, &mut admin_state.table_list_state); f.render_stateful_widget(table_list, tables_inner_area, &mut admin_state.table_list_state);
// --- Dependencies Display --- // --- Dependencies Pane (Right) ---
let selected_table_name = selected_profile_idx let selected_table_name = selected_profile_idx
.and_then(|p_idx| app_state.profile_tree.profiles.get(p_idx)) .and_then(|p_idx| app_state.profile_tree.profiles.get(p_idx))
.and_then(|p| admin_state.table_list_state.selected().and_then(|t_idx| p.tables.get(t_idx))) .and_then(|p| admin_state.table_list_state.selected().and_then(|t_idx| p.tables.get(t_idx)))
.map_or("N/A", |t| &t.name); .map_or("N/A", |t| &t.name); // Get name of the selected table
let deps_text = if !selected_table_deps.is_empty() { // Block for the dependencies pane
format!("Dependencies for {}: {}", selected_table_name, selected_table_deps.join(", ")) let deps_block = Block::default()
} else { .title(format!(" Dependencies (Table: {}) ", selected_table_name))
format!("Dependencies for {}: None", 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
let deps_paragraph = Paragraph::new(deps_text) // Prepare content for the dependencies paragraph
.style(Style::default().fg(theme.secondary)) let mut deps_content = Text::default();
.block( deps_content.lines.push(Line::from(Span::styled(
Block::default() "Depends On:",
.borders(Borders::TOP) Style::default().fg(theme.accent), // Use accent color for the label
.border_style(Style::default().fg(theme.border)), )));
);
f.render_widget(deps_paragraph, deps_area); 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);
}

View File

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

View File

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

View File

@@ -1 +1,125 @@
// src/functions/modes/navigation/admin_nav.rs // 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
}
}

View File

@@ -39,6 +39,7 @@ use crate::modes::{
highlight::highlight, highlight::highlight,
general::{navigation, dialog}, general::{navigation, dialog},
}; };
use crate::functions::modes::navigation::admin_nav;
use crate::config::binds::key_sequences::KeySequenceTracker; use crate::config::binds::key_sequences::KeySequenceTracker;
#[derive(Debug, Clone, PartialEq, Eq)] #[derive(Debug, Clone, PartialEq, Eq)]
@@ -159,6 +160,19 @@ impl EventHandler {
match current_mode { match current_mode {
AppMode::General => { AppMode::General => {
// Prioritize Admin Panel navigation if it's visible
if app_state.ui.show_admin {
if admin_nav::handle_admin_navigation(
key,
config,
app_state,
admin_state,
&mut self.command_message,
) {
return Ok(EventOutcome::Ok(self.command_message.clone()));
}
}
let nav_outcome = navigation::handle_navigation_event( let nav_outcome = navigation::handle_navigation_event(
key, key,
config, config,
@@ -179,10 +193,9 @@ impl EventHandler {
UiContext::Intro => { UiContext::Intro => {
intro::handle_intro_selection(app_state, buffer_state, index); intro::handle_intro_selection(app_state, buffer_state, index);
if app_state.ui.show_admin { if app_state.ui.show_admin {
let profile_names = app_state.profile_tree.profiles.iter() if !app_state.profile_tree.profiles.is_empty() {
.map(|p| p.name.clone()) admin_state.profile_list_state.select(Some(0));
.collect(); }
admin_state.set_profiles(profile_names);
} }
message = format!("Intro Option {} selected", index); message = format!("Intro Option {} selected", index);
} }