Compare commits
34 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
14b81cba19 | ||
|
|
2b37de3b4d | ||
|
|
73d9a6367c | ||
|
|
c90233b56f | ||
|
|
39dcf38462 | ||
|
|
efa27cd2dd | ||
|
|
ca231964f2 | ||
|
|
2bb83cb990 | ||
|
|
305bcfcf62 | ||
|
|
92a9011f27 | ||
|
|
e64cebdfc2 | ||
|
|
0db426d278 | ||
|
|
6bfef1c7a0 | ||
|
|
f50fe788cb | ||
|
|
4db78ecf1b | ||
|
|
f22dd7749f | ||
|
|
75af0c3be1 | ||
|
|
6f36b84f85 | ||
|
|
e723198b72 | ||
|
|
8f74febff1 | ||
|
|
d9bd6f8e1d | ||
|
|
bf55417901 | ||
|
|
9511970a1a | ||
|
|
5c8557b369 | ||
|
|
5e47c53fcf | ||
|
|
f7493a8bc4 | ||
|
|
4f39b93edd | ||
|
|
ff8b4eb0f6 | ||
|
|
e921862a7f | ||
|
|
4d7177f15a | ||
|
|
c5d7f56399 | ||
|
|
57f789290d | ||
|
|
f34317e504 | ||
|
|
8159a84447 |
4
Cargo.lock
generated
4
Cargo.lock
generated
@@ -1656,9 +1656,9 @@ dependencies = [
|
|||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "log"
|
name = "log"
|
||||||
version = "0.4.26"
|
version = "0.4.27"
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "30bde2b3dc3671ae49d8e2e9f044c7c005836e7a023ee57cffa25ab82764bb9e"
|
checksum = "13dc2df351e3202783a1fe0d44375f7295ffb4049267b0f3018346dc122a1d94"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "lru"
|
name = "lru"
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ toggle_sidebar = ["ctrl+t"]
|
|||||||
toggle_buffer_list = ["ctrl+b"]
|
toggle_buffer_list = ["ctrl+b"]
|
||||||
next_field = ["Tab"]
|
next_field = ["Tab"]
|
||||||
prev_field = ["Shift+Tab"]
|
prev_field = ["Shift+Tab"]
|
||||||
|
exit_table_scroll = ["esc"]
|
||||||
|
|
||||||
[keybindings.common]
|
[keybindings.common]
|
||||||
save = ["ctrl+s"]
|
save = ["ctrl+s"]
|
||||||
@@ -60,16 +61,17 @@ enter_highlight_mode_linewise = ["ctrl+v"]
|
|||||||
# BIG CHANGES NOW EXIT HANDLES EITHER IF THOSE
|
# BIG CHANGES NOW EXIT HANDLES EITHER IF THOSE
|
||||||
# exit_edit_mode = ["esc","ctrl+e"]
|
# exit_edit_mode = ["esc","ctrl+e"]
|
||||||
# exit_suggestion_mode = ["esc"]
|
# exit_suggestion_mode = ["esc"]
|
||||||
|
# select_suggestion = ["enter"]
|
||||||
|
# next_field = ["enter"]
|
||||||
|
enter_decider = ["enter"]
|
||||||
|
prev_field = ["shift+enter"]
|
||||||
exit = ["esc", "ctrl+e"]
|
exit = ["esc", "ctrl+e"]
|
||||||
delete_char_forward = ["delete"]
|
delete_char_forward = ["delete"]
|
||||||
delete_char_backward = ["backspace"]
|
delete_char_backward = ["backspace"]
|
||||||
next_field = ["enter"]
|
|
||||||
prev_field = ["shift+enter"]
|
|
||||||
move_left = ["left"]
|
move_left = ["left"]
|
||||||
move_right = ["right"]
|
move_right = ["right"]
|
||||||
suggestion_down = ["ctrl+n", "tab"]
|
suggestion_down = ["ctrl+n", "tab"]
|
||||||
suggestion_up = ["ctrl+p", "shift+tab"]
|
suggestion_up = ["ctrl+p", "shift+tab"]
|
||||||
select_suggestion = ["enter"]
|
|
||||||
|
|
||||||
[keybindings.command]
|
[keybindings.command]
|
||||||
exit_command_mode = ["ctrl+g", "esc"]
|
exit_command_mode = ["ctrl+g", "esc"]
|
||||||
|
|||||||
@@ -1,209 +1,501 @@
|
|||||||
// src/components/admin/add_table.rs
|
// src/components/admin/add_table.rs
|
||||||
use crate::config::colors::themes::Theme;
|
use crate::config::colors::themes::Theme;
|
||||||
use crate::state::{
|
use crate::state::app::highlight::HighlightState;
|
||||||
app::state::AppState,
|
use crate::state::app::state::AppState;
|
||||||
pages::add_table::{AddTableFocus, AddTableState},
|
use crate::state::pages::add_table::{AddTableFocus, AddTableState};
|
||||||
};
|
use crate::state::pages::canvas_state::CanvasState;
|
||||||
|
// use crate::state::pages::add_table::{ColumnDefinition, LinkDefinition}; // Not directly used here
|
||||||
use ratatui::{
|
use ratatui::{
|
||||||
layout::{Alignment, Constraint, Direction, Layout, Rect},
|
layout::{Alignment, Constraint, Direction, Layout, Rect},
|
||||||
style::{Modifier, Style, Stylize},
|
style::{Modifier, Style},
|
||||||
text::{Line, Span},
|
text::{Line, Span},
|
||||||
widgets::{
|
widgets::{Block, BorderType, Borders, Cell, Paragraph, Row, Table},
|
||||||
Block, BorderType, Borders, Cell, Paragraph, Row, Table, TableState,
|
|
||||||
},
|
|
||||||
Frame,
|
Frame,
|
||||||
};
|
};
|
||||||
|
use crate::components::handlers::canvas::render_canvas;
|
||||||
|
use crate::components::common::dialog;
|
||||||
|
|
||||||
/// Renders the detailed page for adding a new table.
|
/// Renders the Add New Table page layout, structuring the display of table information,
|
||||||
|
/// input fields, and action buttons. Adapts layout based on terminal width.
|
||||||
pub fn render_add_table(
|
pub fn render_add_table(
|
||||||
f: &mut Frame,
|
f: &mut Frame,
|
||||||
area: Rect,
|
area: Rect,
|
||||||
theme: &Theme,
|
theme: &Theme,
|
||||||
app_state: &AppState,
|
app_state: &AppState, // Currently unused, might be needed later
|
||||||
add_table_state: &mut AddTableState,
|
add_table_state: &mut AddTableState,
|
||||||
|
is_edit_mode: bool, // Determines if canvas inputs are in edit mode
|
||||||
|
highlight_state: &HighlightState, // For text highlighting in canvas
|
||||||
) {
|
) {
|
||||||
let focused_style = Style::default().fg(theme.highlight);
|
// --- Configuration ---
|
||||||
let unfocused_style = Style::default().fg(theme.border);
|
// Threshold width to switch between wide and narrow layouts
|
||||||
let base_style = Style::default().fg(theme.fg);
|
const NARROW_LAYOUT_THRESHOLD: u16 = 120; // Adjust this value as needed
|
||||||
let header_style = Style::default().fg(theme.secondary);
|
|
||||||
let selected_row_style = Style::default().bg(theme.highlight).fg(theme.bg);
|
|
||||||
|
|
||||||
// --- Main Block ---
|
// --- State Checks ---
|
||||||
|
let focus_on_canvas_inputs = matches!(
|
||||||
|
add_table_state.current_focus,
|
||||||
|
AddTableFocus::InputTableName
|
||||||
|
| AddTableFocus::InputColumnName
|
||||||
|
| AddTableFocus::InputColumnType
|
||||||
|
);
|
||||||
|
|
||||||
|
// --- Main Page Block ---
|
||||||
let main_block = Block::default()
|
let main_block = Block::default()
|
||||||
.title(" Create New Table ")
|
.title(" Add New Table ")
|
||||||
|
.title_alignment(Alignment::Center)
|
||||||
.borders(Borders::ALL)
|
.borders(Borders::ALL)
|
||||||
.border_type(BorderType::Rounded)
|
.border_type(BorderType::Rounded)
|
||||||
.border_style(Style::default().fg(theme.accent))
|
.border_style(Style::default().fg(theme.border))
|
||||||
.style(Style::default().bg(theme.bg));
|
.style(Style::default().bg(theme.bg));
|
||||||
let inner_area = main_block.inner(area);
|
let inner_area = main_block.inner(area);
|
||||||
f.render_widget(main_block, area);
|
f.render_widget(main_block, area);
|
||||||
|
|
||||||
// --- Layout ---
|
// --- Fullscreen Columns Table Check (Narrow Screens Only) ---
|
||||||
let constraints = [
|
if area.width < NARROW_LAYOUT_THRESHOLD && add_table_state.current_focus == AddTableFocus::InsideColumnsTable {
|
||||||
Constraint::Length(1), // Profile line
|
// Render ONLY the columns table taking the full inner area
|
||||||
Constraint::Length(1), // Table Name line
|
let columns_border_style = Style::default().fg(theme.highlight); // Always highlighted when fullscreen
|
||||||
Constraint::Length(1), // Spacer
|
let column_rows: Vec<Row<'_>> = add_table_state
|
||||||
Constraint::Length(1), // Columns Header
|
.columns
|
||||||
Constraint::Min(3), // Columns Table (at least 3 rows visible)
|
.iter()
|
||||||
Constraint::Length(1), // Spacer
|
.map(|col_def| {
|
||||||
Constraint::Length(1), // Indexes Header
|
Row::new(vec![
|
||||||
Constraint::Min(2), // Indexes Table
|
Cell::from(if col_def.selected { "[*]" } else { "[ ]" }),
|
||||||
Constraint::Length(1), // Spacer
|
Cell::from(col_def.name.clone()),
|
||||||
Constraint::Length(1), // Links Header
|
Cell::from(col_def.data_type.clone()),
|
||||||
Constraint::Min(3), // Links Table
|
])
|
||||||
Constraint::Length(1), // Spacer
|
.style(Style::default().fg(theme.fg))
|
||||||
Constraint::Length(1), // Action Buttons
|
})
|
||||||
];
|
.collect();
|
||||||
let chunks = Layout::default()
|
let header_cells = ["Sel", "Name", "Type"]
|
||||||
.direction(Direction::Vertical)
|
.iter()
|
||||||
.constraints(constraints)
|
.map(|h| Cell::from(*h).style(Style::default().fg(theme.accent)));
|
||||||
.margin(1) // Add margin inside the main block
|
let header = Row::new(header_cells).height(1).bottom_margin(1);
|
||||||
.split(inner_area);
|
let columns_table = Table::new(column_rows, [Constraint::Length(5), Constraint::Percentage(50), Constraint::Percentage(50)])
|
||||||
|
.header(header)
|
||||||
|
.block(
|
||||||
|
Block::default()
|
||||||
|
.title(Span::styled(" Columns (Fullscreen) ", theme.fg)) // Indicate fullscreen
|
||||||
|
.title_alignment(Alignment::Center)
|
||||||
|
.borders(Borders::ALL)
|
||||||
|
.border_type(BorderType::Rounded)
|
||||||
|
.border_style(columns_border_style),
|
||||||
|
)
|
||||||
|
.row_highlight_style(
|
||||||
|
Style::default()
|
||||||
|
.add_modifier(Modifier::REVERSED)
|
||||||
|
.fg(theme.highlight),
|
||||||
|
)
|
||||||
|
.highlight_symbol(" > "); // Use the inside symbol
|
||||||
|
f.render_stateful_widget(columns_table, inner_area, &mut add_table_state.column_table_state);
|
||||||
|
return; // IMPORTANT: Stop rendering here for fullscreen mode
|
||||||
|
}
|
||||||
|
|
||||||
// --- Helper: Get Border Style based on Focus ---
|
// --- Area Variable Declarations ---
|
||||||
let get_border_style = |focus: AddTableFocus| {
|
let top_info_area: Rect;
|
||||||
if add_table_state.current_focus == focus {
|
let columns_area: Rect;
|
||||||
focused_style
|
let canvas_area: Rect;
|
||||||
} else {
|
let add_button_area: Rect;
|
||||||
unfocused_style
|
let indexes_area: Rect;
|
||||||
}
|
let links_area: Rect;
|
||||||
};
|
let bottom_buttons_area: Rect;
|
||||||
|
|
||||||
// --- 1. Profile Line ---
|
// --- Layout Decision ---
|
||||||
// TODO: Fetch actual selected profile if needed, using app_state.selected_profile or admin_state
|
if area.width >= NARROW_LAYOUT_THRESHOLD {
|
||||||
let profile_text = format!("Profile: {}", add_table_state.profile_name);
|
// --- WIDE Layout (Based on first screenshot) ---
|
||||||
f.render_widget(Paragraph::new(profile_text).style(base_style), chunks[0]);
|
let main_chunks = Layout::default()
|
||||||
|
.direction(Direction::Vertical)
|
||||||
|
.constraints([
|
||||||
|
Constraint::Length(3), // Top Info (Profile/Table Name) - Increased to 3 lines
|
||||||
|
Constraint::Min(10), // Middle Area (Columns | Right Pane)
|
||||||
|
Constraint::Length(3), // Bottom Buttons
|
||||||
|
])
|
||||||
|
.split(inner_area);
|
||||||
|
|
||||||
// --- 2. Table Name Line ---
|
top_info_area = main_chunks[0];
|
||||||
let table_name_block = Block::default()
|
let middle_area = main_chunks[1];
|
||||||
.borders(Borders::BOTTOM)
|
bottom_buttons_area = main_chunks[2];
|
||||||
.border_style(get_border_style(AddTableFocus::TableName));
|
|
||||||
// Basic rendering for now, cursor needs event handling logic
|
|
||||||
let table_name_text = format!("Table Name: [ {} ]", add_table_state.table_name);
|
|
||||||
let table_name_paragraph = Paragraph::new(table_name_text)
|
|
||||||
.style(base_style)
|
|
||||||
.block(table_name_block);
|
|
||||||
f.render_widget(table_name_paragraph, chunks[1]);
|
|
||||||
|
|
||||||
// --- 4. Columns Header ---
|
// Split Middle Horizontally
|
||||||
let columns_header = Paragraph::new(Line::from(vec![
|
let middle_chunks = Layout::default()
|
||||||
Span::styled(" Name ", header_style),
|
.direction(Direction::Horizontal)
|
||||||
Span::raw("| "),
|
.constraints([
|
||||||
Span::styled("Type ", header_style),
|
Constraint::Percentage(50), // Left: Columns Table
|
||||||
Span::raw("| "),
|
Constraint::Percentage(50), // Right: Inputs etc.
|
||||||
Span::styled(" <- Add Column (Ctrl+A)", header_style.add_modifier(Modifier::ITALIC)),
|
])
|
||||||
]));
|
.split(middle_area);
|
||||||
f.render_widget(columns_header, chunks[3]);
|
|
||||||
|
|
||||||
// --- 5. Columns Table ---
|
columns_area = middle_chunks[0];
|
||||||
let columns_block = Block::default()
|
let right_pane_area = middle_chunks[1];
|
||||||
.borders(Borders::TOP | Borders::BOTTOM) // Only top/bottom for visual separation
|
|
||||||
.border_style(get_border_style(AddTableFocus::Columns));
|
|
||||||
let columns_table_area = columns_block.inner(chunks[4]);
|
|
||||||
f.render_widget(columns_block, chunks[4]);
|
|
||||||
|
|
||||||
let column_rows = add_table_state.columns.iter().map(|col| {
|
// Split Right Pane Vertically
|
||||||
Row::new(vec![
|
let right_pane_chunks = Layout::default()
|
||||||
Cell::from(col.name.as_str()),
|
.direction(Direction::Vertical)
|
||||||
Cell::from(col.data_type.as_str()),
|
.constraints([
|
||||||
|
Constraint::Length(5), // Input Canvas Area
|
||||||
|
Constraint::Length(3), // Add Button Area
|
||||||
|
Constraint::Min(5), // Indexes & Links Area
|
||||||
|
])
|
||||||
|
.split(right_pane_area);
|
||||||
|
|
||||||
|
canvas_area = right_pane_chunks[0];
|
||||||
|
add_button_area = right_pane_chunks[1];
|
||||||
|
let indexes_links_area = right_pane_chunks[2];
|
||||||
|
|
||||||
|
// Split Indexes/Links Horizontally
|
||||||
|
let indexes_links_chunks = Layout::default()
|
||||||
|
.direction(Direction::Horizontal)
|
||||||
|
.constraints([
|
||||||
|
Constraint::Percentage(50), // Indexes Table
|
||||||
|
Constraint::Percentage(50), // Links Table
|
||||||
|
])
|
||||||
|
.split(indexes_links_area);
|
||||||
|
indexes_area = indexes_links_chunks[0];
|
||||||
|
links_area = indexes_links_chunks[1];
|
||||||
|
|
||||||
|
// --- Top Info Rendering (Wide - 2 lines) ---
|
||||||
|
let profile_text = Paragraph::new(vec![
|
||||||
|
Line::from(Span::styled(
|
||||||
|
format!("Profile: {}", add_table_state.profile_name),
|
||||||
|
theme.fg,
|
||||||
|
)),
|
||||||
|
Line::from(Span::styled(
|
||||||
|
format!("Table name: {}", add_table_state.table_name),
|
||||||
|
theme.fg,
|
||||||
|
)),
|
||||||
])
|
])
|
||||||
.style(base_style)
|
.block(
|
||||||
});
|
Block::default()
|
||||||
let column_widths = [Constraint::Percentage(50), Constraint::Percentage(50)];
|
.borders(Borders::BOTTOM)
|
||||||
let columns_table = Table::new(column_rows, column_widths)
|
.border_style(Style::default().fg(theme.secondary)),
|
||||||
.highlight_style(selected_row_style)
|
);
|
||||||
.highlight_symbol("* "); // Indicate selection
|
f.render_widget(profile_text, top_info_area);
|
||||||
|
} else {
|
||||||
|
// --- NARROW Layout (Based on second screenshot) ---
|
||||||
|
let main_chunks = Layout::default()
|
||||||
|
.direction(Direction::Vertical)
|
||||||
|
.constraints([
|
||||||
|
Constraint::Length(1), // Top: Profile & Table Name (Single Row)
|
||||||
|
Constraint::Length(5), // Column Definition Input Canvas Area
|
||||||
|
Constraint::Length(3), // Add Button Area
|
||||||
|
Constraint::Min(5), // Columns Table Area
|
||||||
|
Constraint::Min(5), // Indexes & Links Area
|
||||||
|
Constraint::Length(3), // Bottom: Save/Cancel Buttons
|
||||||
|
])
|
||||||
|
.split(inner_area);
|
||||||
|
|
||||||
|
top_info_area = main_chunks[0];
|
||||||
|
canvas_area = main_chunks[1];
|
||||||
|
add_button_area = main_chunks[2];
|
||||||
|
columns_area = main_chunks[3];
|
||||||
|
let indexes_links_area = main_chunks[4];
|
||||||
|
bottom_buttons_area = main_chunks[5];
|
||||||
|
|
||||||
|
// Split Indexes/Links Horizontally
|
||||||
|
let indexes_links_chunks = Layout::default()
|
||||||
|
.direction(Direction::Horizontal)
|
||||||
|
.constraints([
|
||||||
|
Constraint::Percentage(50), // Indexes Table
|
||||||
|
Constraint::Percentage(50), // Links Table
|
||||||
|
])
|
||||||
|
.split(indexes_links_area);
|
||||||
|
indexes_area = indexes_links_chunks[0];
|
||||||
|
links_area = indexes_links_chunks[1];
|
||||||
|
|
||||||
|
// --- Top Info Rendering (Narrow - 1 line) ---
|
||||||
|
let top_info_chunks = Layout::default()
|
||||||
|
.direction(Direction::Horizontal)
|
||||||
|
.constraints([
|
||||||
|
Constraint::Percentage(50),
|
||||||
|
Constraint::Percentage(50),
|
||||||
|
])
|
||||||
|
.split(top_info_area);
|
||||||
|
|
||||||
|
let profile_text = Paragraph::new(Span::styled(
|
||||||
|
format!("Profile: {}", add_table_state.profile_name),
|
||||||
|
theme.fg,
|
||||||
|
))
|
||||||
|
.alignment(Alignment::Left);
|
||||||
|
f.render_widget(profile_text, top_info_chunks[0]);
|
||||||
|
|
||||||
|
let table_name_text = Paragraph::new(Span::styled(
|
||||||
|
format!("Table: {}", add_table_state.table_name),
|
||||||
|
theme.fg,
|
||||||
|
))
|
||||||
|
.alignment(Alignment::Left);
|
||||||
|
f.render_widget(table_name_text, top_info_chunks[1]);
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- Common Widget Rendering (Uses calculated areas) ---
|
||||||
|
|
||||||
|
// --- Columns Table Rendering ---
|
||||||
|
let columns_focused = matches!(add_table_state.current_focus, AddTableFocus::ColumnsTable | AddTableFocus::InsideColumnsTable);
|
||||||
|
let columns_border_style = if columns_focused {
|
||||||
|
Style::default().fg(theme.highlight)
|
||||||
|
} else {
|
||||||
|
Style::default().fg(theme.secondary)
|
||||||
|
};
|
||||||
|
let column_rows: Vec<Row<'_>> = add_table_state
|
||||||
|
.columns
|
||||||
|
.iter()
|
||||||
|
.map(|col_def| {
|
||||||
|
Row::new(vec![
|
||||||
|
Cell::from(if col_def.selected { "[*]" } else { "[ ]" }),
|
||||||
|
Cell::from(col_def.name.clone()),
|
||||||
|
Cell::from(col_def.data_type.clone()),
|
||||||
|
])
|
||||||
|
.style(Style::default().fg(theme.fg))
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
let header_cells = ["Sel", "Name", "Type"]
|
||||||
|
.iter()
|
||||||
|
.map(|h| Cell::from(*h).style(Style::default().fg(theme.accent)));
|
||||||
|
let header = Row::new(header_cells).height(1).bottom_margin(1);
|
||||||
|
let columns_highlight_symbol = if add_table_state.current_focus == AddTableFocus::InsideColumnsTable { " > " } else { " " };
|
||||||
|
let columns_table = Table::new(
|
||||||
|
column_rows,
|
||||||
|
[ // Define constraints for 3 columns: Sel, Name, Type
|
||||||
|
Constraint::Length(5),
|
||||||
|
Constraint::Percentage(60),
|
||||||
|
Constraint::Percentage(35),
|
||||||
|
],
|
||||||
|
)
|
||||||
|
.header(header)
|
||||||
|
.block(
|
||||||
|
Block::default()
|
||||||
|
.title(Span::styled(" Columns ", theme.fg))
|
||||||
|
.title_alignment(Alignment::Center)
|
||||||
|
.borders(Borders::ALL)
|
||||||
|
.border_type(BorderType::Rounded)
|
||||||
|
.border_style(columns_border_style),
|
||||||
|
)
|
||||||
|
.row_highlight_style(
|
||||||
|
Style::default()
|
||||||
|
.add_modifier(Modifier::REVERSED)
|
||||||
|
.fg(theme.highlight),
|
||||||
|
)
|
||||||
|
.highlight_symbol(" > ");
|
||||||
f.render_stateful_widget(
|
f.render_stateful_widget(
|
||||||
columns_table,
|
columns_table,
|
||||||
columns_table_area,
|
columns_area,
|
||||||
&mut add_table_state.column_table_state,
|
&mut add_table_state.column_table_state,
|
||||||
);
|
);
|
||||||
|
|
||||||
// --- 7. Indexes Header ---
|
// --- Canvas Rendering (Column Definition Input) ---
|
||||||
let indexes_header = Paragraph::new(Line::from(vec![
|
let _active_field_rect = render_canvas(
|
||||||
Span::styled(" Column Name ", header_style),
|
f,
|
||||||
Span::raw("| "),
|
canvas_area,
|
||||||
Span::styled(" <- Add Index (Ctrl+I), Remove (Ctrl+X)", header_style.add_modifier(Modifier::ITALIC)),
|
add_table_state,
|
||||||
]));
|
&add_table_state.fields(),
|
||||||
f.render_widget(indexes_header, chunks[6]);
|
&add_table_state.current_field(),
|
||||||
|
&add_table_state.inputs(),
|
||||||
|
theme,
|
||||||
|
is_edit_mode && focus_on_canvas_inputs,
|
||||||
|
highlight_state,
|
||||||
|
);
|
||||||
|
|
||||||
// --- 8. Indexes Table ---
|
// --- Button Style Helpers ---
|
||||||
let indexes_block = Block::default()
|
let get_button_style = |button_focus: AddTableFocus, current_focus| {
|
||||||
.borders(Borders::TOP | Borders::BOTTOM)
|
// Only handles text style (FG + Bold) now, no BG
|
||||||
.border_style(get_border_style(AddTableFocus::Indexes));
|
let is_focused = current_focus == button_focus;
|
||||||
let indexes_table_area = indexes_block.inner(chunks[7]);
|
let base_style = Style::default().fg(if is_focused {
|
||||||
f.render_widget(indexes_block, chunks[7]);
|
theme.highlight // Highlighted text color
|
||||||
|
} else {
|
||||||
|
theme.secondary // Normal text color
|
||||||
|
});
|
||||||
|
if is_focused {
|
||||||
|
base_style.add_modifier(Modifier::BOLD)
|
||||||
|
} else {
|
||||||
|
base_style
|
||||||
|
}
|
||||||
|
};
|
||||||
|
// Updated signature to accept bool and theme
|
||||||
|
let get_button_border_style = |is_focused: bool, theme: &Theme| {
|
||||||
|
if is_focused {
|
||||||
|
Style::default().fg(theme.highlight)
|
||||||
|
} else {
|
||||||
|
Style::default().fg(theme.secondary)
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
let index_rows = add_table_state.indexes.iter().map(|idx_col_name| {
|
// --- Add Button Rendering ---
|
||||||
Row::new(vec![Cell::from(idx_col_name.as_str())]).style(base_style)
|
// Determine if the add button is focused
|
||||||
});
|
let is_add_button_focused = add_table_state.current_focus == AddTableFocus::AddColumnButton;
|
||||||
let index_widths = [Constraint::Percentage(100)];
|
|
||||||
let indexes_table = Table::new(index_rows, index_widths)
|
// Create the Add button Paragraph widget
|
||||||
.highlight_style(selected_row_style)
|
let add_button = Paragraph::new(" Add ")
|
||||||
.highlight_symbol("* ");
|
.style(get_button_style(AddTableFocus::AddColumnButton, add_table_state.current_focus)) // Use existing closure
|
||||||
|
.alignment(Alignment::Center)
|
||||||
|
.block(
|
||||||
|
Block::default()
|
||||||
|
.borders(Borders::ALL)
|
||||||
|
.border_type(BorderType::Rounded)
|
||||||
|
.border_style(get_button_border_style(is_add_button_focused, theme)), // Pass bool and theme
|
||||||
|
);
|
||||||
|
|
||||||
|
// Render the button in its designated area
|
||||||
|
f.render_widget(add_button, add_button_area);
|
||||||
|
|
||||||
|
// --- Indexes Table Rendering ---
|
||||||
|
let indexes_focused = matches!(add_table_state.current_focus, AddTableFocus::IndexesTable | AddTableFocus::InsideIndexesTable);
|
||||||
|
let indexes_border_style = if indexes_focused {
|
||||||
|
Style::default().fg(theme.highlight)
|
||||||
|
} else {
|
||||||
|
Style::default().fg(theme.secondary)
|
||||||
|
};
|
||||||
|
let index_rows: Vec<Row<'_>> = add_table_state
|
||||||
|
.indexes
|
||||||
|
.iter()
|
||||||
|
.map(|index_name| {
|
||||||
|
Row::new(vec![Cell::from(index_name.clone())])
|
||||||
|
.style(Style::default().fg(theme.fg))
|
||||||
|
})
|
||||||
|
.collect();
|
||||||
|
let index_header_cells = ["Column Name"]
|
||||||
|
.iter()
|
||||||
|
.map(|h| Cell::from(*h).style(Style::default().fg(theme.accent)));
|
||||||
|
let index_header = Row::new(index_header_cells).height(1).bottom_margin(1);
|
||||||
|
let indexes_highlight_symbol = if add_table_state.current_focus == AddTableFocus::InsideIndexesTable { " > " } else { " " };
|
||||||
|
let indexes_table =
|
||||||
|
Table::new(index_rows, [Constraint::Percentage(100)])
|
||||||
|
.header(index_header)
|
||||||
|
.block(
|
||||||
|
Block::default()
|
||||||
|
.title(Span::styled(" Indexes ", theme.fg))
|
||||||
|
.title_alignment(Alignment::Center)
|
||||||
|
.borders(Borders::ALL)
|
||||||
|
.border_type(BorderType::Rounded)
|
||||||
|
.border_style(indexes_border_style),
|
||||||
|
)
|
||||||
|
.row_highlight_style(
|
||||||
|
Style::default()
|
||||||
|
.add_modifier(Modifier::REVERSED)
|
||||||
|
.fg(theme.highlight),
|
||||||
|
)
|
||||||
|
.highlight_symbol(" > ");
|
||||||
f.render_stateful_widget(
|
f.render_stateful_widget(
|
||||||
indexes_table,
|
indexes_table,
|
||||||
indexes_table_area,
|
indexes_area,
|
||||||
&mut add_table_state.index_table_state,
|
&mut add_table_state.index_table_state,
|
||||||
);
|
);
|
||||||
|
|
||||||
// --- 10. Links Header ---
|
// --- Links Table Rendering ---
|
||||||
let links_header = Paragraph::new(Line::from(vec![
|
let links_focused = matches!(add_table_state.current_focus, AddTableFocus::LinksTable | AddTableFocus::InsideLinksTable);
|
||||||
Span::styled(" Linked Table ", header_style),
|
let links_border_style = if links_focused {
|
||||||
Span::raw("| "),
|
Style::default().fg(theme.highlight)
|
||||||
Span::styled("Required? ", header_style),
|
} else {
|
||||||
Span::raw("| "),
|
Style::default().fg(theme.secondary)
|
||||||
Span::styled(" <- Toggle Required (Space)", header_style.add_modifier(Modifier::ITALIC)),
|
};
|
||||||
]));
|
let link_rows: Vec<Row<'_>> = add_table_state
|
||||||
f.render_widget(links_header, chunks[9]);
|
.links
|
||||||
|
.iter()
|
||||||
// --- 11. Links Table ---
|
.map(|link_def| {
|
||||||
let links_block = Block::default()
|
Row::new(vec![
|
||||||
.borders(Borders::TOP | Borders::BOTTOM)
|
Cell::from(link_def.linked_table_name.clone()),
|
||||||
.border_style(get_border_style(AddTableFocus::Links));
|
Cell::from(if link_def.is_required { "[X]" } else { "[ ]" }),
|
||||||
let links_table_area = links_block.inner(chunks[10]);
|
])
|
||||||
f.render_widget(links_block, chunks[10]);
|
.style(Style::default().fg(theme.fg))
|
||||||
|
})
|
||||||
let link_rows = add_table_state.links.iter().map(|link| {
|
.collect();
|
||||||
let required_text = if link.is_required { "[X] Yes" } else { "[ ] No " }; // Pad No for alignment
|
let link_header_cells = ["Linked Table", "Selected"]
|
||||||
Row::new(vec![
|
.iter()
|
||||||
Cell::from(link.linked_table_name.as_str()),
|
.map(|h| Cell::from(*h).style(Style::default().fg(theme.accent)));
|
||||||
Cell::from(required_text),
|
let link_header = Row::new(link_header_cells).height(1).bottom_margin(1);
|
||||||
])
|
let links_highlight_symbol = if add_table_state.current_focus == AddTableFocus::InsideLinksTable { " > " } else { " " };
|
||||||
.style(base_style)
|
let links_table =
|
||||||
});
|
Table::new(link_rows, [Constraint::Percentage(80), Constraint::Min(5)])
|
||||||
let link_widths = [Constraint::Percentage(70), Constraint::Percentage(30)];
|
.header(link_header)
|
||||||
let links_table = Table::new(link_rows, link_widths)
|
.block(
|
||||||
.highlight_style(selected_row_style)
|
Block::default()
|
||||||
.highlight_symbol("* ");
|
.title(Span::styled(" Links ", theme.fg))
|
||||||
|
.title_alignment(Alignment::Center)
|
||||||
|
.borders(Borders::ALL)
|
||||||
|
.border_type(BorderType::Rounded)
|
||||||
|
.border_style(links_border_style),
|
||||||
|
)
|
||||||
|
.row_highlight_style(
|
||||||
|
Style::default()
|
||||||
|
.add_modifier(Modifier::REVERSED)
|
||||||
|
.fg(theme.highlight),
|
||||||
|
)
|
||||||
|
.highlight_symbol(" > ");
|
||||||
f.render_stateful_widget(
|
f.render_stateful_widget(
|
||||||
links_table,
|
links_table,
|
||||||
links_table_area,
|
links_area,
|
||||||
&mut add_table_state.link_table_state,
|
&mut add_table_state.link_table_state,
|
||||||
);
|
);
|
||||||
|
|
||||||
// --- 13. Action Buttons ---
|
// --- Save/Cancel Buttons Rendering ---
|
||||||
let button_layout = Layout::default()
|
let bottom_button_chunks = Layout::default()
|
||||||
.direction(Direction::Horizontal)
|
.direction(Direction::Horizontal)
|
||||||
.constraints([Constraint::Percentage(50), Constraint::Percentage(50)])
|
.constraints([
|
||||||
.split(chunks[12]);
|
Constraint::Percentage(33), // Save Button
|
||||||
|
Constraint::Percentage(34), // Delete Button
|
||||||
|
Constraint::Percentage(33), // Cancel Button
|
||||||
|
])
|
||||||
|
.split(bottom_buttons_area);
|
||||||
|
|
||||||
let save_button = Paragraph::new("[ Save Table ]")
|
let save_button = Paragraph::new(" Save table ")
|
||||||
|
.style(get_button_style(
|
||||||
|
AddTableFocus::SaveButton,
|
||||||
|
add_table_state.current_focus,
|
||||||
|
))
|
||||||
.alignment(Alignment::Center)
|
.alignment(Alignment::Center)
|
||||||
.style(if add_table_state.current_focus == AddTableFocus::SaveButton {
|
.block(
|
||||||
selected_row_style // Use selected style for focused button
|
Block::default()
|
||||||
} else {
|
.borders(Borders::ALL)
|
||||||
base_style
|
.border_type(BorderType::Rounded)
|
||||||
});
|
.border_style(get_button_border_style(
|
||||||
let cancel_button = Paragraph::new("[ Cancel ]")
|
add_table_state.current_focus == AddTableFocus::SaveButton, // Pass bool
|
||||||
.alignment(Alignment::Center)
|
theme,
|
||||||
.style(if add_table_state.current_focus == AddTableFocus::CancelButton {
|
)),
|
||||||
selected_row_style
|
);
|
||||||
} else {
|
f.render_widget(save_button, bottom_button_chunks[0]);
|
||||||
base_style
|
|
||||||
});
|
|
||||||
|
|
||||||
f.render_widget(save_button, button_layout[0]);
|
let delete_button = Paragraph::new(" Delete Selected ")
|
||||||
f.render_widget(cancel_button, button_layout[1]);
|
.style(get_button_style(
|
||||||
|
AddTableFocus::DeleteSelectedButton,
|
||||||
|
add_table_state.current_focus,
|
||||||
|
))
|
||||||
|
.alignment(Alignment::Center)
|
||||||
|
.block(
|
||||||
|
Block::default()
|
||||||
|
.borders(Borders::ALL)
|
||||||
|
.border_type(BorderType::Rounded)
|
||||||
|
.border_style(get_button_border_style(
|
||||||
|
add_table_state.current_focus == AddTableFocus::DeleteSelectedButton, // Pass bool
|
||||||
|
theme,
|
||||||
|
)),
|
||||||
|
);
|
||||||
|
f.render_widget(delete_button, bottom_button_chunks[1]);
|
||||||
|
|
||||||
|
let cancel_button = Paragraph::new(" Cancel ")
|
||||||
|
.style(get_button_style(
|
||||||
|
AddTableFocus::CancelButton,
|
||||||
|
add_table_state.current_focus,
|
||||||
|
))
|
||||||
|
.alignment(Alignment::Center)
|
||||||
|
.block(
|
||||||
|
Block::default()
|
||||||
|
.borders(Borders::ALL)
|
||||||
|
.border_type(BorderType::Rounded)
|
||||||
|
.border_style(get_button_border_style(
|
||||||
|
add_table_state.current_focus == AddTableFocus::CancelButton, // Pass bool
|
||||||
|
theme,
|
||||||
|
)),
|
||||||
|
);
|
||||||
|
f.render_widget(cancel_button, bottom_button_chunks[2]);
|
||||||
|
|
||||||
|
// --- DIALOG ---
|
||||||
|
// Render the dialog overlay if it's active
|
||||||
|
if app_state.ui.dialog.dialog_show { // Use the passed-in app_state
|
||||||
|
dialog::render_dialog(
|
||||||
|
f,
|
||||||
|
f.area(), // Render over the whole frame area
|
||||||
|
theme,
|
||||||
|
&app_state.ui.dialog.dialog_title,
|
||||||
|
&app_state.ui.dialog.dialog_message,
|
||||||
|
&app_state.ui.dialog.dialog_buttons,
|
||||||
|
app_state.ui.dialog.dialog_active_button_index,
|
||||||
|
app_state.ui.dialog.is_loading,
|
||||||
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -142,7 +142,8 @@ pub fn render_login(
|
|||||||
&app_state.ui.dialog.dialog_title,
|
&app_state.ui.dialog.dialog_title,
|
||||||
&app_state.ui.dialog.dialog_message,
|
&app_state.ui.dialog.dialog_message,
|
||||||
&app_state.ui.dialog.dialog_buttons, // Pass buttons slice
|
&app_state.ui.dialog.dialog_buttons, // Pass buttons slice
|
||||||
app_state.ui.dialog.dialog_active_button_index, // Pass active index
|
app_state.ui.dialog.dialog_active_button_index,
|
||||||
|
app_state.ui.dialog.is_loading,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -168,6 +168,7 @@ pub fn render_register(
|
|||||||
&app_state.ui.dialog.dialog_message,
|
&app_state.ui.dialog.dialog_message,
|
||||||
&app_state.ui.dialog.dialog_buttons,
|
&app_state.ui.dialog.dialog_buttons,
|
||||||
app_state.ui.dialog.dialog_active_button_index,
|
app_state.ui.dialog.dialog_active_button_index,
|
||||||
|
app_state.ui.dialog.is_loading,
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -18,6 +18,7 @@ pub fn render_dialog(
|
|||||||
dialog_message: &str,
|
dialog_message: &str,
|
||||||
dialog_buttons: &[String],
|
dialog_buttons: &[String],
|
||||||
dialog_active_button_index: usize,
|
dialog_active_button_index: usize,
|
||||||
|
is_loading: bool,
|
||||||
) {
|
) {
|
||||||
// Calculate required height based on the actual number of lines in the message
|
// Calculate required height based on the actual number of lines in the message
|
||||||
let message_lines: Vec<_> = dialog_message.lines().collect();
|
let message_lines: Vec<_> = dialog_message.lines().collect();
|
||||||
@@ -63,27 +64,36 @@ pub fn render_dialog(
|
|||||||
vertical: 1, // Top/Bottom padding inside border
|
vertical: 1, // Top/Bottom padding inside border
|
||||||
});
|
});
|
||||||
|
|
||||||
// Layout for Message and Buttons based on actual message height
|
if is_loading {
|
||||||
let mut constraints = vec![
|
// --- Loading State ---
|
||||||
// Allocate space for message, ensuring at least 1 line height
|
let loading_text = Paragraph::new(dialog_message) // Use the message passed for loading
|
||||||
Constraint::Length(message_height.max(1)), // Use actual calculated height
|
.style(Style::default().fg(theme.fg).add_modifier(Modifier::ITALIC))
|
||||||
];
|
.alignment(Alignment::Center);
|
||||||
if button_row_height > 0 {
|
// Render loading message centered in the inner area
|
||||||
constraints.push(Constraint::Length(button_row_height));
|
f.render_widget(loading_text, inner_area);
|
||||||
}
|
} else {
|
||||||
|
// --- Normal State (Message + Buttons) ---
|
||||||
|
|
||||||
let chunks = Layout::default()
|
// Layout for Message and Buttons based on actual message height
|
||||||
.direction(Direction::Vertical)
|
let mut constraints = vec![
|
||||||
.constraints(constraints)
|
// Allocate space for message, ensuring at least 1 line height
|
||||||
.split(inner_area);
|
Constraint::Length(message_height.max(1)), // Use actual calculated height
|
||||||
|
];
|
||||||
|
if button_row_height > 0 {
|
||||||
|
constraints.push(Constraint::Length(button_row_height));
|
||||||
|
}
|
||||||
|
|
||||||
// Render Message
|
let chunks = Layout::default()
|
||||||
let available_width = inner_area.width as usize;
|
.direction(Direction::Vertical)
|
||||||
let ellipsis = "...";
|
.constraints(constraints)
|
||||||
let ellipsis_width = UnicodeWidthStr::width(ellipsis);
|
.split(inner_area);
|
||||||
|
|
||||||
let processed_lines: Vec<Line> =
|
// Render Message
|
||||||
message_lines
|
let available_width = inner_area.width as usize;
|
||||||
|
let ellipsis = "...";
|
||||||
|
let ellipsis_width = UnicodeWidthStr::width(ellipsis);
|
||||||
|
|
||||||
|
let processed_lines: Vec<Line> = message_lines
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|line| {
|
.map(|line| {
|
||||||
let line_width = UnicodeWidthStr::width(line);
|
let line_width = UnicodeWidthStr::width(line);
|
||||||
@@ -91,81 +101,83 @@ pub fn render_dialog(
|
|||||||
// Truncate with ellipsis
|
// Truncate with ellipsis
|
||||||
let mut truncated_len = 0;
|
let mut truncated_len = 0;
|
||||||
let mut current_width = 0;
|
let mut current_width = 0;
|
||||||
// Iterate over graphemes to handle multi-byte characters correctly
|
|
||||||
for (idx, grapheme) in line.grapheme_indices(true) {
|
for (idx, grapheme) in line.grapheme_indices(true) {
|
||||||
let grapheme_width = UnicodeWidthStr::width(grapheme);
|
let grapheme_width = UnicodeWidthStr::width(grapheme);
|
||||||
if current_width + grapheme_width > available_width.saturating_sub(ellipsis_width) {
|
if current_width + grapheme_width
|
||||||
break; // Stop before exceeding width needed for text + ellipsis
|
> available_width.saturating_sub(ellipsis_width)
|
||||||
|
{
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
current_width += grapheme_width;
|
current_width += grapheme_width;
|
||||||
truncated_len = idx + grapheme.len(); // Store the byte index of the end of the last fitting grapheme
|
truncated_len = idx + grapheme.len();
|
||||||
}
|
}
|
||||||
let truncated_line = format!("{}{}", &line[..truncated_len], ellipsis);
|
let truncated_line =
|
||||||
Line::from(Span::styled(truncated_line, Style::default().fg(theme.fg)))
|
format!("{}{}", &line[..truncated_len], ellipsis);
|
||||||
|
Line::from(Span::styled(
|
||||||
|
truncated_line,
|
||||||
|
Style::default().fg(theme.fg),
|
||||||
|
))
|
||||||
} else {
|
} else {
|
||||||
// Line fits, use it as is
|
|
||||||
Line::from(Span::styled(line, Style::default().fg(theme.fg)))
|
Line::from(Span::styled(line, Style::default().fg(theme.fg)))
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
let message_paragraph =
|
let message_paragraph =
|
||||||
Paragraph::new(Text::from(processed_lines)).alignment(Alignment::Center);
|
Paragraph::new(Text::from(processed_lines)).alignment(Alignment::Center);
|
||||||
// Render message in the first chunk
|
f.render_widget(message_paragraph, chunks[0]); // Render message in the first chunk
|
||||||
f.render_widget(message_paragraph, chunks[0]);
|
|
||||||
|
|
||||||
// Render Buttons if they exist and there's a chunk for them
|
// Render Buttons if they exist and there's a chunk for them
|
||||||
if !dialog_buttons.is_empty() && chunks.len() > 1 {
|
if !dialog_buttons.is_empty() && chunks.len() > 1 {
|
||||||
let button_area = chunks[1];
|
let button_area = chunks[1];
|
||||||
let button_count = dialog_buttons.len();
|
let button_count = dialog_buttons.len();
|
||||||
|
|
||||||
// Use Ratio for potentially more even distribution with few buttons
|
let button_constraints = std::iter::repeat(Constraint::Ratio(
|
||||||
let button_constraints = std::iter::repeat(Constraint::Ratio(
|
1,
|
||||||
1,
|
button_count as u32,
|
||||||
button_count as u32,
|
))
|
||||||
))
|
.take(button_count)
|
||||||
.take(button_count)
|
.collect::<Vec<_>>();
|
||||||
.collect::<Vec<_>>();
|
|
||||||
|
|
||||||
let button_chunks = Layout::default()
|
let button_chunks = Layout::default()
|
||||||
.direction(Direction::Horizontal)
|
.direction(Direction::Horizontal)
|
||||||
.constraints(button_constraints)
|
.constraints(button_constraints)
|
||||||
.horizontal_margin(1) // Add space between buttons
|
.horizontal_margin(1) // Add space between buttons
|
||||||
.split(button_area);
|
.split(button_area);
|
||||||
|
|
||||||
for (i, button_label) in dialog_buttons.iter().enumerate() {
|
for (i, button_label) in dialog_buttons.iter().enumerate() {
|
||||||
// Ensure we don't try to render into a non-existent chunk
|
if i >= button_chunks.len() {
|
||||||
if i >= button_chunks.len() {
|
break;
|
||||||
break;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
let is_active = i == dialog_active_button_index;
|
let is_active = i == dialog_active_button_index;
|
||||||
let (button_style, border_style) = if is_active {
|
let (button_style, border_style) = if is_active {
|
||||||
(
|
(
|
||||||
Style::default()
|
Style::default()
|
||||||
.fg(theme.highlight)
|
.fg(theme.highlight)
|
||||||
.add_modifier(Modifier::BOLD),
|
.add_modifier(Modifier::BOLD),
|
||||||
Style::default().fg(theme.accent), // Highlight border
|
Style::default().fg(theme.accent),
|
||||||
)
|
)
|
||||||
} else {
|
} else {
|
||||||
(
|
(
|
||||||
Style::default().fg(theme.fg),
|
Style::default().fg(theme.fg),
|
||||||
Style::default().fg(theme.border), // Normal border
|
Style::default().fg(theme.border),
|
||||||
)
|
)
|
||||||
};
|
};
|
||||||
|
|
||||||
let button_block = Block::default()
|
let button_block = Block::default()
|
||||||
.borders(Borders::ALL)
|
.borders(Borders::ALL)
|
||||||
.border_type(BorderType::Plain)
|
.border_type(BorderType::Plain)
|
||||||
.border_style(border_style);
|
.border_style(border_style);
|
||||||
|
|
||||||
f.render_widget(
|
f.render_widget(
|
||||||
Paragraph::new(button_label.as_str())
|
Paragraph::new(button_label.as_str())
|
||||||
.block(button_block)
|
.block(button_block)
|
||||||
.style(button_style)
|
.style(button_style)
|
||||||
.alignment(Alignment::Center),
|
.alignment(Alignment::Center),
|
||||||
button_chunks[i],
|
button_chunks[i],
|
||||||
);
|
);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -2,3 +2,4 @@
|
|||||||
|
|
||||||
pub mod form_e;
|
pub mod form_e;
|
||||||
pub mod auth_e;
|
pub mod auth_e;
|
||||||
|
pub mod add_table_e;
|
||||||
|
|||||||
341
client/src/functions/modes/edit/add_table_e.rs
Normal file
341
client/src/functions/modes/edit/add_table_e.rs
Normal file
@@ -0,0 +1,341 @@
|
|||||||
|
// src/functions/modes/edit/add_table_e.rs
|
||||||
|
use crate::state::pages::add_table::AddTableState;
|
||||||
|
use crate::state::pages::canvas_state::CanvasState; // Use trait
|
||||||
|
use crossterm::event::{KeyCode, KeyEvent};
|
||||||
|
use std::error::Error;
|
||||||
|
|
||||||
|
#[derive(PartialEq)]
|
||||||
|
enum CharType {
|
||||||
|
Whitespace,
|
||||||
|
Alphanumeric,
|
||||||
|
Punctuation,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_char_type(c: char) -> CharType {
|
||||||
|
if c.is_whitespace() {
|
||||||
|
CharType::Whitespace
|
||||||
|
} else if c.is_alphanumeric() {
|
||||||
|
CharType::Alphanumeric
|
||||||
|
} else {
|
||||||
|
CharType::Punctuation
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn find_next_word_start(text: &str, current_pos: usize) -> usize {
|
||||||
|
let chars: Vec<char> = text.chars().collect();
|
||||||
|
let len = chars.len();
|
||||||
|
if len == 0 || current_pos >= len {
|
||||||
|
return len;
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut pos = current_pos;
|
||||||
|
let initial_type = get_char_type(chars[pos]);
|
||||||
|
|
||||||
|
while pos < len && get_char_type(chars[pos]) == initial_type {
|
||||||
|
pos += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
while pos < len && get_char_type(chars[pos]) == CharType::Whitespace {
|
||||||
|
pos += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
pos
|
||||||
|
}
|
||||||
|
|
||||||
|
fn find_word_end(text: &str, current_pos: usize) -> usize {
|
||||||
|
let chars: Vec<char> = text.chars().collect();
|
||||||
|
let len = chars.len();
|
||||||
|
if len == 0 {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut pos = current_pos.min(len - 1);
|
||||||
|
|
||||||
|
if get_char_type(chars[pos]) == CharType::Whitespace {
|
||||||
|
pos = find_next_word_start(text, pos);
|
||||||
|
}
|
||||||
|
|
||||||
|
if pos >= len {
|
||||||
|
return len.saturating_sub(1);
|
||||||
|
}
|
||||||
|
|
||||||
|
let word_type = get_char_type(chars[pos]);
|
||||||
|
while pos < len && get_char_type(chars[pos]) == word_type {
|
||||||
|
pos += 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
pos.saturating_sub(1).min(len.saturating_sub(1))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn find_prev_word_start(text: &str, current_pos: usize) -> usize {
|
||||||
|
let chars: Vec<char> = text.chars().collect();
|
||||||
|
if chars.is_empty() || current_pos == 0 {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut pos = current_pos.saturating_sub(1);
|
||||||
|
|
||||||
|
while pos > 0 && get_char_type(chars[pos]) == CharType::Whitespace {
|
||||||
|
pos -= 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if pos == 0 && get_char_type(chars[pos]) == CharType::Whitespace {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
let word_type = get_char_type(chars[pos]);
|
||||||
|
while pos > 0 && get_char_type(chars[pos - 1]) == word_type {
|
||||||
|
pos -= 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
pos
|
||||||
|
}
|
||||||
|
|
||||||
|
fn find_prev_word_end(text: &str, current_pos: usize) -> usize {
|
||||||
|
let chars: Vec<char> = text.chars().collect();
|
||||||
|
let len = chars.len();
|
||||||
|
if len == 0 || current_pos == 0 {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
let mut pos = current_pos.saturating_sub(1);
|
||||||
|
|
||||||
|
while pos > 0 && get_char_type(chars[pos]) == CharType::Whitespace {
|
||||||
|
pos -= 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if pos == 0 && get_char_type(chars[pos]) == CharType::Whitespace {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if pos == 0 && get_char_type(chars[pos]) != CharType::Whitespace {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
let word_type = get_char_type(chars[pos]);
|
||||||
|
while pos > 0 && get_char_type(chars[pos - 1]) == word_type {
|
||||||
|
pos -= 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
while pos > 0 && get_char_type(chars[pos - 1]) == CharType::Whitespace {
|
||||||
|
pos -= 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if pos > 0 {
|
||||||
|
pos - 1
|
||||||
|
} else {
|
||||||
|
0
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Executes edit actions for the AddTable view canvas.
|
||||||
|
pub async fn execute_edit_action(
|
||||||
|
action: &str,
|
||||||
|
key: KeyEvent, // Needed for insert_char
|
||||||
|
state: &mut AddTableState,
|
||||||
|
ideal_cursor_column: &mut usize,
|
||||||
|
// Add other params like grpc_client if needed for future actions (e.g., validation)
|
||||||
|
) -> Result<String, Box<dyn Error>> {
|
||||||
|
// Use the CanvasState trait methods implemented for AddTableState
|
||||||
|
match action {
|
||||||
|
"insert_char" => {
|
||||||
|
if let KeyCode::Char(c) = key.code {
|
||||||
|
let cursor_pos = state.current_cursor_pos();
|
||||||
|
let field_value = state.get_current_input_mut();
|
||||||
|
let mut chars: Vec<char> = field_value.chars().collect();
|
||||||
|
if cursor_pos <= chars.len() {
|
||||||
|
chars.insert(cursor_pos, c);
|
||||||
|
*field_value = chars.into_iter().collect();
|
||||||
|
state.set_current_cursor_pos(cursor_pos + 1);
|
||||||
|
state.set_has_unsaved_changes(true);
|
||||||
|
*ideal_cursor_column = state.current_cursor_pos();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
return Ok("Error: insert_char called without a char key.".to_string());
|
||||||
|
}
|
||||||
|
Ok("".to_string()) // No message needed for char insertion
|
||||||
|
}
|
||||||
|
"delete_char_backward" => {
|
||||||
|
if state.current_cursor_pos() > 0 {
|
||||||
|
let cursor_pos = state.current_cursor_pos();
|
||||||
|
let field_value = state.get_current_input_mut();
|
||||||
|
let mut chars: Vec<char> = field_value.chars().collect();
|
||||||
|
if cursor_pos <= chars.len() {
|
||||||
|
chars.remove(cursor_pos - 1);
|
||||||
|
*field_value = chars.into_iter().collect();
|
||||||
|
let new_pos = cursor_pos - 1;
|
||||||
|
state.set_current_cursor_pos(new_pos);
|
||||||
|
state.set_has_unsaved_changes(true);
|
||||||
|
*ideal_cursor_column = new_pos;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok("".to_string())
|
||||||
|
}
|
||||||
|
"delete_char_forward" => {
|
||||||
|
let cursor_pos = state.current_cursor_pos();
|
||||||
|
let field_value = state.get_current_input_mut();
|
||||||
|
let mut chars: Vec<char> = field_value.chars().collect();
|
||||||
|
if cursor_pos < chars.len() {
|
||||||
|
chars.remove(cursor_pos);
|
||||||
|
*field_value = chars.into_iter().collect();
|
||||||
|
state.set_has_unsaved_changes(true);
|
||||||
|
*ideal_cursor_column = cursor_pos;
|
||||||
|
}
|
||||||
|
Ok("".to_string())
|
||||||
|
}
|
||||||
|
"next_field" => {
|
||||||
|
let num_fields = AddTableState::INPUT_FIELD_COUNT;
|
||||||
|
if num_fields > 0 {
|
||||||
|
let current_field = state.current_field();
|
||||||
|
let last_field_index = num_fields - 1;
|
||||||
|
// Prevent cycling forward
|
||||||
|
if current_field < last_field_index {
|
||||||
|
state.set_current_field(current_field + 1);
|
||||||
|
}
|
||||||
|
let current_input = state.get_current_input();
|
||||||
|
let max_pos = current_input.len();
|
||||||
|
state.set_current_cursor_pos((*ideal_cursor_column).min(max_pos));
|
||||||
|
}
|
||||||
|
Ok("".to_string())
|
||||||
|
}
|
||||||
|
"prev_field" => {
|
||||||
|
let num_fields = AddTableState::INPUT_FIELD_COUNT;
|
||||||
|
if num_fields > 0 {
|
||||||
|
let current_field = state.current_field();
|
||||||
|
if current_field > 0 {
|
||||||
|
state.set_current_field(current_field - 1);
|
||||||
|
}
|
||||||
|
let current_input = state.get_current_input();
|
||||||
|
let max_pos = current_input.len();
|
||||||
|
state.set_current_cursor_pos((*ideal_cursor_column).min(max_pos));
|
||||||
|
}
|
||||||
|
Ok("".to_string())
|
||||||
|
}
|
||||||
|
"move_left" => {
|
||||||
|
let new_pos = state.current_cursor_pos().saturating_sub(1);
|
||||||
|
state.set_current_cursor_pos(new_pos);
|
||||||
|
*ideal_cursor_column = new_pos;
|
||||||
|
Ok("".to_string())
|
||||||
|
}
|
||||||
|
"move_right" => {
|
||||||
|
let current_input = state.get_current_input();
|
||||||
|
let current_pos = state.current_cursor_pos();
|
||||||
|
if current_pos < current_input.len() {
|
||||||
|
let new_pos = current_pos + 1;
|
||||||
|
state.set_current_cursor_pos(new_pos);
|
||||||
|
*ideal_cursor_column = new_pos;
|
||||||
|
}
|
||||||
|
Ok("".to_string())
|
||||||
|
}
|
||||||
|
"move_up" => {
|
||||||
|
let current_field = state.current_field();
|
||||||
|
// Prevent moving up from the first field
|
||||||
|
if current_field > 0 {
|
||||||
|
let new_field = current_field - 1;
|
||||||
|
state.set_current_field(new_field);
|
||||||
|
let current_input = state.get_current_input();
|
||||||
|
let max_pos = current_input.len();
|
||||||
|
state.set_current_cursor_pos((*ideal_cursor_column).min(max_pos));
|
||||||
|
}
|
||||||
|
Ok("ahoj".to_string())
|
||||||
|
}
|
||||||
|
"move_down" => {
|
||||||
|
let num_fields = AddTableState::INPUT_FIELD_COUNT;
|
||||||
|
if num_fields > 0 {
|
||||||
|
let current_field = state.current_field();
|
||||||
|
let last_field_index = num_fields - 1;
|
||||||
|
if current_field < last_field_index {
|
||||||
|
let new_field = current_field + 1;
|
||||||
|
state.set_current_field(new_field);
|
||||||
|
let current_input = state.get_current_input();
|
||||||
|
let max_pos = current_input.len();
|
||||||
|
state.set_current_cursor_pos((*ideal_cursor_column).min(max_pos));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Ok("".to_string())
|
||||||
|
}
|
||||||
|
"move_line_start" => {
|
||||||
|
state.set_current_cursor_pos(0);
|
||||||
|
*ideal_cursor_column = 0;
|
||||||
|
Ok("".to_string())
|
||||||
|
}
|
||||||
|
"move_line_end" => {
|
||||||
|
let current_input = state.get_current_input();
|
||||||
|
let new_pos = current_input.len();
|
||||||
|
state.set_current_cursor_pos(new_pos);
|
||||||
|
*ideal_cursor_column = new_pos;
|
||||||
|
Ok("".to_string())
|
||||||
|
}
|
||||||
|
"move_first_line" => {
|
||||||
|
if AddTableState::INPUT_FIELD_COUNT > 0 {
|
||||||
|
state.set_current_field(0);
|
||||||
|
let current_input = state.get_current_input();
|
||||||
|
let max_pos = current_input.len();
|
||||||
|
state.set_current_cursor_pos((*ideal_cursor_column).min(max_pos));
|
||||||
|
}
|
||||||
|
Ok("".to_string())
|
||||||
|
}
|
||||||
|
"move_last_line" => {
|
||||||
|
let num_fields = AddTableState::INPUT_FIELD_COUNT;
|
||||||
|
if num_fields > 0 {
|
||||||
|
let new_field = num_fields - 1;
|
||||||
|
state.set_current_field(new_field);
|
||||||
|
let current_input = state.get_current_input();
|
||||||
|
let max_pos = current_input.len();
|
||||||
|
state.set_current_cursor_pos((*ideal_cursor_column).min(max_pos));
|
||||||
|
}
|
||||||
|
Ok("".to_string())
|
||||||
|
}
|
||||||
|
"move_word_next" => {
|
||||||
|
let current_input = state.get_current_input();
|
||||||
|
if !current_input.is_empty() {
|
||||||
|
let new_pos = find_next_word_start(current_input, state.current_cursor_pos());
|
||||||
|
let final_pos = new_pos.min(current_input.len());
|
||||||
|
state.set_current_cursor_pos(final_pos);
|
||||||
|
*ideal_cursor_column = final_pos;
|
||||||
|
}
|
||||||
|
Ok("".to_string())
|
||||||
|
}
|
||||||
|
"move_word_end" => {
|
||||||
|
let current_input = state.get_current_input();
|
||||||
|
if !current_input.is_empty() {
|
||||||
|
let current_pos = state.current_cursor_pos();
|
||||||
|
let new_pos = find_word_end(current_input, current_pos);
|
||||||
|
|
||||||
|
let final_pos = if new_pos == current_pos {
|
||||||
|
find_word_end(current_input, new_pos + 1)
|
||||||
|
} else {
|
||||||
|
new_pos
|
||||||
|
};
|
||||||
|
|
||||||
|
let max_valid_index = current_input.len().saturating_sub(1);
|
||||||
|
let clamped_pos = final_pos.min(max_valid_index);
|
||||||
|
state.set_current_cursor_pos(clamped_pos);
|
||||||
|
*ideal_cursor_column = clamped_pos;
|
||||||
|
}
|
||||||
|
Ok("".to_string())
|
||||||
|
}
|
||||||
|
"move_word_prev" => {
|
||||||
|
let current_input = state.get_current_input();
|
||||||
|
if !current_input.is_empty() {
|
||||||
|
let new_pos = find_prev_word_start(current_input, state.current_cursor_pos());
|
||||||
|
state.set_current_cursor_pos(new_pos);
|
||||||
|
*ideal_cursor_column = new_pos;
|
||||||
|
}
|
||||||
|
Ok("".to_string())
|
||||||
|
}
|
||||||
|
"move_word_end_prev" => {
|
||||||
|
let current_input = state.get_current_input();
|
||||||
|
if !current_input.is_empty() {
|
||||||
|
let new_pos = find_prev_word_end(current_input, state.current_cursor_pos());
|
||||||
|
state.set_current_cursor_pos(new_pos);
|
||||||
|
*ideal_cursor_column = new_pos;
|
||||||
|
}
|
||||||
|
Ok("".to_string())
|
||||||
|
}
|
||||||
|
// Actions handled by main event loop (mode changes, save, revert)
|
||||||
|
"exit_edit_mode" | "save" | "revert" => {
|
||||||
|
Ok("Action handled by main loop".to_string())
|
||||||
|
}
|
||||||
|
_ => Ok(format!("Unknown or unhandled edit action: {}", action)),
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,3 +1,4 @@
|
|||||||
// src/functions/modes/navigation.rs
|
// src/functions/modes/navigation.rs
|
||||||
|
|
||||||
pub mod admin_nav;
|
pub mod admin_nav;
|
||||||
|
pub mod add_table_nav;
|
||||||
|
|||||||
377
client/src/functions/modes/navigation/add_table_nav.rs
Normal file
377
client/src/functions/modes/navigation/add_table_nav.rs
Normal file
@@ -0,0 +1,377 @@
|
|||||||
|
// 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;
|
||||||
|
use crate::tui::functions::common::add_table::handle_add_column_action;
|
||||||
|
use crate::ui::handlers::context::DialogPurpose;
|
||||||
|
|
||||||
|
/// 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: &mut 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_block_focus = matches!(current_focus, // Focus on the table blocks
|
||||||
|
AddTableFocus::ColumnsTable | AddTableFocus::IndexesTable | AddTableFocus::LinksTable
|
||||||
|
);
|
||||||
|
let is_inside_table_focus = matches!(current_focus, // Focus inside for scrolling
|
||||||
|
AddTableFocus::InsideColumnsTable | AddTableFocus::InsideIndexesTable | AddTableFocus::InsideLinksTable
|
||||||
|
);
|
||||||
|
let is_right_pane_general_focus = matches!(current_focus, // Non-canvas elements in right pane
|
||||||
|
AddTableFocus::AddColumnButton | AddTableFocus::SaveButton | AddTableFocus::DeleteSelectedButton | AddTableFocus::CancelButton
|
||||||
|
);
|
||||||
|
let is_canvas_input_focus = matches!(current_focus,
|
||||||
|
AddTableFocus::InputTableName | AddTableFocus::InputColumnName | AddTableFocus::InputColumnType
|
||||||
|
);
|
||||||
|
|
||||||
|
match action.as_deref() {
|
||||||
|
// --- Handle Exiting Table Scroll Mode ---
|
||||||
|
Some("exit_table_scroll") => {
|
||||||
|
match current_focus {
|
||||||
|
AddTableFocus::InsideColumnsTable => {
|
||||||
|
add_table_state.column_table_state.select(None);
|
||||||
|
new_focus = AddTableFocus::ColumnsTable;
|
||||||
|
*command_message = "Exited Columns Table".to_string();
|
||||||
|
}
|
||||||
|
AddTableFocus::InsideIndexesTable => {
|
||||||
|
add_table_state.index_table_state.select(None);
|
||||||
|
new_focus = AddTableFocus::IndexesTable;
|
||||||
|
*command_message = "Exited Indexes Table".to_string();
|
||||||
|
}
|
||||||
|
AddTableFocus::InsideLinksTable => {
|
||||||
|
add_table_state.link_table_state.select(None);
|
||||||
|
new_focus = AddTableFocus::LinksTable;
|
||||||
|
*command_message = "Exited Links Table".to_string();
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
// Action triggered but not applicable in this focus state
|
||||||
|
handled = false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// If handled (i.e., focus changed), handled remains true.
|
||||||
|
// If not handled, handled becomes false.
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- Vertical Navigation (Up/Down) ---
|
||||||
|
Some("move_up") => {
|
||||||
|
match current_focus {
|
||||||
|
AddTableFocus::InputTableName => new_focus = AddTableFocus::CancelButton,
|
||||||
|
AddTableFocus::InputColumnName => new_focus = AddTableFocus::InputTableName,
|
||||||
|
AddTableFocus::InputColumnType => new_focus = AddTableFocus::InputColumnName,
|
||||||
|
AddTableFocus::AddColumnButton => new_focus = AddTableFocus::InputColumnType,
|
||||||
|
// Navigate between blocks when focus is on the table block itself
|
||||||
|
AddTableFocus::ColumnsTable => new_focus = AddTableFocus::AddColumnButton, // Move up to right pane
|
||||||
|
AddTableFocus::IndexesTable => new_focus = AddTableFocus::ColumnsTable,
|
||||||
|
AddTableFocus::LinksTable => new_focus = AddTableFocus::IndexesTable,
|
||||||
|
// Scroll inside the table when focus is internal
|
||||||
|
AddTableFocus::InsideColumnsTable => {
|
||||||
|
navigate_table_up(&mut add_table_state.column_table_state, add_table_state.columns.len());
|
||||||
|
// Stay inside the table, don't change new_focus
|
||||||
|
}
|
||||||
|
AddTableFocus::InsideIndexesTable => {
|
||||||
|
navigate_table_up(&mut add_table_state.index_table_state, add_table_state.indexes.len());
|
||||||
|
// Stay inside the table
|
||||||
|
}
|
||||||
|
AddTableFocus::InsideLinksTable => {
|
||||||
|
navigate_table_up(&mut add_table_state.link_table_state, add_table_state.links.len());
|
||||||
|
// Stay inside the table
|
||||||
|
}
|
||||||
|
AddTableFocus::SaveButton => new_focus = AddTableFocus::LinksTable,
|
||||||
|
AddTableFocus::DeleteSelectedButton => new_focus = AddTableFocus::SaveButton,
|
||||||
|
AddTableFocus::CancelButton => new_focus = AddTableFocus::DeleteSelectedButton,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Some("move_down") => {
|
||||||
|
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,
|
||||||
|
// Navigate between blocks when focus is on the table block itself
|
||||||
|
AddTableFocus::ColumnsTable => new_focus = AddTableFocus::IndexesTable,
|
||||||
|
AddTableFocus::IndexesTable => new_focus = AddTableFocus::LinksTable,
|
||||||
|
AddTableFocus::LinksTable => new_focus = AddTableFocus::SaveButton, // Move down to right pane
|
||||||
|
// Scroll inside the table when focus is internal
|
||||||
|
AddTableFocus::InsideColumnsTable => {
|
||||||
|
navigate_table_down(&mut add_table_state.column_table_state, add_table_state.columns.len());
|
||||||
|
// Stay inside the table
|
||||||
|
}
|
||||||
|
AddTableFocus::InsideIndexesTable => {
|
||||||
|
navigate_table_down(&mut add_table_state.index_table_state, add_table_state.indexes.len());
|
||||||
|
// Stay inside the table
|
||||||
|
}
|
||||||
|
AddTableFocus::InsideLinksTable => {
|
||||||
|
navigate_table_down(&mut add_table_state.link_table_state, add_table_state.links.len());
|
||||||
|
// Stay inside the table
|
||||||
|
}
|
||||||
|
AddTableFocus::SaveButton => new_focus = AddTableFocus::DeleteSelectedButton,
|
||||||
|
AddTableFocus::DeleteSelectedButton => new_focus = AddTableFocus::CancelButton,
|
||||||
|
AddTableFocus::CancelButton => new_focus = AddTableFocus::InputTableName,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- Horizontal Navigation (Left/Right) ---
|
||||||
|
Some("next_option") => { // 'l' or Right: Move from Left Pane to Right Pane
|
||||||
|
// Horizontal nav within bottom buttons
|
||||||
|
if current_focus == AddTableFocus::SaveButton {
|
||||||
|
new_focus = AddTableFocus::DeleteSelectedButton;
|
||||||
|
} else if current_focus == AddTableFocus::DeleteSelectedButton {
|
||||||
|
new_focus = AddTableFocus::CancelButton;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Some("previous_option") => { // 'h' or Left: Move from Right Pane to Left Pane
|
||||||
|
// Horizontal nav within bottom buttons
|
||||||
|
if current_focus == AddTableFocus::CancelButton {
|
||||||
|
new_focus = AddTableFocus::DeleteSelectedButton;
|
||||||
|
} else if current_focus == AddTableFocus::DeleteSelectedButton {
|
||||||
|
new_focus = AddTableFocus::SaveButton;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- Tab / Shift+Tab Navigation (Keep as vertical cycle) ---
|
||||||
|
Some("next_field") => { // Tab
|
||||||
|
new_focus = match current_focus {
|
||||||
|
AddTableFocus::InputTableName => AddTableFocus::InputColumnName,
|
||||||
|
AddTableFocus::InputColumnName => AddTableFocus::InputColumnType,
|
||||||
|
AddTableFocus::InputColumnType => AddTableFocus::AddColumnButton,
|
||||||
|
AddTableFocus::AddColumnButton => AddTableFocus::ColumnsTable,
|
||||||
|
// Treat Inside* same as block focus for tabbing out
|
||||||
|
AddTableFocus::ColumnsTable | AddTableFocus::InsideColumnsTable => AddTableFocus::IndexesTable,
|
||||||
|
AddTableFocus::IndexesTable | AddTableFocus::InsideIndexesTable => AddTableFocus::LinksTable,
|
||||||
|
AddTableFocus::LinksTable | AddTableFocus::InsideLinksTable => AddTableFocus::SaveButton,
|
||||||
|
AddTableFocus::SaveButton => AddTableFocus::DeleteSelectedButton,
|
||||||
|
AddTableFocus::DeleteSelectedButton => AddTableFocus::CancelButton,
|
||||||
|
AddTableFocus::CancelButton => AddTableFocus::InputTableName, // Wrap
|
||||||
|
};
|
||||||
|
}
|
||||||
|
Some("prev_field") => { // Shift+Tab
|
||||||
|
new_focus = match current_focus {
|
||||||
|
AddTableFocus::InputTableName => AddTableFocus::CancelButton, // Wrap
|
||||||
|
AddTableFocus::InputColumnName => AddTableFocus::InputTableName,
|
||||||
|
AddTableFocus::InputColumnType => AddTableFocus::InputColumnName,
|
||||||
|
AddTableFocus::AddColumnButton => AddTableFocus::InputColumnType,
|
||||||
|
// Treat Inside* same as block focus for tabbing out
|
||||||
|
AddTableFocus::ColumnsTable | AddTableFocus::InsideColumnsTable => AddTableFocus::AddColumnButton,
|
||||||
|
AddTableFocus::IndexesTable | AddTableFocus::InsideIndexesTable => AddTableFocus::ColumnsTable,
|
||||||
|
AddTableFocus::LinksTable | AddTableFocus::InsideLinksTable => AddTableFocus::IndexesTable,
|
||||||
|
AddTableFocus::SaveButton => AddTableFocus::LinksTable,
|
||||||
|
AddTableFocus::DeleteSelectedButton => AddTableFocus::SaveButton,
|
||||||
|
AddTableFocus::CancelButton => AddTableFocus::DeleteSelectedButton,
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- Selection ---
|
||||||
|
Some("select") => {
|
||||||
|
match current_focus {
|
||||||
|
// --- Enter/Exit Table Focus ---
|
||||||
|
AddTableFocus::ColumnsTable => {
|
||||||
|
new_focus = AddTableFocus::InsideColumnsTable;
|
||||||
|
// Select first item if none selected when entering
|
||||||
|
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));
|
||||||
|
}
|
||||||
|
*command_message = "Entered Columns Table (Scroll with Up/Down, Select to exit)".to_string();
|
||||||
|
}
|
||||||
|
AddTableFocus::IndexesTable => {
|
||||||
|
new_focus = AddTableFocus::InsideIndexesTable;
|
||||||
|
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));
|
||||||
|
}
|
||||||
|
*command_message = "Entered Indexes Table (Scroll with Up/Down, Select to exit)".to_string();
|
||||||
|
}
|
||||||
|
AddTableFocus::LinksTable => {
|
||||||
|
new_focus = AddTableFocus::InsideLinksTable;
|
||||||
|
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));
|
||||||
|
}
|
||||||
|
*command_message = "Entered Links Table (Scroll with Up/Down, Select to toggle/exit)".to_string();
|
||||||
|
}
|
||||||
|
AddTableFocus::InsideColumnsTable => {
|
||||||
|
// Toggle selection when pressing select *inside* the columns table
|
||||||
|
if let Some(index) = add_table_state.column_table_state.selected() {
|
||||||
|
if let Some(col) = add_table_state.columns.get_mut(index) {
|
||||||
|
col.selected = !col.selected;
|
||||||
|
add_table_state.has_unsaved_changes = true;
|
||||||
|
*command_message = format!(
|
||||||
|
"Toggled selection for column: {} to {}",
|
||||||
|
col.name, col.selected
|
||||||
|
);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
*command_message = "No column highlighted to toggle selection".to_string();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
AddTableFocus::InsideIndexesTable => {
|
||||||
|
// Select does nothing here anymore, only Esc exits.
|
||||||
|
if let Some(index) = add_table_state.index_table_state.selected() {
|
||||||
|
*command_message = format!("Selected index index {} (Press Esc to exit scroll mode)", index);
|
||||||
|
} else {
|
||||||
|
*command_message = "No index selected (Press Esc to exit scroll mode)".to_string();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
AddTableFocus::InsideLinksTable => {
|
||||||
|
// Toggle selection when pressing select *inside* the links table
|
||||||
|
if let Some(index) = add_table_state.link_table_state.selected() {
|
||||||
|
if let Some(link) = add_table_state.links.get_mut(index) {
|
||||||
|
link.selected = !link.selected; // Toggle the selected state
|
||||||
|
add_table_state.has_unsaved_changes = true; // Mark changes
|
||||||
|
*command_message = format!(
|
||||||
|
"Toggled selection for link: {} to {}",
|
||||||
|
link.linked_table_name, link.selected
|
||||||
|
);
|
||||||
|
} else {
|
||||||
|
*command_message = "Error: Selected link index out of bounds".to_string();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
*command_message = "No link selected to toggle".to_string();
|
||||||
|
}
|
||||||
|
// Stay inside the links table after toggling
|
||||||
|
new_focus = AddTableFocus::InsideLinksTable;
|
||||||
|
// Alternative: Exit after toggle:
|
||||||
|
// new_focus = AddTableFocus::LinksTable;
|
||||||
|
// *command_message = format!("{} - Exited Links Table", command_message);
|
||||||
|
}
|
||||||
|
// --- Other Select Actions ---
|
||||||
|
AddTableFocus::AddColumnButton => {
|
||||||
|
if let Some(focus_after_add) = handle_add_column_action(add_table_state, command_message) {
|
||||||
|
new_focus = focus_after_add;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
AddTableFocus::SaveButton => {
|
||||||
|
*command_message = "Action: Save Table (Not Implemented)".to_string();
|
||||||
|
// TODO: Implement logic
|
||||||
|
}
|
||||||
|
AddTableFocus::DeleteSelectedButton => {
|
||||||
|
// --- Show Confirmation Dialog ---
|
||||||
|
// Collect tuples of (index, name, type) for selected columns
|
||||||
|
let columns_to_delete: Vec<(usize, String, String)> = add_table_state
|
||||||
|
.columns
|
||||||
|
.iter()
|
||||||
|
.enumerate() // Get index along with the column
|
||||||
|
.filter(|(_index, col)| col.selected) // Filter based on selection
|
||||||
|
.map(|(index, col)| (index, col.name.clone(), col.data_type.clone())) // Map to (index, name, type)
|
||||||
|
.collect();
|
||||||
|
|
||||||
|
if columns_to_delete.is_empty() {
|
||||||
|
*command_message = "No columns selected for deletion.".to_string();
|
||||||
|
} else {
|
||||||
|
// Format the message to include index, name, and type
|
||||||
|
let column_details: String = columns_to_delete
|
||||||
|
.iter()
|
||||||
|
// Add 1 to index for 1-based numbering for user display
|
||||||
|
.map(|(index, name, dtype)| format!("{}. {} ({})", index + 1, name, dtype))
|
||||||
|
.collect::<Vec<String>>()
|
||||||
|
.join("\n");
|
||||||
|
|
||||||
|
// Use the formatted column_details string in the message
|
||||||
|
let message = format!(
|
||||||
|
"Delete the following columns?\n\n{}",
|
||||||
|
column_details
|
||||||
|
);
|
||||||
|
let buttons = vec!["Confirm".to_string(), "Cancel".to_string()];
|
||||||
|
app_state.show_dialog(
|
||||||
|
"Confirm Deletion",
|
||||||
|
&message,
|
||||||
|
buttons,
|
||||||
|
DialogPurpose::ConfirmDeleteColumns,
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
AddTableFocus::CancelButton => {
|
||||||
|
*command_message = "Action: Cancel Add Table".to_string();
|
||||||
|
// TODO: Implement logic
|
||||||
|
}
|
||||||
|
_ => { // 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 ---
|
||||||
|
Some("toggle_sidebar") | Some("toggle_buffer_list") => {
|
||||||
|
handled = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
// --- No matching action ---
|
||||||
|
_ => handled = false,
|
||||||
|
}
|
||||||
|
|
||||||
|
// Update focus state if it changed and was handled
|
||||||
|
if handled && current_focus != new_focus {
|
||||||
|
add_table_state.current_focus = new_focus;
|
||||||
|
// Avoid overwriting specific messages set during 'select' handling
|
||||||
|
if command_message.is_empty() || command_message.starts_with("Focus set to") {
|
||||||
|
*command_message = format!("Focus set to {:?}", add_table_state.current_focus);
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if the *new* focus target is one of the canvas input fields
|
||||||
|
let new_is_canvas_input_focus = matches!(new_focus,
|
||||||
|
AddTableFocus::InputTableName | AddTableFocus::InputColumnName | AddTableFocus::InputColumnType
|
||||||
|
);
|
||||||
|
// Focus is outside canvas if it's not an input field
|
||||||
|
app_state.ui.focus_outside_canvas = !new_is_canvas_input_focus;
|
||||||
|
} else if !handled {
|
||||||
|
// command_message.clear(); // Optional: Clear message if not handled here
|
||||||
|
}
|
||||||
|
|
||||||
|
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; }
|
||||||
|
let current_selection = table_state.selected();
|
||||||
|
match current_selection {
|
||||||
|
Some(index) => {
|
||||||
|
if index > 0 {
|
||||||
|
table_state.select(Some(index - 1));
|
||||||
|
true // Navigation happened
|
||||||
|
} else {
|
||||||
|
false // Was at the top
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None => { // No item selected, select the last one
|
||||||
|
table_state.select(Some(item_count - 1));
|
||||||
|
true // Navigation happened (selection set)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// 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; }
|
||||||
|
let current_selection = table_state.selected();
|
||||||
|
match current_selection {
|
||||||
|
Some(index) => {
|
||||||
|
if index < item_count - 1 {
|
||||||
|
table_state.select(Some(index + 1));
|
||||||
|
true // Navigation happened
|
||||||
|
} else {
|
||||||
|
false // Was at the bottom
|
||||||
|
}
|
||||||
|
}
|
||||||
|
None => { // No item selected, select the first one
|
||||||
|
table_state.select(Some(0));
|
||||||
|
true // Navigation happened (selection set)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -8,13 +8,14 @@ use crate::state::{
|
|||||||
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
|
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
|
||||||
use crate::state::app::buffer::AppView;
|
use crate::state::app::buffer::AppView;
|
||||||
use crate::state::app::buffer::BufferState;
|
use crate::state::app::buffer::BufferState;
|
||||||
|
use crate::state::pages::add_table::AddTableState;
|
||||||
|
|
||||||
/// Handles navigation events specifically for the Admin Panel view.
|
/// Handles navigation events specifically for the Admin Panel view.
|
||||||
/// Returns true if the event was handled, false otherwise.
|
/// Returns true if the event was handled, false otherwise.
|
||||||
pub fn handle_admin_navigation(
|
pub fn handle_admin_navigation(
|
||||||
key: KeyEvent,
|
key: KeyEvent,
|
||||||
config: &Config,
|
config: &Config,
|
||||||
app_state: &AppState,
|
app_state: &mut AppState,
|
||||||
admin_state: &mut AdminState,
|
admin_state: &mut AdminState,
|
||||||
buffer_state: &mut BufferState,
|
buffer_state: &mut BufferState,
|
||||||
command_message: &mut String,
|
command_message: &mut String,
|
||||||
@@ -160,8 +161,37 @@ pub fn handle_admin_navigation(
|
|||||||
// TODO: Trigger action for Button 1
|
// TODO: Trigger action for Button 1
|
||||||
}
|
}
|
||||||
AdminFocus::Button2 => {
|
AdminFocus::Button2 => {
|
||||||
buffer_state.update_history(AppView::AddTable);
|
// --- Prepare AddTableState based on persistent selections ---
|
||||||
*command_message = "Navigating to Add Table page...".to_string();
|
if let Some(p_idx) = admin_state.selected_profile_index {
|
||||||
|
if let Some(profile) = app_state.profile_tree.profiles.get(p_idx) {
|
||||||
|
let selected_profile_name = profile.name.clone();
|
||||||
|
|
||||||
|
|
||||||
|
// Create and populate the new AddTableState
|
||||||
|
let new_add_table_state = AddTableState {
|
||||||
|
profile_name: selected_profile_name,
|
||||||
|
// Reset other fields to defaults for a fresh start
|
||||||
|
..AddTableState::default()
|
||||||
|
};
|
||||||
|
|
||||||
|
// Assign the prepared state
|
||||||
|
admin_state.add_table_state = new_add_table_state;
|
||||||
|
|
||||||
|
// Switch view
|
||||||
|
buffer_state.update_history(AppView::AddTable);
|
||||||
|
app_state.ui.focus_outside_canvas = false;
|
||||||
|
*command_message = format!(
|
||||||
|
"Navigating to Add Table for profile '{}'...",
|
||||||
|
admin_state.add_table_state.profile_name
|
||||||
|
);
|
||||||
|
|
||||||
|
} else {
|
||||||
|
*command_message = "Error: Selected profile index out of bounds.".to_string();
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
*command_message = "Please select a profile ([*]) first.".to_string();
|
||||||
|
}
|
||||||
|
// --- End preparation ---
|
||||||
}
|
}
|
||||||
AdminFocus::Button3 => {
|
AdminFocus::Button3 => {
|
||||||
*command_message = "Action: Change Table (Not Implemented)".to_string();
|
*command_message = "Action: Change Table (Not Implemented)".to_string();
|
||||||
|
|||||||
@@ -2,3 +2,4 @@
|
|||||||
|
|
||||||
pub mod auth_ro;
|
pub mod auth_ro;
|
||||||
pub mod form_ro;
|
pub mod form_ro;
|
||||||
|
pub mod add_table_ro;
|
||||||
|
|||||||
244
client/src/functions/modes/read_only/add_table_ro.rs
Normal file
244
client/src/functions/modes/read_only/add_table_ro.rs
Normal file
@@ -0,0 +1,244 @@
|
|||||||
|
// src/functions/modes/read_only/add_table_ro.rs
|
||||||
|
use crate::config::binds::key_sequences::KeySequenceTracker;
|
||||||
|
use crate::state::pages::add_table::AddTableState;
|
||||||
|
use crate::state::pages::canvas_state::CanvasState; // Use trait for common actions
|
||||||
|
use crate::state::app::state::AppState;
|
||||||
|
use std::error::Error;
|
||||||
|
|
||||||
|
// Re-use word navigation helpers if they are public or move them to a common module
|
||||||
|
// For now, duplicating them here for simplicity. Consider refactoring later.
|
||||||
|
#[derive(PartialEq)]
|
||||||
|
enum CharType {
|
||||||
|
Whitespace,
|
||||||
|
Alphanumeric,
|
||||||
|
Punctuation,
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_char_type(c: char) -> CharType {
|
||||||
|
if c.is_whitespace() { CharType::Whitespace }
|
||||||
|
else if c.is_alphanumeric() { CharType::Alphanumeric }
|
||||||
|
else { CharType::Punctuation }
|
||||||
|
}
|
||||||
|
|
||||||
|
fn find_next_word_start(text: &str, current_pos: usize) -> usize {
|
||||||
|
let chars: Vec<char> = text.chars().collect();
|
||||||
|
let len = chars.len();
|
||||||
|
if len == 0 || current_pos >= len { return len; }
|
||||||
|
let mut pos = current_pos;
|
||||||
|
let initial_type = get_char_type(chars[pos]);
|
||||||
|
while pos < len && get_char_type(chars[pos]) == initial_type { pos += 1; }
|
||||||
|
while pos < len && get_char_type(chars[pos]) == CharType::Whitespace { pos += 1; }
|
||||||
|
pos
|
||||||
|
}
|
||||||
|
|
||||||
|
fn find_word_end(text: &str, current_pos: usize) -> usize {
|
||||||
|
let chars: Vec<char> = text.chars().collect();
|
||||||
|
let len = chars.len();
|
||||||
|
if len == 0 { return 0; }
|
||||||
|
let mut pos = current_pos.min(len - 1);
|
||||||
|
if get_char_type(chars[pos]) == CharType::Whitespace {
|
||||||
|
pos = find_next_word_start(text, pos);
|
||||||
|
}
|
||||||
|
if pos >= len { return len.saturating_sub(1); }
|
||||||
|
let word_type = get_char_type(chars[pos]);
|
||||||
|
while pos < len && get_char_type(chars[pos]) == word_type { pos += 1; }
|
||||||
|
pos.saturating_sub(1).min(len.saturating_sub(1))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn find_prev_word_start(text: &str, current_pos: usize) -> usize {
|
||||||
|
let chars: Vec<char> = text.chars().collect();
|
||||||
|
if chars.is_empty() || current_pos == 0 { return 0; }
|
||||||
|
let mut pos = current_pos.saturating_sub(1);
|
||||||
|
while pos > 0 && get_char_type(chars[pos]) == CharType::Whitespace { pos -= 1; }
|
||||||
|
if pos == 0 && get_char_type(chars[pos]) == CharType::Whitespace { return 0; }
|
||||||
|
let word_type = get_char_type(chars[pos]);
|
||||||
|
while pos > 0 && get_char_type(chars[pos - 1]) == word_type { pos -= 1; }
|
||||||
|
pos
|
||||||
|
}
|
||||||
|
|
||||||
|
// Note: find_prev_word_end might need adjustments based on desired behavior.
|
||||||
|
// This version finds the end of the word *before* the previous word start.
|
||||||
|
fn find_prev_word_end(text: &str, current_pos: usize) -> usize {
|
||||||
|
let prev_start = find_prev_word_start(text, current_pos);
|
||||||
|
if prev_start == 0 { return 0; }
|
||||||
|
// Find the end of the word that starts at prev_start - 1
|
||||||
|
find_word_end(text, prev_start.saturating_sub(1))
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
/// Executes read-only actions for the AddTable view canvas.
|
||||||
|
pub async fn execute_action(
|
||||||
|
action: &str,
|
||||||
|
app_state: &mut AppState, // Needed for focus_outside_canvas
|
||||||
|
state: &mut AddTableState,
|
||||||
|
ideal_cursor_column: &mut usize,
|
||||||
|
key_sequence_tracker: &mut KeySequenceTracker,
|
||||||
|
command_message: &mut String, // Keep for potential messages
|
||||||
|
) -> Result<String, Box<dyn Error>> {
|
||||||
|
// Use the CanvasState trait methods implemented for AddTableState
|
||||||
|
match action {
|
||||||
|
"move_up" => {
|
||||||
|
key_sequence_tracker.reset();
|
||||||
|
let num_fields = AddTableState::INPUT_FIELD_COUNT;
|
||||||
|
if num_fields == 0 { return Ok("No fields.".to_string()); }
|
||||||
|
let current_field = state.current_field(); // Gets the index (0, 1, or 2)
|
||||||
|
|
||||||
|
if current_field > 0 {
|
||||||
|
// This handles moving from field 2 -> 1, or 1 -> 0
|
||||||
|
let new_field = current_field - 1;
|
||||||
|
state.set_current_field(new_field);
|
||||||
|
// ... (rest of the logic to set cursor position) ...
|
||||||
|
} else {
|
||||||
|
// --- THIS IS WHERE THE FIX GOES ---
|
||||||
|
// current_field is 0 (InputTableName), and user pressed Up.
|
||||||
|
// We need to move focus *outside* the canvas.
|
||||||
|
|
||||||
|
// Set the flag to indicate focus is leaving the canvas
|
||||||
|
app_state.ui.focus_outside_canvas = true;
|
||||||
|
|
||||||
|
// Decide which element gets focus. Based on your layout and the
|
||||||
|
// downward navigation (CancelButton wraps to InputTableName),
|
||||||
|
// moving up from InputTableName should likely go to CancelButton.
|
||||||
|
state.current_focus = crate::state::pages::add_table::AddTableFocus::CancelButton;
|
||||||
|
|
||||||
|
// Reset the sequence tracker as the action is complete
|
||||||
|
key_sequence_tracker.reset();
|
||||||
|
|
||||||
|
// Return a message indicating the focus change
|
||||||
|
return Ok("Focus moved above canvas".to_string());
|
||||||
|
// --- END FIX ---
|
||||||
|
}
|
||||||
|
// If we moved within the canvas (e.g., 1 -> 0), return empty string
|
||||||
|
Ok("".to_string())
|
||||||
|
}
|
||||||
|
"move_down" => {
|
||||||
|
key_sequence_tracker.reset();
|
||||||
|
let num_fields = AddTableState::INPUT_FIELD_COUNT;
|
||||||
|
if num_fields == 0 { return Ok("No fields.".to_string()); }
|
||||||
|
let current_field = state.current_field();
|
||||||
|
let last_field_index = num_fields - 1;
|
||||||
|
|
||||||
|
if current_field < last_field_index {
|
||||||
|
let new_field = current_field + 1;
|
||||||
|
state.set_current_field(new_field);
|
||||||
|
let current_input = state.get_current_input();
|
||||||
|
let max_cursor_pos = current_input.len(); // Allow cursor at end
|
||||||
|
let new_pos = (*ideal_cursor_column).min(max_cursor_pos);
|
||||||
|
state.set_current_cursor_pos(new_pos);
|
||||||
|
} else {
|
||||||
|
// Move focus outside canvas when moving down from the last field
|
||||||
|
app_state.ui.focus_outside_canvas = true;
|
||||||
|
// Set focus to the first element outside canvas (AddColumnButton)
|
||||||
|
state.current_focus = crate::state::pages::add_table::AddTableFocus::AddColumnButton;
|
||||||
|
key_sequence_tracker.reset();
|
||||||
|
return Ok("Focus moved below canvas".to_string());
|
||||||
|
}
|
||||||
|
Ok("".to_string())
|
||||||
|
}
|
||||||
|
"move_first_line" => {
|
||||||
|
key_sequence_tracker.reset();
|
||||||
|
if AddTableState::INPUT_FIELD_COUNT > 0 {
|
||||||
|
state.set_current_field(0);
|
||||||
|
let current_input = state.get_current_input();
|
||||||
|
let max_cursor_pos = current_input.len();
|
||||||
|
let new_pos = (*ideal_cursor_column).min(max_cursor_pos);
|
||||||
|
state.set_current_cursor_pos(new_pos);
|
||||||
|
*ideal_cursor_column = new_pos; // Update ideal column
|
||||||
|
}
|
||||||
|
Ok("".to_string())
|
||||||
|
}
|
||||||
|
"move_last_line" => {
|
||||||
|
key_sequence_tracker.reset();
|
||||||
|
let num_fields = AddTableState::INPUT_FIELD_COUNT;
|
||||||
|
if num_fields > 0 {
|
||||||
|
let last_field_index = num_fields - 1;
|
||||||
|
state.set_current_field(last_field_index);
|
||||||
|
let current_input = state.get_current_input();
|
||||||
|
let max_cursor_pos = current_input.len();
|
||||||
|
let new_pos = (*ideal_cursor_column).min(max_cursor_pos);
|
||||||
|
state.set_current_cursor_pos(new_pos);
|
||||||
|
*ideal_cursor_column = new_pos; // Update ideal column
|
||||||
|
}
|
||||||
|
Ok("".to_string())
|
||||||
|
}
|
||||||
|
"move_left" => {
|
||||||
|
let current_pos = state.current_cursor_pos();
|
||||||
|
let new_pos = current_pos.saturating_sub(1);
|
||||||
|
state.set_current_cursor_pos(new_pos);
|
||||||
|
*ideal_cursor_column = new_pos;
|
||||||
|
Ok("".to_string())
|
||||||
|
}
|
||||||
|
"move_right" => {
|
||||||
|
let current_input = state.get_current_input();
|
||||||
|
let current_pos = state.current_cursor_pos();
|
||||||
|
// Allow moving cursor one position past the end
|
||||||
|
if current_pos < current_input.len() {
|
||||||
|
let new_pos = current_pos + 1;
|
||||||
|
state.set_current_cursor_pos(new_pos);
|
||||||
|
*ideal_cursor_column = new_pos;
|
||||||
|
}
|
||||||
|
Ok("".to_string())
|
||||||
|
}
|
||||||
|
"move_word_next" => {
|
||||||
|
let current_input = state.get_current_input();
|
||||||
|
let new_pos = find_next_word_start(current_input, state.current_cursor_pos());
|
||||||
|
let final_pos = new_pos.min(current_input.len()); // Allow cursor at end
|
||||||
|
state.set_current_cursor_pos(final_pos);
|
||||||
|
*ideal_cursor_column = final_pos;
|
||||||
|
Ok("".to_string())
|
||||||
|
}
|
||||||
|
"move_word_end" => {
|
||||||
|
let current_input = state.get_current_input();
|
||||||
|
let current_pos = state.current_cursor_pos();
|
||||||
|
let new_pos = find_word_end(current_input, current_pos);
|
||||||
|
// If find_word_end returns current_pos, try starting search from next char
|
||||||
|
let final_pos = if new_pos == current_pos && current_pos < current_input.len() {
|
||||||
|
find_word_end(current_input, current_pos + 1)
|
||||||
|
} else {
|
||||||
|
new_pos
|
||||||
|
};
|
||||||
|
let max_valid_index = current_input.len(); // Allow cursor at end
|
||||||
|
let clamped_pos = final_pos.min(max_valid_index);
|
||||||
|
state.set_current_cursor_pos(clamped_pos);
|
||||||
|
*ideal_cursor_column = clamped_pos;
|
||||||
|
Ok("".to_string())
|
||||||
|
}
|
||||||
|
"move_word_prev" => {
|
||||||
|
let current_input = state.get_current_input();
|
||||||
|
let new_pos = find_prev_word_start(current_input, state.current_cursor_pos());
|
||||||
|
state.set_current_cursor_pos(new_pos);
|
||||||
|
*ideal_cursor_column = new_pos;
|
||||||
|
Ok("".to_string())
|
||||||
|
}
|
||||||
|
"move_word_end_prev" => {
|
||||||
|
let current_input = state.get_current_input();
|
||||||
|
let new_pos = find_prev_word_end(current_input, state.current_cursor_pos());
|
||||||
|
state.set_current_cursor_pos(new_pos);
|
||||||
|
*ideal_cursor_column = new_pos;
|
||||||
|
Ok("".to_string())
|
||||||
|
}
|
||||||
|
"move_line_start" => {
|
||||||
|
state.set_current_cursor_pos(0);
|
||||||
|
*ideal_cursor_column = 0;
|
||||||
|
Ok("".to_string())
|
||||||
|
}
|
||||||
|
"move_line_end" => {
|
||||||
|
let current_input = state.get_current_input();
|
||||||
|
let new_pos = current_input.len(); // Allow cursor at end
|
||||||
|
state.set_current_cursor_pos(new_pos);
|
||||||
|
*ideal_cursor_column = new_pos;
|
||||||
|
Ok("".to_string())
|
||||||
|
}
|
||||||
|
// Actions handled by main event loop (mode changes)
|
||||||
|
"enter_edit_mode_before" | "enter_edit_mode_after" | "enter_command_mode" | "exit_highlight_mode" => {
|
||||||
|
key_sequence_tracker.reset();
|
||||||
|
Ok("Mode change handled by main loop".to_string())
|
||||||
|
}
|
||||||
|
_ => {
|
||||||
|
key_sequence_tracker.reset();
|
||||||
|
command_message.clear(); // Clear message for unhandled actions
|
||||||
|
Ok(format!("Unknown read-only action: {}", action))
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -1,13 +1,17 @@
|
|||||||
// src/modes/canvas/edit.rs
|
// src/modes/canvas/edit.rs
|
||||||
use crate::config::binds::config::Config;
|
use crate::config::binds::config::Config;
|
||||||
use crate::services::grpc_client::GrpcClient;
|
use crate::services::grpc_client::GrpcClient;
|
||||||
use crate::state::pages::{auth::{LoginState, RegisterState}, canvas_state::CanvasState};
|
use crate::state::pages::{
|
||||||
|
auth::{LoginState, RegisterState},
|
||||||
|
canvas_state::CanvasState,
|
||||||
|
};
|
||||||
use crate::state::pages::form::FormState;
|
use crate::state::pages::form::FormState;
|
||||||
|
use crate::state::pages::add_table::AddTableState;
|
||||||
use crate::modes::handlers::event::EventOutcome;
|
use crate::modes::handlers::event::EventOutcome;
|
||||||
use crate::functions::modes::edit::{auth_e, form_e};
|
use crate::functions::modes::edit::{auth_e, form_e};
|
||||||
|
use crate::functions::modes::edit::add_table_e;
|
||||||
use crate::state::app::state::AppState;
|
use crate::state::app::state::AppState;
|
||||||
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
|
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
|
||||||
// Removed duplicate/unused imports
|
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
pub enum EditEventOutcome {
|
pub enum EditEventOutcome {
|
||||||
@@ -21,73 +25,136 @@ pub async fn handle_edit_event(
|
|||||||
form_state: &mut FormState,
|
form_state: &mut FormState,
|
||||||
login_state: &mut LoginState,
|
login_state: &mut LoginState,
|
||||||
register_state: &mut RegisterState,
|
register_state: &mut RegisterState,
|
||||||
|
add_table_state: &mut AddTableState,
|
||||||
ideal_cursor_column: &mut usize,
|
ideal_cursor_column: &mut usize,
|
||||||
// command_message: &mut String, // Removed as messages are returned
|
|
||||||
current_position: &mut u64,
|
current_position: &mut u64,
|
||||||
total_count: u64,
|
total_count: u64,
|
||||||
grpc_client: &mut GrpcClient,
|
grpc_client: &mut GrpcClient,
|
||||||
app_state: &AppState,
|
app_state: &AppState,
|
||||||
) -> Result<EditEventOutcome, Box<dyn std::error::Error>> {
|
) -> Result<EditEventOutcome, Box<dyn std::error::Error>> {
|
||||||
|
// Global command mode check (should ideally be handled before calling this function)
|
||||||
// Global command mode check
|
|
||||||
if let Some("enter_command_mode") = config.get_action_for_key_in_mode(
|
if let Some("enter_command_mode") = config.get_action_for_key_in_mode(
|
||||||
&config.keybindings.global,
|
&config.keybindings.global,
|
||||||
key.code,
|
key.code,
|
||||||
key.modifiers
|
key.modifiers,
|
||||||
) {
|
) {
|
||||||
return Ok(EditEventOutcome::Message("Switching to Command Mode...".to_string()));
|
return Ok(EditEventOutcome::Message(
|
||||||
|
"Command mode entry handled globally.".to_string(),
|
||||||
|
));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Common actions (save/revert)
|
|
||||||
if let Some(action) = config.get_action_for_key_in_mode(
|
if let Some(action) = config.get_action_for_key_in_mode(
|
||||||
&config.keybindings.common,
|
&config.keybindings.common,
|
||||||
key.code,
|
key.code,
|
||||||
key.modifiers
|
key.modifiers,
|
||||||
) {
|
).as_deref() {
|
||||||
if matches!(action, "save" | "revert") {
|
if matches!(action, "save" | "revert") {
|
||||||
// Ensure all branches result in Result<String, Error> before the final Ok(...) wrap
|
|
||||||
let message_string: String = if app_state.ui.show_login {
|
let message_string: String = if app_state.ui.show_login {
|
||||||
auth_e::execute_common_action(
|
auth_e::execute_common_action(
|
||||||
action,
|
action,
|
||||||
login_state,
|
login_state,
|
||||||
grpc_client,
|
grpc_client,
|
||||||
current_position,
|
current_position,
|
||||||
total_count
|
total_count,
|
||||||
).await? // Results in String on success
|
)
|
||||||
|
.await?
|
||||||
} else if app_state.ui.show_register {
|
} else if app_state.ui.show_register {
|
||||||
auth_e::execute_common_action(
|
auth_e::execute_common_action(
|
||||||
action,
|
action,
|
||||||
register_state,
|
register_state,
|
||||||
grpc_client,
|
grpc_client,
|
||||||
current_position,
|
current_position,
|
||||||
total_count
|
total_count,
|
||||||
).await? // Results in String on success
|
)
|
||||||
|
.await?
|
||||||
|
} else if app_state.ui.show_add_table {
|
||||||
|
format!(
|
||||||
|
"Action '{}' not fully implemented for Add Table view here.",
|
||||||
|
action
|
||||||
|
)
|
||||||
} else {
|
} else {
|
||||||
let outcome = form_e::execute_common_action(
|
let outcome = form_e::execute_common_action(
|
||||||
action,
|
action,
|
||||||
form_state,
|
form_state,
|
||||||
grpc_client,
|
grpc_client,
|
||||||
current_position,
|
current_position,
|
||||||
total_count
|
total_count,
|
||||||
).await?; // This returns EventOutcome on success
|
)
|
||||||
|
.await?;
|
||||||
// Extract the message string from the EventOutcome
|
|
||||||
match outcome {
|
match outcome {
|
||||||
EventOutcome::Ok(msg) => msg,
|
EventOutcome::Ok(msg) => msg,
|
||||||
EventOutcome::DataSaved(_, msg) => msg,
|
EventOutcome::DataSaved(_, msg) => msg,
|
||||||
_ => format!("Unexpected outcome from common action: {:?}", outcome),
|
_ => format!(
|
||||||
|
"Unexpected outcome from common action: {:?}",
|
||||||
|
outcome
|
||||||
|
),
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
// Wrap the resulting String message
|
|
||||||
return Ok(EditEventOutcome::Message(message_string));
|
return Ok(EditEventOutcome::Message(message_string));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Edit-specific actions
|
// Edit-specific actions
|
||||||
if let Some(action) = config.get_edit_action_for_key(key.code, key.modifiers) {
|
if let Some(action) =
|
||||||
|
config.get_edit_action_for_key(key.code, key.modifiers)
|
||||||
|
.as_deref() {
|
||||||
|
// Handle enter_decider first
|
||||||
|
if action == "enter_decider" {
|
||||||
|
let effective_action = if app_state.ui.show_register
|
||||||
|
&& register_state.in_suggestion_mode
|
||||||
|
&& register_state.current_field() == 4 {
|
||||||
|
"select_suggestion"
|
||||||
|
} else {
|
||||||
|
"next_field"
|
||||||
|
};
|
||||||
|
|
||||||
|
let msg = if app_state.ui.show_login {
|
||||||
|
auth_e::execute_edit_action(
|
||||||
|
effective_action,
|
||||||
|
key,
|
||||||
|
login_state,
|
||||||
|
ideal_cursor_column,
|
||||||
|
grpc_client,
|
||||||
|
current_position,
|
||||||
|
total_count,
|
||||||
|
)
|
||||||
|
.await?
|
||||||
|
} else if app_state.ui.show_add_table {
|
||||||
|
add_table_e::execute_edit_action(
|
||||||
|
effective_action,
|
||||||
|
key,
|
||||||
|
add_table_state,
|
||||||
|
ideal_cursor_column,
|
||||||
|
)
|
||||||
|
.await?
|
||||||
|
} else if app_state.ui.show_register {
|
||||||
|
auth_e::execute_edit_action(
|
||||||
|
effective_action,
|
||||||
|
key,
|
||||||
|
register_state,
|
||||||
|
ideal_cursor_column,
|
||||||
|
grpc_client,
|
||||||
|
current_position,
|
||||||
|
total_count,
|
||||||
|
)
|
||||||
|
.await?
|
||||||
|
} else {
|
||||||
|
form_e::execute_edit_action(
|
||||||
|
effective_action,
|
||||||
|
key,
|
||||||
|
form_state,
|
||||||
|
ideal_cursor_column,
|
||||||
|
grpc_client,
|
||||||
|
current_position,
|
||||||
|
total_count,
|
||||||
|
)
|
||||||
|
.await?
|
||||||
|
};
|
||||||
|
return Ok(EditEventOutcome::Message(msg));
|
||||||
|
}
|
||||||
|
|
||||||
if action == "exit" {
|
if action == "exit" {
|
||||||
if app_state.ui.show_register && register_state.in_suggestion_mode {
|
if app_state.ui.show_register && register_state.in_suggestion_mode {
|
||||||
// Call the action, get Result<String, Error>
|
|
||||||
let msg = auth_e::execute_edit_action(
|
let msg = auth_e::execute_edit_action(
|
||||||
"exit_suggestion_mode",
|
"exit_suggestion_mode",
|
||||||
key,
|
key,
|
||||||
@@ -96,30 +163,54 @@ pub async fn handle_edit_event(
|
|||||||
grpc_client,
|
grpc_client,
|
||||||
current_position,
|
current_position,
|
||||||
total_count,
|
total_count,
|
||||||
).await?; // Results in String on success
|
)
|
||||||
// Wrap the String message
|
.await?;
|
||||||
return Ok(EditEventOutcome::Message(msg));
|
return Ok(EditEventOutcome::Message(msg));
|
||||||
} else {
|
} else {
|
||||||
// Signal exit
|
|
||||||
return Ok(EditEventOutcome::ExitEditMode);
|
return Ok(EditEventOutcome::ExitEditMode);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Special handling for role field suggestions
|
// Special handling for role field suggestions (Register view only)
|
||||||
if app_state.ui.show_register && register_state.current_field() == 4 {
|
if app_state.ui.show_register && register_state.current_field() == 4 {
|
||||||
if !register_state.in_suggestion_mode && key.code == KeyCode::Tab && key.modifiers == KeyModifiers::NONE {
|
if !register_state.in_suggestion_mode
|
||||||
|
&& key.code == KeyCode::Tab
|
||||||
|
&& key.modifiers == KeyModifiers::NONE
|
||||||
|
{
|
||||||
register_state.update_role_suggestions();
|
register_state.update_role_suggestions();
|
||||||
if !register_state.role_suggestions.is_empty() {
|
if !register_state.role_suggestions.is_empty() {
|
||||||
register_state.in_suggestion_mode = true;
|
register_state.in_suggestion_mode = true;
|
||||||
register_state.selected_suggestion_index = Some(0);
|
register_state.selected_suggestion_index = Some(0);
|
||||||
return Ok(EditEventOutcome::Message("Suggestions shown".to_string()));
|
return Ok(EditEventOutcome::Message(
|
||||||
} else { // Added else here for clarity
|
"Suggestions shown".to_string(),
|
||||||
return Ok(EditEventOutcome::Message("No suggestions available".to_string()));
|
));
|
||||||
|
} else {
|
||||||
|
return Ok(EditEventOutcome::Message(
|
||||||
|
"No suggestions available".to_string(),
|
||||||
|
));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if register_state.in_suggestion_mode
|
||||||
|
&& matches!(
|
||||||
|
action,
|
||||||
|
"suggestion_down" | "suggestion_up"
|
||||||
|
)
|
||||||
|
{
|
||||||
|
let msg = auth_e::execute_edit_action(
|
||||||
|
action,
|
||||||
|
key,
|
||||||
|
register_state,
|
||||||
|
ideal_cursor_column,
|
||||||
|
grpc_client,
|
||||||
|
current_position,
|
||||||
|
total_count,
|
||||||
|
)
|
||||||
|
.await?;
|
||||||
|
return Ok(EditEventOutcome::Message(msg));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Execute other edit actions
|
// Execute other edit actions based on the current view
|
||||||
let msg = if app_state.ui.show_login {
|
let msg = if app_state.ui.show_login {
|
||||||
auth_e::execute_edit_action(
|
auth_e::execute_edit_action(
|
||||||
action,
|
action,
|
||||||
@@ -128,8 +219,17 @@ pub async fn handle_edit_event(
|
|||||||
ideal_cursor_column,
|
ideal_cursor_column,
|
||||||
grpc_client,
|
grpc_client,
|
||||||
current_position,
|
current_position,
|
||||||
total_count
|
total_count,
|
||||||
).await? // Results in String
|
)
|
||||||
|
.await?
|
||||||
|
} else if app_state.ui.show_add_table {
|
||||||
|
add_table_e::execute_edit_action(
|
||||||
|
action,
|
||||||
|
key,
|
||||||
|
add_table_state,
|
||||||
|
ideal_cursor_column,
|
||||||
|
)
|
||||||
|
.await?
|
||||||
} else if app_state.ui.show_register {
|
} else if app_state.ui.show_register {
|
||||||
auth_e::execute_edit_action(
|
auth_e::execute_edit_action(
|
||||||
action,
|
action,
|
||||||
@@ -138,8 +238,9 @@ pub async fn handle_edit_event(
|
|||||||
ideal_cursor_column,
|
ideal_cursor_column,
|
||||||
grpc_client,
|
grpc_client,
|
||||||
current_position,
|
current_position,
|
||||||
total_count
|
total_count,
|
||||||
).await? // Results in String
|
)
|
||||||
|
.await?
|
||||||
} else {
|
} else {
|
||||||
form_e::execute_edit_action(
|
form_e::execute_edit_action(
|
||||||
action,
|
action,
|
||||||
@@ -148,22 +249,21 @@ pub async fn handle_edit_event(
|
|||||||
ideal_cursor_column,
|
ideal_cursor_column,
|
||||||
grpc_client,
|
grpc_client,
|
||||||
current_position,
|
current_position,
|
||||||
total_count
|
total_count,
|
||||||
).await? // Results in String
|
)
|
||||||
|
.await?
|
||||||
};
|
};
|
||||||
// Wrap the resulting String message
|
|
||||||
return Ok(EditEventOutcome::Message(msg));
|
return Ok(EditEventOutcome::Message(msg));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Character insertion
|
// --- Character insertion ---
|
||||||
if let KeyCode::Char(_) = key.code {
|
if let KeyCode::Char(c) = key.code {
|
||||||
if app_state.ui.show_register && register_state.in_suggestion_mode {
|
if app_state.ui.show_register && register_state.in_suggestion_mode {
|
||||||
register_state.in_suggestion_mode = false;
|
register_state.in_suggestion_mode = false;
|
||||||
register_state.show_role_suggestions = false;
|
register_state.show_role_suggestions = false;
|
||||||
register_state.selected_suggestion_index = None;
|
register_state.selected_suggestion_index = None;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Execute insert_char action
|
|
||||||
let msg = if app_state.ui.show_login {
|
let msg = if app_state.ui.show_login {
|
||||||
auth_e::execute_edit_action(
|
auth_e::execute_edit_action(
|
||||||
"insert_char",
|
"insert_char",
|
||||||
@@ -172,8 +272,17 @@ pub async fn handle_edit_event(
|
|||||||
ideal_cursor_column,
|
ideal_cursor_column,
|
||||||
grpc_client,
|
grpc_client,
|
||||||
current_position,
|
current_position,
|
||||||
total_count
|
total_count,
|
||||||
).await? // Results in String
|
)
|
||||||
|
.await?
|
||||||
|
} else if app_state.ui.show_add_table {
|
||||||
|
add_table_e::execute_edit_action(
|
||||||
|
"insert_char",
|
||||||
|
key,
|
||||||
|
add_table_state,
|
||||||
|
ideal_cursor_column,
|
||||||
|
)
|
||||||
|
.await?
|
||||||
} else if app_state.ui.show_register {
|
} else if app_state.ui.show_register {
|
||||||
auth_e::execute_edit_action(
|
auth_e::execute_edit_action(
|
||||||
"insert_char",
|
"insert_char",
|
||||||
@@ -182,8 +291,9 @@ pub async fn handle_edit_event(
|
|||||||
ideal_cursor_column,
|
ideal_cursor_column,
|
||||||
grpc_client,
|
grpc_client,
|
||||||
current_position,
|
current_position,
|
||||||
total_count
|
total_count,
|
||||||
).await? // Results in String
|
)
|
||||||
|
.await?
|
||||||
} else {
|
} else {
|
||||||
form_e::execute_edit_action(
|
form_e::execute_edit_action(
|
||||||
"insert_char",
|
"insert_char",
|
||||||
@@ -192,14 +302,19 @@ pub async fn handle_edit_event(
|
|||||||
ideal_cursor_column,
|
ideal_cursor_column,
|
||||||
grpc_client,
|
grpc_client,
|
||||||
current_position,
|
current_position,
|
||||||
total_count
|
total_count,
|
||||||
).await? // Results in String
|
)
|
||||||
|
.await?
|
||||||
};
|
};
|
||||||
// Wrap the resulting String message
|
|
||||||
|
if app_state.ui.show_register && register_state.current_field() == 4 {
|
||||||
|
register_state.update_role_suggestions();
|
||||||
|
}
|
||||||
|
|
||||||
return Ok(EditEventOutcome::Message(msg));
|
return Ok(EditEventOutcome::Message(msg));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Handle Backspace/Delete
|
// --- Handle Backspace/Delete ---
|
||||||
if matches!(key.code, KeyCode::Backspace | KeyCode::Delete) {
|
if matches!(key.code, KeyCode::Backspace | KeyCode::Delete) {
|
||||||
if app_state.ui.show_register && register_state.in_suggestion_mode {
|
if app_state.ui.show_register && register_state.in_suggestion_mode {
|
||||||
register_state.in_suggestion_mode = false;
|
register_state.in_suggestion_mode = false;
|
||||||
@@ -207,28 +322,60 @@ pub async fn handle_edit_event(
|
|||||||
register_state.selected_suggestion_index = None;
|
register_state.selected_suggestion_index = None;
|
||||||
}
|
}
|
||||||
|
|
||||||
let action_str = if key.code == KeyCode::Backspace { "backspace" } else { "delete_char" };
|
let action_str = if key.code == KeyCode::Backspace {
|
||||||
// Ensure both branches result in a String *before* wrapping
|
"delete_char_backward"
|
||||||
let result_msg: String = if app_state.ui.show_register {
|
} else {
|
||||||
auth_e::execute_edit_action(
|
"delete_char_forward"
|
||||||
|
};
|
||||||
|
|
||||||
|
let result_msg: String = if app_state.ui.show_login {
|
||||||
|
auth_e::execute_edit_action(
|
||||||
|
action_str,
|
||||||
|
key,
|
||||||
|
login_state,
|
||||||
|
ideal_cursor_column,
|
||||||
|
grpc_client,
|
||||||
|
current_position,
|
||||||
|
total_count,
|
||||||
|
)
|
||||||
|
.await?
|
||||||
|
} else if app_state.ui.show_add_table {
|
||||||
|
add_table_e::execute_edit_action(
|
||||||
|
action_str,
|
||||||
|
key,
|
||||||
|
add_table_state,
|
||||||
|
ideal_cursor_column,
|
||||||
|
)
|
||||||
|
.await?
|
||||||
|
} else if app_state.ui.show_register {
|
||||||
|
auth_e::execute_edit_action(
|
||||||
action_str,
|
action_str,
|
||||||
key,
|
key,
|
||||||
register_state,
|
register_state,
|
||||||
ideal_cursor_column,
|
ideal_cursor_column,
|
||||||
grpc_client,
|
grpc_client,
|
||||||
current_position,
|
current_position,
|
||||||
total_count
|
total_count,
|
||||||
).await? // Results in String
|
)
|
||||||
|
.await?
|
||||||
} else {
|
} else {
|
||||||
// Return String directly, not Ok(String)
|
form_e::execute_edit_action(
|
||||||
"Action not applicable here".to_string()
|
action_str,
|
||||||
}; // Semicolon here ends the if/else expression
|
key,
|
||||||
|
form_state,
|
||||||
|
ideal_cursor_column,
|
||||||
|
grpc_client,
|
||||||
|
current_position,
|
||||||
|
total_count
|
||||||
|
).await?
|
||||||
|
};
|
||||||
|
|
||||||
|
if app_state.ui.show_register && register_state.current_field() == 4 {
|
||||||
|
register_state.update_role_suggestions();
|
||||||
|
}
|
||||||
|
|
||||||
// Wrap the resulting String message
|
|
||||||
return Ok(EditEventOutcome::Message(result_msg));
|
return Ok(EditEventOutcome::Message(result_msg));
|
||||||
}
|
}
|
||||||
|
|
||||||
// Default return if no other handler matched
|
|
||||||
Ok(EditEventOutcome::Message("".to_string()))
|
Ok(EditEventOutcome::Message("".to_string()))
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,8 +6,9 @@ use crate::services::grpc_client::GrpcClient;
|
|||||||
use crate::state::pages::{canvas_state::CanvasState, auth::RegisterState};
|
use crate::state::pages::{canvas_state::CanvasState, auth::RegisterState};
|
||||||
use crate::state::pages::auth::LoginState;
|
use crate::state::pages::auth::LoginState;
|
||||||
use crate::state::pages::form::FormState;
|
use crate::state::pages::form::FormState;
|
||||||
|
use crate::state::pages::add_table::AddTableState;
|
||||||
use crate::state::app::state::AppState;
|
use crate::state::app::state::AppState;
|
||||||
use crate::functions::modes::read_only::{auth_ro, form_ro};
|
use crate::functions::modes::read_only::{auth_ro, form_ro, add_table_ro};
|
||||||
use crossterm::event::KeyEvent;
|
use crossterm::event::KeyEvent;
|
||||||
|
|
||||||
pub async fn handle_read_only_event(
|
pub async fn handle_read_only_event(
|
||||||
@@ -17,6 +18,7 @@ pub async fn handle_read_only_event(
|
|||||||
form_state: &mut FormState,
|
form_state: &mut FormState,
|
||||||
login_state: &mut LoginState,
|
login_state: &mut LoginState,
|
||||||
register_state: &mut RegisterState,
|
register_state: &mut RegisterState,
|
||||||
|
add_table_state: &mut AddTableState,
|
||||||
key_sequence_tracker: &mut KeySequenceTracker,
|
key_sequence_tracker: &mut KeySequenceTracker,
|
||||||
current_position: &mut u64,
|
current_position: &mut u64,
|
||||||
total_count: u64,
|
total_count: u64,
|
||||||
@@ -32,34 +34,17 @@ pub async fn handle_read_only_event(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if config.is_enter_edit_mode_after(key.code, key.modifiers) {
|
if config.is_enter_edit_mode_after(key.code, key.modifiers) {
|
||||||
let (current_input, current_pos) = if app_state.ui.show_login { // Check Login first
|
// Determine target state to adjust cursor
|
||||||
(
|
let target_state: &mut dyn CanvasState = if app_state.ui.show_login { login_state }
|
||||||
login_state.get_current_input(),
|
else if app_state.ui.show_register { register_state }
|
||||||
login_state.current_cursor_pos(),
|
else if app_state.ui.show_add_table { add_table_state }
|
||||||
)
|
else { form_state };
|
||||||
} else if app_state.ui.show_register { // Then check Register
|
let current_input = target_state.get_current_input();
|
||||||
(
|
let current_pos = target_state.current_cursor_pos();
|
||||||
register_state.get_current_input(),
|
|
||||||
register_state.current_cursor_pos(),
|
|
||||||
)
|
|
||||||
} else {
|
|
||||||
(
|
|
||||||
form_state.get_current_input(),
|
|
||||||
form_state.current_cursor_pos(),
|
|
||||||
) // Default to Form
|
|
||||||
};
|
|
||||||
|
|
||||||
if !current_input.is_empty() && current_pos < current_input.len() {
|
if !current_input.is_empty() && current_pos < current_input.len() {
|
||||||
if app_state.ui.show_login {
|
target_state.set_current_cursor_pos(current_pos + 1);
|
||||||
login_state.set_current_cursor_pos(current_pos + 1);
|
*ideal_cursor_column = target_state.current_cursor_pos();
|
||||||
*ideal_cursor_column = login_state.current_cursor_pos();
|
|
||||||
} else if app_state.ui.show_register {
|
|
||||||
register_state.set_current_cursor_pos(current_pos + 1);
|
|
||||||
*ideal_cursor_column = register_state.current_cursor_pos();
|
|
||||||
} else { // Default to Form
|
|
||||||
form_state.set_current_cursor_pos(current_pos + 1);
|
|
||||||
*ideal_cursor_column = form_state.current_cursor_pos();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
*edit_mode_cooldown = true;
|
*edit_mode_cooldown = true;
|
||||||
*command_message = "Entering Edit mode (after cursor)".to_string();
|
*command_message = "Entering Edit mode (after cursor)".to_string();
|
||||||
@@ -83,7 +68,7 @@ pub async fn handle_read_only_event(
|
|||||||
key_sequence_tracker.add_key(key.code);
|
key_sequence_tracker.add_key(key.code);
|
||||||
let sequence = key_sequence_tracker.get_sequence();
|
let sequence = key_sequence_tracker.get_sequence();
|
||||||
|
|
||||||
if let Some(action) = config.matches_key_sequence_generalized(&sequence) {
|
if let Some(action) = config.matches_key_sequence_generalized(&sequence).as_deref() {
|
||||||
let result = if app_state.ui.show_form && CONTEXT_ACTIONS_FORM.contains(&action) {
|
let result = if app_state.ui.show_form && CONTEXT_ACTIONS_FORM.contains(&action) {
|
||||||
crate::tui::functions::form::handle_action(
|
crate::tui::functions::form::handle_action(
|
||||||
action,
|
action,
|
||||||
@@ -96,6 +81,15 @@ pub async fn handle_read_only_event(
|
|||||||
.await?
|
.await?
|
||||||
} else if app_state.ui.show_login && CONTEXT_ACTIONS_LOGIN.contains(&action) { // Handle login context actions
|
} else if app_state.ui.show_login && CONTEXT_ACTIONS_LOGIN.contains(&action) { // Handle login context actions
|
||||||
crate::tui::functions::login::handle_action(action).await?
|
crate::tui::functions::login::handle_action(action).await?
|
||||||
|
} else if app_state.ui.show_add_table {
|
||||||
|
add_table_ro::execute_action(
|
||||||
|
action,
|
||||||
|
app_state,
|
||||||
|
add_table_state,
|
||||||
|
ideal_cursor_column,
|
||||||
|
key_sequence_tracker,
|
||||||
|
command_message,
|
||||||
|
).await?
|
||||||
} else if app_state.ui.show_register{
|
} else if app_state.ui.show_register{
|
||||||
auth_ro::execute_action(
|
auth_ro::execute_action(
|
||||||
action,
|
action,
|
||||||
@@ -134,7 +128,7 @@ pub async fn handle_read_only_event(
|
|||||||
}
|
}
|
||||||
|
|
||||||
if sequence.len() == 1 && !config.is_key_sequence_prefix(&sequence) {
|
if sequence.len() == 1 && !config.is_key_sequence_prefix(&sequence) {
|
||||||
if let Some(action) = config.get_read_only_action_for_key(key.code, key.modifiers) {
|
if let Some(action) = config.get_read_only_action_for_key(key.code, key.modifiers).as_deref() {
|
||||||
let result = if app_state.ui.show_form && CONTEXT_ACTIONS_FORM.contains(&action) {
|
let result = if app_state.ui.show_form && CONTEXT_ACTIONS_FORM.contains(&action) {
|
||||||
crate::tui::functions::form::handle_action(
|
crate::tui::functions::form::handle_action(
|
||||||
action,
|
action,
|
||||||
@@ -147,6 +141,15 @@ pub async fn handle_read_only_event(
|
|||||||
.await?
|
.await?
|
||||||
} else if app_state.ui.show_login && CONTEXT_ACTIONS_LOGIN.contains(&action) { // Handle login context actions
|
} else if app_state.ui.show_login && CONTEXT_ACTIONS_LOGIN.contains(&action) { // Handle login context actions
|
||||||
crate::tui::functions::login::handle_action(action).await?
|
crate::tui::functions::login::handle_action(action).await?
|
||||||
|
} else if app_state.ui.show_add_table {
|
||||||
|
add_table_ro::execute_action(
|
||||||
|
action,
|
||||||
|
app_state,
|
||||||
|
add_table_state,
|
||||||
|
ideal_cursor_column,
|
||||||
|
key_sequence_tracker,
|
||||||
|
command_message,
|
||||||
|
).await?
|
||||||
} else if app_state.ui.show_register /* && CONTEXT_ACTIONS_REGISTER.contains(&action) */ { // Handle register general actions
|
} else if app_state.ui.show_register /* && CONTEXT_ACTIONS_REGISTER.contains(&action) */ { // Handle register general actions
|
||||||
auth_ro::execute_action(
|
auth_ro::execute_action(
|
||||||
action,
|
action,
|
||||||
@@ -184,7 +187,7 @@ pub async fn handle_read_only_event(
|
|||||||
} else {
|
} else {
|
||||||
key_sequence_tracker.reset();
|
key_sequence_tracker.reset();
|
||||||
|
|
||||||
if let Some(action) = config.get_read_only_action_for_key(key.code, key.modifiers) {
|
if let Some(action) = config.get_read_only_action_for_key(key.code, key.modifiers).as_deref() {
|
||||||
let result = if app_state.ui.show_form && CONTEXT_ACTIONS_FORM.contains(&action) {
|
let result = if app_state.ui.show_form && CONTEXT_ACTIONS_FORM.contains(&action) {
|
||||||
crate::tui::functions::form::handle_action(
|
crate::tui::functions::form::handle_action(
|
||||||
action,
|
action,
|
||||||
@@ -195,8 +198,17 @@ pub async fn handle_read_only_event(
|
|||||||
ideal_cursor_column,
|
ideal_cursor_column,
|
||||||
)
|
)
|
||||||
.await?
|
.await?
|
||||||
} else if app_state.ui.show_login && CONTEXT_ACTIONS_LOGIN.contains(&action) { // Handle login context actions
|
} else if app_state.ui.show_login && CONTEXT_ACTIONS_LOGIN.contains(&action) {
|
||||||
crate::tui::functions::login::handle_action(action).await?
|
crate::tui::functions::login::handle_action(action).await?
|
||||||
|
} else if app_state.ui.show_add_table {
|
||||||
|
add_table_ro::execute_action(
|
||||||
|
action,
|
||||||
|
app_state,
|
||||||
|
add_table_state,
|
||||||
|
ideal_cursor_column,
|
||||||
|
key_sequence_tracker,
|
||||||
|
command_message,
|
||||||
|
).await?
|
||||||
} else if app_state.ui.show_register /* && CONTEXT_ACTIONS_REGISTER.contains(&action) */ { // Handle register general actions
|
} else if app_state.ui.show_register /* && CONTEXT_ACTIONS_REGISTER.contains(&action) */ { // Handle register general actions
|
||||||
auth_ro::execute_action(
|
auth_ro::execute_action(
|
||||||
action,
|
action,
|
||||||
|
|||||||
@@ -6,10 +6,11 @@ use crate::ui::handlers::context::DialogPurpose;
|
|||||||
use crate::state::app::state::AppState;
|
use crate::state::app::state::AppState;
|
||||||
use crate::state::app::buffer::BufferState;
|
use crate::state::app::buffer::BufferState;
|
||||||
use crate::state::pages::auth::AuthState;
|
use crate::state::pages::auth::AuthState;
|
||||||
use crate::state::pages::auth::LoginState;
|
use crate::state::pages::auth::{LoginState, RegisterState};
|
||||||
use crate::state::pages::auth::RegisterState;
|
use crate::state::pages::admin::AdminState;
|
||||||
use crate::modes::handlers::event::EventOutcome;
|
use crate::modes::handlers::event::EventOutcome;
|
||||||
use crate::tui::functions::common::{login, register};
|
use crate::tui::functions::common::{login, register};
|
||||||
|
use crate::tui::functions::common::add_table::handle_delete_selected_columns;
|
||||||
|
|
||||||
/// Handles key events specifically when a dialog is active.
|
/// Handles key events specifically when a dialog is active.
|
||||||
/// Returns Some(Result<EventOutcome, Error>) if the event was handled (consumed),
|
/// Returns Some(Result<EventOutcome, Error>) if the event was handled (consumed),
|
||||||
@@ -22,6 +23,7 @@ pub async fn handle_dialog_event(
|
|||||||
login_state: &mut LoginState,
|
login_state: &mut LoginState,
|
||||||
register_state: &mut RegisterState,
|
register_state: &mut RegisterState,
|
||||||
buffer_state: &mut BufferState,
|
buffer_state: &mut BufferState,
|
||||||
|
admin_state: &mut AdminState,
|
||||||
) -> Option<Result<EventOutcome, Box<dyn std::error::Error>>> {
|
) -> Option<Result<EventOutcome, Box<dyn std::error::Error>>> {
|
||||||
if let Event::Key(key) = event {
|
if let Event::Key(key) = event {
|
||||||
// Always allow Esc to dismiss
|
// Always allow Esc to dismiss
|
||||||
@@ -114,6 +116,20 @@ pub async fn handle_dialog_event(
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
DialogPurpose::ConfirmDeleteColumns => {
|
||||||
|
match selected_index {
|
||||||
|
0 => { // "Confirm" button selected
|
||||||
|
let outcome_message = handle_delete_selected_columns(&mut admin_state.add_table_state);
|
||||||
|
app_state.hide_dialog();
|
||||||
|
return Some(Ok(EventOutcome::Ok(outcome_message)));
|
||||||
|
}
|
||||||
|
1 => { // "Cancel" button selected
|
||||||
|
app_state.hide_dialog();
|
||||||
|
return Some(Ok(EventOutcome::Ok("Deletion cancelled.".to_string())));
|
||||||
|
}
|
||||||
|
_ => { /* Handle unexpected index */ }
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
_ => {} // Ignore other general actions when dialog is shown
|
_ => {} // Ignore other general actions when dialog is shown
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ use crate::services::auth::AuthClient;
|
|||||||
use crate::config::binds::config::Config;
|
use crate::config::binds::config::Config;
|
||||||
use crate::ui::handlers::rat_state::UiStateHandler;
|
use crate::ui::handlers::rat_state::UiStateHandler;
|
||||||
use crate::ui::handlers::context::UiContext;
|
use crate::ui::handlers::context::UiContext;
|
||||||
|
use crate::ui::handlers::context::DialogPurpose;
|
||||||
use crate::functions::common::buffer;
|
use crate::functions::common::buffer;
|
||||||
use crate::tui::{
|
use crate::tui::{
|
||||||
terminal::core::TerminalCore,
|
terminal::core::TerminalCore,
|
||||||
@@ -28,6 +29,7 @@ use crate::state::{
|
|||||||
auth::{AuthState, LoginState, RegisterState},
|
auth::{AuthState, LoginState, RegisterState},
|
||||||
admin::AdminState,
|
admin::AdminState,
|
||||||
canvas_state::CanvasState,
|
canvas_state::CanvasState,
|
||||||
|
add_table::AddTableState,
|
||||||
form::FormState,
|
form::FormState,
|
||||||
intro::IntroState,
|
intro::IntroState,
|
||||||
},
|
},
|
||||||
@@ -39,7 +41,7 @@ use crate::modes::{
|
|||||||
highlight::highlight,
|
highlight::highlight,
|
||||||
general::{navigation, dialog},
|
general::{navigation, dialog},
|
||||||
};
|
};
|
||||||
use crate::functions::modes::navigation::admin_nav;
|
use crate::functions::modes::navigation::{admin_nav, add_table_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)]
|
||||||
@@ -115,7 +117,14 @@ impl EventHandler {
|
|||||||
|
|
||||||
if app_state.ui.dialog.dialog_show {
|
if app_state.ui.dialog.dialog_show {
|
||||||
if let Some(dialog_result) = dialog::handle_dialog_event(
|
if let Some(dialog_result) = dialog::handle_dialog_event(
|
||||||
&event, config, app_state, auth_state, login_state, register_state, buffer_state
|
&event,
|
||||||
|
config,
|
||||||
|
app_state,
|
||||||
|
auth_state,
|
||||||
|
login_state,
|
||||||
|
register_state,
|
||||||
|
buffer_state,
|
||||||
|
admin_state,
|
||||||
).await {
|
).await {
|
||||||
return dialog_result;
|
return dialog_result;
|
||||||
}
|
}
|
||||||
@@ -175,6 +184,21 @@ impl EventHandler {
|
|||||||
return Ok(EventOutcome::Ok(self.command_message.clone()));
|
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(
|
let nav_outcome = navigation::handle_navigation_event(
|
||||||
key,
|
key,
|
||||||
@@ -203,11 +227,21 @@ impl EventHandler {
|
|||||||
message = format!("Intro Option {} selected", index);
|
message = format!("Intro Option {} selected", index);
|
||||||
}
|
}
|
||||||
UiContext::Login => {
|
UiContext::Login => {
|
||||||
message = match index {
|
let login_action_message = match index {
|
||||||
0 => login::save(auth_state, login_state, &mut self.auth_client, app_state).await?,
|
0 => { // Index 0 corresponds to the "Login" button
|
||||||
|
match login::initiate_login(app_state, login_state).await {
|
||||||
|
Ok(outcome) => return Ok(outcome),
|
||||||
|
Err(e) => {
|
||||||
|
app_state.show_dialog("Error", &format!("Failed to initiate login: {}", e), vec!["OK".to_string()], DialogPurpose::LoginFailed);
|
||||||
|
login_state.login_request_pending = false;
|
||||||
|
"Error initiating login".to_string()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
},
|
||||||
1 => login::back_to_main(login_state, app_state, buffer_state).await,
|
1 => login::back_to_main(login_state, app_state, buffer_state).await,
|
||||||
_ => "Invalid Login Option".to_string(),
|
_ => "Invalid Login Option".to_string(),
|
||||||
};
|
};
|
||||||
|
message = login_action_message;
|
||||||
}
|
}
|
||||||
UiContext::Register => {
|
UiContext::Register => {
|
||||||
message = match index {
|
message = match index {
|
||||||
@@ -258,7 +292,7 @@ impl EventHandler {
|
|||||||
return Ok(EventOutcome::Ok(self.command_message.clone()));
|
return Ok(EventOutcome::Ok(self.command_message.clone()));
|
||||||
}
|
}
|
||||||
// Check for entering edit mode (before cursor)
|
// Check for entering edit mode (before cursor)
|
||||||
else if config.get_read_only_action_for_key(key_code, modifiers) == Some("enter_edit_mode_before")
|
else if config.get_read_only_action_for_key(key_code, modifiers).as_deref() == Some("enter_edit_mode_before")
|
||||||
&& ModeManager::can_enter_edit_mode(current_mode) {
|
&& ModeManager::can_enter_edit_mode(current_mode) {
|
||||||
self.is_edit_mode = true;
|
self.is_edit_mode = true;
|
||||||
self.edit_mode_cooldown = true;
|
self.edit_mode_cooldown = true;
|
||||||
@@ -267,7 +301,7 @@ impl EventHandler {
|
|||||||
return Ok(EventOutcome::Ok(self.command_message.clone()));
|
return Ok(EventOutcome::Ok(self.command_message.clone()));
|
||||||
}
|
}
|
||||||
// Check for entering edit mode (after cursor)
|
// Check for entering edit mode (after cursor)
|
||||||
else if config.get_read_only_action_for_key(key_code, modifiers) == Some("enter_edit_mode_after")
|
else if config.get_read_only_action_for_key(key_code, modifiers).as_deref() == Some("enter_edit_mode_after")
|
||||||
&& ModeManager::can_enter_edit_mode(current_mode) {
|
&& ModeManager::can_enter_edit_mode(current_mode) {
|
||||||
let current_input = if app_state.ui.show_login || app_state.ui.show_register{
|
let current_input = if app_state.ui.show_login || app_state.ui.show_register{
|
||||||
login_state.get_current_input()
|
login_state.get_current_input()
|
||||||
@@ -291,6 +325,7 @@ impl EventHandler {
|
|||||||
}
|
}
|
||||||
self.is_edit_mode = true;
|
self.is_edit_mode = true;
|
||||||
self.edit_mode_cooldown = true;
|
self.edit_mode_cooldown = true;
|
||||||
|
app_state.ui.focus_outside_canvas = false;
|
||||||
self.command_message = "Edit mode (after cursor)".to_string();
|
self.command_message = "Edit mode (after cursor)".to_string();
|
||||||
terminal.set_cursor_style(SetCursorStyle::BlinkingBar)?;
|
terminal.set_cursor_style(SetCursorStyle::BlinkingBar)?;
|
||||||
return Ok(EventOutcome::Ok(self.command_message.clone()));
|
return Ok(EventOutcome::Ok(self.command_message.clone()));
|
||||||
@@ -334,6 +369,7 @@ impl EventHandler {
|
|||||||
form_state,
|
form_state,
|
||||||
login_state,
|
login_state,
|
||||||
register_state,
|
register_state,
|
||||||
|
&mut admin_state.add_table_state,
|
||||||
&mut self.key_sequence_tracker,
|
&mut self.key_sequence_tracker,
|
||||||
current_position,
|
current_position,
|
||||||
total_count,
|
total_count,
|
||||||
@@ -368,7 +404,7 @@ impl EventHandler {
|
|||||||
|
|
||||||
let (_should_exit, message) = read_only::handle_read_only_event(
|
let (_should_exit, message) = read_only::handle_read_only_event(
|
||||||
app_state, key, config, form_state, login_state,
|
app_state, key, config, form_state, login_state,
|
||||||
register_state, &mut self.key_sequence_tracker,
|
register_state, &mut admin_state.add_table_state, &mut self.key_sequence_tracker,
|
||||||
current_position, total_count, grpc_client,
|
current_position, total_count, grpc_client,
|
||||||
&mut self.command_message, &mut self.edit_mode_cooldown,
|
&mut self.command_message, &mut self.edit_mode_cooldown,
|
||||||
&mut self.ideal_cursor_column,
|
&mut self.ideal_cursor_column,
|
||||||
@@ -416,6 +452,7 @@ impl EventHandler {
|
|||||||
form_state,
|
form_state,
|
||||||
login_state,
|
login_state,
|
||||||
register_state,
|
register_state,
|
||||||
|
&mut admin_state.add_table_state,
|
||||||
&mut self.ideal_cursor_column,
|
&mut self.ideal_cursor_column,
|
||||||
current_position,
|
current_position,
|
||||||
total_count,
|
total_count,
|
||||||
|
|||||||
@@ -2,6 +2,8 @@
|
|||||||
use crate::state::app::state::AppState;
|
use crate::state::app::state::AppState;
|
||||||
use crate::modes::handlers::event::EventHandler;
|
use crate::modes::handlers::event::EventHandler;
|
||||||
use crate::state::app::highlight::HighlightState;
|
use crate::state::app::highlight::HighlightState;
|
||||||
|
use crate::state::pages::add_table::AddTableFocus;
|
||||||
|
use crate::state::pages::admin::AdminState;
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||||
pub enum AppMode {
|
pub enum AppMode {
|
||||||
@@ -29,17 +31,20 @@ impl ModeManager {
|
|||||||
return AppMode::General;
|
return AppMode::General;
|
||||||
}
|
}
|
||||||
|
|
||||||
if app_state.ui.show_login || app_state.ui.show_register {
|
let is_canvas_view = app_state.ui.show_login
|
||||||
if event_handler.is_edit_mode {
|
|| app_state.ui.show_register
|
||||||
AppMode::Edit
|
|| app_state.ui.show_form
|
||||||
|
|| app_state.ui.show_add_table;
|
||||||
|
|
||||||
|
if is_canvas_view {
|
||||||
|
if app_state.ui.focus_outside_canvas {
|
||||||
|
AppMode::General
|
||||||
} else {
|
} else {
|
||||||
AppMode::ReadOnly
|
if event_handler.is_edit_mode {
|
||||||
}
|
AppMode::Edit
|
||||||
} else if app_state.ui.show_form {
|
} else {
|
||||||
if event_handler.is_edit_mode {
|
AppMode::ReadOnly
|
||||||
AppMode::Edit
|
}
|
||||||
} else {
|
|
||||||
AppMode::ReadOnly
|
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
AppMode::General
|
AppMode::General
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ use crate::config::binds::key_sequences::KeySequenceTracker;
|
|||||||
use crate::services::grpc_client::GrpcClient;
|
use crate::services::grpc_client::GrpcClient;
|
||||||
use crate::state::app::state::AppState;
|
use crate::state::app::state::AppState;
|
||||||
use crate::state::pages::auth::{LoginState, RegisterState};
|
use crate::state::pages::auth::{LoginState, RegisterState};
|
||||||
|
use crate::state::pages::add_table::AddTableState;
|
||||||
use crate::state::pages::form::FormState;
|
use crate::state::pages::form::FormState;
|
||||||
use crate::modes::handlers::event::EventOutcome;
|
use crate::modes::handlers::event::EventOutcome;
|
||||||
use crate::modes::read_only; // Import the ReadOnly handler
|
use crate::modes::read_only; // Import the ReadOnly handler
|
||||||
@@ -21,6 +22,7 @@ pub async fn handle_highlight_event(
|
|||||||
form_state: &mut FormState,
|
form_state: &mut FormState,
|
||||||
login_state: &mut LoginState,
|
login_state: &mut LoginState,
|
||||||
register_state: &mut RegisterState,
|
register_state: &mut RegisterState,
|
||||||
|
add_table_state: &mut AddTableState,
|
||||||
key_sequence_tracker: &mut KeySequenceTracker,
|
key_sequence_tracker: &mut KeySequenceTracker,
|
||||||
current_position: &mut u64,
|
current_position: &mut u64,
|
||||||
total_count: u64,
|
total_count: u64,
|
||||||
@@ -38,6 +40,7 @@ pub async fn handle_highlight_event(
|
|||||||
form_state,
|
form_state,
|
||||||
login_state,
|
login_state,
|
||||||
register_state,
|
register_state,
|
||||||
|
add_table_state,
|
||||||
key_sequence_tracker,
|
key_sequence_tracker,
|
||||||
current_position,
|
current_position,
|
||||||
total_count,
|
total_count,
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ pub struct DialogState {
|
|||||||
pub dialog_buttons: Vec<String>,
|
pub dialog_buttons: Vec<String>,
|
||||||
pub dialog_active_button_index: usize,
|
pub dialog_active_button_index: usize,
|
||||||
pub purpose: Option<DialogPurpose>,
|
pub purpose: Option<DialogPurpose>,
|
||||||
|
pub is_loading: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct UiState {
|
pub struct UiState {
|
||||||
@@ -86,10 +87,41 @@ impl AppState {
|
|||||||
self.ui.dialog.dialog_buttons = buttons;
|
self.ui.dialog.dialog_buttons = buttons;
|
||||||
self.ui.dialog.dialog_active_button_index = 0;
|
self.ui.dialog.dialog_active_button_index = 0;
|
||||||
self.ui.dialog.purpose = Some(purpose);
|
self.ui.dialog.purpose = Some(purpose);
|
||||||
|
self.ui.dialog.is_loading = false;
|
||||||
self.ui.dialog.dialog_show = true;
|
self.ui.dialog.dialog_show = true;
|
||||||
self.ui.focus_outside_canvas = true;
|
self.ui.focus_outside_canvas = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Shows a dialog specifically for loading states.
|
||||||
|
pub fn show_loading_dialog(&mut self, title: &str, message: &str) {
|
||||||
|
self.ui.dialog.dialog_title = title.to_string();
|
||||||
|
self.ui.dialog.dialog_message = message.to_string();
|
||||||
|
self.ui.dialog.dialog_buttons.clear(); // No buttons during loading
|
||||||
|
self.ui.dialog.dialog_active_button_index = 0;
|
||||||
|
self.ui.dialog.purpose = None; // Purpose is set when loading finishes
|
||||||
|
self.ui.dialog.is_loading = true;
|
||||||
|
self.ui.dialog.dialog_show = true;
|
||||||
|
self.ui.focus_outside_canvas = true; // Keep focus management consistent
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Updates the content of an existing dialog, typically after loading.
|
||||||
|
pub fn update_dialog_content(
|
||||||
|
&mut self,
|
||||||
|
message: &str,
|
||||||
|
buttons: Vec<String>,
|
||||||
|
purpose: DialogPurpose,
|
||||||
|
) {
|
||||||
|
if self.ui.dialog.dialog_show {
|
||||||
|
self.ui.dialog.dialog_message = message.to_string();
|
||||||
|
self.ui.dialog.dialog_buttons = buttons;
|
||||||
|
self.ui.dialog.dialog_active_button_index = 0; // Reset focus
|
||||||
|
self.ui.dialog.purpose = Some(purpose);
|
||||||
|
self.ui.dialog.is_loading = false; // Loading finished
|
||||||
|
// Keep dialog_show = true
|
||||||
|
// Keep focus_outside_canvas = true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Hides the dialog and clears its content.
|
/// Hides the dialog and clears its content.
|
||||||
pub fn hide_dialog(&mut self) {
|
pub fn hide_dialog(&mut self) {
|
||||||
self.ui.dialog.dialog_show = false;
|
self.ui.dialog.dialog_show = false;
|
||||||
@@ -156,6 +188,7 @@ impl Default for DialogState {
|
|||||||
dialog_buttons: Vec::new(),
|
dialog_buttons: Vec::new(),
|
||||||
dialog_active_button_index: 0,
|
dialog_active_button_index: 0,
|
||||||
purpose: None,
|
purpose: None,
|
||||||
|
is_loading: false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,26 +1,39 @@
|
|||||||
// src/state/pages/add_table.rs
|
// src/state/pages/add_table.rs
|
||||||
|
use crate::state::pages::canvas_state::CanvasState;
|
||||||
use ratatui::widgets::TableState;
|
use ratatui::widgets::TableState;
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
pub struct ColumnDefinition {
|
pub struct ColumnDefinition {
|
||||||
pub name: String,
|
pub name: String,
|
||||||
pub data_type: String,
|
pub data_type: String,
|
||||||
|
pub selected: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||||
pub struct LinkDefinition {
|
pub struct LinkDefinition {
|
||||||
pub linked_table_name: String,
|
pub linked_table_name: String,
|
||||||
pub is_required: bool,
|
pub is_required: bool,
|
||||||
|
pub selected: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
|
||||||
pub enum AddTableFocus {
|
pub enum AddTableFocus {
|
||||||
#[default]
|
#[default]
|
||||||
TableName,
|
InputTableName, // Field 0 for CanvasState
|
||||||
Columns,
|
InputColumnName, // Field 1 for CanvasState
|
||||||
Indexes,
|
InputColumnType, // Field 2 for CanvasState
|
||||||
Links,
|
AddColumnButton,
|
||||||
|
// Result Tables
|
||||||
|
ColumnsTable,
|
||||||
|
IndexesTable,
|
||||||
|
LinksTable,
|
||||||
|
// Inside Tables (Scrolling Focus)
|
||||||
|
InsideColumnsTable,
|
||||||
|
InsideIndexesTable,
|
||||||
|
InsideLinksTable,
|
||||||
|
// Buttons
|
||||||
SaveButton,
|
SaveButton,
|
||||||
|
DeleteSelectedButton,
|
||||||
CancelButton,
|
CancelButton,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -28,6 +41,9 @@ pub enum AddTableFocus {
|
|||||||
pub struct AddTableState {
|
pub struct AddTableState {
|
||||||
pub profile_name: String,
|
pub profile_name: String,
|
||||||
pub table_name: String,
|
pub table_name: String,
|
||||||
|
pub table_name_input: String,
|
||||||
|
pub column_name_input: String,
|
||||||
|
pub column_type_input: String,
|
||||||
pub columns: Vec<ColumnDefinition>,
|
pub columns: Vec<ColumnDefinition>,
|
||||||
pub indexes: Vec<String>,
|
pub indexes: Vec<String>,
|
||||||
pub links: Vec<LinkDefinition>,
|
pub links: Vec<LinkDefinition>,
|
||||||
@@ -36,33 +52,120 @@ pub struct AddTableState {
|
|||||||
pub index_table_state: TableState,
|
pub index_table_state: TableState,
|
||||||
pub link_table_state: TableState,
|
pub link_table_state: TableState,
|
||||||
pub table_name_cursor_pos: usize,
|
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 {
|
impl Default for AddTableState {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
// Initialize with some dummy data for demonstration
|
// Initialize with some dummy data for demonstration
|
||||||
AddTableState {
|
AddTableState {
|
||||||
profile_name: "default".to_string(), // Should be set dynamically
|
profile_name: "default".to_string(),
|
||||||
table_name: "new_table".to_string(),
|
table_name: String::new(),
|
||||||
columns: vec![
|
table_name_input: String::new(),
|
||||||
ColumnDefinition { name: "id".to_string(), data_type: "INTEGER".to_string() },
|
column_name_input: String::new(),
|
||||||
ColumnDefinition { name: "name".to_string(), data_type: "TEXT".to_string() },
|
column_type_input: String::new(),
|
||||||
],
|
columns: Vec::new(),
|
||||||
indexes: vec!["id".to_string()],
|
indexes: Vec::new(),
|
||||||
links: vec![
|
links: Vec::new(),
|
||||||
LinkDefinition { linked_table_name: "related_table".to_string(), is_required: true },
|
current_focus: AddTableFocus::InputTableName,
|
||||||
LinkDefinition { linked_table_name: "another_table".to_string(), is_required: false },
|
column_table_state: TableState::default(),
|
||||||
],
|
index_table_state: TableState::default(),
|
||||||
current_focus: AddTableFocus::TableName,
|
link_table_state: TableState::default(),
|
||||||
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,
|
table_name_cursor_pos: 0,
|
||||||
|
column_name_cursor_pos: 0,
|
||||||
|
column_type_cursor_pos: 0,
|
||||||
|
has_unsaved_changes: false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl AddTableState {
|
impl AddTableState {
|
||||||
|
pub 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_input, &self.column_name_input, &self.column_type_input]
|
||||||
|
}
|
||||||
|
|
||||||
|
fn get_current_input(&self) -> &str {
|
||||||
|
match self.current_focus {
|
||||||
|
AddTableFocus::InputTableName => &self.table_name_input,
|
||||||
|
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_input,
|
||||||
|
AddTableFocus::InputColumnName => &mut self.column_name_input,
|
||||||
|
AddTableFocus::InputColumnType => &mut self.column_type_input,
|
||||||
|
_ => &mut self.table_name_input,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -29,6 +29,7 @@ pub struct LoginState {
|
|||||||
pub current_field: usize,
|
pub current_field: usize,
|
||||||
pub current_cursor_pos: usize,
|
pub current_cursor_pos: usize,
|
||||||
pub has_unsaved_changes: bool,
|
pub has_unsaved_changes: bool,
|
||||||
|
pub login_request_pending: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Represents the state of the Registration form UI
|
/// Represents the state of the Registration form UI
|
||||||
@@ -71,6 +72,7 @@ impl LoginState {
|
|||||||
current_field: 0,
|
current_field: 0,
|
||||||
current_cursor_pos: 0,
|
current_cursor_pos: 0,
|
||||||
has_unsaved_changes: false,
|
has_unsaved_changes: false,
|
||||||
|
login_request_pending: false,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -3,3 +3,4 @@
|
|||||||
pub mod form;
|
pub mod form;
|
||||||
pub mod login;
|
pub mod login;
|
||||||
pub mod register;
|
pub mod register;
|
||||||
|
pub mod add_table;
|
||||||
|
|||||||
100
client/src/tui/functions/common/add_table.rs
Normal file
100
client/src/tui/functions/common/add_table.rs
Normal file
@@ -0,0 +1,100 @@
|
|||||||
|
// src/tui/functions/common/add_table.rs
|
||||||
|
use crate::state::pages::add_table::{
|
||||||
|
AddTableFocus, AddTableState, ColumnDefinition,
|
||||||
|
};
|
||||||
|
|
||||||
|
/// Handles the logic for adding a column when the "Add" button is activated.
|
||||||
|
///
|
||||||
|
/// Takes the mutable state and command message string.
|
||||||
|
/// Returns `Some(AddTableFocus)` indicating the desired focus state after a successful add,
|
||||||
|
/// or `None` if the action failed (e.g., validation error).
|
||||||
|
pub fn handle_add_column_action(
|
||||||
|
add_table_state: &mut AddTableState,
|
||||||
|
command_message: &mut String,
|
||||||
|
) -> Option<AddTableFocus> {
|
||||||
|
|
||||||
|
// Trim and create owned Strings from inputs
|
||||||
|
let table_name_in = add_table_state.table_name_input.trim();
|
||||||
|
let column_name_in = add_table_state.column_name_input.trim();
|
||||||
|
let column_type_in = add_table_state.column_type_input.trim();
|
||||||
|
|
||||||
|
// Validate all inputs needed for this combined action
|
||||||
|
let has_table_name = !table_name_in.is_empty();
|
||||||
|
let has_column_name = !column_name_in.is_empty();
|
||||||
|
let has_column_type = !column_type_in.is_empty();
|
||||||
|
|
||||||
|
match (has_table_name, has_column_name, has_column_type) {
|
||||||
|
// Case 1: Both column fields have input (Table name is optional here)
|
||||||
|
(_, true, true) => {
|
||||||
|
let mut msg = String::new();
|
||||||
|
// Optionally update table name if provided
|
||||||
|
if has_table_name {
|
||||||
|
add_table_state.table_name = table_name_in.to_string();
|
||||||
|
msg.push_str(&format!("Table name set to '{}'. ", add_table_state.table_name));
|
||||||
|
}
|
||||||
|
// Add the column
|
||||||
|
let new_column = ColumnDefinition {
|
||||||
|
name: column_name_in.to_string(),
|
||||||
|
data_type: column_type_in.to_string(),
|
||||||
|
selected: false,
|
||||||
|
};
|
||||||
|
add_table_state.columns.push(new_column.clone()); // Clone for msg
|
||||||
|
msg.push_str(&format!("Column '{}' added.", new_column.name));
|
||||||
|
*command_message = msg;
|
||||||
|
|
||||||
|
// Clear all inputs and reset cursors
|
||||||
|
add_table_state.table_name_input.clear();
|
||||||
|
add_table_state.column_name_input.clear();
|
||||||
|
add_table_state.column_type_input.clear();
|
||||||
|
add_table_state.table_name_cursor_pos = 0;
|
||||||
|
add_table_state.column_name_cursor_pos = 0;
|
||||||
|
add_table_state.column_type_cursor_pos = 0;
|
||||||
|
add_table_state.has_unsaved_changes = true;
|
||||||
|
Some(AddTableFocus::InputColumnName) // Focus for next column
|
||||||
|
}
|
||||||
|
// Case 2: Only one column field has input (Error)
|
||||||
|
(_, true, false) | (_, false, true) => {
|
||||||
|
*command_message = "Both Column Name and Type are required to add a column.".to_string();
|
||||||
|
None // Indicate validation failure
|
||||||
|
}
|
||||||
|
// Case 3: Only Table name has input (No column input)
|
||||||
|
(true, false, false) => {
|
||||||
|
add_table_state.table_name = table_name_in.to_string();
|
||||||
|
*command_message = format!("Table name set to '{}'.", add_table_state.table_name);
|
||||||
|
// Clear only table name input
|
||||||
|
add_table_state.table_name_input.clear();
|
||||||
|
add_table_state.table_name_cursor_pos = 0;
|
||||||
|
add_table_state.has_unsaved_changes = true;
|
||||||
|
Some(AddTableFocus::InputTableName) // Keep focus here
|
||||||
|
}
|
||||||
|
// Case 4: All fields are empty
|
||||||
|
(false, false, false) => {
|
||||||
|
*command_message = "No input provided.".to_string();
|
||||||
|
None
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Handles deleting columns marked as selected in the AddTableState.
|
||||||
|
pub fn handle_delete_selected_columns(
|
||||||
|
add_table_state: &mut AddTableState,
|
||||||
|
) -> String {
|
||||||
|
let initial_count = add_table_state.columns.len();
|
||||||
|
// Keep only the columns that are NOT selected
|
||||||
|
add_table_state.columns.retain(|col| !col.selected);
|
||||||
|
let deleted_count = initial_count - add_table_state.columns.len();
|
||||||
|
|
||||||
|
if deleted_count > 0 {
|
||||||
|
add_table_state.has_unsaved_changes = true;
|
||||||
|
// Reset selection highlight as indices have changed
|
||||||
|
add_table_state.column_table_state.select(None);
|
||||||
|
// Optionally, select the first item if the list is not empty
|
||||||
|
// if !add_table_state.columns.is_empty() {
|
||||||
|
// add_table_state.column_table_state.select(Some(0));
|
||||||
|
// }
|
||||||
|
format!("Deleted {} selected column(s).", deleted_count)
|
||||||
|
} else {
|
||||||
|
"No columns marked for deletion.".to_string()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@@ -7,9 +7,11 @@ use crate::state::app::state::AppState;
|
|||||||
use crate::state::app::buffer::{AppView, BufferState};
|
use crate::state::app::buffer::{AppView, BufferState};
|
||||||
use crate::state::pages::canvas_state::CanvasState;
|
use crate::state::pages::canvas_state::CanvasState;
|
||||||
use crate::ui::handlers::context::DialogPurpose;
|
use crate::ui::handlers::context::DialogPurpose;
|
||||||
|
use crate::modes::handlers::event::EventOutcome; // Make sure this is imported
|
||||||
|
|
||||||
/// Attempts to log the user in using the provided credentials via gRPC.
|
/// Attempts to log the user in using the provided credentials via gRPC.
|
||||||
/// Updates AuthState and AppState on success or failure.
|
/// Updates AuthState and AppState on success or failure.
|
||||||
|
/// (This is your existing function - remains unchanged)
|
||||||
pub async fn save(
|
pub async fn save(
|
||||||
auth_state: &mut AuthState,
|
auth_state: &mut AuthState,
|
||||||
login_state: &mut LoginState,
|
login_state: &mut LoginState,
|
||||||
@@ -21,24 +23,25 @@ pub async fn save(
|
|||||||
|
|
||||||
// Clear previous error/dialog state before attempting
|
// Clear previous error/dialog state before attempting
|
||||||
login_state.error_message = None;
|
login_state.error_message = None;
|
||||||
// Use the helper to ensure dialog is hidden and cleared properly
|
app_state.hide_dialog(); // Hide any previous dialog
|
||||||
app_state.hide_dialog();
|
|
||||||
|
|
||||||
// Call the gRPC login method
|
// Call the gRPC login method
|
||||||
match auth_client.login(identifier, password).await {
|
match auth_client.login(identifier, password).await {
|
||||||
Ok(response) => {
|
Ok(response) => {
|
||||||
// Store authentication details on success
|
// Store authentication details using correct field names
|
||||||
auth_state.auth_token = Some(response.access_token.clone());
|
auth_state.auth_token = Some(response.access_token.clone());
|
||||||
auth_state.user_id = Some(response.user_id.clone());
|
auth_state.user_id = Some(response.user_id.clone());
|
||||||
auth_state.role = Some(response.role.clone());
|
auth_state.role = Some(response.role.clone());
|
||||||
auth_state.decoded_username = Some(response.username.clone());
|
auth_state.decoded_username = Some(response.username.clone());
|
||||||
login_state.set_has_unsaved_changes(false);
|
login_state.set_has_unsaved_changes(false);
|
||||||
|
login_state.error_message = None;
|
||||||
|
|
||||||
|
// Format the success message using response data
|
||||||
let success_message = format!(
|
let success_message = format!(
|
||||||
"Login Successful!\n\n\
|
"Login Successful!\n\n\
|
||||||
Username: {}\n\
|
Username: {}\n\
|
||||||
User ID: {}\n\
|
User ID: {}\n\
|
||||||
Role: {}",
|
Role: {}",
|
||||||
response.username,
|
response.username,
|
||||||
response.user_id,
|
response.user_id,
|
||||||
response.role
|
response.role
|
||||||
@@ -50,37 +53,53 @@ pub async fn save(
|
|||||||
vec!["Menu".to_string(), "Exit".to_string()],
|
vec!["Menu".to_string(), "Exit".to_string()],
|
||||||
DialogPurpose::LoginSuccess,
|
DialogPurpose::LoginSuccess,
|
||||||
);
|
);
|
||||||
|
login_state.password.clear();
|
||||||
|
login_state.current_cursor_pos = 0;
|
||||||
Ok("Login successful, details shown in dialog.".to_string())
|
Ok("Login successful, details shown in dialog.".to_string())
|
||||||
}
|
}
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
let error_message = format!("{}", e);
|
let error_message = format!("{}", e);
|
||||||
|
|
||||||
// Use the helper method to configure and show the dialog
|
|
||||||
app_state.show_dialog(
|
app_state.show_dialog(
|
||||||
"Login Failed",
|
"Login Failed",
|
||||||
&error_message,
|
&error_message,
|
||||||
vec!["OK".to_string()],
|
vec!["OK".to_string()],
|
||||||
DialogPurpose::LoginFailed,
|
DialogPurpose::LoginFailed,
|
||||||
);
|
);
|
||||||
|
login_state.error_message = Some(error_message.clone());
|
||||||
login_state.set_has_unsaved_changes(true);
|
login_state.set_has_unsaved_changes(true);
|
||||||
|
|
||||||
Ok(format!("Login failed: {}", error_message))
|
Ok(format!("Login failed: {}", error_message))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// --- Add this new function ---
|
||||||
|
/// Sets the stage for login: shows loading dialog and sets the pending flag.
|
||||||
|
/// Call this from the event handler when login is triggered.
|
||||||
|
pub async fn initiate_login(
|
||||||
|
app_state: &mut AppState,
|
||||||
|
login_state: &mut LoginState,
|
||||||
|
) -> Result<EventOutcome, Box<dyn std::error::Error>> {
|
||||||
|
// Show the loading dialog immediately
|
||||||
|
app_state.show_loading_dialog("Logging In", "Please wait...");
|
||||||
|
// Set the flag in LoginState to indicate the actual save should run next loop
|
||||||
|
login_state.login_request_pending = true;
|
||||||
|
// Return immediately to allow redraw
|
||||||
|
Ok(EventOutcome::Ok("Login initiated.".to_string()))
|
||||||
|
}
|
||||||
|
// --- End of new function ---
|
||||||
|
|
||||||
|
|
||||||
/// Reverts the login form fields to empty and returns to the previous screen (Intro).
|
/// Reverts the login form fields to empty and returns to the previous screen (Intro).
|
||||||
pub async fn revert(
|
pub async fn revert(
|
||||||
login_state: &mut LoginState,
|
login_state: &mut LoginState,
|
||||||
app_state: &mut AppState,
|
_app_state: &mut AppState, // Keep signature consistent if needed elsewhere
|
||||||
) -> String {
|
) -> String {
|
||||||
// Clear the input fields
|
// Clear the input fields
|
||||||
login_state.username.clear();
|
login_state.username.clear();
|
||||||
login_state.password.clear();
|
login_state.password.clear();
|
||||||
login_state.error_message = None;
|
login_state.error_message = None;
|
||||||
login_state.set_has_unsaved_changes(false);
|
login_state.set_has_unsaved_changes(false);
|
||||||
|
login_state.login_request_pending = false; // Ensure flag is reset on revert
|
||||||
|
|
||||||
"Login reverted".to_string()
|
"Login reverted".to_string()
|
||||||
}
|
}
|
||||||
@@ -95,9 +114,10 @@ pub async fn back_to_main(
|
|||||||
login_state.password.clear();
|
login_state.password.clear();
|
||||||
login_state.error_message = None;
|
login_state.error_message = None;
|
||||||
login_state.set_has_unsaved_changes(false);
|
login_state.set_has_unsaved_changes(false);
|
||||||
|
login_state.login_request_pending = false; // Ensure flag is reset
|
||||||
|
|
||||||
// Ensure dialog is hidden if revert is called
|
// Ensure dialog is hidden if revert is called
|
||||||
app_state.hide_dialog(); // Uncomment if needed
|
app_state.hide_dialog();
|
||||||
|
|
||||||
// Navigation logic
|
// Navigation logic
|
||||||
buffer_state.close_active_buffer();
|
buffer_state.close_active_buffer();
|
||||||
@@ -109,3 +129,4 @@ pub async fn back_to_main(
|
|||||||
|
|
||||||
"Returned to main menu".to_string()
|
"Returned to main menu".to_string()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -15,6 +15,7 @@ pub enum DialogPurpose {
|
|||||||
LoginFailed,
|
LoginFailed,
|
||||||
RegisterSuccess,
|
RegisterSuccess,
|
||||||
RegisterFailed,
|
RegisterFailed,
|
||||||
|
ConfirmDeleteColumns, // add_table delete selected Columns
|
||||||
// TODO in the future:
|
// TODO in the future:
|
||||||
// ConfirmQuit,
|
// ConfirmQuit,
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -104,6 +104,8 @@ pub fn render_ui(
|
|||||||
theme,
|
theme,
|
||||||
app_state,
|
app_state,
|
||||||
&mut admin_state.add_table_state,
|
&mut admin_state.add_table_state,
|
||||||
|
login_state.current_field < 3,
|
||||||
|
highlight_state,
|
||||||
);
|
);
|
||||||
} else if app_state.ui.show_login {
|
} else if app_state.ui.show_login {
|
||||||
render_login(
|
render_login(
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
use crate::config::binds::config::Config;
|
use crate::config::binds::config::Config;
|
||||||
use crate::config::colors::themes::Theme;
|
use crate::config::colors::themes::Theme;
|
||||||
use crate::services::grpc_client::GrpcClient;
|
use crate::services::grpc_client::GrpcClient;
|
||||||
|
use crate::services::auth::AuthClient; // <-- Add AuthClient import
|
||||||
use crate::services::ui_service::UiService;
|
use crate::services::ui_service::UiService;
|
||||||
use crate::modes::common::commands::CommandHandler;
|
use crate::modes::common::commands::CommandHandler;
|
||||||
use crate::modes::handlers::event::{EventHandler, EventOutcome};
|
use crate::modes::handlers::event::{EventHandler, EventOutcome};
|
||||||
@@ -17,11 +18,15 @@ use crate::state::pages::intro::IntroState;
|
|||||||
use crate::state::app::buffer::BufferState;
|
use crate::state::app::buffer::BufferState;
|
||||||
use crate::state::app::buffer::AppView;
|
use crate::state::app::buffer::AppView;
|
||||||
use crate::state::app::state::AppState;
|
use crate::state::app::state::AppState;
|
||||||
// Import SaveOutcome
|
use crate::ui::handlers::context::DialogPurpose; // <-- Add DialogPurpose import
|
||||||
|
// Import SaveOutcome
|
||||||
use crate::tui::terminal::{EventReader, TerminalCore};
|
use crate::tui::terminal::{EventReader, TerminalCore};
|
||||||
use crate::ui::handlers::render::render_ui;
|
use crate::ui::handlers::render::render_ui;
|
||||||
|
use crate::tui::functions::common::login; // <-- Add login module import
|
||||||
use std::time::Instant;
|
use std::time::Instant;
|
||||||
use crossterm::cursor::SetCursorStyle;
|
use crossterm::cursor::SetCursorStyle;
|
||||||
|
use crossterm::event as crossterm_event;
|
||||||
|
use tracing::{info, error};
|
||||||
|
|
||||||
pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
|
pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
|
||||||
let config = Config::load()?;
|
let config = Config::load()?;
|
||||||
@@ -57,9 +62,6 @@ pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
let mut current_fps = 0.0;
|
let mut current_fps = 0.0;
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
// Determine edit mode based on EventHandler state
|
|
||||||
let is_edit_mode = event_handler.is_edit_mode;
|
|
||||||
|
|
||||||
// --- Synchronize UI View from Active Buffer ---
|
// --- Synchronize UI View from Active Buffer ---
|
||||||
if let Some(active_view) = buffer_state.get_active_view() {
|
if let Some(active_view) = buffer_state.get_active_view() {
|
||||||
// Reset all flags first
|
// Reset all flags first
|
||||||
@@ -87,6 +89,10 @@ pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
}
|
}
|
||||||
// --- End Synchronization ---
|
// --- End Synchronization ---
|
||||||
|
|
||||||
|
// --- 3. Draw UI ---
|
||||||
|
// Draw the current state *first*. This ensures the loading dialog
|
||||||
|
// set in the *previous* iteration gets rendered before the pending
|
||||||
|
// action check below.
|
||||||
terminal.draw(|f| {
|
terminal.draw(|f| {
|
||||||
render_ui(
|
render_ui(
|
||||||
f,
|
f,
|
||||||
@@ -98,7 +104,7 @@ pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
&mut admin_state,
|
&mut admin_state,
|
||||||
&buffer_state,
|
&buffer_state,
|
||||||
&theme,
|
&theme,
|
||||||
is_edit_mode,
|
event_handler.is_edit_mode, // Use event_handler's state
|
||||||
&event_handler.highlight_state,
|
&event_handler.highlight_state,
|
||||||
app_state.total_count,
|
app_state.total_count,
|
||||||
app_state.current_position,
|
app_state.current_position,
|
||||||
@@ -112,77 +118,108 @@ pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
})?;
|
})?;
|
||||||
|
|
||||||
// --- Cursor Visibility Logic ---
|
// --- Cursor Visibility Logic ---
|
||||||
|
// (Keep existing cursor logic here - depends on state drawn above)
|
||||||
let current_mode = ModeManager::derive_mode(&app_state, &event_handler);
|
let current_mode = ModeManager::derive_mode(&app_state, &event_handler);
|
||||||
match current_mode {
|
match current_mode {
|
||||||
AppMode::Edit => {
|
AppMode::Edit => { terminal.show_cursor()?; }
|
||||||
terminal.show_cursor()?;
|
AppMode::Highlight => { terminal.set_cursor_style(SetCursorStyle::SteadyBlock)?; terminal.show_cursor()?; }
|
||||||
}
|
|
||||||
AppMode::Highlight => {
|
|
||||||
terminal.set_cursor_style(SetCursorStyle::SteadyBlock)?;
|
|
||||||
terminal.show_cursor()?;
|
|
||||||
}
|
|
||||||
AppMode::ReadOnly => {
|
AppMode::ReadOnly => {
|
||||||
if !app_state.ui.focus_outside_canvas {
|
if !app_state.ui.focus_outside_canvas { terminal.set_cursor_style(SetCursorStyle::SteadyBlock)?; }
|
||||||
terminal.set_cursor_style(SetCursorStyle::SteadyBlock)?;
|
else { terminal.set_cursor_style(SetCursorStyle::SteadyUnderScore)?; }
|
||||||
} else {
|
|
||||||
terminal.set_cursor_style(SetCursorStyle::SteadyUnderScore)?;
|
|
||||||
}
|
|
||||||
terminal.show_cursor()?;
|
terminal.show_cursor()?;
|
||||||
}
|
}
|
||||||
AppMode::General => {
|
AppMode::General => {
|
||||||
if app_state.ui.focus_outside_canvas {
|
if app_state.ui.focus_outside_canvas { terminal.set_cursor_style(SetCursorStyle::SteadyUnderScore)?; terminal.show_cursor()?; }
|
||||||
terminal.set_cursor_style(SetCursorStyle::SteadyUnderScore)?;
|
else { terminal.hide_cursor()?; }
|
||||||
terminal.show_cursor()?;
|
|
||||||
} else {
|
|
||||||
terminal.hide_cursor()?;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
AppMode::Command => {
|
|
||||||
terminal.set_cursor_style(SetCursorStyle::SteadyUnderScore)?;
|
|
||||||
terminal.show_cursor()?;
|
|
||||||
}
|
}
|
||||||
|
AppMode::Command => { terminal.set_cursor_style(SetCursorStyle::SteadyUnderScore)?; terminal.show_cursor()?; }
|
||||||
}
|
}
|
||||||
// --- End Cursor Visibility Logic ---
|
// --- End Cursor Visibility Logic ---
|
||||||
|
|
||||||
let total_count = app_state.total_count; // Keep track for save logic
|
// --- 2. Check for Pending Login Action ---
|
||||||
|
if login_state.login_request_pending {
|
||||||
|
// Reset the flag *before* calling save
|
||||||
|
login_state.login_request_pending = false;
|
||||||
|
|
||||||
|
// Create AuthClient and call save
|
||||||
|
match AuthClient::new().await {
|
||||||
|
Ok(mut auth_client_instance) => {
|
||||||
|
// Call the ORIGINAL save function from the login module
|
||||||
|
let save_result = login::save(
|
||||||
|
&mut auth_state,
|
||||||
|
&mut login_state,
|
||||||
|
&mut auth_client_instance, // Pass the new client instance
|
||||||
|
&mut app_state,
|
||||||
|
).await;
|
||||||
|
|
||||||
|
// Use tracing for logging the outcome
|
||||||
|
match save_result {
|
||||||
|
// save returns Result<String, Error>, Ok contains the message
|
||||||
|
Ok(msg) => info!(message = %msg, "Login save result"), // Use tracing::info!
|
||||||
|
Err(e) => error!(error = %e, "Error during login save"), // Use tracing::error!
|
||||||
|
}
|
||||||
|
// Note: save already handles showing the final dialog (success/failure)
|
||||||
|
}
|
||||||
|
Err(e) => {
|
||||||
|
// Handle client connection error - show dialog directly
|
||||||
|
// Ensure flag is already false here
|
||||||
|
app_state.show_dialog( // Use show_dialog, not update_dialog_content
|
||||||
|
"Login Failed",
|
||||||
|
&format!("Connection Error: {}", e),
|
||||||
|
vec!["OK".to_string()],
|
||||||
|
DialogPurpose::LoginFailed, // Use appropriate purpose
|
||||||
|
);
|
||||||
|
login_state.error_message = Some(format!("Connection Error: {}", e));
|
||||||
|
error!(error = %e, "Failed to create AuthClient"); // Use tracing::error!
|
||||||
|
}
|
||||||
|
}
|
||||||
|
// After save runs, the state (dialog content, etc.) is updated.
|
||||||
|
// The *next* iteration's draw call will show the final result.
|
||||||
|
} // --- End Pending Login Check ---
|
||||||
|
|
||||||
|
let total_count = app_state.total_count;
|
||||||
let mut current_position = app_state.current_position;
|
let mut current_position = app_state.current_position;
|
||||||
let position_before_event = current_position;
|
let position_before_event = current_position;
|
||||||
|
|
||||||
let event = event_reader.read_event()?;
|
// --- 1. Handle Terminal Events ---
|
||||||
|
let mut event_outcome_result = Ok(EventOutcome::Ok(String::new()));
|
||||||
// Get the outcome from the event handler
|
// Poll for events *after* drawing and checking pending actions
|
||||||
let event_outcome_result = event_handler
|
if crossterm_event::poll(std::time::Duration::from_millis(20))? {
|
||||||
.handle_event(
|
let event = event_reader.read_event()?;
|
||||||
event,
|
event_outcome_result = event_handler
|
||||||
&config,
|
.handle_event(
|
||||||
&mut terminal,
|
event,
|
||||||
&mut grpc_client,
|
&config,
|
||||||
&mut command_handler,
|
&mut terminal,
|
||||||
&mut form_state,
|
&mut grpc_client,
|
||||||
&mut auth_state,
|
&mut command_handler,
|
||||||
&mut login_state,
|
&mut form_state,
|
||||||
&mut register_state,
|
&mut auth_state,
|
||||||
&mut intro_state,
|
&mut login_state,
|
||||||
&mut admin_state,
|
&mut register_state,
|
||||||
&mut buffer_state,
|
&mut intro_state,
|
||||||
&mut app_state,
|
&mut admin_state,
|
||||||
total_count,
|
&mut buffer_state,
|
||||||
&mut current_position,
|
&mut app_state,
|
||||||
)
|
total_count,
|
||||||
.await;
|
&mut current_position,
|
||||||
|
)
|
||||||
|
.await;
|
||||||
|
}
|
||||||
|
|
||||||
// Update position based on handler's modification
|
// Update position based on handler's modification
|
||||||
|
// This happens *after* the event is handled
|
||||||
app_state.current_position = current_position;
|
app_state.current_position = current_position;
|
||||||
|
|
||||||
// --- Centralized Consequence Handling ---
|
// --- Centralized Consequence Handling ---
|
||||||
let mut should_exit = false;
|
let mut should_exit = false;
|
||||||
match event_outcome_result {
|
match event_outcome_result {
|
||||||
// Handle the Result first
|
|
||||||
Ok(outcome) => match outcome {
|
Ok(outcome) => match outcome {
|
||||||
// Handle the Ok variant containing EventOutcome
|
|
||||||
EventOutcome::Ok(message) => {
|
EventOutcome::Ok(message) => {
|
||||||
if !message.is_empty() {
|
if !message.is_empty() {
|
||||||
event_handler.command_message = message;
|
// Update command message only if event handling produced one
|
||||||
|
// Avoid overwriting messages potentially set by pending actions
|
||||||
|
// event_handler.command_message = message;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
EventOutcome::Exit(message) => {
|
EventOutcome::Exit(message) => {
|
||||||
@@ -191,8 +228,6 @@ pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
}
|
}
|
||||||
EventOutcome::DataSaved(save_outcome, message) => {
|
EventOutcome::DataSaved(save_outcome, message) => {
|
||||||
event_handler.command_message = message; // Show save status
|
event_handler.command_message = message; // Show save status
|
||||||
|
|
||||||
// *** Delegate outcome handling to UiService ***
|
|
||||||
if let Err(e) = UiService::handle_save_outcome(
|
if let Err(e) = UiService::handle_save_outcome(
|
||||||
save_outcome,
|
save_outcome,
|
||||||
&mut grpc_client,
|
&mut grpc_client,
|
||||||
@@ -201,30 +236,26 @@ pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
)
|
)
|
||||||
.await
|
.await
|
||||||
{
|
{
|
||||||
// Handle potential errors from the outcome handler itself
|
|
||||||
event_handler.command_message =
|
event_handler.command_message =
|
||||||
format!("Error handling save outcome: {}", e);
|
format!("Error handling save outcome: {}", e);
|
||||||
}
|
}
|
||||||
// No count update needed for UpdatedExisting or NoChange
|
|
||||||
}
|
}
|
||||||
EventOutcome::ButtonSelected { context, index } => {
|
EventOutcome::ButtonSelected { context: _, index: _ } => {
|
||||||
event_handler.command_message = "Internal error: Unexpected button state".to_string();
|
// This case should ideally be fully handled within handle_event
|
||||||
|
// If initiate_login was called, it returned early.
|
||||||
|
// If not, the message was set and returned via Ok(message).
|
||||||
|
// Log if necessary, but likely no action needed here.
|
||||||
|
// log::warn!("ButtonSelected outcome reached main loop unexpectedly.");
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
Err(e) => {
|
Err(e) => {
|
||||||
// Handle errors from handle_event, e.g., log or display
|
|
||||||
event_handler.command_message = format!("Error: {}", e);
|
event_handler.command_message = format!("Error: {}", e);
|
||||||
// Decide if the error is fatal, maybe set should_exit = true;
|
|
||||||
}
|
}
|
||||||
}
|
} // --- End Consequence Handling ---
|
||||||
|
|
||||||
// --- Position Change Handling (after outcome processing) ---
|
// --- Position Change Handling (after outcome processing and pending actions) ---
|
||||||
let position_changed =
|
let position_changed = app_state.current_position != position_before_event;
|
||||||
app_state.current_position != position_before_event; // Calculate after potential update
|
|
||||||
// Recalculate total_count *after* potential update
|
|
||||||
let current_total_count = app_state.total_count;
|
let current_total_count = app_state.total_count;
|
||||||
|
|
||||||
// Handle position changes and update form state (Only when form is shown)
|
|
||||||
if app_state.ui.show_form {
|
if app_state.ui.show_form {
|
||||||
if position_changed && !event_handler.is_edit_mode {
|
if position_changed && !event_handler.is_edit_mode {
|
||||||
let current_input = form_state.get_current_input();
|
let current_input = form_state.get_current_input();
|
||||||
@@ -295,7 +326,7 @@ pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
event_handler.ideal_cursor_column.min(max_cursor_pos);
|
event_handler.ideal_cursor_column.min(max_cursor_pos);
|
||||||
}
|
}
|
||||||
} else if app_state.ui.show_register {
|
} else if app_state.ui.show_register {
|
||||||
if !event_handler.is_edit_mode {
|
if !event_handler.is_edit_mode {
|
||||||
let current_input = register_state.get_current_input();
|
let current_input = register_state.get_current_input();
|
||||||
let max_cursor_pos = if !current_input.is_empty() {
|
let max_cursor_pos = if !current_input.is_empty() {
|
||||||
current_input.len() - 1
|
current_input.len() - 1
|
||||||
@@ -305,7 +336,7 @@ pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
register_state.current_cursor_pos = event_handler.ideal_cursor_column.min(max_cursor_pos);
|
register_state.current_cursor_pos = event_handler.ideal_cursor_column.min(max_cursor_pos);
|
||||||
}
|
}
|
||||||
} else if app_state.ui.show_login {
|
} else if app_state.ui.show_login {
|
||||||
if !event_handler.is_edit_mode {
|
if !event_handler.is_edit_mode {
|
||||||
let current_input = login_state.get_current_input();
|
let current_input = login_state.get_current_input();
|
||||||
let max_cursor_pos = if !current_input.is_empty() {
|
let max_cursor_pos = if !current_input.is_empty() {
|
||||||
current_input.len() - 1
|
current_input.len() - 1
|
||||||
@@ -315,10 +346,10 @@ pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
login_state.current_cursor_pos = event_handler.ideal_cursor_column.min(max_cursor_pos);
|
login_state.current_cursor_pos = event_handler.ideal_cursor_column.min(max_cursor_pos);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// --- End Position Change Handling ---
|
||||||
|
|
||||||
// Check exit condition *after* processing outcome
|
// Check exit condition *after* all processing for the iteration
|
||||||
if should_exit {
|
if should_exit {
|
||||||
// terminal.cleanup()?; // Optional: Drop handles this
|
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -329,6 +360,6 @@ pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
|
|||||||
if frame_duration.as_secs_f64() > 1e-6 {
|
if frame_duration.as_secs_f64() > 1e-6 {
|
||||||
current_fps = 1.0 / frame_duration.as_secs_f64();
|
current_fps = 1.0 / frame_duration.as_secs_f64();
|
||||||
}
|
}
|
||||||
}
|
} // End main loop
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user