Compare commits
46 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
a0757efe8b | ||
|
|
10f4b9d8e2 | ||
|
|
42db496ad7 | ||
|
|
d6fd672409 | ||
|
|
60eb1c9f51 | ||
|
|
a09c804595 | ||
|
|
a17f73fd54 | ||
|
|
2373ae4b8c | ||
|
|
16dd460469 | ||
|
|
58f109ca91 | ||
|
|
75da9c0f4b | ||
|
|
833b918c5b | ||
|
|
72c2691a17 | ||
|
|
cf79bc7bd5 | ||
|
|
f5f2f2cdef | ||
|
|
19a9bab8c2 | ||
|
|
6e221ef8c1 | ||
|
|
e142f56706 | ||
|
|
a794f22366 | ||
|
|
cfe4903c79 | ||
|
|
a0a473f96c | ||
|
|
9e4dd3b4c7 | ||
|
|
e5db0334c0 | ||
|
|
d641ad1bbb | ||
|
|
18393ff661 | ||
|
|
b2a82fba30 | ||
|
|
f6c2fd627f | ||
|
|
15d9b31cb6 | ||
|
|
06cc1663b3 | ||
|
|
88a4b2d69c | ||
|
|
e6072d25c5 | ||
|
|
fc2b65601e | ||
|
|
597bdde7e1 | ||
|
|
f56092e86c | ||
|
|
d5cfe59f47 | ||
|
|
f281eaa662 | ||
|
|
cbb3ed7c48 | ||
|
|
41a0b85376 | ||
|
|
b5a31ee81c | ||
|
|
dceb031822 | ||
|
|
78bc9fc432 | ||
|
|
b9072e4d7c | ||
|
|
5d97e63f93 | ||
|
|
957f5bf9f0 | ||
|
|
6833ac5fad | ||
|
|
3dff2ced6c |
@@ -7,16 +7,14 @@ previous_buffer = ["space+b+p"]
|
||||
close_buffer = ["space+b+d"]
|
||||
|
||||
[keybindings.general]
|
||||
move_up = ["k", "Up"]
|
||||
move_down = ["j", "Down"]
|
||||
next_option = ["l", "Right"]
|
||||
previous_option = ["h", "Left"]
|
||||
up = ["k", "Up"]
|
||||
down = ["j", "Down"]
|
||||
left = ["h", "Left"]
|
||||
right = ["l", "Right"]
|
||||
next = ["Tab"]
|
||||
previous = ["Shift+Tab"]
|
||||
select = ["Enter"]
|
||||
toggle_sidebar = ["ctrl+t"]
|
||||
toggle_buffer_list = ["ctrl+b"]
|
||||
next_field = ["Tab"]
|
||||
prev_field = ["Shift+Tab"]
|
||||
exit_table_scroll = ["esc"]
|
||||
esc = ["esc"]
|
||||
open_search = ["ctrl+f"]
|
||||
|
||||
[keybindings.common]
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
// src/components/common/find_file_palette.rs
|
||||
// src/bottom_panel/find_file_palette.rs
|
||||
|
||||
use crate::config::colors::themes::Theme;
|
||||
use crate::modes::general::command_navigation::NavigationState; // Corrected path
|
||||
98
client/src/bottom_panel/layout.rs
Normal file
98
client/src/bottom_panel/layout.rs
Normal file
@@ -0,0 +1,98 @@
|
||||
// src/bottom_panel/layout.rs
|
||||
|
||||
use ratatui::{layout::Constraint, layout::Rect, Frame};
|
||||
use crate::bottom_panel::{status_line::render_status_line, command_line::render_command_line};
|
||||
use crate::bottom_panel::find_file_palette;
|
||||
use crate::config::colors::themes::Theme;
|
||||
use crate::modes::general::command_navigation::NavigationState;
|
||||
use crate::state::app::state::AppState;
|
||||
use crate::pages::routing::Router;
|
||||
|
||||
/// Calculate the layout constraints for the bottom panel (status line + command line/palette).
|
||||
pub fn bottom_panel_constraints(
|
||||
app_state: &AppState,
|
||||
navigation_state: &NavigationState,
|
||||
event_handler_command_mode_active: bool,
|
||||
) -> Vec<Constraint> {
|
||||
let mut status_line_height = 1;
|
||||
#[cfg(feature = "ui-debug")]
|
||||
{
|
||||
if let Some(debug_state) = &app_state.debug_state {
|
||||
if debug_state.is_error {
|
||||
status_line_height = 4;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const PALETTE_OPTIONS_HEIGHT_FOR_LAYOUT: u16 = 15;
|
||||
let command_palette_area_height = if navigation_state.active {
|
||||
1 + PALETTE_OPTIONS_HEIGHT_FOR_LAYOUT
|
||||
} else if event_handler_command_mode_active {
|
||||
1
|
||||
} else {
|
||||
0
|
||||
};
|
||||
|
||||
let mut constraints = vec![Constraint::Length(status_line_height)];
|
||||
if command_palette_area_height > 0 {
|
||||
constraints.push(Constraint::Length(command_palette_area_height));
|
||||
}
|
||||
constraints
|
||||
}
|
||||
|
||||
/// Render the bottom panel (status line + command line/palette).
|
||||
pub fn render_bottom_panel(
|
||||
f: &mut Frame,
|
||||
root_chunks: &[Rect],
|
||||
chunk_idx: &mut usize,
|
||||
current_dir: &str,
|
||||
theme: &Theme,
|
||||
current_fps: f64,
|
||||
app_state: &AppState,
|
||||
router: &Router,
|
||||
navigation_state: &NavigationState,
|
||||
event_handler_command_input: &str,
|
||||
event_handler_command_mode_active: bool,
|
||||
event_handler_command_message: &str,
|
||||
) {
|
||||
// --- Status line area ---
|
||||
let status_line_area = root_chunks[*chunk_idx];
|
||||
*chunk_idx += 1;
|
||||
|
||||
// --- Command line / palette area ---
|
||||
let command_render_area = if root_chunks.len() > *chunk_idx {
|
||||
Some(root_chunks[*chunk_idx])
|
||||
} else {
|
||||
None
|
||||
};
|
||||
if command_render_area.is_some() {
|
||||
*chunk_idx += 1;
|
||||
}
|
||||
|
||||
// --- Render status line ---
|
||||
render_status_line(
|
||||
f,
|
||||
status_line_area,
|
||||
current_dir,
|
||||
theme,
|
||||
current_fps,
|
||||
app_state,
|
||||
router,
|
||||
);
|
||||
|
||||
// --- Render command line or palette ---
|
||||
if let Some(area) = command_render_area {
|
||||
if navigation_state.active {
|
||||
find_file_palette::render_find_file_palette(f, area, theme, navigation_state);
|
||||
} else if event_handler_command_mode_active {
|
||||
render_command_line(
|
||||
f,
|
||||
area,
|
||||
event_handler_command_input,
|
||||
true,
|
||||
theme,
|
||||
event_handler_command_message,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
6
client/src/bottom_panel/mod.rs
Normal file
6
client/src/bottom_panel/mod.rs
Normal file
@@ -0,0 +1,6 @@
|
||||
// src/bottom_panel/mod.rs
|
||||
|
||||
pub mod status_line;
|
||||
pub mod command_line;
|
||||
pub mod layout;
|
||||
pub mod find_file_palette;
|
||||
@@ -4,10 +4,12 @@ use crate::state::app::state::AppState;
|
||||
use ratatui::{
|
||||
layout::Rect,
|
||||
style::Style,
|
||||
text::{Line, Span},
|
||||
widgets::Paragraph,
|
||||
text::{Line, Span, Text},
|
||||
widgets::{Paragraph, Wrap},
|
||||
Frame,
|
||||
};
|
||||
use crate::pages::routing::Page;
|
||||
use crate::pages::routing::Router;
|
||||
use std::path::Path;
|
||||
use unicode_width::UnicodeWidthStr;
|
||||
|
||||
@@ -16,9 +18,9 @@ pub fn render_status_line(
|
||||
area: Rect,
|
||||
current_dir: &str,
|
||||
theme: &Theme,
|
||||
is_edit_mode: bool,
|
||||
current_fps: f64,
|
||||
app_state: &AppState,
|
||||
router: &Router,
|
||||
) {
|
||||
#[cfg(feature = "ui-debug")]
|
||||
{
|
||||
@@ -48,7 +50,20 @@ pub fn render_status_line(
|
||||
|
||||
// --- The normal status line rendering logic (unchanged) ---
|
||||
let program_info = format!("komp_ac v{}", env!("CARGO_PKG_VERSION"));
|
||||
let mode_text = if is_edit_mode { "[EDIT]" } else { "[READ-ONLY]" };
|
||||
let mode_text = if let Page::Form(path) = &router.current {
|
||||
if let Some(editor) = app_state.editor_for_path_ref(path) {
|
||||
match editor.mode() {
|
||||
canvas::AppMode::Edit => "[EDIT]",
|
||||
canvas::AppMode::ReadOnly => "[READ-ONLY]",
|
||||
canvas::AppMode::Highlight => "[VISUAL]",
|
||||
_ => "",
|
||||
}
|
||||
} else {
|
||||
""
|
||||
}
|
||||
} else {
|
||||
"" // No canvas active
|
||||
};
|
||||
|
||||
let home_dir = dirs::home_dir()
|
||||
.map(|p| p.to_string_lossy().into_owned())
|
||||
@@ -7,7 +7,7 @@ pub fn get_view_layer(view: &AppView) -> u8 {
|
||||
match view {
|
||||
AppView::Intro => 1,
|
||||
AppView::Login | AppView::Register | AppView::Admin | AppView::AddTable | AppView::AddLogic => 2,
|
||||
AppView::Form | AppView::Scratch => 3,
|
||||
AppView::Form(_) | AppView::Scratch => 3,
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -8,7 +8,7 @@ pub enum AppView {
|
||||
Admin,
|
||||
AddTable,
|
||||
AddLogic,
|
||||
Form,
|
||||
Form(String),
|
||||
Scratch,
|
||||
}
|
||||
|
||||
@@ -23,7 +23,7 @@ impl AppView {
|
||||
AppView::Admin => "Admin_Panel",
|
||||
AppView::AddTable => "Add_Table",
|
||||
AppView::AddLogic => "Add_Logic",
|
||||
AppView::Form => "Form",
|
||||
AppView::Form(_) => "Form",
|
||||
AppView::Scratch => "*scratch*",
|
||||
}
|
||||
}
|
||||
@@ -31,10 +31,14 @@ impl AppView {
|
||||
/// Returns the display name with dynamic context (for Form buffers)
|
||||
pub fn display_name_with_context(&self, current_table_name: Option<&str>) -> String {
|
||||
match self {
|
||||
AppView::Form => {
|
||||
current_table_name
|
||||
.unwrap_or("Data Form")
|
||||
.to_string()
|
||||
AppView::Form(path) => {
|
||||
// Derive table name from "profile/table" path
|
||||
let table = path.split('/').nth(1).unwrap_or("");
|
||||
if !table.is_empty() {
|
||||
table.to_string()
|
||||
} else {
|
||||
current_table_name.unwrap_or("Data Form").to_string()
|
||||
}
|
||||
}
|
||||
_ => self.display_name().to_string(),
|
||||
}
|
||||
|
||||
@@ -1,10 +0,0 @@
|
||||
// src/components/admin.rs
|
||||
pub mod admin_panel;
|
||||
pub mod admin_panel_admin;
|
||||
pub mod add_table;
|
||||
pub mod add_logic;
|
||||
|
||||
pub use admin_panel::*;
|
||||
pub use admin_panel_admin::*;
|
||||
pub use add_table::*;
|
||||
pub use add_logic::*;
|
||||
@@ -1,6 +0,0 @@
|
||||
// src/components/form.rs
|
||||
pub mod login;
|
||||
pub mod register;
|
||||
|
||||
pub use login::*;
|
||||
pub use register::*;
|
||||
@@ -1,16 +1,9 @@
|
||||
// src/components/common.rs
|
||||
pub mod command_line;
|
||||
pub mod status_line;
|
||||
|
||||
pub mod text_editor;
|
||||
pub mod background;
|
||||
pub mod dialog;
|
||||
pub mod autocomplete;
|
||||
pub mod find_file_palette;
|
||||
|
||||
pub use command_line::*;
|
||||
pub use status_line::*;
|
||||
pub use text_editor::*;
|
||||
pub use background::*;
|
||||
pub use dialog::*;
|
||||
pub use autocomplete::*;
|
||||
pub use find_file_palette::*;
|
||||
|
||||
@@ -1,8 +1,8 @@
|
||||
// src/components/common/autocomplete.rs
|
||||
|
||||
use crate::config::colors::themes::Theme;
|
||||
use crate::state::pages::form::FormState;
|
||||
use common::proto::komp_ac::search::search_response::Hit;
|
||||
use crate::pages::forms::FormState;
|
||||
use ratatui::{
|
||||
layout::Rect,
|
||||
style::{Color, Modifier, Style},
|
||||
|
||||
@@ -1,4 +0,0 @@
|
||||
// src/components/form.rs
|
||||
pub mod form;
|
||||
|
||||
pub use form::*;
|
||||
@@ -1,4 +0,0 @@
|
||||
// src/components/intro.rs
|
||||
pub mod intro;
|
||||
|
||||
pub use intro::*;
|
||||
@@ -1,14 +1,7 @@
|
||||
// src/components/mod.rs
|
||||
pub mod intro;
|
||||
pub mod admin;
|
||||
|
||||
pub mod common;
|
||||
pub mod form;
|
||||
pub mod auth;
|
||||
pub mod utils;
|
||||
|
||||
pub use intro::*;
|
||||
pub use admin::*;
|
||||
pub use common::*;
|
||||
pub use form::*;
|
||||
pub use auth::*;
|
||||
pub use utils::*;
|
||||
|
||||
@@ -148,19 +148,17 @@ impl Config {
|
||||
/// Context-aware keybinding resolution
|
||||
pub fn get_action_for_current_context(
|
||||
&self,
|
||||
is_edit_mode: bool,
|
||||
command_mode: bool,
|
||||
key: KeyCode,
|
||||
modifiers: KeyModifiers
|
||||
) -> Option<&str> {
|
||||
match (command_mode, is_edit_mode) {
|
||||
(true, _) => self.get_command_action_for_key(key, modifiers),
|
||||
(_, true) => self.get_edit_action_for_key(key, modifiers)
|
||||
.or_else(|| self.get_common_action(key, modifiers)),
|
||||
_ => self.get_read_only_action_for_key(key, modifiers)
|
||||
if command_mode {
|
||||
self.get_command_action_for_key(key, modifiers)
|
||||
} else {
|
||||
// fallback: read-only + common + global
|
||||
self.get_read_only_action_for_key(key, modifiers)
|
||||
.or_else(|| self.get_common_action(key, modifiers))
|
||||
// Add global bindings check for read-only mode
|
||||
.or_else(|| self.get_action_for_key_in_mode(&self.keybindings.global, key, modifiers)),
|
||||
.or_else(|| self.get_action_for_key_in_mode(&self.keybindings.global, key, modifiers))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
85
client/src/dialog/functions.rs
Normal file
85
client/src/dialog/functions.rs
Normal file
@@ -0,0 +1,85 @@
|
||||
// src/dialog/functions.rs
|
||||
|
||||
use crate::dialog::DialogState;
|
||||
use crate::state::app::state::AppState;
|
||||
use crate::ui::handlers::context::DialogPurpose;
|
||||
|
||||
impl AppState {
|
||||
pub fn show_dialog(
|
||||
&mut self,
|
||||
title: &str,
|
||||
message: &str,
|
||||
buttons: Vec<String>,
|
||||
purpose: DialogPurpose,
|
||||
) {
|
||||
self.ui.dialog.dialog_title = title.to_string();
|
||||
self.ui.dialog.dialog_message = message.to_string();
|
||||
self.ui.dialog.dialog_buttons = buttons;
|
||||
self.ui.dialog.dialog_active_button_index = 0;
|
||||
self.ui.dialog.purpose = Some(purpose);
|
||||
self.ui.dialog.is_loading = false;
|
||||
self.ui.dialog.dialog_show = true;
|
||||
self.ui.focus_outside_canvas = true;
|
||||
}
|
||||
|
||||
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();
|
||||
self.ui.dialog.dialog_active_button_index = 0;
|
||||
self.ui.dialog.purpose = None;
|
||||
self.ui.dialog.is_loading = true;
|
||||
self.ui.dialog.dialog_show = true;
|
||||
self.ui.focus_outside_canvas = true;
|
||||
}
|
||||
|
||||
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;
|
||||
self.ui.dialog.purpose = Some(purpose);
|
||||
self.ui.dialog.is_loading = false;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn hide_dialog(&mut self) {
|
||||
self.ui.dialog.dialog_show = false;
|
||||
self.ui.dialog.dialog_title.clear();
|
||||
self.ui.dialog.dialog_message.clear();
|
||||
self.ui.dialog.dialog_buttons.clear();
|
||||
self.ui.dialog.dialog_active_button_index = 0;
|
||||
self.ui.dialog.purpose = None;
|
||||
self.ui.focus_outside_canvas = false;
|
||||
self.ui.dialog.is_loading = false;
|
||||
}
|
||||
|
||||
pub fn next_dialog_button(&mut self) {
|
||||
if !self.ui.dialog.dialog_buttons.is_empty() {
|
||||
let next_index = (self.ui.dialog.dialog_active_button_index + 1)
|
||||
% self.ui.dialog.dialog_buttons.len();
|
||||
self.ui.dialog.dialog_active_button_index = next_index;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn previous_dialog_button(&mut self) {
|
||||
if !self.ui.dialog.dialog_buttons.is_empty() {
|
||||
let len = self.ui.dialog.dialog_buttons.len();
|
||||
let prev_index =
|
||||
(self.ui.dialog.dialog_active_button_index + len - 1) % len;
|
||||
self.ui.dialog.dialog_active_button_index = prev_index;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_active_dialog_button_label(&self) -> Option<&str> {
|
||||
self.ui.dialog
|
||||
.dialog_buttons
|
||||
.get(self.ui.dialog.dialog_active_button_index)
|
||||
.map(|s| s.as_str())
|
||||
}
|
||||
}
|
||||
207
client/src/dialog/logic.rs
Normal file
207
client/src/dialog/logic.rs
Normal file
@@ -0,0 +1,207 @@
|
||||
// src/dialog/logic.rs
|
||||
|
||||
// TODO(dialog-refactor):
|
||||
// Currently this module (`handle_dialog_event`) contains page-specific logic
|
||||
// (e.g. Login, Register, Admin, SaveTable). This couples the dialog crate
|
||||
// to application pages and business logic.
|
||||
//
|
||||
// Refactor plan:
|
||||
// 1. Keep dialog generic: only handle navigation (next/prev/select) and return
|
||||
// a `DialogResult` (Dismissed | Selected { purpose, index }).
|
||||
// 2. Move all page-specific actions (e.g. login::back_to_main, register::back_to_login,
|
||||
// handle_delete_selected_columns, buffer_state.update_history) into the
|
||||
// respective page or event handler (e.g. modes/handlers/event.rs).
|
||||
// 3. Dialog crate should only provide state, rendering, and generic navigation.
|
||||
// Pages decide what to do when a dialog button is pressed.
|
||||
|
||||
use crossterm::event::{Event, KeyCode};
|
||||
use crate::config::binds::config::Config;
|
||||
use crate::ui::handlers::context::DialogPurpose;
|
||||
use crate::state::app::state::AppState;
|
||||
use crate::buffer::AppView;
|
||||
use crate::buffer::state::BufferState;
|
||||
use crate::modes::handlers::event::EventOutcome;
|
||||
use crate::pages::register;
|
||||
use crate::pages::login;
|
||||
use crate::pages::admin_panel::add_table::logic::handle_delete_selected_columns;
|
||||
use crate::pages::routing::{Router, Page};
|
||||
use anyhow::Result;
|
||||
|
||||
/// Handles key events specifically when a dialog is active.
|
||||
/// Returns Some(Result<EventOutcome, Error>) if the event was handled (consumed),
|
||||
/// otherwise returns None.
|
||||
pub async fn handle_dialog_event(
|
||||
event: &Event,
|
||||
config: &Config,
|
||||
app_state: &mut AppState,
|
||||
buffer_state: &mut BufferState,
|
||||
router: &mut Router,
|
||||
) -> Option<Result<EventOutcome>> {
|
||||
if let Event::Key(key) = event {
|
||||
// Always allow Esc to dismiss
|
||||
if key.code == KeyCode::Esc {
|
||||
app_state.hide_dialog();
|
||||
return Some(Ok(EventOutcome::Ok("Dialog dismissed".to_string())));
|
||||
}
|
||||
|
||||
// Check general bindings for dialog actions
|
||||
if let Some(action) = config.get_general_action(key.code, key.modifiers) {
|
||||
match action {
|
||||
"move_down" | "next_option" => {
|
||||
let current_index = app_state.ui.dialog.dialog_active_button_index;
|
||||
let num_buttons = app_state.ui.dialog.dialog_buttons.len();
|
||||
if num_buttons > 0 && current_index < num_buttons - 1 {
|
||||
app_state.ui.dialog.dialog_active_button_index += 1;
|
||||
}
|
||||
return Some(Ok(EventOutcome::Ok(String::new())));
|
||||
}
|
||||
"move_up" | "previous_option" => {
|
||||
let current_index = app_state.ui.dialog.dialog_active_button_index;
|
||||
if current_index > 0 {
|
||||
app_state.ui.dialog.dialog_active_button_index -= 1;
|
||||
}
|
||||
return Some(Ok(EventOutcome::Ok(String::new())));
|
||||
}
|
||||
"select" => {
|
||||
let selected_index = app_state.ui.dialog.dialog_active_button_index;
|
||||
let purpose = match app_state.ui.dialog.purpose {
|
||||
Some(p) => p,
|
||||
None => {
|
||||
app_state.hide_dialog();
|
||||
return Some(Ok(EventOutcome::Ok(
|
||||
"Internal Error: Dialog context lost".to_string(),
|
||||
)));
|
||||
}
|
||||
};
|
||||
|
||||
// Handle Dialog Actions Directly Here
|
||||
match purpose {
|
||||
DialogPurpose::LoginSuccess => match selected_index {
|
||||
0 => {
|
||||
// "Menu" button selected
|
||||
app_state.hide_dialog();
|
||||
if let Page::Login(state) = &mut router.current {
|
||||
let message =
|
||||
login::back_to_main(state, app_state, buffer_state).await;
|
||||
return Some(Ok(EventOutcome::Ok(message)));
|
||||
}
|
||||
return Some(Ok(EventOutcome::Ok(
|
||||
"Login state not active".to_string(),
|
||||
)));
|
||||
}
|
||||
1 => {
|
||||
app_state.hide_dialog();
|
||||
return Some(Ok(EventOutcome::Ok("Exiting dialog".to_string())));
|
||||
}
|
||||
_ => {
|
||||
app_state.hide_dialog();
|
||||
return Some(Ok(EventOutcome::Ok(
|
||||
"Unknown dialog button selected".to_string(),
|
||||
)));
|
||||
}
|
||||
},
|
||||
DialogPurpose::LoginFailed => match selected_index {
|
||||
0 => {
|
||||
// "OK" button selected
|
||||
app_state.hide_dialog();
|
||||
return Some(Ok(EventOutcome::Ok(
|
||||
"Login failed dialog dismissed".to_string(),
|
||||
)));
|
||||
}
|
||||
_ => {
|
||||
app_state.hide_dialog();
|
||||
return Some(Ok(EventOutcome::Ok(
|
||||
"Unknown dialog button selected".to_string(),
|
||||
)));
|
||||
}
|
||||
},
|
||||
DialogPurpose::RegisterSuccess => match selected_index {
|
||||
0 => {
|
||||
// "OK" button for RegisterSuccess
|
||||
app_state.hide_dialog();
|
||||
if let Page::Register(state) = &mut router.current {
|
||||
let message =
|
||||
register::back_to_login(state, app_state, buffer_state)
|
||||
.await;
|
||||
return Some(Ok(EventOutcome::Ok(message)));
|
||||
}
|
||||
return Some(Ok(EventOutcome::Ok(
|
||||
"Register state not active".to_string(),
|
||||
)));
|
||||
}
|
||||
_ => {
|
||||
app_state.hide_dialog();
|
||||
return Some(Ok(EventOutcome::Ok(
|
||||
"Unknown dialog button selected".to_string(),
|
||||
)));
|
||||
}
|
||||
},
|
||||
DialogPurpose::RegisterFailed => match selected_index {
|
||||
0 => {
|
||||
// "OK" button for RegisterFailed
|
||||
app_state.hide_dialog(); // Just dismiss
|
||||
return Some(Ok(EventOutcome::Ok(
|
||||
"Register failed dialog dismissed".to_string(),
|
||||
)));
|
||||
}
|
||||
_ => {
|
||||
app_state.hide_dialog();
|
||||
return Some(Ok(EventOutcome::Ok(
|
||||
"Unknown dialog button selected".to_string(),
|
||||
)));
|
||||
}
|
||||
},
|
||||
DialogPurpose::ConfirmDeleteColumns => match selected_index {
|
||||
0 => {
|
||||
// "Confirm" button selected
|
||||
if let Page::Admin(state) = &mut router.current {
|
||||
let outcome_message =
|
||||
handle_delete_selected_columns(&mut state.add_table_state);
|
||||
app_state.hide_dialog();
|
||||
return Some(Ok(EventOutcome::Ok(outcome_message)));
|
||||
}
|
||||
return Some(Ok(EventOutcome::Ok(
|
||||
"Admin state not active".to_string(),
|
||||
)));
|
||||
}
|
||||
1 => {
|
||||
// "Cancel" button selected
|
||||
app_state.hide_dialog();
|
||||
return Some(Ok(EventOutcome::Ok("Deletion cancelled.".to_string())));
|
||||
}
|
||||
_ => { /* Handle unexpected index */ }
|
||||
},
|
||||
DialogPurpose::SaveTableSuccess => match selected_index {
|
||||
0 => {
|
||||
// "OK" button selected
|
||||
app_state.hide_dialog();
|
||||
buffer_state.update_history(AppView::Admin); // Navigate back
|
||||
return Some(Ok(EventOutcome::Ok(
|
||||
"Save success dialog dismissed.".to_string(),
|
||||
)));
|
||||
}
|
||||
_ => { /* Handle unexpected index */ }
|
||||
},
|
||||
DialogPurpose::SaveLogicSuccess => match selected_index {
|
||||
0 => {
|
||||
// "OK" button selected
|
||||
app_state.hide_dialog();
|
||||
buffer_state.update_history(AppView::Admin);
|
||||
return Some(Ok(EventOutcome::Ok(
|
||||
"Save success dialog dismissed.".to_string(),
|
||||
)));
|
||||
}
|
||||
_ => { /* Handle unexpected index */ }
|
||||
},
|
||||
}
|
||||
}
|
||||
_ => {} // Ignore other general actions when dialog is shown
|
||||
}
|
||||
}
|
||||
// If it was a key event but not handled above, consume it
|
||||
Some(Ok(EventOutcome::Ok(String::new())))
|
||||
} else {
|
||||
// If it wasn't a key event, consume it too while dialog is active
|
||||
Some(Ok(EventOutcome::Ok(String::new())))
|
||||
}
|
||||
}
|
||||
10
client/src/dialog/mod.rs
Normal file
10
client/src/dialog/mod.rs
Normal file
@@ -0,0 +1,10 @@
|
||||
// src/dialog/mod.rs
|
||||
|
||||
pub mod ui;
|
||||
pub mod logic;
|
||||
pub mod state;
|
||||
pub mod functions;
|
||||
|
||||
pub use ui::render_dialog;
|
||||
pub use logic::handle_dialog_event;
|
||||
pub use state::DialogState;
|
||||
26
client/src/dialog/state.rs
Normal file
26
client/src/dialog/state.rs
Normal file
@@ -0,0 +1,26 @@
|
||||
// src/dialog/state.rs
|
||||
use crate::ui::handlers::context::DialogPurpose;
|
||||
|
||||
pub struct DialogState {
|
||||
pub dialog_show: bool,
|
||||
pub dialog_title: String,
|
||||
pub dialog_message: String,
|
||||
pub dialog_buttons: Vec<String>,
|
||||
pub dialog_active_button_index: usize,
|
||||
pub purpose: Option<DialogPurpose>,
|
||||
pub is_loading: bool,
|
||||
}
|
||||
|
||||
impl Default for DialogState {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
dialog_show: false,
|
||||
dialog_title: String::new(),
|
||||
dialog_message: String::new(),
|
||||
dialog_buttons: Vec::new(),
|
||||
dialog_active_button_index: 0,
|
||||
purpose: None,
|
||||
is_loading: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,3 +1,5 @@
|
||||
// src/dialog/ui.rs
|
||||
|
||||
use crate::config::colors::themes::Theme;
|
||||
use ratatui::{
|
||||
layout::{Constraint, Direction, Layout, Margin, Rect},
|
||||
@@ -1,5 +0,0 @@
|
||||
// src/functions/mod.rs
|
||||
|
||||
pub mod modes;
|
||||
|
||||
pub use modes::*;
|
||||
@@ -1,5 +0,0 @@
|
||||
// src/functions/modes.rs
|
||||
|
||||
pub mod navigation;
|
||||
|
||||
pub use navigation::*;
|
||||
@@ -1,5 +0,0 @@
|
||||
// src/functions/modes/navigation.rs
|
||||
|
||||
pub mod admin_nav;
|
||||
pub mod add_table_nav;
|
||||
pub mod add_logic_nav;
|
||||
@@ -1,439 +0,0 @@
|
||||
// src/functions/modes/navigation/add_logic_nav.rs
|
||||
use crate::config::binds::config::{Config, EditorKeybindingMode};
|
||||
use crate::state::{
|
||||
app::state::AppState,
|
||||
pages::add_logic::{AddLogicFocus, AddLogicState},
|
||||
};
|
||||
use crate::buffer::{AppView, BufferState};
|
||||
use crossterm::event::{KeyEvent, KeyCode, KeyModifiers};
|
||||
use crate::services::GrpcClient;
|
||||
use tokio::sync::mpsc;
|
||||
use anyhow::Result;
|
||||
use crate::components::common::text_editor::TextEditor;
|
||||
use crate::services::ui_service::UiService;
|
||||
use tui_textarea::CursorMove; // Ensure this import is present
|
||||
|
||||
pub type SaveLogicResultSender = mpsc::Sender<Result<String>>;
|
||||
|
||||
pub fn handle_add_logic_navigation(
|
||||
key_event: KeyEvent,
|
||||
config: &Config,
|
||||
app_state: &mut AppState,
|
||||
add_logic_state: &mut AddLogicState,
|
||||
is_edit_mode: &mut bool,
|
||||
buffer_state: &mut BufferState,
|
||||
grpc_client: GrpcClient,
|
||||
_save_logic_sender: SaveLogicResultSender, // Marked as unused
|
||||
command_message: &mut String,
|
||||
) -> bool {
|
||||
// === FULLSCREEN SCRIPT EDITING - COMPLETE ISOLATION ===
|
||||
if add_logic_state.current_focus == AddLogicFocus::InsideScriptContent {
|
||||
// === AUTOCOMPLETE HANDLING ===
|
||||
if add_logic_state.script_editor_autocomplete_active {
|
||||
match key_event.code {
|
||||
// ... (Char, Backspace, Tab, Down, Up cases remain the same) ...
|
||||
KeyCode::Char(c) if c.is_alphanumeric() || c == '_' => {
|
||||
add_logic_state.script_editor_filter_text.push(c);
|
||||
add_logic_state.update_script_editor_suggestions();
|
||||
{
|
||||
let mut editor_borrow = add_logic_state.script_content_editor.borrow_mut();
|
||||
TextEditor::handle_input(
|
||||
&mut editor_borrow,
|
||||
key_event,
|
||||
&add_logic_state.editor_keybinding_mode,
|
||||
&mut add_logic_state.vim_state,
|
||||
);
|
||||
}
|
||||
*command_message = format!("Filtering: @{}", add_logic_state.script_editor_filter_text);
|
||||
return true;
|
||||
}
|
||||
KeyCode::Backspace => {
|
||||
if !add_logic_state.script_editor_filter_text.is_empty() {
|
||||
add_logic_state.script_editor_filter_text.pop();
|
||||
add_logic_state.update_script_editor_suggestions();
|
||||
{
|
||||
let mut editor_borrow = add_logic_state.script_content_editor.borrow_mut();
|
||||
TextEditor::handle_input(
|
||||
&mut editor_borrow,
|
||||
key_event,
|
||||
&add_logic_state.editor_keybinding_mode,
|
||||
&mut add_logic_state.vim_state,
|
||||
);
|
||||
}
|
||||
*command_message = if add_logic_state.script_editor_filter_text.is_empty() {
|
||||
"Autocomplete: @".to_string()
|
||||
} else {
|
||||
format!("Filtering: @{}", add_logic_state.script_editor_filter_text)
|
||||
};
|
||||
} else {
|
||||
let should_deactivate = if let Some((trigger_line, trigger_col)) = add_logic_state.script_editor_trigger_position {
|
||||
let current_cursor = {
|
||||
let editor_borrow = add_logic_state.script_content_editor.borrow();
|
||||
editor_borrow.cursor()
|
||||
};
|
||||
current_cursor.0 == trigger_line && current_cursor.1 == trigger_col + 1
|
||||
} else {
|
||||
false
|
||||
};
|
||||
if should_deactivate {
|
||||
add_logic_state.deactivate_script_editor_autocomplete();
|
||||
*command_message = "Autocomplete cancelled".to_string();
|
||||
}
|
||||
{
|
||||
let mut editor_borrow = add_logic_state.script_content_editor.borrow_mut();
|
||||
TextEditor::handle_input(
|
||||
&mut editor_borrow,
|
||||
key_event,
|
||||
&add_logic_state.editor_keybinding_mode,
|
||||
&mut add_logic_state.vim_state,
|
||||
);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
KeyCode::Tab | KeyCode::Down => {
|
||||
if !add_logic_state.script_editor_suggestions.is_empty() {
|
||||
let current = add_logic_state.script_editor_selected_suggestion_index.unwrap_or(0);
|
||||
let next = (current + 1) % add_logic_state.script_editor_suggestions.len();
|
||||
add_logic_state.script_editor_selected_suggestion_index = Some(next);
|
||||
*command_message = format!("Selected: {}", add_logic_state.script_editor_suggestions[next]);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
KeyCode::Up => {
|
||||
if !add_logic_state.script_editor_suggestions.is_empty() {
|
||||
let current = add_logic_state.script_editor_selected_suggestion_index.unwrap_or(0);
|
||||
let prev = if current == 0 {
|
||||
add_logic_state.script_editor_suggestions.len() - 1
|
||||
} else {
|
||||
current - 1
|
||||
};
|
||||
add_logic_state.script_editor_selected_suggestion_index = Some(prev);
|
||||
*command_message = format!("Selected: {}", add_logic_state.script_editor_suggestions[prev]);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
KeyCode::Enter => {
|
||||
if let Some(selected_idx) = add_logic_state.script_editor_selected_suggestion_index {
|
||||
if let Some(suggestion) = add_logic_state.script_editor_suggestions.get(selected_idx).cloned() {
|
||||
let trigger_pos = add_logic_state.script_editor_trigger_position;
|
||||
let filter_len = add_logic_state.script_editor_filter_text.len();
|
||||
|
||||
add_logic_state.deactivate_script_editor_autocomplete();
|
||||
add_logic_state.has_unsaved_changes = true;
|
||||
|
||||
if let Some(pos) = trigger_pos {
|
||||
let mut editor_borrow = add_logic_state.script_content_editor.borrow_mut();
|
||||
|
||||
if suggestion == "sql" {
|
||||
replace_autocomplete_text(&mut editor_borrow, pos, filter_len, "sql");
|
||||
editor_borrow.insert_str("('')");
|
||||
// Move cursor back twice to be between the single quotes
|
||||
editor_borrow.move_cursor(CursorMove::Back); // Before ')'
|
||||
editor_borrow.move_cursor(CursorMove::Back); // Before ''' (inside '')
|
||||
*command_message = "Inserted: @sql('')".to_string();
|
||||
} else {
|
||||
let is_table_selection = add_logic_state.is_table_name_suggestion(&suggestion);
|
||||
replace_autocomplete_text(&mut editor_borrow, pos, filter_len, &suggestion);
|
||||
|
||||
if is_table_selection {
|
||||
editor_borrow.insert_str(".");
|
||||
let new_cursor = editor_borrow.cursor();
|
||||
drop(editor_borrow); // Release borrow before calling add_logic_state methods
|
||||
|
||||
add_logic_state.script_editor_trigger_position = Some(new_cursor);
|
||||
add_logic_state.script_editor_autocomplete_active = true;
|
||||
add_logic_state.script_editor_filter_text.clear();
|
||||
add_logic_state.trigger_column_autocomplete_for_table(suggestion.clone());
|
||||
|
||||
let profile_name = add_logic_state.profile_name.clone();
|
||||
let table_name_for_fetch = suggestion.clone();
|
||||
let mut client_clone = grpc_client.clone();
|
||||
tokio::spawn(async move {
|
||||
match UiService::fetch_columns_for_table(&mut client_clone, &profile_name, &table_name_for_fetch).await {
|
||||
Ok(_columns) => {
|
||||
// Result handled by main UI loop
|
||||
}
|
||||
Err(e) => {
|
||||
tracing::error!("Failed to fetch columns for {}.{}: {}", profile_name, table_name_for_fetch, e);
|
||||
}
|
||||
}
|
||||
});
|
||||
*command_message = format!("Selected table '{}', fetching columns...", suggestion);
|
||||
} else {
|
||||
*command_message = format!("Inserted: {}", suggestion);
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
add_logic_state.deactivate_script_editor_autocomplete();
|
||||
{
|
||||
let mut editor_borrow = add_logic_state.script_content_editor.borrow_mut();
|
||||
TextEditor::handle_input(
|
||||
&mut editor_borrow,
|
||||
key_event,
|
||||
&add_logic_state.editor_keybinding_mode,
|
||||
&mut add_logic_state.vim_state,
|
||||
);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
KeyCode::Esc => {
|
||||
add_logic_state.deactivate_script_editor_autocomplete();
|
||||
*command_message = "Autocomplete cancelled".to_string();
|
||||
}
|
||||
_ => {
|
||||
add_logic_state.deactivate_script_editor_autocomplete();
|
||||
*command_message = "Autocomplete cancelled".to_string();
|
||||
{
|
||||
let mut editor_borrow = add_logic_state.script_content_editor.borrow_mut();
|
||||
TextEditor::handle_input(
|
||||
&mut editor_borrow,
|
||||
key_event,
|
||||
&add_logic_state.editor_keybinding_mode,
|
||||
&mut add_logic_state.vim_state,
|
||||
);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if key_event.code == KeyCode::Char('@') && key_event.modifiers == KeyModifiers::NONE {
|
||||
let should_trigger = match add_logic_state.editor_keybinding_mode {
|
||||
EditorKeybindingMode::Vim => *is_edit_mode,
|
||||
_ => true,
|
||||
};
|
||||
if should_trigger {
|
||||
let cursor_before = {
|
||||
let editor_borrow = add_logic_state.script_content_editor.borrow();
|
||||
editor_borrow.cursor()
|
||||
};
|
||||
{
|
||||
let mut editor_borrow = add_logic_state.script_content_editor.borrow_mut();
|
||||
TextEditor::handle_input(
|
||||
&mut editor_borrow,
|
||||
key_event,
|
||||
&add_logic_state.editor_keybinding_mode,
|
||||
&mut add_logic_state.vim_state,
|
||||
);
|
||||
}
|
||||
add_logic_state.script_editor_trigger_position = Some(cursor_before);
|
||||
add_logic_state.script_editor_autocomplete_active = true;
|
||||
add_logic_state.script_editor_filter_text.clear();
|
||||
add_logic_state.update_script_editor_suggestions();
|
||||
add_logic_state.has_unsaved_changes = true;
|
||||
*command_message = "Autocomplete: @ (Tab/↑↓ to navigate, Enter to select, Esc to cancel)".to_string();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
if key_event.code == KeyCode::Esc && key_event.modifiers == KeyModifiers::NONE {
|
||||
match add_logic_state.editor_keybinding_mode {
|
||||
EditorKeybindingMode::Vim => {
|
||||
if *is_edit_mode {
|
||||
{
|
||||
let mut editor_borrow = add_logic_state.script_content_editor.borrow_mut();
|
||||
TextEditor::handle_input(
|
||||
&mut editor_borrow,
|
||||
key_event,
|
||||
&add_logic_state.editor_keybinding_mode,
|
||||
&mut add_logic_state.vim_state,
|
||||
);
|
||||
}
|
||||
if TextEditor::is_vim_normal_mode(&add_logic_state.vim_state) {
|
||||
*is_edit_mode = false;
|
||||
*command_message = "VIM: Normal Mode. Esc again to exit script.".to_string();
|
||||
}
|
||||
} else {
|
||||
add_logic_state.current_focus = AddLogicFocus::ScriptContentPreview;
|
||||
app_state.ui.focus_outside_canvas = true;
|
||||
*is_edit_mode = false;
|
||||
*command_message = "Exited script editing.".to_string();
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
if *is_edit_mode {
|
||||
*is_edit_mode = false;
|
||||
*command_message = "Exited script edit. Esc again to exit script.".to_string();
|
||||
} else {
|
||||
add_logic_state.current_focus = AddLogicFocus::ScriptContentPreview;
|
||||
app_state.ui.focus_outside_canvas = true;
|
||||
*is_edit_mode = false;
|
||||
*command_message = "Exited script editing.".to_string();
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
let changed = {
|
||||
let mut editor_borrow = add_logic_state.script_content_editor.borrow_mut();
|
||||
TextEditor::handle_input(
|
||||
&mut editor_borrow,
|
||||
key_event,
|
||||
&add_logic_state.editor_keybinding_mode,
|
||||
&mut add_logic_state.vim_state,
|
||||
)
|
||||
};
|
||||
if changed {
|
||||
add_logic_state.has_unsaved_changes = true;
|
||||
}
|
||||
if add_logic_state.editor_keybinding_mode == EditorKeybindingMode::Vim {
|
||||
*is_edit_mode = !TextEditor::is_vim_normal_mode(&add_logic_state.vim_state);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
let action = config.get_general_action(key_event.code, key_event.modifiers);
|
||||
let current_focus = add_logic_state.current_focus;
|
||||
let mut handled = true;
|
||||
let mut new_focus = current_focus;
|
||||
|
||||
match action.as_deref() {
|
||||
Some("exit_table_scroll") => {
|
||||
handled = false;
|
||||
}
|
||||
Some("move_up") => {
|
||||
match current_focus {
|
||||
AddLogicFocus::InputLogicName => {}
|
||||
AddLogicFocus::InputTargetColumn => new_focus = AddLogicFocus::InputLogicName,
|
||||
AddLogicFocus::InputDescription => new_focus = AddLogicFocus::InputTargetColumn,
|
||||
AddLogicFocus::ScriptContentPreview => new_focus = AddLogicFocus::InputDescription,
|
||||
AddLogicFocus::SaveButton => new_focus = AddLogicFocus::ScriptContentPreview,
|
||||
AddLogicFocus::CancelButton => new_focus = AddLogicFocus::SaveButton,
|
||||
_ => handled = false,
|
||||
}
|
||||
}
|
||||
Some("move_down") => {
|
||||
match current_focus {
|
||||
AddLogicFocus::InputLogicName => new_focus = AddLogicFocus::InputTargetColumn,
|
||||
AddLogicFocus::InputTargetColumn => new_focus = AddLogicFocus::InputDescription,
|
||||
AddLogicFocus::InputDescription => {
|
||||
add_logic_state.last_canvas_field = 2;
|
||||
new_focus = AddLogicFocus::ScriptContentPreview;
|
||||
},
|
||||
AddLogicFocus::ScriptContentPreview => new_focus = AddLogicFocus::SaveButton,
|
||||
AddLogicFocus::SaveButton => new_focus = AddLogicFocus::CancelButton,
|
||||
AddLogicFocus::CancelButton => {}
|
||||
_ => handled = false,
|
||||
}
|
||||
}
|
||||
Some("next_option") => {
|
||||
match current_focus {
|
||||
AddLogicFocus::InputLogicName | AddLogicFocus::InputTargetColumn | AddLogicFocus::InputDescription =>
|
||||
{ new_focus = AddLogicFocus::ScriptContentPreview; }
|
||||
AddLogicFocus::ScriptContentPreview => new_focus = AddLogicFocus::SaveButton,
|
||||
AddLogicFocus::SaveButton => new_focus = AddLogicFocus::CancelButton,
|
||||
AddLogicFocus::CancelButton => { }
|
||||
_ => handled = false,
|
||||
}
|
||||
}
|
||||
Some("previous_option") => {
|
||||
match current_focus {
|
||||
AddLogicFocus::InputLogicName | AddLogicFocus::InputTargetColumn | AddLogicFocus::InputDescription =>
|
||||
{ }
|
||||
AddLogicFocus::ScriptContentPreview => new_focus = AddLogicFocus::InputDescription,
|
||||
AddLogicFocus::SaveButton => new_focus = AddLogicFocus::ScriptContentPreview,
|
||||
AddLogicFocus::CancelButton => new_focus = AddLogicFocus::SaveButton,
|
||||
_ => handled = false,
|
||||
}
|
||||
}
|
||||
Some("next_field") => {
|
||||
new_focus = match current_focus {
|
||||
AddLogicFocus::InputLogicName => AddLogicFocus::InputTargetColumn,
|
||||
AddLogicFocus::InputTargetColumn => AddLogicFocus::InputDescription,
|
||||
AddLogicFocus::InputDescription => AddLogicFocus::ScriptContentPreview,
|
||||
AddLogicFocus::ScriptContentPreview => AddLogicFocus::SaveButton,
|
||||
AddLogicFocus::SaveButton => AddLogicFocus::CancelButton,
|
||||
AddLogicFocus::CancelButton => AddLogicFocus::InputLogicName,
|
||||
_ => current_focus,
|
||||
};
|
||||
}
|
||||
Some("prev_field") => {
|
||||
new_focus = match current_focus {
|
||||
AddLogicFocus::InputLogicName => AddLogicFocus::CancelButton,
|
||||
AddLogicFocus::InputTargetColumn => AddLogicFocus::InputLogicName,
|
||||
AddLogicFocus::InputDescription => AddLogicFocus::InputTargetColumn,
|
||||
AddLogicFocus::ScriptContentPreview => AddLogicFocus::InputDescription,
|
||||
AddLogicFocus::SaveButton => AddLogicFocus::ScriptContentPreview,
|
||||
AddLogicFocus::CancelButton => AddLogicFocus::SaveButton,
|
||||
_ => current_focus,
|
||||
};
|
||||
}
|
||||
Some("select") => {
|
||||
match current_focus {
|
||||
AddLogicFocus::ScriptContentPreview => {
|
||||
new_focus = AddLogicFocus::InsideScriptContent;
|
||||
*is_edit_mode = false;
|
||||
app_state.ui.focus_outside_canvas = false;
|
||||
let mode_hint = match add_logic_state.editor_keybinding_mode {
|
||||
EditorKeybindingMode::Vim => "VIM mode - 'i'/'a'/'o' to edit",
|
||||
_ => "Enter/Ctrl+E to edit",
|
||||
};
|
||||
*command_message = format!("Fullscreen script editing. {} or Esc to exit.", mode_hint);
|
||||
}
|
||||
AddLogicFocus::SaveButton => {
|
||||
*command_message = "Save logic action".to_string();
|
||||
}
|
||||
AddLogicFocus::CancelButton => {
|
||||
buffer_state.update_history(AppView::Admin);
|
||||
app_state.ui.show_add_logic = false;
|
||||
*command_message = "Cancelled Add Logic".to_string();
|
||||
*is_edit_mode = false;
|
||||
}
|
||||
AddLogicFocus::InputLogicName | AddLogicFocus::InputTargetColumn | AddLogicFocus::InputDescription => {
|
||||
*is_edit_mode = !*is_edit_mode;
|
||||
*command_message = format!("Field edit mode: {}", if *is_edit_mode { "ON" } else { "OFF" });
|
||||
}
|
||||
_ => handled = false,
|
||||
}
|
||||
}
|
||||
Some("toggle_edit_mode") => {
|
||||
match current_focus {
|
||||
AddLogicFocus::InputLogicName | AddLogicFocus::InputTargetColumn | AddLogicFocus::InputDescription => {
|
||||
*is_edit_mode = !*is_edit_mode;
|
||||
*command_message = format!("Canvas field edit mode: {}", if *is_edit_mode { "ON" } else { "OFF" });
|
||||
}
|
||||
_ => {
|
||||
*command_message = "Cannot toggle edit mode here.".to_string();
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => handled = false,
|
||||
}
|
||||
|
||||
if handled && current_focus != new_focus {
|
||||
add_logic_state.current_focus = new_focus;
|
||||
let new_is_canvas_input_focus = matches!(new_focus,
|
||||
AddLogicFocus::InputLogicName | AddLogicFocus::InputTargetColumn | AddLogicFocus::InputDescription
|
||||
);
|
||||
if new_is_canvas_input_focus {
|
||||
*is_edit_mode = false;
|
||||
app_state.ui.focus_outside_canvas = false;
|
||||
} else {
|
||||
app_state.ui.focus_outside_canvas = true;
|
||||
if matches!(new_focus, AddLogicFocus::ScriptContentPreview) {
|
||||
*is_edit_mode = false;
|
||||
}
|
||||
}
|
||||
}
|
||||
handled
|
||||
}
|
||||
|
||||
fn replace_autocomplete_text(
|
||||
editor: &mut tui_textarea::TextArea,
|
||||
trigger_pos: (usize, usize),
|
||||
filter_len: usize,
|
||||
replacement: &str,
|
||||
) {
|
||||
// use tui_textarea::CursorMove; // Already imported at the top of the module
|
||||
let filter_start_pos = (trigger_pos.0, trigger_pos.1 + 1);
|
||||
editor.move_cursor(CursorMove::Jump(filter_start_pos.0 as u16, filter_start_pos.1 as u16));
|
||||
for _ in 0..filter_len {
|
||||
editor.delete_next_char();
|
||||
}
|
||||
editor.insert_str(replacement);
|
||||
}
|
||||
@@ -5,12 +5,15 @@ pub mod config;
|
||||
pub mod state;
|
||||
pub mod components;
|
||||
pub mod modes;
|
||||
pub mod functions;
|
||||
pub mod services;
|
||||
pub mod utils;
|
||||
pub mod buffer;
|
||||
pub mod sidebar;
|
||||
pub mod dialog;
|
||||
pub mod search;
|
||||
pub mod bottom_panel;
|
||||
pub mod pages;
|
||||
pub mod movement;
|
||||
|
||||
pub use ui::run_ui;
|
||||
|
||||
|
||||
@@ -1,86 +1,95 @@
|
||||
// src/modes/canvas/common_mode.rs
|
||||
|
||||
use crate::tui::terminal::core::TerminalCore;
|
||||
use crate::state::pages::{form::FormState, auth::LoginState, auth::RegisterState, auth::AuthState};
|
||||
use crate::state::pages::auth::AuthState;
|
||||
use crate::state::app::state::AppState;
|
||||
use crate::services::grpc_client::GrpcClient;
|
||||
use crate::services::auth::AuthClient;
|
||||
use crate::modes::handlers::event::EventOutcome;
|
||||
use crate::tui::functions::common::form::SaveOutcome;
|
||||
crate::pages::forms::logic::SaveOutcome;
|
||||
use anyhow::{Context, Result};
|
||||
use crate::tui::functions::common::{
|
||||
form::{save as form_save, revert as form_revert},
|
||||
login::{save as login_save, revert as login_revert},
|
||||
register::{revert as register_revert},
|
||||
};
|
||||
use crate::pages::routing::{Router, Page};
|
||||
|
||||
pub async fn handle_core_action(
|
||||
action: &str,
|
||||
form_state: &mut FormState,
|
||||
auth_state: &mut AuthState,
|
||||
login_state: &mut LoginState,
|
||||
register_state: &mut RegisterState,
|
||||
grpc_client: &mut GrpcClient,
|
||||
auth_client: &mut AuthClient,
|
||||
terminal: &mut TerminalCore,
|
||||
app_state: &mut AppState,
|
||||
router: &mut Router,
|
||||
) -> Result<EventOutcome> {
|
||||
match action {
|
||||
"save" => {
|
||||
if app_state.ui.show_login {
|
||||
let message = login_save(auth_state, login_state, auth_client, app_state).await.context("Login save action failed")?;
|
||||
Ok(EventOutcome::Ok(message))
|
||||
} else {
|
||||
let save_outcome = form_save(
|
||||
app_state,
|
||||
form_state,
|
||||
grpc_client,
|
||||
).await.context("Register save action failed")?;
|
||||
let message = match save_outcome {
|
||||
SaveOutcome::NoChange => "No changes to save.".to_string(),
|
||||
SaveOutcome::UpdatedExisting => "Entry updated.".to_string(),
|
||||
SaveOutcome::CreatedNew(_) => "New entry created.".to_string(),
|
||||
};
|
||||
Ok(EventOutcome::DataSaved(save_outcome, message))
|
||||
match &mut router.current {
|
||||
Page::Login(state) => {
|
||||
let message = login_save(auth_state, state, auth_client, app_state)
|
||||
.await
|
||||
.context("Login save action failed")?;
|
||||
Ok(EventOutcome::Ok(message))
|
||||
}
|
||||
Page::Form(form_state) => {
|
||||
let save_outcome = form_save(app_state, form_state, grpc_client)
|
||||
.await
|
||||
.context("Form save action failed")?;
|
||||
let message = match save_outcome {
|
||||
SaveOutcome::NoChange => "No changes to save.".to_string(),
|
||||
SaveOutcome::UpdatedExisting => "Entry updated.".to_string(),
|
||||
SaveOutcome::CreatedNew(_) => "New entry created.".to_string(),
|
||||
};
|
||||
Ok(EventOutcome::DataSaved(save_outcome, message))
|
||||
}
|
||||
_ => Ok(EventOutcome::Ok("Save not applicable".into())),
|
||||
}
|
||||
},
|
||||
}
|
||||
"force_quit" => {
|
||||
terminal.cleanup()?;
|
||||
Ok(EventOutcome::Exit("Force exiting without saving.".to_string()))
|
||||
},
|
||||
}
|
||||
"save_and_quit" => {
|
||||
let message = if app_state.ui.show_login {
|
||||
login_save(auth_state, login_state, auth_client, app_state).await.context("Login save n quit action failed")?
|
||||
} else {
|
||||
let save_outcome = form_save(
|
||||
app_state,
|
||||
form_state,
|
||||
grpc_client,
|
||||
).await?;
|
||||
match save_outcome {
|
||||
SaveOutcome::NoChange => "No changes to save.".to_string(),
|
||||
SaveOutcome::UpdatedExisting => "Entry updated.".to_string(),
|
||||
SaveOutcome::CreatedNew(_) => "New entry created.".to_string(),
|
||||
let message = match &mut router.current {
|
||||
Page::Login(state) => {
|
||||
login_save(auth_state, state, auth_client, app_state)
|
||||
.await
|
||||
.context("Login save and quit action failed")?
|
||||
}
|
||||
Page::Form(form_state) => {
|
||||
let save_outcome = form_save(app_state, form_state, grpc_client).await?;
|
||||
match save_outcome {
|
||||
SaveOutcome::NoChange => "No changes to save.".to_string(),
|
||||
SaveOutcome::UpdatedExisting => "Entry updated.".to_string(),
|
||||
SaveOutcome::CreatedNew(_) => "New entry created.".to_string(),
|
||||
}
|
||||
}
|
||||
_ => "Save not applicable".to_string(),
|
||||
};
|
||||
terminal.cleanup()?;
|
||||
Ok(EventOutcome::Exit(format!("{}. Exiting application.", message)))
|
||||
},
|
||||
}
|
||||
"revert" => {
|
||||
if app_state.ui.show_login {
|
||||
let message = login_revert(login_state, app_state).await;
|
||||
Ok(EventOutcome::Ok(message))
|
||||
} else if app_state.ui.show_register {
|
||||
let message = register_revert(register_state, app_state).await;
|
||||
Ok(EventOutcome::Ok(message))
|
||||
} else {
|
||||
let message = form_revert(
|
||||
form_state,
|
||||
grpc_client,
|
||||
).await.context("Form revert x action failed")?;
|
||||
Ok(EventOutcome::Ok(message))
|
||||
match &mut router.current {
|
||||
Page::Login(state) => {
|
||||
let message = login_revert(state, app_state).await;
|
||||
Ok(EventOutcome::Ok(message))
|
||||
}
|
||||
Page::Register(state) => {
|
||||
let message = register_revert(state, app_state).await;
|
||||
Ok(EventOutcome::Ok(message))
|
||||
}
|
||||
Page::Form(form_state) => {
|
||||
let message = form_revert(form_state, grpc_client)
|
||||
.await
|
||||
.context("Form revert action failed")?;
|
||||
Ok(EventOutcome::Ok(message))
|
||||
}
|
||||
_ => Ok(EventOutcome::Ok("Revert not applicable".into())),
|
||||
}
|
||||
},
|
||||
}
|
||||
_ => Ok(EventOutcome::Ok(format!("Core action not handled: {}", action))),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -3,20 +3,19 @@
|
||||
use crossterm::event::{KeyEvent, KeyCode, KeyModifiers};
|
||||
use crate::config::binds::config::Config;
|
||||
use crate::services::grpc_client::GrpcClient;
|
||||
use crate::state::{app::state::AppState, pages::auth::LoginState, pages::auth::RegisterState};
|
||||
use crate::state::app::state::AppState;
|
||||
use crate::modes::common::commands::CommandHandler;
|
||||
use crate::tui::terminal::core::TerminalCore;
|
||||
use crate::tui::functions::common::form::{save, revert};
|
||||
use crate::pages::forms::logic::{save, revert ,SaveOutcome};
|
||||
use crate::modes::handlers::event::EventOutcome;
|
||||
use crate::tui::functions::common::form::SaveOutcome;
|
||||
use crate::pages::routing::{Router, Page};
|
||||
use anyhow::Result;
|
||||
|
||||
pub async fn handle_command_event(
|
||||
key: KeyEvent,
|
||||
config: &Config,
|
||||
app_state: &mut AppState,
|
||||
login_state: &LoginState,
|
||||
register_state: &RegisterState,
|
||||
router: &mut Router,
|
||||
command_input: &mut String,
|
||||
command_message: &mut String,
|
||||
grpc_client: &mut GrpcClient,
|
||||
@@ -25,20 +24,19 @@ pub async fn handle_command_event(
|
||||
current_position: &mut u64,
|
||||
total_count: u64,
|
||||
) -> Result<EventOutcome> {
|
||||
// Exit command mode (via configurable keybinding)
|
||||
// Exit command mode
|
||||
if config.is_exit_command_mode(key.code, key.modifiers) {
|
||||
command_input.clear();
|
||||
*command_message = "".to_string();
|
||||
return Ok(EventOutcome::Ok("Exited command mode".to_string()));
|
||||
}
|
||||
|
||||
// Execute command (via configurable keybinding, defaults to Enter)
|
||||
// Execute command
|
||||
if config.is_command_execute(key.code, key.modifiers) {
|
||||
return process_command(
|
||||
config,
|
||||
app_state,
|
||||
login_state,
|
||||
register_state,
|
||||
router,
|
||||
command_input,
|
||||
command_message,
|
||||
grpc_client,
|
||||
@@ -46,33 +44,31 @@ pub async fn handle_command_event(
|
||||
terminal,
|
||||
current_position,
|
||||
total_count,
|
||||
).await;
|
||||
)
|
||||
.await;
|
||||
}
|
||||
|
||||
// Backspace (via configurable keybinding, defaults to Backspace)
|
||||
// Backspace
|
||||
if config.is_command_backspace(key.code, key.modifiers) {
|
||||
command_input.pop();
|
||||
return Ok(EventOutcome::Ok("".to_string()));
|
||||
}
|
||||
|
||||
// Regular character input - accept any character in command mode
|
||||
// Regular character input
|
||||
if let KeyCode::Char(c) = key.code {
|
||||
// Accept regular or shifted characters (e.g., 'a' or 'A')
|
||||
if key.modifiers.is_empty() || key.modifiers == KeyModifiers::SHIFT {
|
||||
command_input.push(c);
|
||||
return Ok(EventOutcome::Ok("".to_string()));
|
||||
}
|
||||
}
|
||||
|
||||
// Ignore all other keys
|
||||
Ok(EventOutcome::Ok("".to_string()))
|
||||
}
|
||||
|
||||
async fn process_command(
|
||||
config: &Config,
|
||||
app_state: &mut AppState,
|
||||
login_state: &LoginState,
|
||||
register_state: &RegisterState,
|
||||
router: &mut Router,
|
||||
command_input: &mut String,
|
||||
command_message: &mut String,
|
||||
grpc_client: &mut GrpcClient,
|
||||
@@ -81,27 +77,18 @@ async fn process_command(
|
||||
current_position: &mut u64,
|
||||
total_count: u64,
|
||||
) -> Result<EventOutcome> {
|
||||
// Clone the trimmed command to avoid borrow issues
|
||||
let command = command_input.trim().to_string();
|
||||
if command.is_empty() {
|
||||
*command_message = "Empty command".to_string();
|
||||
return Ok(EventOutcome::Ok(command_message.clone()));
|
||||
}
|
||||
|
||||
// Get the action for the command (now checks global and common bindings too)
|
||||
let action = config.get_action_for_command(&command)
|
||||
.unwrap_or("unknown");
|
||||
let action = config.get_action_for_command(&command).unwrap_or("unknown");
|
||||
|
||||
match action {
|
||||
"force_quit" | "save_and_quit" | "quit" => {
|
||||
let (should_exit, message) = command_handler
|
||||
.handle_command(
|
||||
action,
|
||||
terminal,
|
||||
app_state,
|
||||
login_state,
|
||||
register_state,
|
||||
)
|
||||
.handle_command(action, terminal, app_state, router)
|
||||
.await?;
|
||||
command_input.clear();
|
||||
if should_exit {
|
||||
@@ -109,28 +96,30 @@ async fn process_command(
|
||||
} else {
|
||||
Ok(EventOutcome::Ok(message))
|
||||
}
|
||||
},
|
||||
}
|
||||
"save" => {
|
||||
let outcome = save(
|
||||
app_state,
|
||||
grpc_client,
|
||||
).await?;
|
||||
let message = match outcome {
|
||||
SaveOutcome::CreatedNew(_) => "New entry created".to_string(),
|
||||
SaveOutcome::UpdatedExisting => "Entry updated".to_string(),
|
||||
SaveOutcome::NoChange => "No changes to save".to_string(),
|
||||
};
|
||||
command_input.clear();
|
||||
Ok(EventOutcome::DataSaved(outcome, message))
|
||||
},
|
||||
if let Page::Form(path) = &router.current {
|
||||
let outcome = save(app_state, path, grpc_client).await?;
|
||||
let message = match outcome {
|
||||
SaveOutcome::CreatedNew(_) => "New entry created".to_string(),
|
||||
SaveOutcome::UpdatedExisting => "Entry updated".to_string(),
|
||||
SaveOutcome::NoChange => "No changes to save".to_string(),
|
||||
};
|
||||
command_input.clear();
|
||||
Ok(EventOutcome::DataSaved(outcome, message))
|
||||
} else {
|
||||
Ok(EventOutcome::Ok("Not in a form page".to_string()))
|
||||
}
|
||||
}
|
||||
"revert" => {
|
||||
let message = revert(
|
||||
app_state,
|
||||
grpc_client,
|
||||
).await?;
|
||||
command_input.clear();
|
||||
Ok(EventOutcome::Ok(message))
|
||||
},
|
||||
if let Page::Form(path) = &router.current {
|
||||
let message = revert(app_state, path, grpc_client).await?;
|
||||
command_input.clear();
|
||||
Ok(EventOutcome::Ok(message))
|
||||
} else {
|
||||
Ok(EventOutcome::Ok("Not in a form page".to_string()))
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
let message = format!("Unhandled action: {}", action);
|
||||
command_input.clear();
|
||||
|
||||
@@ -1,7 +1,7 @@
|
||||
// src/modes/common/commands.rs
|
||||
use crate::tui::terminal::core::TerminalCore;
|
||||
use crate::state::app::state::AppState;
|
||||
use crate::state::pages::{auth::LoginState, auth::RegisterState};
|
||||
use crate::pages::routing::{Router, Page};
|
||||
use anyhow::Result;
|
||||
|
||||
pub struct CommandHandler;
|
||||
@@ -16,11 +16,10 @@ impl CommandHandler {
|
||||
action: &str,
|
||||
terminal: &mut TerminalCore,
|
||||
app_state: &mut AppState,
|
||||
login_state: &LoginState,
|
||||
register_state: &RegisterState,
|
||||
router: &Router,
|
||||
) -> Result<(bool, String)> {
|
||||
match action {
|
||||
"quit" => self.handle_quit(terminal, app_state, login_state, register_state).await,
|
||||
"quit" => self.handle_quit(terminal, app_state, router).await,
|
||||
"force_quit" => self.handle_force_quit(terminal).await,
|
||||
"save_and_quit" => self.handle_save_quit(terminal).await,
|
||||
_ => Ok((false, format!("Unknown command: {}", action))),
|
||||
@@ -31,18 +30,17 @@ impl CommandHandler {
|
||||
&self,
|
||||
terminal: &mut TerminalCore,
|
||||
app_state: &mut AppState,
|
||||
login_state: &LoginState,
|
||||
register_state: &RegisterState,
|
||||
router: &Router,
|
||||
) -> Result<(bool, String)> {
|
||||
// Use actual unsaved changes state instead of is_saved flag
|
||||
let has_unsaved = if app_state.ui.show_login {
|
||||
login_state.has_unsaved_changes()
|
||||
} else if app_state.ui.show_register {
|
||||
register_state.has_unsaved_changes()
|
||||
} else if let Some(fs) = app_state.form_state_mut() {
|
||||
fs.has_unsaved_changes
|
||||
} else {
|
||||
false
|
||||
// Use router to check unsaved changes
|
||||
let has_unsaved = match &router.current {
|
||||
Page::Login(page) => page.state.has_unsaved_changes(),
|
||||
Page::Register(state) => state.has_unsaved_changes(),
|
||||
Page::Form(path) => app_state
|
||||
.form_state_for_path_ref(path)
|
||||
.map(|fs| fs.has_unsaved_changes())
|
||||
.unwrap_or(false),
|
||||
_ => false,
|
||||
};
|
||||
|
||||
if !has_unsaved {
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
// src/client/modes/general.rs
|
||||
pub mod navigation;
|
||||
pub mod dialog;
|
||||
pub mod command_navigation;
|
||||
|
||||
@@ -1,164 +0,0 @@
|
||||
// src/modes/general/dialog.rs
|
||||
|
||||
use crossterm::event::{Event, KeyCode};
|
||||
use crate::config::binds::config::Config;
|
||||
use crate::ui::handlers::context::DialogPurpose;
|
||||
use crate::state::app::state::AppState;
|
||||
use crate::buffer::AppView;
|
||||
use crate::buffer::state::BufferState;
|
||||
use crate::state::pages::auth::{LoginState, RegisterState};
|
||||
use crate::state::pages::admin::AdminState;
|
||||
use crate::modes::handlers::event::EventOutcome;
|
||||
use crate::tui::functions::common::{login, register};
|
||||
use crate::tui::functions::common::add_table::handle_delete_selected_columns;
|
||||
use anyhow::Result;
|
||||
|
||||
/// Handles key events specifically when a dialog is active.
|
||||
/// Returns Some(Result<EventOutcome, Error>) if the event was handled (consumed),
|
||||
/// otherwise returns None.
|
||||
pub async fn handle_dialog_event(
|
||||
event: &Event,
|
||||
config: &Config,
|
||||
app_state: &mut AppState,
|
||||
login_state: &mut LoginState,
|
||||
register_state: &mut RegisterState,
|
||||
buffer_state: &mut BufferState,
|
||||
admin_state: &mut AdminState,
|
||||
) -> Option<Result<EventOutcome>> {
|
||||
if let Event::Key(key) = event {
|
||||
// Always allow Esc to dismiss
|
||||
if key.code == KeyCode::Esc {
|
||||
app_state.hide_dialog();
|
||||
return Some(Ok(EventOutcome::Ok("Dialog dismissed".to_string())));
|
||||
}
|
||||
|
||||
// Check general bindings for dialog actions
|
||||
if let Some(action) = config.get_general_action(key.code, key.modifiers) {
|
||||
match action {
|
||||
"move_down" | "next_option" => {
|
||||
let current_index = app_state.ui.dialog.dialog_active_button_index;
|
||||
let num_buttons = app_state.ui.dialog.dialog_buttons.len();
|
||||
if num_buttons > 0 && current_index < num_buttons - 1 {
|
||||
app_state.ui.dialog.dialog_active_button_index += 1;
|
||||
}
|
||||
return Some(Ok(EventOutcome::Ok(String::new())));
|
||||
}
|
||||
"move_up" | "previous_option" => {
|
||||
let current_index = app_state.ui.dialog.dialog_active_button_index;
|
||||
if current_index > 0 {
|
||||
app_state.ui.dialog.dialog_active_button_index -= 1;
|
||||
}
|
||||
return Some(Ok(EventOutcome::Ok(String::new())));
|
||||
}
|
||||
"select" => {
|
||||
let selected_index = app_state.ui.dialog.dialog_active_button_index;
|
||||
let purpose = match app_state.ui.dialog.purpose {
|
||||
Some(p) => p,
|
||||
None => {
|
||||
app_state.hide_dialog();
|
||||
return Some(Ok(EventOutcome::Ok("Internal Error: Dialog context lost".to_string())));
|
||||
}
|
||||
};
|
||||
|
||||
// Handle Dialog Actions Directly Here
|
||||
match purpose {
|
||||
DialogPurpose::LoginSuccess => {
|
||||
match selected_index {
|
||||
0 => { // "Menu" button selected
|
||||
app_state.hide_dialog();
|
||||
let message = login::back_to_main(login_state, app_state, buffer_state).await;
|
||||
return Some(Ok(EventOutcome::Ok(message)));
|
||||
}
|
||||
1 => {
|
||||
app_state.hide_dialog();
|
||||
return Some(Ok(EventOutcome::Ok("Exiting dialog".to_string())));
|
||||
}
|
||||
_ => {
|
||||
app_state.hide_dialog();
|
||||
return Some(Ok(EventOutcome::Ok("Unknown dialog button selected".to_string())));
|
||||
}
|
||||
}
|
||||
}
|
||||
DialogPurpose::LoginFailed => {
|
||||
match selected_index {
|
||||
0 => { // "OK" button selected
|
||||
app_state.hide_dialog();
|
||||
return Some(Ok(EventOutcome::Ok("Login failed dialog dismissed".to_string())));
|
||||
}
|
||||
_ => {
|
||||
app_state.hide_dialog();
|
||||
return Some(Ok(EventOutcome::Ok("Unknown dialog button selected".to_string())));
|
||||
}
|
||||
}
|
||||
}
|
||||
DialogPurpose::RegisterSuccess => { // Add this arm
|
||||
match selected_index {
|
||||
0 => { // "OK" button for RegisterSuccess
|
||||
app_state.hide_dialog();
|
||||
let message = register::back_to_login(register_state, app_state, buffer_state).await;
|
||||
return Some(Ok(EventOutcome::Ok(message)));
|
||||
}
|
||||
_ => { // Default for RegisterSuccess
|
||||
app_state.hide_dialog();
|
||||
return Some(Ok(EventOutcome::Ok("Unknown dialog button selected".to_string())));
|
||||
}
|
||||
}
|
||||
}
|
||||
DialogPurpose::RegisterFailed => { // Add this arm
|
||||
match selected_index {
|
||||
0 => { // "OK" button for RegisterFailed
|
||||
app_state.hide_dialog(); // Just dismiss
|
||||
return Some(Ok(EventOutcome::Ok("Register failed dialog dismissed".to_string())));
|
||||
}
|
||||
_ => { // Default for RegisterFailed
|
||||
app_state.hide_dialog();
|
||||
return Some(Ok(EventOutcome::Ok("Unknown dialog button selected".to_string())));
|
||||
}
|
||||
}
|
||||
}
|
||||
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 */ }
|
||||
}
|
||||
}
|
||||
DialogPurpose::SaveTableSuccess => {
|
||||
match selected_index {
|
||||
0 => { // "OK" button selected
|
||||
app_state.hide_dialog();
|
||||
buffer_state.update_history(AppView::Admin); // Navigate back
|
||||
return Some(Ok(EventOutcome::Ok("Save success dialog dismissed.".to_string())));
|
||||
}
|
||||
_ => { /* Handle unexpected index */ }
|
||||
}
|
||||
}
|
||||
DialogPurpose::SaveLogicSuccess => {
|
||||
match selected_index {
|
||||
0 => { // "OK" button selected
|
||||
app_state.hide_dialog();
|
||||
buffer_state.update_history(AppView::Admin);
|
||||
return Some(Ok(EventOutcome::Ok("Save success dialog dismissed.".to_string())));
|
||||
}
|
||||
_ => { /* Handle unexpected index */ }
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => {} // Ignore other general actions when dialog is shown
|
||||
}
|
||||
}
|
||||
// If it was a key event but not handled above, consume it
|
||||
Some(Ok(EventOutcome::Ok(String::new())))
|
||||
} else {
|
||||
// If it wasn't a key event, consume it too while dialog is active
|
||||
Some(Ok(EventOutcome::Ok(String::new())))
|
||||
}
|
||||
}
|
||||
@@ -3,11 +3,8 @@
|
||||
use crossterm::event::KeyEvent;
|
||||
use crate::config::binds::config::Config;
|
||||
use crate::state::app::state::AppState;
|
||||
use crate::state::pages::form::FormState;
|
||||
use crate::state::pages::auth::LoginState;
|
||||
use crate::state::pages::auth::RegisterState;
|
||||
use crate::state::pages::intro::IntroState;
|
||||
use crate::state::pages::admin::AdminState;
|
||||
use crate::pages::routing::{Router, Page};
|
||||
use crate::pages::forms::FormState;
|
||||
use crate::ui::handlers::context::UiContext;
|
||||
use crate::modes::handlers::event::EventOutcome;
|
||||
use crate::modes::general::command_navigation::{handle_command_navigation_event, NavigationState};
|
||||
@@ -18,10 +15,7 @@ pub async fn handle_navigation_event(
|
||||
key: KeyEvent,
|
||||
config: &Config,
|
||||
app_state: &mut AppState,
|
||||
login_state: &mut LoginState,
|
||||
register_state: &mut RegisterState,
|
||||
intro_state: &mut IntroState,
|
||||
admin_state: &mut AdminState,
|
||||
router: &mut Router,
|
||||
command_mode: &mut bool,
|
||||
command_input: &mut String,
|
||||
command_message: &mut String,
|
||||
@@ -34,31 +28,35 @@ pub async fn handle_navigation_event(
|
||||
|
||||
if let Some(action) = config.get_general_action(key.code, key.modifiers) {
|
||||
match action {
|
||||
"move_up" => {
|
||||
move_up(app_state, login_state, register_state, intro_state, admin_state);
|
||||
"up" => {
|
||||
up(app_state, router);
|
||||
return Ok(EventOutcome::Ok(String::new()));
|
||||
}
|
||||
"move_down" => {
|
||||
move_down(app_state, intro_state, admin_state);
|
||||
"down" => {
|
||||
down(app_state, router);
|
||||
return Ok(EventOutcome::Ok(String::new()));
|
||||
}
|
||||
"next_option" => {
|
||||
next_option(app_state, intro_state);
|
||||
next_option(app_state, router);
|
||||
return Ok(EventOutcome::Ok(String::new()));
|
||||
}
|
||||
"previous_option" => {
|
||||
previous_option(app_state, intro_state);
|
||||
previous_option(app_state, router);
|
||||
return Ok(EventOutcome::Ok(String::new()));
|
||||
}
|
||||
"next_field" => {
|
||||
if let Some(fs) = app_state.form_state_mut() {
|
||||
next_field(fs);
|
||||
if let Page::Form(path) = &router.current {
|
||||
if let Some(fs) = app_state.form_state_for_path(path) {
|
||||
next_field(fs);
|
||||
}
|
||||
}
|
||||
return Ok(EventOutcome::Ok(String::new()));
|
||||
}
|
||||
"prev_field" => {
|
||||
if let Some(fs) = app_state.form_state_mut() {
|
||||
prev_field(fs);
|
||||
if let Page::Form(path) = &router.current {
|
||||
if let Some(fs) = app_state.form_state_for_path(path) {
|
||||
prev_field(fs);
|
||||
}
|
||||
}
|
||||
return Ok(EventOutcome::Ok(String::new()));
|
||||
}
|
||||
@@ -67,18 +65,21 @@ pub async fn handle_navigation_event(
|
||||
return Ok(EventOutcome::Ok(String::new()));
|
||||
}
|
||||
"select" => {
|
||||
let (context, index) = if app_state.ui.show_intro {
|
||||
(UiContext::Intro, intro_state.selected_option)
|
||||
} else if app_state.ui.show_login && app_state.ui.focus_outside_canvas {
|
||||
(UiContext::Login, app_state.focused_button_index)
|
||||
} else if app_state.ui.show_register && app_state.ui.focus_outside_canvas {
|
||||
(UiContext::Register, app_state.focused_button_index)
|
||||
} else if app_state.ui.show_admin {
|
||||
(UiContext::Admin, admin_state.get_selected_index().unwrap_or(0))
|
||||
} else if app_state.ui.dialog.dialog_show {
|
||||
(UiContext::Dialog, app_state.ui.dialog.dialog_active_button_index)
|
||||
} else {
|
||||
return Ok(EventOutcome::Ok("Select (No Action)".to_string()));
|
||||
let (context, index) = match &router.current {
|
||||
Page::Intro(state) => (UiContext::Intro, state.selected_option),
|
||||
Page::Login(_) if app_state.ui.focus_outside_canvas => {
|
||||
(UiContext::Login, app_state.focused_button_index)
|
||||
}
|
||||
Page::Register(_) if app_state.ui.focus_outside_canvas => {
|
||||
(UiContext::Register, app_state.focused_button_index)
|
||||
}
|
||||
Page::Admin(state) => {
|
||||
(UiContext::Admin, state.get_selected_index().unwrap_or(0))
|
||||
}
|
||||
_ if app_state.ui.dialog.dialog_show => {
|
||||
(UiContext::Dialog, app_state.ui.dialog.dialog_active_button_index)
|
||||
}
|
||||
_ => return Ok(EventOutcome::Ok("Select (No Action)".to_string())),
|
||||
};
|
||||
return Ok(EventOutcome::ButtonSelected { context, index });
|
||||
}
|
||||
@@ -88,60 +89,76 @@ pub async fn handle_navigation_event(
|
||||
Ok(EventOutcome::Ok(String::new()))
|
||||
}
|
||||
|
||||
pub fn move_up(app_state: &mut AppState, login_state: &mut LoginState, register_state: &mut RegisterState, intro_state: &mut IntroState, admin_state: &mut AdminState) {
|
||||
if app_state.ui.focus_outside_canvas && app_state.ui.show_login || app_state.ui.show_register{
|
||||
if app_state.focused_button_index == 0 {
|
||||
app_state.ui.focus_outside_canvas = false;
|
||||
if app_state.ui.show_login {
|
||||
let last_field_index = login_state.field_count().saturating_sub(1);
|
||||
login_state.set_current_field(last_field_index);
|
||||
pub fn up(app_state: &mut AppState, router: &mut Router) {
|
||||
match &mut router.current {
|
||||
Page::Login(page) if app_state.ui.focus_outside_canvas => {
|
||||
if app_state.focused_button_index == 0 {
|
||||
app_state.ui.focus_outside_canvas = false;
|
||||
let last_field_index = page.state.field_count().saturating_sub(1);
|
||||
page.state.set_current_field(last_field_index);
|
||||
} else {
|
||||
let last_field_index = register_state.field_count().saturating_sub(1);
|
||||
register_state.set_current_field(last_field_index);
|
||||
app_state.focused_button_index =
|
||||
app_state.focused_button_index.saturating_sub(1);
|
||||
}
|
||||
} else {
|
||||
app_state.focused_button_index = app_state.focused_button_index.saturating_sub(1);
|
||||
}
|
||||
} else if app_state.ui.show_intro {
|
||||
intro_state.previous_option();
|
||||
} else if app_state.ui.show_admin {
|
||||
admin_state.previous();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn move_down(app_state: &mut AppState, intro_state: &mut IntroState, admin_state: &mut AdminState) {
|
||||
if app_state.ui.focus_outside_canvas && app_state.ui.show_login || app_state.ui.show_register {
|
||||
let num_general_elements = 2;
|
||||
if app_state.focused_button_index < num_general_elements - 1 {
|
||||
app_state.focused_button_index += 1;
|
||||
Page::Register(state) if app_state.ui.focus_outside_canvas => {
|
||||
if app_state.focused_button_index == 0 {
|
||||
app_state.ui.focus_outside_canvas = false;
|
||||
let last_field_index = state.state.field_count().saturating_sub(1);
|
||||
state.set_current_field(last_field_index);
|
||||
} else {
|
||||
app_state.focused_button_index =
|
||||
app_state.focused_button_index.saturating_sub(1);
|
||||
}
|
||||
}
|
||||
} else if app_state.ui.show_intro {
|
||||
intro_state.next_option();
|
||||
} else if app_state.ui.show_admin {
|
||||
admin_state.next();
|
||||
Page::Intro(state) => state.previous_option(),
|
||||
Page::Admin(state) => state.previous(),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn next_option(app_state: &mut AppState, intro_state: &mut IntroState) {
|
||||
if app_state.ui.show_intro {
|
||||
intro_state.next_option();
|
||||
} else {
|
||||
// Get option count from state instead of parameter
|
||||
let option_count = app_state.profile_tree.profiles.len();
|
||||
app_state.focused_button_index = (app_state.focused_button_index + 1) % option_count;
|
||||
pub fn down(app_state: &mut AppState, router: &mut Router) {
|
||||
match &mut router.current {
|
||||
Page::Login(_) | Page::Register(_) if app_state.ui.focus_outside_canvas => {
|
||||
let num_general_elements = 2;
|
||||
if app_state.focused_button_index < num_general_elements - 1 {
|
||||
app_state.focused_button_index += 1;
|
||||
}
|
||||
}
|
||||
Page::Intro(state) => state.next_option(),
|
||||
Page::Admin(state) => state.next(),
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn previous_option(app_state: &mut AppState, intro_state: &mut IntroState) {
|
||||
if app_state.ui.show_intro {
|
||||
intro_state.previous_option();
|
||||
} else {
|
||||
let option_count = app_state.profile_tree.profiles.len();
|
||||
app_state.focused_button_index = if app_state.focused_button_index == 0 {
|
||||
option_count.saturating_sub(1)
|
||||
} else {
|
||||
app_state.focused_button_index - 1
|
||||
};
|
||||
pub fn next_option(app_state: &mut AppState, router: &mut Router) {
|
||||
match &mut router.current {
|
||||
Page::Intro(state) => state.next_option(),
|
||||
Page::Admin(_) => {
|
||||
let option_count = app_state.profile_tree.profiles.len();
|
||||
if option_count > 0 {
|
||||
app_state.focused_button_index =
|
||||
(app_state.focused_button_index + 1) % option_count;
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn previous_option(app_state: &mut AppState, router: &mut Router) {
|
||||
match &mut router.current {
|
||||
Page::Intro(state) => state.previous_option(),
|
||||
Page::Admin(_) => {
|
||||
let option_count = app_state.profile_tree.profiles.len();
|
||||
if option_count > 0 {
|
||||
app_state.focused_button_index = if app_state.focused_button_index == 0 {
|
||||
option_count.saturating_sub(1)
|
||||
} else {
|
||||
app_state.focused_button_index - 1
|
||||
};
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -164,7 +181,7 @@ pub fn prev_field(form_state: &mut FormState) {
|
||||
pub fn handle_enter_command_mode(
|
||||
command_mode: &mut bool,
|
||||
command_input: &mut String,
|
||||
command_message: &mut String
|
||||
command_message: &mut String,
|
||||
) {
|
||||
*command_mode = true;
|
||||
command_input.clear();
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,109 +1,59 @@
|
||||
// src/modes/handlers/mode_manager.rs
|
||||
|
||||
use crate::state::app::state::AppState;
|
||||
use crate::modes::handlers::event::EventHandler;
|
||||
use crate::state::pages::add_logic::AddLogicFocus;
|
||||
use crate::state::pages::admin::AdminState;
|
||||
use crate::pages::routing::{Router, Page};
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum AppMode {
|
||||
General, // For intro and admin screens
|
||||
ReadOnly, // Canvas read-only mode
|
||||
Edit, // Canvas edit mode
|
||||
Highlight, // Canvas highlight/visual mode
|
||||
Command, // Command mode overlay
|
||||
}
|
||||
/// General mode = when focus is outside any canvas
|
||||
/// (Intro, Admin, Login/Register buttons, AddTable/AddLogic menus, dialogs, etc.)
|
||||
General,
|
||||
|
||||
impl From<canvas::AppMode> for AppMode {
|
||||
fn from(mode: canvas::AppMode) -> Self {
|
||||
match mode {
|
||||
canvas::AppMode::General => AppMode::General,
|
||||
canvas::AppMode::ReadOnly => AppMode::ReadOnly,
|
||||
canvas::AppMode::Edit => AppMode::Edit,
|
||||
canvas::AppMode::Highlight => AppMode::Highlight,
|
||||
canvas::AppMode::Command => AppMode::Command,
|
||||
}
|
||||
}
|
||||
/// Command overlay (":" or "ctrl+;"), available globally
|
||||
Command,
|
||||
}
|
||||
|
||||
pub struct ModeManager;
|
||||
|
||||
impl ModeManager {
|
||||
/// Determine current mode based on app state
|
||||
/// Determine current mode:
|
||||
/// - If navigation palette is active → General
|
||||
/// - If command overlay is active → Command
|
||||
/// - If focus is inside a canvas (Form, Login, Register, AddTable, AddLogic) → let canvas handle its own mode
|
||||
/// - Otherwise → General
|
||||
pub fn derive_mode(
|
||||
app_state: &AppState,
|
||||
event_handler: &EventHandler,
|
||||
admin_state: &AdminState,
|
||||
router: &Router,
|
||||
) -> AppMode {
|
||||
// Navigation palette always forces General
|
||||
if event_handler.navigation_state.active {
|
||||
return AppMode::General;
|
||||
}
|
||||
|
||||
// Explicit command mode flag
|
||||
// Explicit command overlay flag
|
||||
if event_handler.command_mode {
|
||||
return AppMode::Command;
|
||||
}
|
||||
|
||||
// Always trust the FormEditor when a form is active
|
||||
if app_state.ui.show_form && !app_state.ui.focus_outside_canvas {
|
||||
if let Some(editor) = &app_state.form_editor {
|
||||
return AppMode::from(editor.mode());
|
||||
}
|
||||
}
|
||||
|
||||
// --- Non-form views (add_logic, add_table, etc.) ---
|
||||
if app_state.ui.show_add_logic {
|
||||
match admin_state.add_logic_state.current_focus {
|
||||
AddLogicFocus::InputLogicName
|
||||
| AddLogicFocus::InputTargetColumn
|
||||
| AddLogicFocus::InputDescription => {
|
||||
if event_handler.is_edit_mode {
|
||||
AppMode::Edit
|
||||
} else {
|
||||
AppMode::ReadOnly
|
||||
}
|
||||
}
|
||||
_ => AppMode::General,
|
||||
}
|
||||
} else if app_state.ui.show_add_table {
|
||||
if app_state.ui.focus_outside_canvas {
|
||||
// If focus is inside a canvas, we don't duplicate canvas modes here.
|
||||
// Canvas crate owns ReadOnly/Edit/Highlight internally.
|
||||
match &router.current {
|
||||
Page::Form(_)
|
||||
| Page::Login(_)
|
||||
| Page::Register(_)
|
||||
| Page::AddTable(_)
|
||||
| Page::AddLogic(_) if !app_state.ui.focus_outside_canvas => {
|
||||
// Canvas active → let canvas handle its own AppMode
|
||||
AppMode::General
|
||||
} else if event_handler.is_edit_mode {
|
||||
AppMode::Edit
|
||||
} else {
|
||||
AppMode::ReadOnly
|
||||
}
|
||||
} else if app_state.ui.show_login
|
||||
|| app_state.ui.show_register
|
||||
{
|
||||
// login/register still use the old flag
|
||||
if event_handler.is_edit_mode {
|
||||
AppMode::Edit
|
||||
} else {
|
||||
AppMode::ReadOnly
|
||||
}
|
||||
} else {
|
||||
AppMode::General
|
||||
_ => AppMode::General,
|
||||
}
|
||||
}
|
||||
|
||||
// Mode transition rules
|
||||
pub fn can_enter_command_mode(current_mode: AppMode) -> bool {
|
||||
!matches!(current_mode, AppMode::Edit)
|
||||
}
|
||||
|
||||
pub fn can_enter_edit_mode(current_mode: AppMode) -> bool {
|
||||
matches!(current_mode, AppMode::ReadOnly)
|
||||
}
|
||||
|
||||
pub fn can_enter_read_only_mode(current_mode: AppMode) -> bool {
|
||||
matches!(
|
||||
current_mode,
|
||||
AppMode::Edit | AppMode::Command | AppMode::Highlight
|
||||
)
|
||||
}
|
||||
|
||||
pub fn can_enter_highlight_mode(current_mode: AppMode) -> bool {
|
||||
matches!(current_mode, AppMode::ReadOnly)
|
||||
/// Command overlay can be entered from anywhere (General or Canvas).
|
||||
pub fn can_enter_command_mode(_current_mode: AppMode) -> bool {
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
12
client/src/movement/actions.rs
Normal file
12
client/src/movement/actions.rs
Normal file
@@ -0,0 +1,12 @@
|
||||
// src/movement/actions.rs
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum MovementAction {
|
||||
Next,
|
||||
Previous,
|
||||
Up,
|
||||
Down,
|
||||
Left,
|
||||
Right,
|
||||
Select,
|
||||
Esc,
|
||||
}
|
||||
32
client/src/movement/lib.rs
Normal file
32
client/src/movement/lib.rs
Normal file
@@ -0,0 +1,32 @@
|
||||
// src/movement/lib.rs
|
||||
|
||||
use crate::movement::MovementAction;
|
||||
|
||||
#[inline]
|
||||
pub fn move_focus<T: Copy + Eq>(
|
||||
order: &[T],
|
||||
current: &mut T,
|
||||
action: MovementAction,
|
||||
) -> bool {
|
||||
if order.is_empty() {
|
||||
return false;
|
||||
}
|
||||
if let Some(pos) = order.iter().position(|k| *k == *current) {
|
||||
match action {
|
||||
MovementAction::Previous | MovementAction::Up | MovementAction::Left => {
|
||||
if pos > 0 {
|
||||
*current = order[pos - 1];
|
||||
return true;
|
||||
}
|
||||
}
|
||||
MovementAction::Next | MovementAction::Down | MovementAction::Right => {
|
||||
if pos + 1 < order.len() {
|
||||
*current = order[pos + 1];
|
||||
return true;
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
6
client/src/movement/mod.rs
Normal file
6
client/src/movement/mod.rs
Normal file
@@ -0,0 +1,6 @@
|
||||
// src/movement/mod.rs
|
||||
pub mod actions;
|
||||
pub mod lib;
|
||||
|
||||
pub use actions::MovementAction;
|
||||
pub use lib::move_focus;
|
||||
7
client/src/pages/admin/admin/mod.rs
Normal file
7
client/src/pages/admin/admin/mod.rs
Normal file
@@ -0,0 +1,7 @@
|
||||
// src/pages/admin/admin/mod.rs
|
||||
|
||||
pub mod state;
|
||||
pub mod ui;
|
||||
pub mod tui;
|
||||
|
||||
pub use state::{AdminState, AdminFocus};
|
||||
195
client/src/pages/admin/admin/state.rs
Normal file
195
client/src/pages/admin/admin/state.rs
Normal file
@@ -0,0 +1,195 @@
|
||||
// src/pages/admin/admin/state.rs
|
||||
use ratatui::widgets::ListState;
|
||||
use crate::pages::admin_panel::add_table::state::AddTableState;
|
||||
use crate::pages::admin_panel::add_logic::state::AddLogicState;
|
||||
use crate::movement::{move_focus, MovementAction};
|
||||
use crate::state::app::state::AppState;
|
||||
|
||||
/// Focus states for the admin panel
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
|
||||
pub enum AdminFocus {
|
||||
#[default]
|
||||
ProfilesPane,
|
||||
InsideProfilesList,
|
||||
Tables,
|
||||
InsideTablesList,
|
||||
Button1,
|
||||
Button2,
|
||||
Button3,
|
||||
}
|
||||
|
||||
/// Full admin panel state (for logged-in admins)
|
||||
#[derive(Default, Clone, Debug)]
|
||||
pub struct AdminState {
|
||||
pub profiles: Vec<String>,
|
||||
pub profile_list_state: ListState,
|
||||
pub table_list_state: ListState,
|
||||
pub selected_profile_index: Option<usize>,
|
||||
pub selected_table_index: Option<usize>,
|
||||
pub current_focus: AdminFocus,
|
||||
pub add_table_state: AddTableState,
|
||||
pub add_logic_state: AddLogicState,
|
||||
}
|
||||
|
||||
impl AdminState {
|
||||
pub fn get_selected_index(&self) -> Option<usize> {
|
||||
self.profile_list_state.selected()
|
||||
}
|
||||
|
||||
pub fn get_selected_profile_name(&self) -> Option<&String> {
|
||||
self.profile_list_state.selected().and_then(|i| self.profiles.get(i))
|
||||
}
|
||||
|
||||
pub fn set_profiles(&mut self, new_profiles: Vec<String>) {
|
||||
let current_selection_index = self.profile_list_state.selected();
|
||||
self.profiles = new_profiles;
|
||||
|
||||
if self.profiles.is_empty() {
|
||||
self.profile_list_state.select(None);
|
||||
} else {
|
||||
let new_selection = match current_selection_index {
|
||||
Some(index) => Some(index.min(self.profiles.len() - 1)),
|
||||
None => Some(0),
|
||||
};
|
||||
self.profile_list_state.select(new_selection);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn next(&mut self) {
|
||||
if self.profiles.is_empty() {
|
||||
self.profile_list_state.select(None);
|
||||
return;
|
||||
}
|
||||
let i = match self.profile_list_state.selected() {
|
||||
Some(i) => if i >= self.profiles.len() - 1 { 0 } else { i + 1 },
|
||||
None => 0,
|
||||
};
|
||||
self.profile_list_state.select(Some(i));
|
||||
}
|
||||
|
||||
pub fn previous(&mut self) {
|
||||
if self.profiles.is_empty() {
|
||||
self.profile_list_state.select(None);
|
||||
return;
|
||||
}
|
||||
let i = match self.profile_list_state.selected() {
|
||||
Some(i) => if i == 0 { self.profiles.len() - 1 } else { i - 1 },
|
||||
None => self.profiles.len() - 1,
|
||||
};
|
||||
self.profile_list_state.select(Some(i));
|
||||
}
|
||||
|
||||
pub fn handle_movement(
|
||||
&mut self,
|
||||
app: &AppState,
|
||||
action: MovementAction,
|
||||
) -> bool {
|
||||
use AdminFocus::*;
|
||||
|
||||
const ORDER: [AdminFocus; 5] = [
|
||||
ProfilesPane,
|
||||
Tables,
|
||||
Button1,
|
||||
Button2,
|
||||
Button3,
|
||||
];
|
||||
|
||||
match (self.current_focus, action) {
|
||||
(ProfilesPane, MovementAction::Select) => {
|
||||
if !app.profile_tree.profiles.is_empty()
|
||||
&& self.profile_list_state.selected().is_none()
|
||||
{
|
||||
self.profile_list_state.select(Some(0));
|
||||
}
|
||||
self.current_focus = InsideProfilesList;
|
||||
return true;
|
||||
}
|
||||
(Tables, MovementAction::Select) => {
|
||||
let p_idx = self
|
||||
.selected_profile_index
|
||||
.or_else(|| self.profile_list_state.selected());
|
||||
if let Some(pi) = p_idx {
|
||||
let len = app
|
||||
.profile_tree
|
||||
.profiles
|
||||
.get(pi)
|
||||
.map(|p| p.tables.len())
|
||||
.unwrap_or(0);
|
||||
if len > 0 && self.table_list_state.selected().is_none() {
|
||||
self.table_list_state.select(Some(0));
|
||||
}
|
||||
}
|
||||
self.current_focus = InsideTablesList;
|
||||
return true;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
||||
match self.current_focus {
|
||||
InsideProfilesList => match action {
|
||||
MovementAction::Up => {
|
||||
if !app.profile_tree.profiles.is_empty() {
|
||||
let curr = self.profile_list_state.selected().unwrap_or(0);
|
||||
let next = curr.saturating_sub(1);
|
||||
self.profile_list_state.select(Some(next));
|
||||
}
|
||||
true
|
||||
}
|
||||
MovementAction::Down => {
|
||||
let len = app.profile_tree.profiles.len();
|
||||
if len > 0 {
|
||||
let curr = self.profile_list_state.selected().unwrap_or(0);
|
||||
let next = if curr + 1 < len { curr + 1 } else { curr };
|
||||
self.profile_list_state.select(Some(next));
|
||||
}
|
||||
true
|
||||
}
|
||||
MovementAction::Esc => {
|
||||
self.current_focus = ProfilesPane;
|
||||
true
|
||||
}
|
||||
MovementAction::Next | MovementAction::Previous => true,
|
||||
MovementAction::Select => false,
|
||||
_ => false,
|
||||
},
|
||||
InsideTablesList => {
|
||||
let tables_len = {
|
||||
let p_idx = self
|
||||
.selected_profile_index
|
||||
.or_else(|| self.profile_list_state.selected());
|
||||
p_idx.and_then(|pi| app.profile_tree.profiles.get(pi))
|
||||
.map(|p| p.tables.len())
|
||||
.unwrap_or(0)
|
||||
};
|
||||
match action {
|
||||
MovementAction::Up => {
|
||||
if tables_len > 0 {
|
||||
let curr = self.table_list_state.selected().unwrap_or(0);
|
||||
let next = curr.saturating_sub(1);
|
||||
self.table_list_state.select(Some(next));
|
||||
}
|
||||
true
|
||||
}
|
||||
MovementAction::Down => {
|
||||
if tables_len > 0 {
|
||||
let curr = self.table_list_state.selected().unwrap_or(0);
|
||||
let next = if curr + 1 < tables_len { curr + 1 } else { curr };
|
||||
self.table_list_state.select(Some(next));
|
||||
}
|
||||
true
|
||||
}
|
||||
MovementAction::Esc => {
|
||||
self.current_focus = Tables;
|
||||
true
|
||||
}
|
||||
MovementAction::Next | MovementAction::Previous => true,
|
||||
MovementAction::Select => false,
|
||||
_ => false,
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
move_focus(&ORDER, &mut self.current_focus, action)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,6 @@
|
||||
// src/pages/admin/admin/tui.rs
|
||||
use crate::state::app::state::AppState;
|
||||
use crate::state::pages::admin::AdminState;
|
||||
use crate::pages::admin::AdminState;
|
||||
|
||||
pub fn handle_admin_selection(app_state: &mut AppState, admin_state: &AdminState) {
|
||||
let profiles = &app_state.profile_tree.profiles;
|
||||
@@ -1,7 +1,7 @@
|
||||
// src/components/admin/admin_panel_admin.rs
|
||||
// src/pages/admin/admin/ui.rs
|
||||
|
||||
use crate::config::colors::themes::Theme;
|
||||
use crate::state::pages::admin::{AdminFocus, AdminState};
|
||||
use crate::pages::admin::{AdminFocus, AdminState};
|
||||
use crate::state::app::state::AppState;
|
||||
use ratatui::{
|
||||
layout::{Alignment, Constraint, Direction, Layout, Rect},
|
||||
@@ -1,11 +1,11 @@
|
||||
// src/functions/modes/navigation/admin_nav.rs
|
||||
use crate::state::pages::admin::{AdminFocus, AdminState};
|
||||
// src/pages/admin/main/logic.rs
|
||||
use crate::pages::admin::{AdminFocus, AdminState};
|
||||
use crate::state::app::state::AppState;
|
||||
use crate::config::binds::config::Config;
|
||||
use crate::buffer::state::{BufferState, AppView};
|
||||
use crate::state::pages::add_table::{AddTableState, LinkDefinition};
|
||||
use crate::pages::admin_panel::add_table::state::{AddTableState, LinkDefinition};
|
||||
use ratatui::widgets::ListState;
|
||||
use crate::state::pages::add_logic::{AddLogicState, AddLogicFocus}; // Added AddLogicFocus import
|
||||
use crate::pages::admin_panel::add_logic::state::{AddLogicState, AddLogicFocus};
|
||||
|
||||
// Helper functions list_select_next and list_select_previous remain the same
|
||||
fn list_select_next(list_state: &mut ListState, item_count: usize) {
|
||||
7
client/src/pages/admin/main/mod.rs
Normal file
7
client/src/pages/admin/main/mod.rs
Normal file
@@ -0,0 +1,7 @@
|
||||
// src/pages/admin/main/mod.rs
|
||||
|
||||
pub mod state;
|
||||
pub mod ui;
|
||||
pub mod logic;
|
||||
|
||||
pub use state::NonAdminState;
|
||||
55
client/src/pages/admin/main/state.rs
Normal file
55
client/src/pages/admin/main/state.rs
Normal file
@@ -0,0 +1,55 @@
|
||||
// src/pages/admin/main/state.rs
|
||||
use ratatui::widgets::ListState;
|
||||
|
||||
/// State for non-admin users (simple profile browser)
|
||||
#[derive(Default, Clone, Debug)]
|
||||
pub struct NonAdminState {
|
||||
pub profiles: Vec<String>, // profile names
|
||||
pub profile_list_state: ListState, // highlight state
|
||||
pub selected_profile_index: Option<usize>, // persistent selection
|
||||
}
|
||||
|
||||
impl NonAdminState {
|
||||
pub fn get_selected_index(&self) -> Option<usize> {
|
||||
self.profile_list_state.selected()
|
||||
}
|
||||
|
||||
pub fn set_profiles(&mut self, new_profiles: Vec<String>) {
|
||||
let current_selection_index = self.profile_list_state.selected();
|
||||
self.profiles = new_profiles;
|
||||
|
||||
if self.profiles.is_empty() {
|
||||
self.profile_list_state.select(None);
|
||||
} else {
|
||||
let new_selection = match current_selection_index {
|
||||
Some(index) => Some(index.min(self.profiles.len() - 1)),
|
||||
None => Some(0),
|
||||
};
|
||||
self.profile_list_state.select(new_selection);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn next(&mut self) {
|
||||
if self.profiles.is_empty() {
|
||||
self.profile_list_state.select(None);
|
||||
return;
|
||||
}
|
||||
let i = match self.profile_list_state.selected() {
|
||||
Some(i) => if i >= self.profiles.len() - 1 { 0 } else { i + 1 },
|
||||
None => 0,
|
||||
};
|
||||
self.profile_list_state.select(Some(i));
|
||||
}
|
||||
|
||||
pub fn previous(&mut self) {
|
||||
if self.profiles.is_empty() {
|
||||
self.profile_list_state.select(None);
|
||||
return;
|
||||
}
|
||||
let i = match self.profile_list_state.selected() {
|
||||
Some(i) => if i == 0 { self.profiles.len() - 1 } else { i - 1 },
|
||||
None => self.profiles.len() - 1,
|
||||
};
|
||||
self.profile_list_state.select(Some(i));
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,9 @@
|
||||
// src/components/admin/admin_panel.rs
|
||||
// src/pages/admin/main/ui.rs
|
||||
|
||||
use crate::config::colors::themes::Theme;
|
||||
use crate::state::pages::auth::AuthState;
|
||||
use crate::state::app::state::AppState;
|
||||
use crate::state::pages::admin::AdminState;
|
||||
use crate::pages::admin::AdminState;
|
||||
use common::proto::komp_ac::table_definition::ProfileTreeResponse;
|
||||
use ratatui::{
|
||||
layout::{Constraint, Direction, Layout, Rect},
|
||||
@@ -12,7 +12,8 @@ use ratatui::{
|
||||
widgets::{Block, BorderType, Borders, List, ListItem, Paragraph, Wrap},
|
||||
Frame,
|
||||
};
|
||||
use super::admin_panel_admin::render_admin_panel_admin;
|
||||
use crate::state::pages::auth::UserRole;
|
||||
use crate::pages::admin::admin::ui::render_admin_panel_admin;
|
||||
|
||||
pub fn render_admin_panel(
|
||||
f: &mut Frame,
|
||||
@@ -44,30 +45,27 @@ pub fn render_admin_panel(
|
||||
.constraints([Constraint::Percentage(30), Constraint::Percentage(70)])
|
||||
.split(chunks[1]);
|
||||
|
||||
if auth_state.role.as_deref() != Some("admin") {
|
||||
render_admin_panel_non_admin(
|
||||
f,
|
||||
admin_state,
|
||||
&content_chunks,
|
||||
theme,
|
||||
profile_tree,
|
||||
selected_profile,
|
||||
);
|
||||
} else {
|
||||
render_admin_panel_admin(
|
||||
f,
|
||||
chunks[1],
|
||||
app_state,
|
||||
admin_state,
|
||||
theme,
|
||||
);
|
||||
match auth_state.role {
|
||||
Some(UserRole::Admin) => {
|
||||
render_admin_panel_admin(f, chunks[1], app_state, admin_state, theme);
|
||||
}
|
||||
_ => {
|
||||
render_admin_panel_non_admin(
|
||||
f,
|
||||
admin_state,
|
||||
&content_chunks,
|
||||
theme,
|
||||
profile_tree,
|
||||
selected_profile,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Renders the view for non-admin users (profile list and details).
|
||||
fn render_admin_panel_non_admin(
|
||||
f: &mut Frame,
|
||||
admin_state: &AdminState,
|
||||
admin_state: &mut AdminState,
|
||||
content_chunks: &[Rect],
|
||||
theme: &Theme,
|
||||
profile_tree: &ProfileTreeResponse,
|
||||
@@ -92,8 +90,7 @@ fn render_admin_panel_non_admin(
|
||||
.block(Block::default().title("Profiles"))
|
||||
.highlight_style(Style::default().bg(theme.highlight).fg(theme.bg));
|
||||
|
||||
let mut profile_list_state_clone = admin_state.profile_list_state.clone();
|
||||
f.render_stateful_widget(list, content_chunks[0], &mut profile_list_state_clone);
|
||||
f.render_stateful_widget(list, content_chunks[0], &mut admin_state.profile_list_state);
|
||||
|
||||
// Profile details - Use selection info from admin_state
|
||||
if let Some(profile) = admin_state
|
||||
7
client/src/pages/admin/mod.rs
Normal file
7
client/src/pages/admin/mod.rs
Normal file
@@ -0,0 +1,7 @@
|
||||
// src/pages/admin/mod.rs
|
||||
|
||||
pub mod main; // non-admin
|
||||
pub mod admin; // full admin panel
|
||||
|
||||
pub use main::NonAdminState;
|
||||
pub use admin::{AdminState, AdminFocus};
|
||||
5
client/src/pages/admin_panel/add_logic/mod.rs
Normal file
5
client/src/pages/admin_panel/add_logic/mod.rs
Normal file
@@ -0,0 +1,5 @@
|
||||
// src/pages/admin_panel/add_logic/mod.rs
|
||||
|
||||
pub mod ui;
|
||||
pub mod nav;
|
||||
pub mod state;
|
||||
531
client/src/pages/admin_panel/add_logic/nav.rs
Normal file
531
client/src/pages/admin_panel/add_logic/nav.rs
Normal file
@@ -0,0 +1,531 @@
|
||||
// src/pages/admin_panel/add_logic/nav.rs
|
||||
|
||||
use crate::config::binds::config::{Config, EditorKeybindingMode};
|
||||
use crate::state::app::state::AppState;
|
||||
use crate::pages::admin_panel::add_logic::state::{AddLogicFocus, AddLogicState};
|
||||
use crate::buffer::{AppView, BufferState};
|
||||
use crossterm::event::{KeyEvent, KeyCode, KeyModifiers};
|
||||
use crate::services::GrpcClient;
|
||||
use tokio::sync::mpsc;
|
||||
use anyhow::Result;
|
||||
use crate::components::common::text_editor::TextEditor;
|
||||
use crate::services::ui_service::UiService;
|
||||
use tui_textarea::CursorMove;
|
||||
use crate::pages::admin::AdminState;
|
||||
use crate::pages::routing::{Router, Page};
|
||||
|
||||
pub type SaveLogicResultSender = mpsc::Sender<Result<String>>;
|
||||
|
||||
pub fn handle_add_logic_navigation(
|
||||
key_event: KeyEvent,
|
||||
config: &Config,
|
||||
app_state: &mut AppState,
|
||||
buffer_state: &mut BufferState,
|
||||
grpc_client: GrpcClient,
|
||||
save_logic_sender: SaveLogicResultSender,
|
||||
command_message: &mut String,
|
||||
router: &mut Router,
|
||||
) -> bool {
|
||||
if let Page::AddLogic(add_logic_state) = &mut router.current {
|
||||
// === FULLSCREEN SCRIPT EDITING ===
|
||||
if add_logic_state.current_focus == AddLogicFocus::InsideScriptContent {
|
||||
// === AUTOCOMPLETE HANDLING ===
|
||||
if add_logic_state.script_editor_autocomplete_active {
|
||||
match key_event.code {
|
||||
KeyCode::Char(c) if c.is_alphanumeric() || c == '_' => {
|
||||
add_logic_state.script_editor_filter_text.push(c);
|
||||
add_logic_state.update_script_editor_suggestions();
|
||||
{
|
||||
let mut editor_borrow =
|
||||
add_logic_state.script_content_editor.borrow_mut();
|
||||
TextEditor::handle_input(
|
||||
&mut editor_borrow,
|
||||
key_event,
|
||||
&add_logic_state.editor_keybinding_mode,
|
||||
&mut add_logic_state.vim_state,
|
||||
);
|
||||
}
|
||||
*command_message =
|
||||
format!("Filtering: @{}", add_logic_state.script_editor_filter_text);
|
||||
return true;
|
||||
}
|
||||
KeyCode::Backspace => {
|
||||
if !add_logic_state.script_editor_filter_text.is_empty() {
|
||||
add_logic_state.script_editor_filter_text.pop();
|
||||
add_logic_state.update_script_editor_suggestions();
|
||||
{
|
||||
let mut editor_borrow =
|
||||
add_logic_state.script_content_editor.borrow_mut();
|
||||
TextEditor::handle_input(
|
||||
&mut editor_borrow,
|
||||
key_event,
|
||||
&add_logic_state.editor_keybinding_mode,
|
||||
&mut add_logic_state.vim_state,
|
||||
);
|
||||
}
|
||||
*command_message =
|
||||
if add_logic_state.script_editor_filter_text.is_empty() {
|
||||
"Autocomplete: @".to_string()
|
||||
} else {
|
||||
format!(
|
||||
"Filtering: @{}",
|
||||
add_logic_state.script_editor_filter_text
|
||||
)
|
||||
};
|
||||
} else {
|
||||
let should_deactivate =
|
||||
if let Some((trigger_line, trigger_col)) =
|
||||
add_logic_state.script_editor_trigger_position
|
||||
{
|
||||
let current_cursor = {
|
||||
let editor_borrow =
|
||||
add_logic_state.script_content_editor.borrow();
|
||||
editor_borrow.cursor()
|
||||
};
|
||||
current_cursor.0 == trigger_line
|
||||
&& current_cursor.1 == trigger_col + 1
|
||||
} else {
|
||||
false
|
||||
};
|
||||
if should_deactivate {
|
||||
add_logic_state.deactivate_script_editor_autocomplete();
|
||||
*command_message = "Autocomplete cancelled".to_string();
|
||||
}
|
||||
{
|
||||
let mut editor_borrow =
|
||||
add_logic_state.script_content_editor.borrow_mut();
|
||||
TextEditor::handle_input(
|
||||
&mut editor_borrow,
|
||||
key_event,
|
||||
&add_logic_state.editor_keybinding_mode,
|
||||
&mut add_logic_state.vim_state,
|
||||
);
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
KeyCode::Tab | KeyCode::Down => {
|
||||
if !add_logic_state.script_editor_suggestions.is_empty() {
|
||||
let current = add_logic_state
|
||||
.script_editor_selected_suggestion_index
|
||||
.unwrap_or(0);
|
||||
let next =
|
||||
(current + 1) % add_logic_state.script_editor_suggestions.len();
|
||||
add_logic_state.script_editor_selected_suggestion_index = Some(next);
|
||||
*command_message = format!(
|
||||
"Selected: {}",
|
||||
add_logic_state.script_editor_suggestions[next]
|
||||
);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
KeyCode::Up => {
|
||||
if !add_logic_state.script_editor_suggestions.is_empty() {
|
||||
let current = add_logic_state
|
||||
.script_editor_selected_suggestion_index
|
||||
.unwrap_or(0);
|
||||
let prev = if current == 0 {
|
||||
add_logic_state.script_editor_suggestions.len() - 1
|
||||
} else {
|
||||
current - 1
|
||||
};
|
||||
add_logic_state.script_editor_selected_suggestion_index = Some(prev);
|
||||
*command_message = format!(
|
||||
"Selected: {}",
|
||||
add_logic_state.script_editor_suggestions[prev]
|
||||
);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
KeyCode::Enter => {
|
||||
if let Some(selected_idx) =
|
||||
add_logic_state.script_editor_selected_suggestion_index
|
||||
{
|
||||
if let Some(suggestion) = add_logic_state
|
||||
.script_editor_suggestions
|
||||
.get(selected_idx)
|
||||
.cloned()
|
||||
{
|
||||
let trigger_pos =
|
||||
add_logic_state.script_editor_trigger_position;
|
||||
let filter_len =
|
||||
add_logic_state.script_editor_filter_text.len();
|
||||
|
||||
add_logic_state.deactivate_script_editor_autocomplete();
|
||||
add_logic_state.has_unsaved_changes = true;
|
||||
|
||||
if let Some(pos) = trigger_pos {
|
||||
let mut editor_borrow =
|
||||
add_logic_state.script_content_editor.borrow_mut();
|
||||
|
||||
if suggestion == "sql" {
|
||||
replace_autocomplete_text(
|
||||
&mut editor_borrow,
|
||||
pos,
|
||||
filter_len,
|
||||
"sql",
|
||||
);
|
||||
editor_borrow.insert_str("('')");
|
||||
editor_borrow.move_cursor(CursorMove::Back);
|
||||
editor_borrow.move_cursor(CursorMove::Back);
|
||||
*command_message = "Inserted: @sql('')".to_string();
|
||||
} else {
|
||||
let is_table_selection =
|
||||
add_logic_state.is_table_name_suggestion(&suggestion);
|
||||
replace_autocomplete_text(
|
||||
&mut editor_borrow,
|
||||
pos,
|
||||
filter_len,
|
||||
&suggestion,
|
||||
);
|
||||
|
||||
if is_table_selection {
|
||||
editor_borrow.insert_str(".");
|
||||
let new_cursor = editor_borrow.cursor();
|
||||
drop(editor_borrow);
|
||||
|
||||
add_logic_state.script_editor_trigger_position =
|
||||
Some(new_cursor);
|
||||
add_logic_state.script_editor_autocomplete_active = true;
|
||||
add_logic_state.script_editor_filter_text.clear();
|
||||
add_logic_state
|
||||
.trigger_column_autocomplete_for_table(
|
||||
suggestion.clone(),
|
||||
);
|
||||
|
||||
let profile_name =
|
||||
add_logic_state.profile_name.clone();
|
||||
let table_name_for_fetch = suggestion.clone();
|
||||
let mut client_clone = grpc_client.clone();
|
||||
tokio::spawn(async move {
|
||||
if let Err(e) = UiService::fetch_columns_for_table(
|
||||
&mut client_clone,
|
||||
&profile_name,
|
||||
&table_name_for_fetch,
|
||||
)
|
||||
.await
|
||||
{
|
||||
tracing::error!(
|
||||
"Failed to fetch columns for {}.{}: {}",
|
||||
profile_name,
|
||||
table_name_for_fetch,
|
||||
e
|
||||
);
|
||||
}
|
||||
});
|
||||
*command_message = format!(
|
||||
"Selected table '{}', fetching columns...",
|
||||
suggestion
|
||||
);
|
||||
} else {
|
||||
*command_message =
|
||||
format!("Inserted: {}", suggestion);
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
add_logic_state.deactivate_script_editor_autocomplete();
|
||||
{
|
||||
let mut editor_borrow =
|
||||
add_logic_state.script_content_editor.borrow_mut();
|
||||
TextEditor::handle_input(
|
||||
&mut editor_borrow,
|
||||
key_event,
|
||||
&add_logic_state.editor_keybinding_mode,
|
||||
&mut add_logic_state.vim_state,
|
||||
);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
KeyCode::Esc => {
|
||||
add_logic_state.deactivate_script_editor_autocomplete();
|
||||
*command_message = "Autocomplete cancelled".to_string();
|
||||
}
|
||||
_ => {
|
||||
add_logic_state.deactivate_script_editor_autocomplete();
|
||||
*command_message = "Autocomplete cancelled".to_string();
|
||||
{
|
||||
let mut editor_borrow =
|
||||
add_logic_state.script_content_editor.borrow_mut();
|
||||
TextEditor::handle_input(
|
||||
&mut editor_borrow,
|
||||
key_event,
|
||||
&add_logic_state.editor_keybinding_mode,
|
||||
&mut add_logic_state.vim_state,
|
||||
);
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Trigger autocomplete with '@'
|
||||
if key_event.code == KeyCode::Char('@') && key_event.modifiers == KeyModifiers::NONE {
|
||||
let should_trigger = match add_logic_state.editor_keybinding_mode {
|
||||
EditorKeybindingMode::Vim => {
|
||||
TextEditor::is_vim_insert_mode(&add_logic_state.vim_state)
|
||||
}
|
||||
_ => true,
|
||||
};
|
||||
if should_trigger {
|
||||
let cursor_before = {
|
||||
let editor_borrow = add_logic_state.script_content_editor.borrow();
|
||||
editor_borrow.cursor()
|
||||
};
|
||||
{
|
||||
let mut editor_borrow =
|
||||
add_logic_state.script_content_editor.borrow_mut();
|
||||
TextEditor::handle_input(
|
||||
&mut editor_borrow,
|
||||
key_event,
|
||||
&add_logic_state.editor_keybinding_mode,
|
||||
&mut add_logic_state.vim_state,
|
||||
);
|
||||
}
|
||||
add_logic_state.script_editor_trigger_position = Some(cursor_before);
|
||||
add_logic_state.script_editor_autocomplete_active = true;
|
||||
add_logic_state.script_editor_filter_text.clear();
|
||||
add_logic_state.update_script_editor_suggestions();
|
||||
add_logic_state.has_unsaved_changes = true;
|
||||
*command_message = "Autocomplete: @ (Tab/↑↓ to navigate, Enter to select, Esc to cancel)".to_string();
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
// Esc handling
|
||||
if key_event.code == KeyCode::Esc && key_event.modifiers == KeyModifiers::NONE {
|
||||
match add_logic_state.editor_keybinding_mode {
|
||||
EditorKeybindingMode::Vim => {
|
||||
let was_insert =
|
||||
TextEditor::is_vim_insert_mode(&add_logic_state.vim_state);
|
||||
{
|
||||
let mut editor_borrow =
|
||||
add_logic_state.script_content_editor.borrow_mut();
|
||||
TextEditor::handle_input(
|
||||
&mut editor_borrow,
|
||||
key_event,
|
||||
&add_logic_state.editor_keybinding_mode,
|
||||
&mut add_logic_state.vim_state,
|
||||
);
|
||||
}
|
||||
if was_insert {
|
||||
*command_message =
|
||||
"VIM: Normal Mode. Esc again to exit script.".to_string();
|
||||
} else {
|
||||
add_logic_state.current_focus =
|
||||
AddLogicFocus::ScriptContentPreview;
|
||||
app_state.ui.focus_outside_canvas = true;
|
||||
*command_message = "Exited script editing.".to_string();
|
||||
}
|
||||
}
|
||||
_ => {
|
||||
add_logic_state.current_focus = AddLogicFocus::ScriptContentPreview;
|
||||
app_state.ui.focus_outside_canvas = true;
|
||||
*command_message = "Exited script editing.".to_string();
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// Normal text input
|
||||
let changed = {
|
||||
let mut editor_borrow = add_logic_state.script_content_editor.borrow_mut();
|
||||
TextEditor::handle_input(
|
||||
&mut editor_borrow,
|
||||
key_event,
|
||||
&add_logic_state.editor_keybinding_mode,
|
||||
&mut add_logic_state.vim_state,
|
||||
)
|
||||
};
|
||||
if changed {
|
||||
add_logic_state.has_unsaved_changes = true;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
// === NON-FULLSCREEN NAVIGATION ===
|
||||
let action = config.get_general_action(key_event.code, key_event.modifiers);
|
||||
let current_focus = add_logic_state.current_focus;
|
||||
let mut handled = true;
|
||||
let mut new_focus = current_focus;
|
||||
|
||||
match action.as_deref() {
|
||||
Some("exit_table_scroll") => {
|
||||
handled = false;
|
||||
}
|
||||
Some("move_up") => {
|
||||
match current_focus {
|
||||
AddLogicFocus::InputLogicName => {}
|
||||
AddLogicFocus::InputTargetColumn => new_focus = AddLogicFocus::InputLogicName,
|
||||
AddLogicFocus::InputDescription => {
|
||||
new_focus = AddLogicFocus::InputTargetColumn
|
||||
}
|
||||
AddLogicFocus::ScriptContentPreview => {
|
||||
new_focus = AddLogicFocus::InputDescription
|
||||
}
|
||||
AddLogicFocus::SaveButton => new_focus = AddLogicFocus::ScriptContentPreview,
|
||||
AddLogicFocus::CancelButton => new_focus = AddLogicFocus::SaveButton,
|
||||
_ => handled = false,
|
||||
}
|
||||
}
|
||||
Some("move_down") => {
|
||||
match current_focus {
|
||||
AddLogicFocus::InputLogicName => {
|
||||
new_focus = AddLogicFocus::InputTargetColumn
|
||||
}
|
||||
AddLogicFocus::InputTargetColumn => {
|
||||
new_focus = AddLogicFocus::InputDescription
|
||||
}
|
||||
AddLogicFocus::InputDescription => {
|
||||
add_logic_state.last_canvas_field = 2;
|
||||
new_focus = AddLogicFocus::ScriptContentPreview;
|
||||
}
|
||||
AddLogicFocus::ScriptContentPreview => {
|
||||
new_focus = AddLogicFocus::SaveButton
|
||||
}
|
||||
AddLogicFocus::SaveButton => new_focus = AddLogicFocus::CancelButton,
|
||||
AddLogicFocus::CancelButton => {}
|
||||
_ => handled = false,
|
||||
}
|
||||
}
|
||||
Some("next_option") => {
|
||||
match current_focus {
|
||||
AddLogicFocus::InputLogicName
|
||||
| AddLogicFocus::InputTargetColumn
|
||||
| AddLogicFocus::InputDescription => {
|
||||
new_focus = AddLogicFocus::ScriptContentPreview
|
||||
}
|
||||
AddLogicFocus::ScriptContentPreview => {
|
||||
new_focus = AddLogicFocus::SaveButton
|
||||
}
|
||||
AddLogicFocus::SaveButton => new_focus = AddLogicFocus::CancelButton,
|
||||
AddLogicFocus::CancelButton => {}
|
||||
_ => handled = false,
|
||||
}
|
||||
}
|
||||
Some("previous_option") => {
|
||||
match current_focus {
|
||||
AddLogicFocus::InputLogicName
|
||||
| AddLogicFocus::InputTargetColumn
|
||||
| AddLogicFocus::InputDescription => {}
|
||||
AddLogicFocus::ScriptContentPreview => {
|
||||
new_focus = AddLogicFocus::InputDescription
|
||||
}
|
||||
AddLogicFocus::SaveButton => {
|
||||
new_focus = AddLogicFocus::ScriptContentPreview
|
||||
}
|
||||
AddLogicFocus::CancelButton => new_focus = AddLogicFocus::SaveButton,
|
||||
_ => handled = false,
|
||||
}
|
||||
}
|
||||
Some("next_field") => {
|
||||
new_focus = match current_focus {
|
||||
AddLogicFocus::InputLogicName => AddLogicFocus::InputTargetColumn,
|
||||
AddLogicFocus::InputTargetColumn => AddLogicFocus::InputDescription,
|
||||
AddLogicFocus::InputDescription => AddLogicFocus::ScriptContentPreview,
|
||||
AddLogicFocus::ScriptContentPreview => AddLogicFocus::SaveButton,
|
||||
AddLogicFocus::SaveButton => AddLogicFocus::CancelButton,
|
||||
AddLogicFocus::CancelButton => AddLogicFocus::InputLogicName,
|
||||
_ => current_focus,
|
||||
};
|
||||
}
|
||||
Some("prev_field") => {
|
||||
new_focus = match current_focus {
|
||||
AddLogicFocus::InputLogicName => AddLogicFocus::CancelButton,
|
||||
AddLogicFocus::InputTargetColumn => AddLogicFocus::InputLogicName,
|
||||
AddLogicFocus::InputDescription => AddLogicFocus::InputTargetColumn,
|
||||
AddLogicFocus::ScriptContentPreview => AddLogicFocus::InputDescription,
|
||||
AddLogicFocus::SaveButton => AddLogicFocus::ScriptContentPreview,
|
||||
AddLogicFocus::CancelButton => AddLogicFocus::SaveButton,
|
||||
_ => current_focus,
|
||||
};
|
||||
}
|
||||
Some("select") => {
|
||||
match current_focus {
|
||||
AddLogicFocus::ScriptContentPreview => {
|
||||
new_focus = AddLogicFocus::InsideScriptContent;
|
||||
app_state.ui.focus_outside_canvas = false;
|
||||
let mode_hint = match add_logic_state.editor_keybinding_mode {
|
||||
EditorKeybindingMode::Vim => {
|
||||
"VIM mode - 'i'/'a'/'o' to edit"
|
||||
}
|
||||
_ => "Enter/Ctrl+E to edit",
|
||||
};
|
||||
*command_message = format!(
|
||||
"Fullscreen script editing. {} or Esc to exit.",
|
||||
mode_hint
|
||||
);
|
||||
handled = true;
|
||||
}
|
||||
AddLogicFocus::SaveButton => {
|
||||
*command_message = "Save logic action".to_string();
|
||||
handled = true;
|
||||
}
|
||||
AddLogicFocus::CancelButton => {
|
||||
buffer_state.update_history(AppView::Admin);
|
||||
*command_message = "Cancelled Add Logic".to_string();
|
||||
handled = true;
|
||||
}
|
||||
AddLogicFocus::InputLogicName
|
||||
| AddLogicFocus::InputTargetColumn
|
||||
| AddLogicFocus::InputDescription => {
|
||||
// Focus canvas inputs; let canvas keymap handle editing
|
||||
app_state.ui.focus_outside_canvas = false;
|
||||
handled = false; // forward to canvas
|
||||
}
|
||||
_ => handled = false,
|
||||
}
|
||||
}
|
||||
Some("toggle_edit_mode") => {
|
||||
match current_focus {
|
||||
AddLogicFocus::InputLogicName
|
||||
| AddLogicFocus::InputTargetColumn
|
||||
| AddLogicFocus::InputDescription => {
|
||||
app_state.ui.focus_outside_canvas = false;
|
||||
*command_message =
|
||||
"Focus moved to input. Use i/a (Vim) or type to edit.".to_string();
|
||||
handled = true;
|
||||
}
|
||||
_ => {
|
||||
*command_message = "Cannot toggle edit mode here.".to_string();
|
||||
}
|
||||
}
|
||||
}
|
||||
_ => handled = false,
|
||||
}
|
||||
|
||||
if handled && current_focus != new_focus {
|
||||
add_logic_state.current_focus = new_focus;
|
||||
let new_is_canvas_input_focus = matches!(
|
||||
new_focus,
|
||||
AddLogicFocus::InputLogicName
|
||||
| AddLogicFocus::InputTargetColumn
|
||||
| AddLogicFocus::InputDescription
|
||||
);
|
||||
if new_is_canvas_input_focus {
|
||||
app_state.ui.focus_outside_canvas = false;
|
||||
} else {
|
||||
app_state.ui.focus_outside_canvas = true;
|
||||
}
|
||||
}
|
||||
handled
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
fn replace_autocomplete_text(
|
||||
editor: &mut tui_textarea::TextArea,
|
||||
trigger_pos: (usize, usize),
|
||||
filter_len: usize,
|
||||
replacement: &str,
|
||||
) {
|
||||
let filter_start_pos = (trigger_pos.0, trigger_pos.1 + 1);
|
||||
editor.move_cursor(CursorMove::Jump(filter_start_pos.0 as u16, filter_start_pos.1 as u16));
|
||||
for _ in 0..filter_len {
|
||||
editor.delete_next_char();
|
||||
}
|
||||
editor.insert_str(replacement);
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
// src/state/pages/add_logic.rs
|
||||
// src/pages/admin_panel/add_logic/state.rs
|
||||
use crate::config::binds::config::{EditorConfig, EditorKeybindingMode};
|
||||
use crate::components::common::text_editor::{TextEditor, VimState};
|
||||
use canvas::{DataProvider, AppMode};
|
||||
@@ -54,7 +54,7 @@ pub struct AddLogicState {
|
||||
// New fields for same-profile table names and column autocomplete
|
||||
pub same_profile_table_names: Vec<String>, // Tables from same profile only
|
||||
pub script_editor_awaiting_column_autocomplete: Option<String>, // Table name waiting for column fetch
|
||||
pub app_mode: AppMode,
|
||||
pub app_mode: canvas::AppMode,
|
||||
}
|
||||
|
||||
impl AddLogicState {
|
||||
@@ -92,7 +92,7 @@ impl AddLogicState {
|
||||
|
||||
same_profile_table_names: Vec::new(),
|
||||
script_editor_awaiting_column_autocomplete: None,
|
||||
app_mode: AppMode::Edit,
|
||||
app_mode: canvas::AppMode::Edit,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -272,7 +272,7 @@ impl AddLogicState {
|
||||
impl Default for AddLogicState {
|
||||
fn default() -> Self {
|
||||
let mut state = Self::new(&EditorConfig::default());
|
||||
state.app_mode = AppMode::Edit;
|
||||
state.app_mode = canvas::AppMode::Edit;
|
||||
state
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
// src/components/admin/add_logic.rs
|
||||
// src/pages/admin_panel/add_logic/ui.rs
|
||||
use crate::config::colors::themes::Theme;
|
||||
use crate::state::app::state::AppState;
|
||||
use crate::state::pages::add_logic::{AddLogicFocus, AddLogicState};
|
||||
use crate::pages::admin_panel::add_logic::state::{AddLogicFocus, AddLogicState};
|
||||
use canvas::{render_canvas, FormEditor};
|
||||
use ratatui::{
|
||||
layout::{Alignment, Constraint, Direction, Layout, Rect},
|
||||
@@ -10,7 +10,8 @@ use ratatui::{
|
||||
widgets::{Block, BorderType, Borders, Paragraph},
|
||||
Frame,
|
||||
};
|
||||
use crate::components::common::{dialog, autocomplete}; // Added autocomplete
|
||||
use crate::components::common::autocomplete;
|
||||
use crate::dialog;
|
||||
use crate::config::binds::config::EditorKeybindingMode;
|
||||
|
||||
pub fn render_add_logic(
|
||||
@@ -19,7 +20,6 @@ pub fn render_add_logic(
|
||||
theme: &Theme,
|
||||
app_state: &AppState,
|
||||
add_logic_state: &mut AddLogicState,
|
||||
is_edit_mode: bool,
|
||||
) {
|
||||
let main_block = Block::default()
|
||||
.title(" Add New Logic Script ")
|
||||
@@ -34,7 +34,11 @@ pub fn render_add_logic(
|
||||
// Handle full-screen script editing
|
||||
if add_logic_state.current_focus == AddLogicFocus::InsideScriptContent {
|
||||
let mut editor_ref = add_logic_state.script_content_editor.borrow_mut();
|
||||
let border_style_color = if is_edit_mode { theme.highlight } else { theme.secondary };
|
||||
let border_style_color = if crate::components::common::text_editor::TextEditor::is_vim_insert_mode(&add_logic_state.vim_state) {
|
||||
theme.highlight
|
||||
} else {
|
||||
theme.secondary
|
||||
};
|
||||
let border_style = Style::default().fg(border_style_color);
|
||||
|
||||
editor_ref.set_cursor_line_style(Style::default());
|
||||
@@ -46,7 +50,7 @@ pub fn render_add_logic(
|
||||
format!("Script {}", vim_mode_status)
|
||||
}
|
||||
EditorKeybindingMode::Emacs | EditorKeybindingMode::Default => {
|
||||
if is_edit_mode {
|
||||
if crate::components::common::text_editor::TextEditor::is_vim_insert_mode(&add_logic_state.vim_state) {
|
||||
"Script (Editing)".to_string()
|
||||
} else {
|
||||
"Script".to_string()
|
||||
@@ -161,8 +165,7 @@ pub fn render_add_logic(
|
||||
let active_field_rect = render_canvas(f, canvas_area, &editor, theme);
|
||||
|
||||
// --- Render Autocomplete for Target Column ---
|
||||
// `is_edit_mode` here refers to the general edit mode of the EventHandler
|
||||
if is_edit_mode && editor.current_field() == 1 { // Target Column field
|
||||
if editor.mode() == canvas::AppMode::Edit && editor.current_field() == 1 { // Target Column field
|
||||
if add_logic_state.in_target_column_suggestion_mode && add_logic_state.show_target_column_suggestions {
|
||||
if !add_logic_state.target_column_suggestions.is_empty() {
|
||||
if let Some(input_rect) = active_field_rect {
|
||||
@@ -1,7 +1,6 @@
|
||||
// src/tui/functions/common/add_table.rs
|
||||
use crate::state::pages::add_table::{
|
||||
AddTableFocus, AddTableState, ColumnDefinition, IndexDefinition,
|
||||
};
|
||||
// src/pages/admin_panel/add_table/logic.rs
|
||||
use crate::pages::admin_panel::add_table::state;
|
||||
use crate::pages::admin_panel::add_table::state::{AddTableState, AddTableFocus, IndexDefinition, ColumnDefinition};
|
||||
use crate::services::GrpcClient;
|
||||
use anyhow::{anyhow, Result};
|
||||
use common::proto::komp_ac::table_definition::{
|
||||
6
client/src/pages/admin_panel/add_table/mod.rs
Normal file
6
client/src/pages/admin_panel/add_table/mod.rs
Normal file
@@ -0,0 +1,6 @@
|
||||
// src/pages/admin_panel/add_table/mod.rs
|
||||
|
||||
pub mod ui;
|
||||
pub mod nav;
|
||||
pub mod state;
|
||||
pub mod logic;
|
||||
@@ -1,12 +1,13 @@
|
||||
// src/functions/modes/navigation/add_table_nav.rs
|
||||
// src/pages/admin_panel/add_table/nav.rs
|
||||
|
||||
use crate::config::binds::config::Config;
|
||||
use crate::state::{
|
||||
app::state::AppState,
|
||||
pages::add_table::{AddTableFocus, AddTableState},
|
||||
};
|
||||
use crate::pages::admin_panel::add_table::state::{AddTableFocus, AddTableState};
|
||||
use crossterm::event::{KeyEvent};
|
||||
use ratatui::widgets::TableState;
|
||||
use crate::tui::functions::common::add_table::{handle_add_column_action, handle_save_table_action};
|
||||
use crate::pages::admin_panel::add_table::logic::{handle_add_column_action, handle_save_table_action};
|
||||
use crate::ui::handlers::context::DialogPurpose;
|
||||
use crate::services::GrpcClient;
|
||||
use tokio::sync::mpsc;
|
||||
383
client/src/pages/admin_panel/add_table/state.rs
Normal file
383
client/src/pages/admin_panel/add_table/state.rs
Normal file
@@ -0,0 +1,383 @@
|
||||
// src/pages/admin_panel/add_table/state.rs
|
||||
|
||||
use canvas::{DataProvider, AppMode};
|
||||
use ratatui::widgets::TableState;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use crate::movement::{move_focus, MovementAction};
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct ColumnDefinition {
|
||||
pub name: String,
|
||||
pub data_type: String,
|
||||
pub selected: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct IndexDefinition {
|
||||
pub name: String,
|
||||
pub selected: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct LinkDefinition {
|
||||
pub linked_table_name: String,
|
||||
pub is_required: bool,
|
||||
pub selected: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
|
||||
pub enum AddTableFocus {
|
||||
#[default]
|
||||
InputTableName, // Field 0 for CanvasState
|
||||
InputColumnName, // Field 1 for CanvasState
|
||||
InputColumnType, // Field 2 for CanvasState
|
||||
AddColumnButton,
|
||||
// Result Tables
|
||||
ColumnsTable,
|
||||
IndexesTable,
|
||||
LinksTable,
|
||||
// Inside Tables (Scrolling Focus)
|
||||
InsideColumnsTable,
|
||||
InsideIndexesTable,
|
||||
InsideLinksTable,
|
||||
// Buttons
|
||||
SaveButton,
|
||||
DeleteSelectedButton,
|
||||
CancelButton,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct AddTableState {
|
||||
pub profile_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 indexes: Vec<IndexDefinition>,
|
||||
pub links: Vec<LinkDefinition>,
|
||||
pub current_focus: AddTableFocus,
|
||||
pub last_canvas_field: usize,
|
||||
pub column_table_state: TableState,
|
||||
pub index_table_state: TableState,
|
||||
pub link_table_state: TableState,
|
||||
pub table_name_cursor_pos: usize,
|
||||
pub column_name_cursor_pos: usize,
|
||||
pub column_type_cursor_pos: usize,
|
||||
pub has_unsaved_changes: bool,
|
||||
pub app_mode: canvas::AppMode,
|
||||
}
|
||||
|
||||
impl Default for AddTableState {
|
||||
fn default() -> Self {
|
||||
AddTableState {
|
||||
profile_name: "default".to_string(),
|
||||
table_name: String::new(),
|
||||
table_name_input: String::new(),
|
||||
column_name_input: String::new(),
|
||||
column_type_input: String::new(),
|
||||
columns: Vec::new(),
|
||||
indexes: Vec::new(),
|
||||
links: Vec::new(),
|
||||
current_focus: AddTableFocus::InputTableName,
|
||||
last_canvas_field: 2,
|
||||
column_table_state: TableState::default(),
|
||||
index_table_state: TableState::default(),
|
||||
link_table_state: TableState::default(),
|
||||
table_name_cursor_pos: 0,
|
||||
column_name_cursor_pos: 0,
|
||||
column_type_cursor_pos: 0,
|
||||
has_unsaved_changes: false,
|
||||
app_mode: canvas::AppMode::Edit,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl AddTableState {
|
||||
pub const INPUT_FIELD_COUNT: usize = 3;
|
||||
|
||||
/// Helper method to add a column from current inputs
|
||||
pub fn add_column_from_inputs(&mut self) -> Option<String> {
|
||||
if self.column_name_input.trim().is_empty() || self.column_type_input.trim().is_empty() {
|
||||
return Some("Both column name and type are required".to_string());
|
||||
}
|
||||
|
||||
// Check for duplicate column names
|
||||
if self.columns.iter().any(|col| col.name == self.column_name_input.trim()) {
|
||||
return Some("Column name already exists".to_string());
|
||||
}
|
||||
|
||||
// Add the column
|
||||
self.columns.push(ColumnDefinition {
|
||||
name: self.column_name_input.trim().to_string(),
|
||||
data_type: self.column_type_input.trim().to_string(),
|
||||
selected: false,
|
||||
});
|
||||
|
||||
// Clear inputs and reset focus to column name for next entry
|
||||
self.column_name_input.clear();
|
||||
self.column_type_input.clear();
|
||||
self.column_name_cursor_pos = 0;
|
||||
self.column_type_cursor_pos = 0;
|
||||
self.current_focus = AddTableFocus::InputColumnName;
|
||||
self.last_canvas_field = 1;
|
||||
self.has_unsaved_changes = true;
|
||||
|
||||
Some(format!("Column '{}' added successfully", self.columns.last().unwrap().name))
|
||||
}
|
||||
|
||||
/// Helper method to delete selected items
|
||||
pub fn delete_selected_items(&mut self) -> Option<String> {
|
||||
let mut deleted_items = Vec::new();
|
||||
|
||||
// Remove selected columns
|
||||
let initial_column_count = self.columns.len();
|
||||
self.columns.retain(|col| {
|
||||
if col.selected {
|
||||
deleted_items.push(format!("column '{}'", col.name));
|
||||
false
|
||||
} else {
|
||||
true
|
||||
}
|
||||
});
|
||||
|
||||
// Remove selected indexes
|
||||
let initial_index_count = self.indexes.len();
|
||||
self.indexes.retain(|idx| {
|
||||
if idx.selected {
|
||||
deleted_items.push(format!("index '{}'", idx.name));
|
||||
false
|
||||
} else {
|
||||
true
|
||||
}
|
||||
});
|
||||
|
||||
// Remove selected links
|
||||
let initial_link_count = self.links.len();
|
||||
self.links.retain(|link| {
|
||||
if link.selected {
|
||||
deleted_items.push(format!("link to '{}'", link.linked_table_name));
|
||||
false
|
||||
} else {
|
||||
true
|
||||
}
|
||||
});
|
||||
|
||||
if deleted_items.is_empty() {
|
||||
Some("No items selected for deletion".to_string())
|
||||
} else {
|
||||
self.has_unsaved_changes = true;
|
||||
Some(format!("Deleted: {}", deleted_items.join(", ")))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl DataProvider for AddTableState {
|
||||
fn field_count(&self) -> usize {
|
||||
3 // Table name, Column name, Column type
|
||||
}
|
||||
|
||||
fn field_name(&self, index: usize) -> &str {
|
||||
match index {
|
||||
0 => "Table name",
|
||||
1 => "Name",
|
||||
2 => "Type",
|
||||
_ => "",
|
||||
}
|
||||
}
|
||||
|
||||
fn field_value(&self, index: usize) -> &str {
|
||||
match index {
|
||||
0 => &self.table_name_input,
|
||||
1 => &self.column_name_input,
|
||||
2 => &self.column_type_input,
|
||||
_ => "",
|
||||
}
|
||||
}
|
||||
|
||||
fn set_field_value(&mut self, index: usize, value: String) {
|
||||
match index {
|
||||
0 => self.table_name_input = value,
|
||||
1 => self.column_name_input = value,
|
||||
2 => self.column_type_input = value,
|
||||
_ => {}
|
||||
}
|
||||
self.has_unsaved_changes = true;
|
||||
}
|
||||
|
||||
fn supports_suggestions(&self, _field_index: usize) -> bool {
|
||||
false // AddTableState doesn’t use suggestions
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
impl AddTableState {
|
||||
pub fn handle_movement(&mut self, action: MovementAction) -> bool {
|
||||
use AddTableFocus::*;
|
||||
|
||||
// Linear outer focus order
|
||||
const ORDER: [AddTableFocus; 10] = [
|
||||
InputTableName,
|
||||
InputColumnName,
|
||||
InputColumnType,
|
||||
AddColumnButton,
|
||||
ColumnsTable,
|
||||
IndexesTable,
|
||||
LinksTable,
|
||||
SaveButton,
|
||||
DeleteSelectedButton,
|
||||
CancelButton,
|
||||
];
|
||||
|
||||
// Enter "inside" on Select from outer panes
|
||||
match (self.current_focus, action) {
|
||||
(ColumnsTable, MovementAction::Select) => {
|
||||
if !self.columns.is_empty() && self.column_table_state.selected().is_none() {
|
||||
self.column_table_state.select(Some(0));
|
||||
}
|
||||
self.current_focus = InsideColumnsTable;
|
||||
return true;
|
||||
}
|
||||
(IndexesTable, MovementAction::Select) => {
|
||||
if !self.indexes.is_empty() && self.index_table_state.selected().is_none() {
|
||||
self.index_table_state.select(Some(0));
|
||||
}
|
||||
self.current_focus = InsideIndexesTable;
|
||||
return true;
|
||||
}
|
||||
(LinksTable, MovementAction::Select) => {
|
||||
if !self.links.is_empty() && self.link_table_state.selected().is_none() {
|
||||
self.link_table_state.select(Some(0));
|
||||
}
|
||||
self.current_focus = InsideLinksTable;
|
||||
return true;
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
||||
// Handle "inside" states: Up/Down/Select/Esc; block outer movement keys
|
||||
match self.current_focus {
|
||||
InsideColumnsTable => {
|
||||
match action {
|
||||
MovementAction::Up => {
|
||||
if let Some(i) = self.column_table_state.selected() {
|
||||
let next = i.saturating_sub(1);
|
||||
self.column_table_state.select(Some(next));
|
||||
} else if !self.columns.is_empty() {
|
||||
self.column_table_state.select(Some(0));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
MovementAction::Down => {
|
||||
if let Some(i) = self.column_table_state.selected() {
|
||||
let last = self.columns.len().saturating_sub(1);
|
||||
let next = if i < last { i + 1 } else { i };
|
||||
self.column_table_state.select(Some(next));
|
||||
} else if !self.columns.is_empty() {
|
||||
self.column_table_state.select(Some(0));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
MovementAction::Select => {
|
||||
if let Some(i) = self.column_table_state.selected() {
|
||||
if let Some(col) = self.columns.get_mut(i) {
|
||||
col.selected = !col.selected;
|
||||
self.has_unsaved_changes = true;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
MovementAction::Esc => {
|
||||
self.column_table_state.select(None);
|
||||
self.current_focus = ColumnsTable;
|
||||
return true;
|
||||
}
|
||||
MovementAction::Next | MovementAction::Previous => return true, // block outer moves
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
InsideIndexesTable => {
|
||||
match action {
|
||||
MovementAction::Up => {
|
||||
if let Some(i) = self.index_table_state.selected() {
|
||||
let next = i.saturating_sub(1);
|
||||
self.index_table_state.select(Some(next));
|
||||
} else if !self.indexes.is_empty() {
|
||||
self.index_table_state.select(Some(0));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
MovementAction::Down => {
|
||||
if let Some(i) = self.index_table_state.selected() {
|
||||
let last = self.indexes.len().saturating_sub(1);
|
||||
let next = if i < last { i + 1 } else { i };
|
||||
self.index_table_state.select(Some(next));
|
||||
} else if !self.indexes.is_empty() {
|
||||
self.index_table_state.select(Some(0));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
MovementAction::Select => {
|
||||
if let Some(i) = self.index_table_state.selected() {
|
||||
if let Some(ix) = self.indexes.get_mut(i) {
|
||||
ix.selected = !ix.selected;
|
||||
self.has_unsaved_changes = true;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
MovementAction::Esc => {
|
||||
self.index_table_state.select(None);
|
||||
self.current_focus = IndexesTable;
|
||||
return true;
|
||||
}
|
||||
MovementAction::Next | MovementAction::Previous => return true, // block outer moves
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
InsideLinksTable => {
|
||||
match action {
|
||||
MovementAction::Up => {
|
||||
if let Some(i) = self.link_table_state.selected() {
|
||||
let next = i.saturating_sub(1);
|
||||
self.link_table_state.select(Some(next));
|
||||
} else if !self.links.is_empty() {
|
||||
self.link_table_state.select(Some(0));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
MovementAction::Down => {
|
||||
if let Some(i) = self.link_table_state.selected() {
|
||||
let last = self.links.len().saturating_sub(1);
|
||||
let next = if i < last { i + 1 } else { i };
|
||||
self.link_table_state.select(Some(next));
|
||||
} else if !self.links.is_empty() {
|
||||
self.link_table_state.select(Some(0));
|
||||
}
|
||||
return true;
|
||||
}
|
||||
MovementAction::Select => {
|
||||
if let Some(i) = self.link_table_state.selected() {
|
||||
if let Some(link) = self.links.get_mut(i) {
|
||||
link.selected = !link.selected;
|
||||
self.has_unsaved_changes = true;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
MovementAction::Esc => {
|
||||
self.link_table_state.select(None);
|
||||
self.current_focus = LinksTable;
|
||||
return true;
|
||||
}
|
||||
MovementAction::Next | MovementAction::Previous => return true, // block outer moves
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
|
||||
// Default: outer navigation via helper
|
||||
move_focus(&ORDER, &mut self.current_focus, action)
|
||||
}
|
||||
}
|
||||
@@ -1,7 +1,7 @@
|
||||
// src/components/admin/add_table.rs
|
||||
// src/pages/admin_panel/add_table/ui.rs
|
||||
use crate::config::colors::themes::Theme;
|
||||
use crate::state::app::state::AppState;
|
||||
use crate::state::pages::add_table::{AddTableFocus, AddTableState};
|
||||
use crate::pages::admin_panel::add_table::state::{AddTableFocus, AddTableState};
|
||||
use canvas::{render_canvas, FormEditor};
|
||||
use ratatui::{
|
||||
layout::{Alignment, Constraint, Direction, Layout, Rect},
|
||||
@@ -10,7 +10,7 @@ use ratatui::{
|
||||
widgets::{Block, BorderType, Borders, Cell, Paragraph, Row, Table},
|
||||
Frame,
|
||||
};
|
||||
use crate::components::common::dialog;
|
||||
use crate::dialog;
|
||||
|
||||
/// Renders the Add New Table page layout, structuring the display of table information,
|
||||
/// input fields, and action buttons. Adapts layout based on terminal width.
|
||||
@@ -20,7 +20,6 @@ pub fn render_add_table(
|
||||
theme: &Theme,
|
||||
app_state: &AppState,
|
||||
add_table_state: &mut AddTableState,
|
||||
is_edit_mode: bool, // Determines if canvas inputs are in edit mode
|
||||
) {
|
||||
// --- Configuration ---
|
||||
// Threshold width to switch between wide and narrow layouts
|
||||
4
client/src/pages/admin_panel/mod.rs
Normal file
4
client/src/pages/admin_panel/mod.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
// src/pages/admin_panel/mod.rs
|
||||
|
||||
pub mod add_table;
|
||||
pub mod add_logic;
|
||||
@@ -1,6 +1,7 @@
|
||||
// src/tui/functions/common/form.rs
|
||||
// src/pages/forms/logic.rs
|
||||
use crate::services::grpc_client::GrpcClient;
|
||||
use crate::state::app::state::AppState;
|
||||
use crate::pages::forms::FormState;
|
||||
use crate::utils::data_converter;
|
||||
use anyhow::{anyhow, Context, Result};
|
||||
use std::collections::HashMap;
|
||||
@@ -14,14 +15,14 @@ pub enum SaveOutcome {
|
||||
|
||||
pub async fn save(
|
||||
app_state: &mut AppState,
|
||||
path: &str,
|
||||
grpc_client: &mut GrpcClient,
|
||||
) -> Result<SaveOutcome> {
|
||||
if let Some(fs) = app_state.form_state_mut() {
|
||||
if let Some(fs) = app_state.form_state_for_path(path) {
|
||||
if !fs.has_unsaved_changes {
|
||||
return Ok(SaveOutcome::NoChange);
|
||||
}
|
||||
|
||||
// Copy out what we need before dropping the mutable borrow
|
||||
let profile_name = fs.profile_name.clone();
|
||||
let table_name = fs.table_name.clone();
|
||||
let fields = fs.fields.clone();
|
||||
@@ -62,7 +63,7 @@ pub async fn save(
|
||||
.context("Failed to post new table data")?;
|
||||
|
||||
if response.success {
|
||||
if let Some(fs) = app_state.form_state_mut() {
|
||||
if let Some(fs) = app_state.form_state_for_path(path) {
|
||||
fs.id = response.inserted_id;
|
||||
fs.total_count += 1;
|
||||
fs.current_position = fs.total_count;
|
||||
@@ -75,7 +76,7 @@ pub async fn save(
|
||||
} else {
|
||||
if id == 0 {
|
||||
return Err(anyhow!(
|
||||
"Cannot update record: ID is 0, but not classified as new entry."
|
||||
"Cannot update record: ID is 0, but not classified as new entry."
|
||||
));
|
||||
}
|
||||
let response = grpc_client
|
||||
@@ -84,7 +85,7 @@ pub async fn save(
|
||||
.context("Failed to put (update) table data")?;
|
||||
|
||||
if response.success {
|
||||
if let Some(fs) = app_state.form_state_mut() {
|
||||
if let Some(fs) = app_state.form_state_for_path(path) {
|
||||
fs.has_unsaved_changes = false;
|
||||
}
|
||||
SaveOutcome::UpdatedExisting
|
||||
@@ -101,12 +102,13 @@ pub async fn save(
|
||||
|
||||
pub async fn revert(
|
||||
app_state: &mut AppState,
|
||||
path: &str,
|
||||
grpc_client: &mut GrpcClient,
|
||||
) -> Result<String> {
|
||||
if let Some(fs) = app_state.form_state_mut() {
|
||||
if let Some(fs) = app_state.form_state_for_path(path) {
|
||||
if fs.id == 0
|
||||
|| (fs.total_count > 0 && fs.current_position > fs.total_count)
|
||||
|| (fs.total_count == 0 && fs.current_position == 1)
|
||||
|| (fs.total_count == 0 && fs.current_position == 1)
|
||||
{
|
||||
let old_total_count = fs.total_count;
|
||||
fs.reset_to_empty();
|
||||
@@ -136,8 +138,8 @@ pub async fn revert(
|
||||
)
|
||||
.await
|
||||
.context(format!(
|
||||
"Failed to get table data by position {} for table {}.{}",
|
||||
fs.current_position, fs.profile_name, fs.table_name
|
||||
"Failed to get table data by position {} for table {}.{}",
|
||||
fs.current_position, fs.profile_name, fs.table_name
|
||||
))?;
|
||||
|
||||
fs.update_from_response(&response.data, fs.current_position);
|
||||
@@ -146,3 +148,37 @@ pub async fn revert(
|
||||
Ok("Nothing to revert".to_string())
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn handle_action(
|
||||
action: &str,
|
||||
form_state: &mut FormState,
|
||||
_grpc_client: &mut GrpcClient,
|
||||
ideal_cursor_column: &mut usize,
|
||||
) -> Result<String> {
|
||||
if form_state.has_unsaved_changes() {
|
||||
return Ok(
|
||||
"Unsaved changes. Save (Ctrl+S) or Revert (Ctrl+R) before navigating."
|
||||
.to_string(),
|
||||
);
|
||||
}
|
||||
|
||||
let total_count = form_state.total_count;
|
||||
|
||||
match action {
|
||||
"previous_entry" => {
|
||||
if form_state.current_position > 1 {
|
||||
form_state.current_position -= 1;
|
||||
*ideal_cursor_column = 0;
|
||||
}
|
||||
}
|
||||
"next_entry" => {
|
||||
if form_state.current_position <= total_count {
|
||||
form_state.current_position += 1;
|
||||
*ideal_cursor_column = 0;
|
||||
}
|
||||
}
|
||||
_ => return Err(anyhow!("Unknown form action: {}", action)),
|
||||
}
|
||||
|
||||
Ok(String::new())
|
||||
}
|
||||
9
client/src/pages/forms/mod.rs
Normal file
9
client/src/pages/forms/mod.rs
Normal file
@@ -0,0 +1,9 @@
|
||||
// src/pages/forms/mod.rs
|
||||
|
||||
pub mod ui;
|
||||
pub mod state;
|
||||
pub mod logic;
|
||||
|
||||
pub use ui::*;
|
||||
pub use state::*;
|
||||
pub use logic::*;
|
||||
@@ -1,4 +1,4 @@
|
||||
// src/state/pages/form.rs
|
||||
// src/pages/forms/state.rs
|
||||
|
||||
use canvas::{DataProvider, AppMode};
|
||||
use common::proto::komp_ac::search::search_response::Hit;
|
||||
@@ -21,7 +21,7 @@ pub struct FieldDefinition {
|
||||
pub link_target_table: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct FormState {
|
||||
pub id: i64,
|
||||
pub profile_name: String,
|
||||
@@ -72,7 +72,7 @@ impl FormState {
|
||||
selected_suggestion_index: None,
|
||||
autocomplete_loading: false,
|
||||
link_display_map: HashMap::new(),
|
||||
app_mode: AppMode::Edit,
|
||||
app_mode: canvas::AppMode::Edit,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,7 +1,5 @@
|
||||
// src/components/form/form.rs
|
||||
// src/pages/forms/ui.rs
|
||||
use crate::config::colors::themes::Theme;
|
||||
use crate::state::app::state::AppState;
|
||||
use crate::state::pages::form::FormState;
|
||||
use ratatui::{
|
||||
layout::{Alignment, Constraint, Direction, Layout, Margin, Rect},
|
||||
style::Style,
|
||||
@@ -9,14 +7,14 @@ use ratatui::{
|
||||
Frame,
|
||||
};
|
||||
use canvas::{
|
||||
render_canvas, render_suggestions_dropdown, DefaultCanvasTheme,
|
||||
render_canvas, render_suggestions_dropdown, DefaultCanvasTheme, FormEditor,
|
||||
};
|
||||
use crate::pages::forms::FormState;
|
||||
|
||||
pub fn render_form(
|
||||
pub fn render_form_page(
|
||||
f: &mut Frame,
|
||||
area: Rect,
|
||||
app_state: &AppState,
|
||||
form_state: &FormState, // not needed directly anymore, editor holds it
|
||||
editor: &FormEditor<FormState>,
|
||||
table_name: &str,
|
||||
theme: &Theme,
|
||||
total_count: u64,
|
||||
@@ -61,18 +59,14 @@ pub fn render_form(
|
||||
f.render_widget(count_para, main_layout[0]);
|
||||
|
||||
// --- FORM RENDERING (Using persistent FormEditor) ---
|
||||
if let Some(editor) = &app_state.form_editor {
|
||||
let active_field_rect = render_canvas(f, main_layout[1], editor, theme);
|
||||
|
||||
// --- SUGGESTIONS DROPDOWN ---
|
||||
if let Some(active_rect) = active_field_rect {
|
||||
render_suggestions_dropdown(
|
||||
f,
|
||||
main_layout[1],
|
||||
active_rect,
|
||||
&DefaultCanvasTheme,
|
||||
editor,
|
||||
);
|
||||
}
|
||||
let active_field_rect = render_canvas(f, main_layout[1], editor, theme);
|
||||
if let Some(active_rect) = active_field_rect {
|
||||
render_suggestions_dropdown(
|
||||
f,
|
||||
main_layout[1],
|
||||
active_rect,
|
||||
&DefaultCanvasTheme,
|
||||
editor,
|
||||
);
|
||||
}
|
||||
}
|
||||
76
client/src/pages/intro/logic.rs
Normal file
76
client/src/pages/intro/logic.rs
Normal file
@@ -0,0 +1,76 @@
|
||||
// src/pages/intro/logic.rs
|
||||
use crate::state::app::state::AppState;
|
||||
use crate::buffer::state::{AppView, BufferState};
|
||||
|
||||
/// Handles intro screen selection by updating view history and managing focus state.
|
||||
/// 0: Continue (restores last form or default)
|
||||
/// 1: Admin view
|
||||
/// 2: Login view
|
||||
/// 3: Register view (with focus reset)
|
||||
pub fn handle_intro_selection(
|
||||
app_state: &mut AppState,
|
||||
buffer_state: &mut BufferState,
|
||||
index: usize,
|
||||
) {
|
||||
match index {
|
||||
// Continue: go to the most recent existing Form tab, or open a sensible default
|
||||
0 => {
|
||||
// 1) Try to switch to an already open Form buffer (most recent)
|
||||
if let Some(existing_path) = buffer_state
|
||||
.history
|
||||
.iter()
|
||||
.rev()
|
||||
.find_map(|view| {
|
||||
if let AppView::Form(p) = view {
|
||||
Some(p.clone())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
})
|
||||
{
|
||||
buffer_state.update_history(AppView::Form(existing_path));
|
||||
return;
|
||||
}
|
||||
|
||||
// 2) Otherwise pick a fallback path
|
||||
let fallback_path = if let (Some(profile), Some(table)) = (
|
||||
app_state.current_view_profile_name.clone(),
|
||||
app_state.current_view_table_name.clone(),
|
||||
) {
|
||||
Some(format!("{}/{}", profile, table))
|
||||
} else if let Some(any_key) = app_state.form_editor.keys().next().cloned() {
|
||||
// Use any existing editor key if available
|
||||
Some(any_key)
|
||||
} else {
|
||||
// Otherwise pick the first available table from the profile tree
|
||||
let mut found: Option<String> = None;
|
||||
for prof in &app_state.profile_tree.profiles {
|
||||
if let Some(tbl) = prof.tables.first() {
|
||||
found = Some(format!("{}/{}", prof.name, tbl.name));
|
||||
break;
|
||||
}
|
||||
}
|
||||
found
|
||||
};
|
||||
|
||||
if let Some(path) = fallback_path {
|
||||
buffer_state.update_history(AppView::Form(path));
|
||||
} else {
|
||||
// No sensible default; stay on Intro
|
||||
}
|
||||
}
|
||||
1 => {
|
||||
buffer_state.update_history(AppView::Admin);
|
||||
}
|
||||
2 => {
|
||||
buffer_state.update_history(AppView::Login);
|
||||
}
|
||||
3 => {
|
||||
buffer_state.update_history(AppView::Register);
|
||||
// Register view requires focus reset
|
||||
app_state.ui.focus_outside_canvas = false;
|
||||
app_state.focused_button_index = 0;
|
||||
}
|
||||
_ => return,
|
||||
}
|
||||
}
|
||||
9
client/src/pages/intro/mod.rs
Normal file
9
client/src/pages/intro/mod.rs
Normal file
@@ -0,0 +1,9 @@
|
||||
// src/pages/intro/mod.rs
|
||||
|
||||
pub mod state;
|
||||
pub mod ui;
|
||||
pub mod logic;
|
||||
|
||||
pub use state::*;
|
||||
pub use ui::render_intro;
|
||||
pub use logic::*;
|
||||
48
client/src/pages/intro/state.rs
Normal file
48
client/src/pages/intro/state.rs
Normal file
@@ -0,0 +1,48 @@
|
||||
// src/state/pages/intro.rs
|
||||
use crate::movement::MovementAction;
|
||||
|
||||
#[derive(Default, Clone, Debug)]
|
||||
pub struct IntroState {
|
||||
pub selected_option: usize,
|
||||
}
|
||||
|
||||
impl IntroState {
|
||||
pub fn new() -> Self {
|
||||
Self::default()
|
||||
}
|
||||
|
||||
pub fn next_option(&mut self) {
|
||||
if self.selected_option < 3 {
|
||||
self.selected_option += 1;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn previous_option(&mut self) {
|
||||
if self.selected_option > 0 {
|
||||
self.selected_option -= 1
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl IntroState {
|
||||
pub fn handle_movement(&mut self, action: MovementAction) -> bool {
|
||||
match action {
|
||||
MovementAction::Next | MovementAction::Right | MovementAction::Down => {
|
||||
self.next_option();
|
||||
true
|
||||
}
|
||||
MovementAction::Previous | MovementAction::Left | MovementAction::Up => {
|
||||
self.previous_option();
|
||||
true
|
||||
}
|
||||
MovementAction::Select => {
|
||||
// Actual selection handled in event loop (UiContext::Intro)
|
||||
false
|
||||
}
|
||||
MovementAction::Esc => {
|
||||
// Nothing special for Intro, but could be used to quit
|
||||
true
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
// src/components/intro/intro.rs
|
||||
// src/pages/intro/ui.rs
|
||||
use ratatui::{
|
||||
layout::{Alignment, Constraint, Direction, Layout, Rect},
|
||||
style::Style,
|
||||
@@ -8,7 +8,7 @@ use ratatui::{
|
||||
Frame,
|
||||
};
|
||||
use crate::config::colors::themes::Theme;
|
||||
use crate::state::pages::intro::IntroState;
|
||||
use crate::pages::intro::IntroState;
|
||||
|
||||
pub fn render_intro(f: &mut Frame, intro_state: &IntroState, area: Rect, theme: &Theme) {
|
||||
let block = Block::default()
|
||||
@@ -1,14 +1,15 @@
|
||||
// src/tui/functions/common/login.rs
|
||||
// src/pages/login/logic.rs
|
||||
|
||||
use crate::services::auth::AuthClient;
|
||||
use crate::state::pages::auth::AuthState;
|
||||
use crate::state::pages::auth::LoginState;
|
||||
use crate::state::app::state::AppState;
|
||||
use crate::buffer::state::{AppView, BufferState};
|
||||
use crate::config::storage::storage::{StoredAuthData, save_auth_data};
|
||||
use crate::ui::handlers::context::DialogPurpose;
|
||||
use common::proto::komp_ac::auth::LoginResponse;
|
||||
use anyhow::{Context, Result};
|
||||
use crate::pages::login::LoginFormState;
|
||||
use crate::state::pages::auth::UserRole;
|
||||
use anyhow::{Context, Result, anyhow};
|
||||
use tokio::spawn;
|
||||
use tokio::sync::mpsc;
|
||||
use tracing::{info, error};
|
||||
@@ -24,15 +25,14 @@ pub enum LoginResult {
|
||||
/// Updates AuthState and AppState on success or failure.
|
||||
pub async fn save(
|
||||
auth_state: &mut AuthState,
|
||||
login_state: &mut LoginState,
|
||||
login_state: &mut LoginFormState,
|
||||
auth_client: &mut AuthClient,
|
||||
app_state: &mut AppState,
|
||||
) -> Result<String> {
|
||||
let identifier = login_state.username.clone();
|
||||
let password = login_state.password.clone();
|
||||
let identifier = login_state.username().to_string();
|
||||
let password = login_state.password().to_string();
|
||||
|
||||
// --- Client-side validation ---
|
||||
// Prevent login attempt if the identifier field is empty or whitespace.
|
||||
if identifier.trim().is_empty() {
|
||||
let error_message = "Username/Email cannot be empty.".to_string();
|
||||
app_state.show_dialog(
|
||||
@@ -41,33 +41,33 @@ pub async fn save(
|
||||
vec!["OK".to_string()],
|
||||
DialogPurpose::LoginFailed,
|
||||
);
|
||||
login_state.error_message = Some(error_message.clone());
|
||||
return Err(anyhow::anyhow!(error_message));
|
||||
login_state.set_error_message(Some(error_message.clone()));
|
||||
return Err(anyhow!(error_message));
|
||||
}
|
||||
|
||||
// Clear previous error/dialog state before attempting
|
||||
login_state.error_message = None;
|
||||
app_state.hide_dialog(); // Hide any previous dialog
|
||||
login_state.set_error_message(None);
|
||||
app_state.hide_dialog();
|
||||
|
||||
// Call the gRPC login method
|
||||
match auth_client.login(identifier.clone(), password).await
|
||||
.with_context(|| format!("gRPC login attempt failed for identifier: {}", identifier))
|
||||
{
|
||||
Ok(response) => {
|
||||
// Store authentication details using correct field names
|
||||
// Store authentication details
|
||||
auth_state.auth_token = Some(response.access_token.clone());
|
||||
auth_state.user_id = Some(response.user_id.clone());
|
||||
auth_state.role = Some(response.role.clone());
|
||||
auth_state.role = Some(UserRole::from_str(&response.role));
|
||||
auth_state.decoded_username = Some(response.username.clone());
|
||||
login_state.set_has_unsaved_changes(false);
|
||||
login_state.error_message = None;
|
||||
|
||||
// Format the success message using response data
|
||||
login_state.set_has_unsaved_changes(false);
|
||||
login_state.set_error_message(None);
|
||||
|
||||
let success_message = format!(
|
||||
"Login Successful!\n\n\
|
||||
Username: {}\n\
|
||||
User ID: {}\n\
|
||||
Role: {}",
|
||||
Username: {}\n\
|
||||
User ID: {}\n\
|
||||
Role: {}",
|
||||
response.username,
|
||||
response.user_id,
|
||||
response.role
|
||||
@@ -79,9 +79,11 @@ pub async fn save(
|
||||
vec!["Menu".to_string(), "Exit".to_string()],
|
||||
DialogPurpose::LoginSuccess,
|
||||
);
|
||||
login_state.password.clear();
|
||||
login_state.username.clear();
|
||||
login_state.current_cursor_pos = 0;
|
||||
|
||||
login_state.username_mut().clear();
|
||||
login_state.password_mut().clear();
|
||||
login_state.set_current_cursor_pos(0);
|
||||
|
||||
Ok("Login successful, details shown in dialog.".to_string())
|
||||
}
|
||||
Err(e) => {
|
||||
@@ -92,10 +94,10 @@ pub async fn save(
|
||||
vec!["OK".to_string()],
|
||||
DialogPurpose::LoginFailed,
|
||||
);
|
||||
login_state.error_message = Some(error_message.clone());
|
||||
login_state.set_error_message(Some(error_message.clone()));
|
||||
login_state.set_has_unsaved_changes(true);
|
||||
login_state.username.clear();
|
||||
login_state.password.clear();
|
||||
login_state.username_mut().clear();
|
||||
login_state.password_mut().clear();
|
||||
Err(e)
|
||||
}
|
||||
}
|
||||
@@ -103,56 +105,43 @@ pub async fn save(
|
||||
|
||||
/// Reverts the login form fields to empty and returns to the previous screen (Intro).
|
||||
pub async fn revert(
|
||||
login_state: &mut LoginState,
|
||||
_app_state: &mut AppState, // Keep signature consistent if needed elsewhere
|
||||
login_state: &mut LoginFormState,
|
||||
app_state: &mut AppState,
|
||||
) -> String {
|
||||
// Clear the input fields
|
||||
login_state.username.clear();
|
||||
login_state.password.clear();
|
||||
login_state.error_message = None;
|
||||
login_state.set_has_unsaved_changes(false);
|
||||
login_state.login_request_pending = false; // Ensure flag is reset on revert
|
||||
|
||||
login_state.clear();
|
||||
app_state.hide_dialog();
|
||||
"Login reverted".to_string()
|
||||
}
|
||||
|
||||
/// Clears login form and navigates back to main menu.
|
||||
pub async fn back_to_main(
|
||||
login_state: &mut LoginState,
|
||||
login_state: &mut LoginFormState,
|
||||
app_state: &mut AppState,
|
||||
buffer_state: &mut BufferState,
|
||||
) -> String {
|
||||
// Clear the input fields
|
||||
login_state.username.clear();
|
||||
login_state.password.clear();
|
||||
login_state.error_message = None;
|
||||
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
|
||||
login_state.clear();
|
||||
app_state.hide_dialog();
|
||||
|
||||
// Navigation logic
|
||||
buffer_state.close_active_buffer();
|
||||
buffer_state.update_history(AppView::Intro);
|
||||
|
||||
// Reset focus state
|
||||
app_state.ui.focus_outside_canvas = false;
|
||||
app_state.focused_button_index= 0;
|
||||
app_state.focused_button_index = 0;
|
||||
|
||||
"Returned to main menu".to_string()
|
||||
}
|
||||
|
||||
/// Validates input, shows loading, and spawns the login task.
|
||||
pub fn initiate_login(
|
||||
login_state: &LoginState,
|
||||
login_state: &mut LoginFormState,
|
||||
app_state: &mut AppState,
|
||||
mut auth_client: AuthClient,
|
||||
sender: mpsc::Sender<LoginResult>,
|
||||
) -> String {
|
||||
let username = login_state.username.clone();
|
||||
let password = login_state.password.clone();
|
||||
login_state.sync_from_editor();
|
||||
let username = login_state.username().to_string();
|
||||
let password = login_state.password().to_string();
|
||||
|
||||
// 1. Client-side validation
|
||||
if username.trim().is_empty() {
|
||||
app_state.show_dialog(
|
||||
"Login Failed",
|
||||
@@ -162,25 +151,20 @@ pub fn initiate_login(
|
||||
);
|
||||
"Username cannot be empty.".to_string()
|
||||
} else {
|
||||
// 2. Show Loading Dialog
|
||||
app_state.show_loading_dialog("Logging In", "Please wait...");
|
||||
|
||||
// 3. Spawn the login task
|
||||
spawn(async move {
|
||||
// Use the passed-in (and moved) auth_client directly
|
||||
let login_outcome = match auth_client.login(username.clone(), password).await
|
||||
.with_context(|| format!("Spawned login task failed for identifier: {}", username))
|
||||
{
|
||||
Ok(response) => LoginResult::Success(response),
|
||||
Err(e) => LoginResult::Failure(format!("{}", e)),
|
||||
};
|
||||
// Send result back to the main UI thread
|
||||
{
|
||||
Ok(response) => LoginResult::Success(response),
|
||||
Err(e) => LoginResult::Failure(format!("{}", e)),
|
||||
};
|
||||
if let Err(e) = sender.send(login_outcome).await {
|
||||
error!("Failed to send login result: {}", e);
|
||||
}
|
||||
});
|
||||
|
||||
// 4. Return immediately
|
||||
"Login initiated.".to_string()
|
||||
}
|
||||
}
|
||||
@@ -191,28 +175,24 @@ pub fn handle_login_result(
|
||||
result: LoginResult,
|
||||
app_state: &mut AppState,
|
||||
auth_state: &mut AuthState,
|
||||
login_state: &mut LoginState,
|
||||
login_state: &mut LoginFormState,
|
||||
) -> bool {
|
||||
match result {
|
||||
LoginResult::Success(response) => {
|
||||
auth_state.auth_token = Some(response.access_token.clone());
|
||||
auth_state.user_id = Some(response.user_id.clone());
|
||||
auth_state.role = Some(response.role.clone());
|
||||
auth_state.role = Some(UserRole::from_str(&response.role));
|
||||
auth_state.decoded_username = Some(response.username.clone());
|
||||
|
||||
// --- NEW: Save auth data to file ---
|
||||
let data_to_store = StoredAuthData {
|
||||
access_token: response.access_token.clone(),
|
||||
user_id: response.user_id.clone(),
|
||||
role: response.role.clone(),
|
||||
username: response.username.clone(),
|
||||
};
|
||||
|
||||
if let Err(e) = save_auth_data(&data_to_store) {
|
||||
error!("Failed to save auth data to file: {}", e);
|
||||
// Continue anyway - user is still logged in for this session
|
||||
}
|
||||
// --- END NEW ---
|
||||
|
||||
let success_message = format!(
|
||||
"Login Successful!\n\nUsername: {}\nUser ID: {}\nRole: {}",
|
||||
@@ -226,14 +206,28 @@ pub fn handle_login_result(
|
||||
info!(message = %success_message, "Login successful");
|
||||
}
|
||||
LoginResult::Failure(err_msg) | LoginResult::ConnectionError(err_msg) => {
|
||||
app_state.update_dialog_content(&err_msg, vec!["OK".to_string()], DialogPurpose::LoginFailed);
|
||||
login_state.error_message = Some(err_msg.clone());
|
||||
app_state.update_dialog_content(
|
||||
&err_msg,
|
||||
vec!["OK".to_string()],
|
||||
DialogPurpose::LoginFailed,
|
||||
);
|
||||
login_state.set_error_message(Some(err_msg.clone()));
|
||||
error!(error = %err_msg, "Login failed/connection error");
|
||||
}
|
||||
}
|
||||
login_state.username.clear();
|
||||
login_state.password.clear();
|
||||
|
||||
login_state.username_mut().clear();
|
||||
login_state.password_mut().clear();
|
||||
login_state.set_has_unsaved_changes(false);
|
||||
login_state.current_cursor_pos = 0;
|
||||
true // Request redraw as dialog content changed
|
||||
login_state.set_current_cursor_pos(0);
|
||||
|
||||
true
|
||||
}
|
||||
|
||||
pub async fn handle_action(action: &str) -> Result<String> {
|
||||
match action {
|
||||
"previous_entry" => Ok("Previous entry not implemented".into()),
|
||||
"next_entry" => Ok("Next entry not implemented".into()),
|
||||
_ => Err(anyhow!("Unknown login action: {}", action)),
|
||||
}
|
||||
}
|
||||
9
client/src/pages/login/mod.rs
Normal file
9
client/src/pages/login/mod.rs
Normal file
@@ -0,0 +1,9 @@
|
||||
// src/pages/login/mod.rs
|
||||
|
||||
pub mod state;
|
||||
pub mod ui;
|
||||
pub mod logic;
|
||||
|
||||
pub use state::*;
|
||||
pub use ui::render_login;
|
||||
pub use logic::*;
|
||||
241
client/src/pages/login/state.rs
Normal file
241
client/src/pages/login/state.rs
Normal file
@@ -0,0 +1,241 @@
|
||||
// src/pages/login/state.rs
|
||||
|
||||
use canvas::{AppMode, DataProvider};
|
||||
use canvas::FormEditor;
|
||||
use std::fmt;
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct LoginState {
|
||||
pub username: String,
|
||||
pub password: String,
|
||||
pub error_message: Option<String>,
|
||||
pub current_field: usize,
|
||||
pub current_cursor_pos: usize,
|
||||
pub has_unsaved_changes: bool,
|
||||
pub login_request_pending: bool,
|
||||
pub app_mode: AppMode,
|
||||
}
|
||||
|
||||
impl Default for LoginState {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
username: String::new(),
|
||||
password: String::new(),
|
||||
error_message: None,
|
||||
current_field: 0,
|
||||
current_cursor_pos: 0,
|
||||
has_unsaved_changes: false,
|
||||
login_request_pending: false,
|
||||
app_mode: canvas::AppMode::Edit,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl LoginState {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
app_mode: canvas::AppMode::Edit,
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn current_field(&self) -> usize {
|
||||
self.current_field
|
||||
}
|
||||
|
||||
pub fn current_cursor_pos(&self) -> usize {
|
||||
self.current_cursor_pos
|
||||
}
|
||||
|
||||
pub fn set_current_field(&mut self, index: usize) {
|
||||
if index < 2 {
|
||||
self.current_field = index;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_current_cursor_pos(&mut self, pos: usize) {
|
||||
self.current_cursor_pos = pos;
|
||||
}
|
||||
|
||||
pub fn get_current_input(&self) -> &str {
|
||||
match self.current_field {
|
||||
0 => &self.username,
|
||||
1 => &self.password,
|
||||
_ => "",
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_current_input_mut(&mut self) -> &mut String {
|
||||
match self.current_field {
|
||||
0 => &mut self.username,
|
||||
1 => &mut self.password,
|
||||
_ => panic!("Invalid current_field index in LoginState"),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn current_mode(&self) -> AppMode {
|
||||
self.app_mode
|
||||
}
|
||||
|
||||
pub fn has_unsaved_changes(&self) -> bool {
|
||||
self.has_unsaved_changes
|
||||
}
|
||||
|
||||
pub fn set_has_unsaved_changes(&mut self, changed: bool) {
|
||||
self.has_unsaved_changes = changed;
|
||||
}
|
||||
}
|
||||
|
||||
// Implement DataProvider for LoginState
|
||||
impl DataProvider for LoginState {
|
||||
fn field_count(&self) -> usize {
|
||||
2
|
||||
}
|
||||
|
||||
fn field_name(&self, index: usize) -> &str {
|
||||
match index {
|
||||
0 => "Username/Email",
|
||||
1 => "Password",
|
||||
_ => "",
|
||||
}
|
||||
}
|
||||
|
||||
fn field_value(&self, index: usize) -> &str {
|
||||
match index {
|
||||
0 => &self.username,
|
||||
1 => &self.password,
|
||||
_ => "",
|
||||
}
|
||||
}
|
||||
|
||||
fn set_field_value(&mut self, index: usize, value: String) {
|
||||
match index {
|
||||
0 => self.username = value,
|
||||
1 => self.password = value,
|
||||
_ => {}
|
||||
}
|
||||
self.has_unsaved_changes = true;
|
||||
}
|
||||
|
||||
fn supports_suggestions(&self, _field_index: usize) -> bool {
|
||||
false // Login form doesn't support suggestions
|
||||
}
|
||||
}
|
||||
|
||||
/// Wrapper that owns both the raw login state and its editor
|
||||
|
||||
pub struct LoginFormState {
|
||||
pub state: LoginState,
|
||||
pub editor: FormEditor<LoginState>,
|
||||
}
|
||||
|
||||
// manual debug because FormEditor doesnt implement debug
|
||||
impl fmt::Debug for LoginFormState {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.debug_struct("LoginFormState")
|
||||
.field("state", &self.state) // ✅ only print the data
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
impl LoginFormState {
|
||||
/// Sync the editor's data provider back into our state
|
||||
pub fn sync_from_editor(&mut self) {
|
||||
// FormEditor holds the authoritative data
|
||||
let dp = self.editor.data_provider();
|
||||
self.state = dp.clone(); // LoginState implements Clone
|
||||
}
|
||||
|
||||
/// Create a new LoginFormState with default LoginState and FormEditor
|
||||
pub fn new() -> Self {
|
||||
let state = LoginState::default();
|
||||
let editor = FormEditor::new(state.clone());
|
||||
Self { state, editor }
|
||||
}
|
||||
|
||||
// === Delegates to LoginState fields ===
|
||||
|
||||
pub fn username(&self) -> &str {
|
||||
&self.state.username
|
||||
}
|
||||
|
||||
pub fn username_mut(&mut self) -> &mut String {
|
||||
&mut self.state.username
|
||||
}
|
||||
|
||||
pub fn password(&self) -> &str {
|
||||
&self.state.password
|
||||
}
|
||||
|
||||
pub fn password_mut(&mut self) -> &mut String {
|
||||
&mut self.state.password
|
||||
}
|
||||
|
||||
pub fn error_message(&self) -> Option<&String> {
|
||||
self.state.error_message.as_ref()
|
||||
}
|
||||
|
||||
pub fn set_error_message(&mut self, msg: Option<String>) {
|
||||
self.state.error_message = msg;
|
||||
}
|
||||
|
||||
pub fn has_unsaved_changes(&self) -> bool {
|
||||
self.state.has_unsaved_changes
|
||||
}
|
||||
|
||||
pub fn set_has_unsaved_changes(&mut self, changed: bool) {
|
||||
self.state.has_unsaved_changes = changed;
|
||||
}
|
||||
|
||||
pub fn clear(&mut self) {
|
||||
self.state.username.clear();
|
||||
self.state.password.clear();
|
||||
self.state.error_message = None;
|
||||
self.state.has_unsaved_changes = false;
|
||||
self.state.login_request_pending = false;
|
||||
self.state.current_cursor_pos = 0;
|
||||
}
|
||||
|
||||
// === Delegates to LoginState cursor/input ===
|
||||
|
||||
pub fn current_field(&self) -> usize {
|
||||
self.state.current_field()
|
||||
}
|
||||
|
||||
pub fn set_current_field(&mut self, index: usize) {
|
||||
self.state.set_current_field(index);
|
||||
}
|
||||
|
||||
pub fn current_cursor_pos(&self) -> usize {
|
||||
self.state.current_cursor_pos()
|
||||
}
|
||||
|
||||
pub fn set_current_cursor_pos(&mut self, pos: usize) {
|
||||
self.state.set_current_cursor_pos(pos);
|
||||
}
|
||||
|
||||
pub fn get_current_input(&self) -> &str {
|
||||
self.state.get_current_input()
|
||||
}
|
||||
|
||||
pub fn get_current_input_mut(&mut self) -> &mut String {
|
||||
self.state.get_current_input_mut()
|
||||
}
|
||||
|
||||
// === Delegates to FormEditor ===
|
||||
|
||||
pub fn mode(&self) -> AppMode {
|
||||
self.editor.mode()
|
||||
}
|
||||
|
||||
pub fn cursor_position(&self) -> usize {
|
||||
self.editor.cursor_position()
|
||||
}
|
||||
|
||||
pub fn handle_key_event(
|
||||
&mut self,
|
||||
key_event: crossterm::event::KeyEvent,
|
||||
) -> canvas::keymap::KeyEventOutcome {
|
||||
self.editor.handle_key_event(key_event)
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,7 @@
|
||||
// src/components/auth/login.rs
|
||||
// src/pages/login/ui.rs
|
||||
|
||||
use crate::{
|
||||
config::colors::themes::Theme,
|
||||
state::pages::auth::LoginState,
|
||||
components::common::dialog,
|
||||
state::app::state::AppState,
|
||||
};
|
||||
use ratatui::{
|
||||
@@ -19,15 +17,19 @@ use canvas::{
|
||||
DefaultCanvasTheme,
|
||||
};
|
||||
|
||||
use crate::pages::login::LoginFormState;
|
||||
use crate::dialog;
|
||||
|
||||
pub fn render_login(
|
||||
f: &mut Frame,
|
||||
area: Rect,
|
||||
theme: &Theme,
|
||||
// FIX: take &LoginState (reference), not owned
|
||||
login_state: &LoginState,
|
||||
login_page: &LoginFormState,
|
||||
app_state: &AppState,
|
||||
is_edit_mode: bool,
|
||||
) {
|
||||
let login_state = &login_page.state;
|
||||
let editor = &login_page.editor;
|
||||
|
||||
// Main container
|
||||
let block = Block::default()
|
||||
.borders(Borders::ALL)
|
||||
@@ -53,14 +55,10 @@ pub fn render_login(
|
||||
])
|
||||
.split(inner_area);
|
||||
|
||||
// Wrap LoginState in FormEditor (no clone needed)
|
||||
let editor = FormEditor::new(login_state.clone());
|
||||
|
||||
// Use DefaultCanvasTheme instead of app Theme
|
||||
let input_rect = render_canvas(
|
||||
f,
|
||||
chunks[0],
|
||||
&editor,
|
||||
editor,
|
||||
&DefaultCanvasTheme,
|
||||
);
|
||||
|
||||
@@ -135,14 +133,14 @@ pub fn render_login(
|
||||
);
|
||||
|
||||
// --- SUGGESTIONS DROPDOWN (if active) ---
|
||||
if app_state.current_mode == crate::modes::handlers::mode_manager::AppMode::Edit {
|
||||
if editor.mode() == canvas::AppMode::Edit {
|
||||
if let Some(input_rect) = input_rect {
|
||||
render_suggestions_dropdown(
|
||||
f,
|
||||
f.area(),
|
||||
chunks[0],
|
||||
input_rect,
|
||||
&DefaultCanvasTheme,
|
||||
&editor, // FIX: pass &editor
|
||||
editor,
|
||||
);
|
||||
}
|
||||
}
|
||||
9
client/src/pages/mod.rs
Normal file
9
client/src/pages/mod.rs
Normal file
@@ -0,0 +1,9 @@
|
||||
// src/pages/mod.rs
|
||||
|
||||
pub mod routing;
|
||||
pub mod intro;
|
||||
pub mod login;
|
||||
pub mod register;
|
||||
pub mod forms;
|
||||
pub mod admin;
|
||||
pub mod admin_panel;
|
||||
@@ -1,13 +1,11 @@
|
||||
// src/tui/functions/common/register.rs
|
||||
// src/pages/register/logic.rs
|
||||
|
||||
use crate::services::auth::AuthClient;
|
||||
use crate::state::{
|
||||
pages::auth::RegisterState,
|
||||
app::state::AppState,
|
||||
};
|
||||
use crate::state::app::state::AppState;
|
||||
use crate::ui::handlers::context::DialogPurpose;
|
||||
use crate::buffer::state::{AppView, BufferState};
|
||||
use common::proto::komp_ac::auth::AuthResponse;
|
||||
use crate::pages::register::RegisterFormState;
|
||||
use anyhow::Context;
|
||||
use tokio::spawn;
|
||||
use tokio::sync::mpsc;
|
||||
@@ -22,24 +20,26 @@ pub enum RegisterResult {
|
||||
|
||||
/// Clears the registration form fields.
|
||||
pub async fn revert(
|
||||
register_state: &mut RegisterState,
|
||||
_app_state: &mut AppState, // Keep signature consistent if needed elsewhere
|
||||
register_state: &mut RegisterFormState,
|
||||
app_state: &mut AppState,
|
||||
) -> String {
|
||||
register_state.username.clear();
|
||||
register_state.email.clear();
|
||||
register_state.password.clear();
|
||||
register_state.password_confirmation.clear();
|
||||
register_state.role.clear();
|
||||
register_state.error_message = None;
|
||||
register_state.username_mut().clear();
|
||||
register_state.email_mut().clear();
|
||||
register_state.password_mut().clear();
|
||||
register_state.password_confirmation_mut().clear();
|
||||
register_state.role_mut().clear();
|
||||
register_state.set_error_message(None);
|
||||
register_state.set_has_unsaved_changes(false);
|
||||
register_state.current_field = 0; // Reset focus to first field
|
||||
register_state.current_cursor_pos = 0;
|
||||
register_state.set_current_field(0); // Reset focus to first field
|
||||
register_state.set_current_cursor_pos(0);
|
||||
|
||||
app_state.hide_dialog();
|
||||
"Registration form cleared".to_string()
|
||||
}
|
||||
|
||||
/// Clears the form and returns to the intro screen.
|
||||
pub async fn back_to_login(
|
||||
register_state: &mut RegisterState,
|
||||
register_state: &mut RegisterFormState,
|
||||
app_state: &mut AppState,
|
||||
buffer_state: &mut BufferState,
|
||||
) -> String {
|
||||
@@ -62,25 +62,35 @@ pub async fn back_to_login(
|
||||
|
||||
/// Validates input, shows loading, and spawns the registration task.
|
||||
pub fn initiate_registration(
|
||||
register_state: &RegisterState,
|
||||
register_state: &mut RegisterFormState,
|
||||
app_state: &mut AppState,
|
||||
mut auth_client: AuthClient,
|
||||
sender: mpsc::Sender<RegisterResult>,
|
||||
) -> String {
|
||||
// Clone necessary data
|
||||
let username = register_state.username.clone();
|
||||
let email = register_state.email.clone();
|
||||
let password = register_state.password.clone();
|
||||
let password_confirmation = register_state.password_confirmation.clone();
|
||||
let role = register_state.role.clone();
|
||||
register_state.sync_from_editor();
|
||||
let username = register_state.username().to_string();
|
||||
let email = register_state.email().to_string();
|
||||
let password = register_state.password().to_string();
|
||||
let password_confirmation = register_state.password_confirmation().to_string();
|
||||
let role = register_state.role().to_string();
|
||||
|
||||
// 1. Client-side validation
|
||||
if username.trim().is_empty() {
|
||||
app_state.show_dialog("Registration Failed", "Username cannot be empty.", vec!["OK".to_string()], DialogPurpose::RegisterFailed);
|
||||
app_state.show_dialog(
|
||||
"Registration Failed",
|
||||
"Username cannot be empty.",
|
||||
vec!["OK".to_string()],
|
||||
DialogPurpose::RegisterFailed,
|
||||
);
|
||||
"Username cannot be empty.".to_string()
|
||||
} else if !password.is_empty() && password != password_confirmation {
|
||||
app_state.show_dialog("Registration Failed", "Passwords do not match.", vec!["OK".to_string()], DialogPurpose::RegisterFailed);
|
||||
"Passwords do not match.".to_string()
|
||||
app_state.show_dialog(
|
||||
"Registration Failed",
|
||||
"Passwords do not match.",
|
||||
vec!["OK".to_string()],
|
||||
DialogPurpose::RegisterFailed,
|
||||
);
|
||||
"Passwords do not match.".to_string()
|
||||
} else {
|
||||
// 2. Show Loading Dialog
|
||||
app_state.show_loading_dialog("Registering", "Please wait...");
|
||||
@@ -88,14 +98,19 @@ pub fn initiate_registration(
|
||||
// 3. Spawn the registration task
|
||||
spawn(async move {
|
||||
let password_opt = if password.is_empty() { None } else { Some(password) };
|
||||
let password_conf_opt = if password_confirmation.is_empty() { None } else { Some(password_confirmation) };
|
||||
let password_conf_opt =
|
||||
if password_confirmation.is_empty() { None } else { Some(password_confirmation) };
|
||||
let role_opt = if role.is_empty() { None } else { Some(role) };
|
||||
let register_outcome = match auth_client.register(username.clone(), email, password_opt, password_conf_opt, role_opt).await
|
||||
|
||||
let register_outcome = match auth_client
|
||||
.register(username.clone(), email, password_opt, password_conf_opt, role_opt)
|
||||
.await
|
||||
.with_context(|| format!("Spawned register task failed for username: {}", username))
|
||||
{
|
||||
Ok(response) => RegisterResult::Success(response),
|
||||
Err(e) => RegisterResult::Failure(format!("{}", e)),
|
||||
};
|
||||
|
||||
// Send result back to the main UI thread
|
||||
if let Err(e) = sender.send(register_outcome).await {
|
||||
error!("Failed to send registration result: {}", e);
|
||||
@@ -112,7 +127,7 @@ pub fn initiate_registration(
|
||||
pub fn handle_registration_result(
|
||||
result: RegisterResult,
|
||||
app_state: &mut AppState,
|
||||
register_state: &mut RegisterState,
|
||||
register_state: &mut RegisterFormState,
|
||||
) -> bool {
|
||||
match result {
|
||||
RegisterResult::Success(response) => {
|
||||
@@ -133,7 +148,7 @@ pub fn handle_registration_result(
|
||||
vec!["OK".to_string()],
|
||||
DialogPurpose::RegisterFailed,
|
||||
);
|
||||
register_state.error_message = Some(err_msg.clone());
|
||||
register_state.set_error_message(Some(err_msg.clone()));
|
||||
error!(error = %err_msg, "Registration failed/connection error");
|
||||
}
|
||||
}
|
||||
12
client/src/pages/register/mod.rs
Normal file
12
client/src/pages/register/mod.rs
Normal file
@@ -0,0 +1,12 @@
|
||||
// src/pages/register/mod.rs
|
||||
|
||||
// pub mod state;
|
||||
pub mod ui;
|
||||
pub mod state;
|
||||
pub mod logic;
|
||||
pub mod suggestions;
|
||||
|
||||
// pub use state::*;
|
||||
pub use ui::render_register;
|
||||
pub use logic::*;
|
||||
pub use state::*;
|
||||
363
client/src/pages/register/state.rs
Normal file
363
client/src/pages/register/state.rs
Normal file
@@ -0,0 +1,363 @@
|
||||
// src/pages/register/state.rs
|
||||
|
||||
use canvas::{DataProvider, AppMode, FormEditor};
|
||||
use std::fmt;
|
||||
|
||||
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
|
||||
use canvas::keymap::KeyEventOutcome;
|
||||
use crate::pages::register::suggestions::role_suggestions_sync;
|
||||
|
||||
/// Represents the state of the Registration form UI
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct RegisterState {
|
||||
pub username: String,
|
||||
pub email: String,
|
||||
pub password: String,
|
||||
pub password_confirmation: String,
|
||||
pub role: String,
|
||||
pub error_message: Option<String>,
|
||||
pub current_field: usize,
|
||||
pub current_cursor_pos: usize,
|
||||
pub has_unsaved_changes: bool,
|
||||
pub app_mode: canvas::AppMode,
|
||||
}
|
||||
|
||||
impl Default for RegisterState {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
username: String::new(),
|
||||
email: String::new(),
|
||||
password: String::new(),
|
||||
password_confirmation: String::new(),
|
||||
role: String::new(),
|
||||
error_message: None,
|
||||
current_field: 0,
|
||||
current_cursor_pos: 0,
|
||||
has_unsaved_changes: false,
|
||||
app_mode: canvas::AppMode::Edit,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl RegisterState {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
app_mode: canvas::AppMode::Edit,
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
|
||||
pub fn current_field(&self) -> usize {
|
||||
self.current_field
|
||||
}
|
||||
|
||||
pub fn current_cursor_pos(&self) -> usize {
|
||||
self.current_cursor_pos
|
||||
}
|
||||
|
||||
pub fn set_current_field(&mut self, index: usize) {
|
||||
if index < 5 {
|
||||
self.current_field = index;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_current_cursor_pos(&mut self, pos: usize) {
|
||||
self.current_cursor_pos = pos;
|
||||
}
|
||||
|
||||
pub fn get_current_input(&self) -> &str {
|
||||
match self.current_field {
|
||||
0 => &self.username,
|
||||
1 => &self.email,
|
||||
2 => &self.password,
|
||||
3 => &self.password_confirmation,
|
||||
4 => &self.role,
|
||||
_ => "",
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_current_input_mut(&mut self, index: usize) -> &mut String {
|
||||
match index {
|
||||
0 => &mut self.username,
|
||||
1 => &mut self.email,
|
||||
2 => &mut self.password,
|
||||
3 => &mut self.password_confirmation,
|
||||
4 => &mut self.role,
|
||||
_ => panic!("Invalid current_field index in RegisterState"),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn current_mode(&self) -> AppMode {
|
||||
self.app_mode
|
||||
}
|
||||
|
||||
pub fn has_unsaved_changes(&self) -> bool {
|
||||
self.has_unsaved_changes
|
||||
}
|
||||
|
||||
pub fn set_has_unsaved_changes(&mut self, changed: bool) {
|
||||
self.has_unsaved_changes = changed;
|
||||
}
|
||||
}
|
||||
|
||||
impl DataProvider for RegisterState {
|
||||
fn field_count(&self) -> usize { 5 }
|
||||
|
||||
fn field_name(&self, index: usize) -> &str {
|
||||
match index {
|
||||
0 => "Username",
|
||||
1 => "Email (Optional)",
|
||||
2 => "Password (Optional)",
|
||||
3 => "Confirm Password",
|
||||
4 => "Role (Optional)",
|
||||
_ => "",
|
||||
}
|
||||
}
|
||||
|
||||
fn field_value(&self, index: usize) -> &str {
|
||||
match index {
|
||||
0 => &self.username,
|
||||
1 => &self.email,
|
||||
2 => &self.password,
|
||||
3 => &self.password_confirmation,
|
||||
4 => &self.role,
|
||||
_ => "",
|
||||
}
|
||||
}
|
||||
|
||||
fn set_field_value(&mut self, index: usize, value: String) {
|
||||
match index {
|
||||
0 => self.username = value,
|
||||
1 => self.email = value,
|
||||
2 => self.password = value,
|
||||
3 => self.password_confirmation = value,
|
||||
4 => self.role = value,
|
||||
_ => {}
|
||||
}
|
||||
self.has_unsaved_changes = true;
|
||||
}
|
||||
|
||||
fn supports_suggestions(&self, field_index: usize) -> bool {
|
||||
field_index == 4 // only Role field supports suggestions
|
||||
}
|
||||
}
|
||||
|
||||
/// Wrapper that owns both the raw register state and its editor
|
||||
pub struct RegisterFormState {
|
||||
pub state: RegisterState,
|
||||
pub editor: FormEditor<RegisterState>,
|
||||
}
|
||||
|
||||
impl Default for RegisterFormState {
|
||||
fn default() -> Self {
|
||||
Self::new()
|
||||
}
|
||||
}
|
||||
|
||||
// manual Debug because FormEditor doesn’t implement Debug
|
||||
impl fmt::Debug for RegisterFormState {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
f.debug_struct("RegisterFormState")
|
||||
.field("state", &self.state)
|
||||
.finish()
|
||||
}
|
||||
}
|
||||
|
||||
impl RegisterFormState {
|
||||
/// Sync the editor's data provider back into our state
|
||||
pub fn sync_from_editor(&mut self) {
|
||||
// The FormEditor holds the authoritative data
|
||||
let dp = self.editor.data_provider();
|
||||
self.state = dp.clone(); // because RegisterState: Clone
|
||||
}
|
||||
|
||||
pub fn new() -> Self {
|
||||
let state = RegisterState::default();
|
||||
let editor = FormEditor::new(state.clone());
|
||||
Self { state, editor }
|
||||
}
|
||||
|
||||
// === Delegates to RegisterState ===
|
||||
pub fn username(&self) -> &str {
|
||||
&self.state.username
|
||||
}
|
||||
pub fn username_mut(&mut self) -> &mut String {
|
||||
&mut self.state.username
|
||||
}
|
||||
|
||||
pub fn email(&self) -> &str {
|
||||
&self.state.email
|
||||
}
|
||||
pub fn email_mut(&mut self) -> &mut String {
|
||||
&mut self.state.email
|
||||
}
|
||||
|
||||
pub fn password(&self) -> &str {
|
||||
&self.state.password
|
||||
}
|
||||
pub fn password_mut(&mut self) -> &mut String {
|
||||
&mut self.state.password
|
||||
}
|
||||
|
||||
pub fn password_confirmation(&self) -> &str {
|
||||
&self.state.password_confirmation
|
||||
}
|
||||
pub fn password_confirmation_mut(&mut self) -> &mut String {
|
||||
&mut self.state.password_confirmation
|
||||
}
|
||||
|
||||
pub fn role(&self) -> &str {
|
||||
&self.state.role
|
||||
}
|
||||
pub fn role_mut(&mut self) -> &mut String {
|
||||
&mut self.state.role
|
||||
}
|
||||
|
||||
pub fn error_message(&self) -> Option<&String> {
|
||||
self.state.error_message.as_ref()
|
||||
}
|
||||
pub fn set_error_message(&mut self, msg: Option<String>) {
|
||||
self.state.error_message = msg;
|
||||
}
|
||||
|
||||
pub fn has_unsaved_changes(&self) -> bool {
|
||||
self.state.has_unsaved_changes
|
||||
}
|
||||
pub fn set_has_unsaved_changes(&mut self, changed: bool) {
|
||||
self.state.has_unsaved_changes = changed;
|
||||
}
|
||||
|
||||
pub fn clear(&mut self) {
|
||||
self.state.username.clear();
|
||||
self.state.email.clear();
|
||||
self.state.password.clear();
|
||||
self.state.password_confirmation.clear();
|
||||
self.state.role.clear();
|
||||
self.state.error_message = None;
|
||||
self.state.has_unsaved_changes = false;
|
||||
self.state.current_field = 0;
|
||||
self.state.current_cursor_pos = 0;
|
||||
}
|
||||
|
||||
// === Delegates to cursor/input ===
|
||||
pub fn current_field(&self) -> usize {
|
||||
self.state.current_field()
|
||||
}
|
||||
pub fn set_current_field(&mut self, index: usize) {
|
||||
self.state.set_current_field(index);
|
||||
}
|
||||
pub fn current_cursor_pos(&self) -> usize {
|
||||
self.state.current_cursor_pos()
|
||||
}
|
||||
pub fn set_current_cursor_pos(&mut self, pos: usize) {
|
||||
self.state.set_current_cursor_pos(pos);
|
||||
}
|
||||
pub fn get_current_input(&self) -> &str {
|
||||
self.state.get_current_input()
|
||||
}
|
||||
pub fn get_current_input_mut(&mut self) -> &mut String {
|
||||
self.state.get_current_input_mut(self.state.current_field)
|
||||
}
|
||||
|
||||
// === Delegates to FormEditor ===
|
||||
pub fn mode(&self) -> canvas::AppMode {
|
||||
self.editor.mode()
|
||||
}
|
||||
pub fn cursor_position(&self) -> usize {
|
||||
self.editor.cursor_position()
|
||||
}
|
||||
|
||||
pub fn handle_key_event(
|
||||
&mut self,
|
||||
key_event: crossterm::event::KeyEvent,
|
||||
) -> canvas::keymap::KeyEventOutcome {
|
||||
// Only customize behavior for the Role field (index 4) in Edit mode
|
||||
let in_role_field = self.editor.current_field() == 4;
|
||||
let in_edit_mode = self.editor.mode() == canvas::AppMode::Edit;
|
||||
|
||||
if in_role_field && in_edit_mode {
|
||||
match key_event.code {
|
||||
// Tab: open suggestions if inactive; otherwise cycle next
|
||||
KeyCode::Tab => {
|
||||
if !self.editor.is_suggestions_active() {
|
||||
if let Some(query) = self.editor.start_suggestions(4) {
|
||||
let items = role_suggestions_sync(&query);
|
||||
let applied =
|
||||
self.editor.apply_suggestions_result(4, &query, items);
|
||||
if applied {
|
||||
self.editor.update_inline_completion();
|
||||
}
|
||||
}
|
||||
} else {
|
||||
// Cycle to next suggestion
|
||||
self.editor.suggestions_next();
|
||||
}
|
||||
return KeyEventOutcome::Consumed(None);
|
||||
}
|
||||
|
||||
// Shift+Tab (BackTab): cycle suggestions too (fallback to next)
|
||||
KeyCode::BackTab => {
|
||||
if self.editor.is_suggestions_active() {
|
||||
// If your canvas exposes suggestions_prev(), use it here.
|
||||
// Fallback: cycle next.
|
||||
self.editor.suggestions_next();
|
||||
return KeyEventOutcome::Consumed(None);
|
||||
}
|
||||
}
|
||||
|
||||
// Enter: if suggestions active — apply selected suggestion
|
||||
KeyCode::Enter => {
|
||||
if self.editor.is_suggestions_active() {
|
||||
let _ = self.editor.apply_suggestion();
|
||||
return KeyEventOutcome::Consumed(None);
|
||||
}
|
||||
}
|
||||
|
||||
// Esc: close suggestions if active
|
||||
KeyCode::Esc => {
|
||||
if self.editor.is_suggestions_active() {
|
||||
self.editor.close_suggestions();
|
||||
return KeyEventOutcome::Consumed(None);
|
||||
}
|
||||
}
|
||||
|
||||
// Character input: first let editor mutate text, then refilter if active
|
||||
KeyCode::Char(_) => {
|
||||
let outcome = self.editor.handle_key_event(key_event);
|
||||
if self.editor.is_suggestions_active() {
|
||||
if let Some(query) = self.editor.start_suggestions(4) {
|
||||
let items = role_suggestions_sync(&query);
|
||||
let applied =
|
||||
self.editor.apply_suggestions_result(4, &query, items);
|
||||
if applied {
|
||||
self.editor.update_inline_completion();
|
||||
}
|
||||
}
|
||||
}
|
||||
return outcome;
|
||||
}
|
||||
|
||||
// Backspace/Delete: mutate then refilter if active
|
||||
KeyCode::Backspace | KeyCode::Delete => {
|
||||
let outcome = self.editor.handle_key_event(key_event);
|
||||
if self.editor.is_suggestions_active() {
|
||||
if let Some(query) = self.editor.start_suggestions(4) {
|
||||
let items = role_suggestions_sync(&query);
|
||||
let applied =
|
||||
self.editor.apply_suggestions_result(4, &query, items);
|
||||
if applied {
|
||||
self.editor.update_inline_completion();
|
||||
}
|
||||
}
|
||||
}
|
||||
return outcome;
|
||||
}
|
||||
|
||||
_ => { /* fall through to default */ }
|
||||
}
|
||||
}
|
||||
|
||||
// Default: let canvas handle it
|
||||
self.editor.handle_key_event(key_event)
|
||||
}
|
||||
}
|
||||
36
client/src/pages/register/suggestions.rs
Normal file
36
client/src/pages/register/suggestions.rs
Normal file
@@ -0,0 +1,36 @@
|
||||
// src/pages/register/suggestions.rs
|
||||
|
||||
use anyhow::Result;
|
||||
use async_trait::async_trait;
|
||||
use canvas::{SuggestionItem, SuggestionsProvider};
|
||||
|
||||
// Keep the async provider if you want, but add this sync helper and shared data.
|
||||
const ROLES: &[&str] = &["admin", "moderator", "accountant", "viewer"];
|
||||
|
||||
pub fn role_suggestions_sync(query: &str) -> Vec<SuggestionItem> {
|
||||
let q = query.to_lowercase();
|
||||
ROLES
|
||||
.iter()
|
||||
.filter(|r| q.is_empty() || r.to_lowercase().contains(&q))
|
||||
.map(|r| SuggestionItem {
|
||||
display_text: (*r).to_string(),
|
||||
value_to_store: (*r).to_string(),
|
||||
})
|
||||
.collect()
|
||||
}
|
||||
|
||||
pub struct RoleSuggestionsProvider;
|
||||
|
||||
#[async_trait]
|
||||
impl SuggestionsProvider for RoleSuggestionsProvider {
|
||||
async fn fetch_suggestions(
|
||||
&mut self,
|
||||
field_index: usize,
|
||||
query: &str,
|
||||
) -> Result<Vec<SuggestionItem>> {
|
||||
if field_index != 4 {
|
||||
return Ok(Vec::new());
|
||||
}
|
||||
Ok(role_suggestions_sync(query))
|
||||
}
|
||||
}
|
||||
@@ -1,11 +1,8 @@
|
||||
// src/components/auth/register.rs
|
||||
// src/pages/register/ui.rs
|
||||
|
||||
use crate::{
|
||||
config::colors::themes::Theme,
|
||||
state::pages::auth::RegisterState,
|
||||
components::common::dialog,
|
||||
state::app::state::AppState,
|
||||
modes::handlers::mode_manager::AppMode,
|
||||
};
|
||||
use ratatui::{
|
||||
layout::{Alignment, Constraint, Direction, Layout, Rect, Margin},
|
||||
@@ -13,16 +10,24 @@ use ratatui::{
|
||||
widgets::{Block, BorderType, Borders, Paragraph},
|
||||
Frame,
|
||||
};
|
||||
use canvas::{FormEditor, render_canvas, render_suggestions_dropdown, DefaultCanvasTheme};
|
||||
use crate::dialog;
|
||||
use crate::pages::register::RegisterFormState;
|
||||
use crate::pages::register::suggestions::RoleSuggestionsProvider;
|
||||
use tokio::runtime::Handle;
|
||||
use canvas::{render_canvas, render_suggestions_dropdown, DefaultCanvasTheme};
|
||||
use canvas::SuggestionsProvider;
|
||||
|
||||
pub fn render_register(
|
||||
f: &mut Frame,
|
||||
area: Rect,
|
||||
theme: &Theme,
|
||||
state: &RegisterState,
|
||||
register_page: &RegisterFormState,
|
||||
app_state: &AppState,
|
||||
is_edit_mode: bool,
|
||||
) {
|
||||
let state = ®ister_page.state;
|
||||
let editor = ®ister_page.editor;
|
||||
|
||||
// Outer block
|
||||
let block = Block::default()
|
||||
.borders(Borders::ALL)
|
||||
.border_type(BorderType::Plain)
|
||||
@@ -47,15 +52,9 @@ pub fn render_register(
|
||||
])
|
||||
.split(inner_area);
|
||||
|
||||
// Wrap RegisterState in FormEditor
|
||||
let editor = FormEditor::new(state.clone());
|
||||
// Render the form canvas
|
||||
let input_rect = render_canvas(f, chunks[0], editor, theme);
|
||||
|
||||
let input_rect = render_canvas(
|
||||
f,
|
||||
chunks[0],
|
||||
&editor,
|
||||
theme,
|
||||
);
|
||||
|
||||
// --- HELP TEXT ---
|
||||
let help_text = Paragraph::new("* are optional fields")
|
||||
@@ -81,11 +80,8 @@ pub fn render_register(
|
||||
|
||||
// Register Button
|
||||
let register_button_index = 0;
|
||||
let register_active = if app_state.ui.focus_outside_canvas {
|
||||
app_state.focused_button_index == register_button_index
|
||||
} else {
|
||||
false
|
||||
};
|
||||
let register_active = app_state.ui.focus_outside_canvas
|
||||
&& app_state.focused_button_index == register_button_index;
|
||||
let mut register_style = Style::default().fg(theme.fg);
|
||||
let mut register_border = Style::default().fg(theme.border);
|
||||
if register_active {
|
||||
@@ -108,11 +104,8 @@ pub fn render_register(
|
||||
|
||||
// Return Button
|
||||
let return_button_index = 1;
|
||||
let return_active = if app_state.ui.focus_outside_canvas {
|
||||
app_state.focused_button_index == return_button_index
|
||||
} else {
|
||||
false
|
||||
};
|
||||
let return_active = app_state.ui.focus_outside_canvas
|
||||
&& app_state.focused_button_index == return_button_index;
|
||||
let mut return_style = Style::default().fg(theme.fg);
|
||||
let mut return_border = Style::default().fg(theme.border);
|
||||
if return_active {
|
||||
@@ -133,19 +126,6 @@ pub fn render_register(
|
||||
button_chunks[1],
|
||||
);
|
||||
|
||||
// --- AUTOCOMPLETE DROPDOWN (Using new canvas suggestions) ---
|
||||
if app_state.current_mode == AppMode::Edit {
|
||||
if let Some(input_rect) = input_rect {
|
||||
render_suggestions_dropdown(
|
||||
f,
|
||||
f.area(), // Frame area
|
||||
input_rect, // Current input field rect
|
||||
&DefaultCanvasTheme,
|
||||
&editor,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// --- DIALOG ---
|
||||
if app_state.ui.dialog.dialog_show {
|
||||
dialog::render_dialog(
|
||||
@@ -159,4 +139,17 @@ pub fn render_register(
|
||||
app_state.ui.dialog.is_loading,
|
||||
);
|
||||
}
|
||||
|
||||
// Render suggestions dropdown if active (library GUI)
|
||||
if editor.mode() == canvas::AppMode::Edit {
|
||||
if let Some(input_rect) = input_rect {
|
||||
render_suggestions_dropdown(
|
||||
f,
|
||||
f.area(),
|
||||
input_rect,
|
||||
&DefaultCanvasTheme,
|
||||
editor,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
5
client/src/pages/routing/mod.rs
Normal file
5
client/src/pages/routing/mod.rs
Normal file
@@ -0,0 +1,5 @@
|
||||
// src/pages/routing/mod.rs
|
||||
|
||||
pub mod router;
|
||||
|
||||
pub use router::{Page, Router};
|
||||
36
client/src/pages/routing/router.rs
Normal file
36
client/src/pages/routing/router.rs
Normal file
@@ -0,0 +1,36 @@
|
||||
// src/pages/routing/router.rs
|
||||
use crate::state::pages::auth::AuthState;
|
||||
use crate::pages::admin_panel::add_logic::state::AddLogicState;
|
||||
use crate::pages::admin_panel::add_table::state::AddTableState;
|
||||
use crate::pages::admin::AdminState;
|
||||
use crate::pages::forms::FormState;
|
||||
use crate::pages::login::LoginFormState;
|
||||
use crate::pages::register::RegisterFormState;
|
||||
use crate::pages::intro::IntroState;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum Page {
|
||||
Intro(IntroState),
|
||||
Login(LoginFormState),
|
||||
Register(RegisterFormState),
|
||||
Admin(AdminState),
|
||||
AddLogic(AddLogicState),
|
||||
AddTable(AddTableState),
|
||||
Form(String),
|
||||
}
|
||||
|
||||
pub struct Router {
|
||||
pub current: Page,
|
||||
}
|
||||
|
||||
impl Router {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
current: Page::Intro(IntroState::default()),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn navigate(&mut self, page: Page) {
|
||||
self.current = page;
|
||||
}
|
||||
}
|
||||
@@ -34,9 +34,16 @@ pub async fn handle_search_palette_event(
|
||||
// Step 2: Process outside the borrow
|
||||
if let Some((id, content_json)) = maybe_data {
|
||||
if let Ok(data) = serde_json::from_str::<HashMap<String, String>>(&content_json) {
|
||||
if let Some(fs) = app_state.form_state_mut() {
|
||||
let detached_pos = fs.total_count + 2;
|
||||
fs.update_from_response(&data, detached_pos);
|
||||
// Use current view path to access the active form
|
||||
if let (Some(profile), Some(table)) = (
|
||||
app_state.current_view_profile_name.clone(),
|
||||
app_state.current_view_table_name.clone(),
|
||||
) {
|
||||
let path = format!("{}/{}", profile, table);
|
||||
if let Some(fs) = app_state.form_state_for_path(&path) {
|
||||
let detached_pos = fs.total_count + 2;
|
||||
fs.update_from_response(&data, detached_pos);
|
||||
}
|
||||
}
|
||||
should_close = true;
|
||||
outcome_message = Some(format!("Loaded record ID {}", id));
|
||||
|
||||
@@ -2,10 +2,10 @@
|
||||
|
||||
use crate::services::grpc_client::GrpcClient;
|
||||
use crate::state::app::state::AppState;
|
||||
use crate::state::pages::add_logic::AddLogicState;
|
||||
use crate::state::pages::form::{FieldDefinition, FormState};
|
||||
use crate::tui::functions::common::form::SaveOutcome;
|
||||
use crate::pages::admin_panel::add_logic::state::AddLogicState;
|
||||
use crate::pages::forms::logic::SaveOutcome;
|
||||
use crate::utils::columns::filter_user_columns;
|
||||
use crate::pages::forms::{FieldDefinition, FormState};
|
||||
use anyhow::{anyhow, Context, Result};
|
||||
use std::sync::Arc;
|
||||
|
||||
|
||||
@@ -7,26 +7,16 @@ use common::proto::komp_ac::table_structure::TableStructureResponse;
|
||||
use crate::modes::handlers::mode_manager::AppMode;
|
||||
use crate::search::state::SearchState;
|
||||
use crate::ui::handlers::context::DialogPurpose;
|
||||
use crate::state::pages::form::FormState;
|
||||
use crate::config::binds::Config;
|
||||
use crate::pages::forms::FormState;
|
||||
use canvas::FormEditor;
|
||||
use crate::dialog::DialogState;
|
||||
use std::collections::HashMap;
|
||||
use std::env;
|
||||
use std::sync::Arc;
|
||||
#[cfg(feature = "ui-debug")]
|
||||
use std::time::Instant;
|
||||
|
||||
// --- DialogState and UiState are unchanged ---
|
||||
pub struct DialogState {
|
||||
pub dialog_show: bool,
|
||||
pub dialog_title: String,
|
||||
pub dialog_message: String,
|
||||
pub dialog_buttons: Vec<String>,
|
||||
pub dialog_active_button_index: usize,
|
||||
pub purpose: Option<DialogPurpose>,
|
||||
pub is_loading: bool,
|
||||
}
|
||||
|
||||
pub struct UiState {
|
||||
pub show_sidebar: bool,
|
||||
pub show_buffer_list: bool,
|
||||
@@ -70,7 +60,7 @@ pub struct AppState {
|
||||
// UI preferences
|
||||
pub ui: UiState,
|
||||
|
||||
pub form_editor: Option<FormEditor<FormState>>,
|
||||
pub form_editor: HashMap<String, FormEditor<FormState>>, // key = "profile/table"
|
||||
|
||||
#[cfg(feature = "ui-debug")]
|
||||
pub debug_state: Option<DebugState>,
|
||||
@@ -91,7 +81,7 @@ impl AppState {
|
||||
pending_table_structure_fetch: None,
|
||||
search_state: None,
|
||||
ui: UiState::default(),
|
||||
form_editor: None,
|
||||
form_editor: HashMap::new(),
|
||||
|
||||
#[cfg(feature = "ui-debug")]
|
||||
debug_state: None,
|
||||
@@ -109,106 +99,59 @@ impl AppState {
|
||||
self.current_view_table_name = Some(table_name);
|
||||
}
|
||||
|
||||
pub fn show_dialog(
|
||||
&mut self,
|
||||
title: &str,
|
||||
message: &str,
|
||||
buttons: Vec<String>,
|
||||
purpose: DialogPurpose,
|
||||
) {
|
||||
self.ui.dialog.dialog_title = title.to_string();
|
||||
self.ui.dialog.dialog_message = message.to_string();
|
||||
self.ui.dialog.dialog_buttons = buttons;
|
||||
self.ui.dialog.dialog_active_button_index = 0;
|
||||
self.ui.dialog.purpose = Some(purpose);
|
||||
self.ui.dialog.is_loading = false;
|
||||
self.ui.dialog.dialog_show = true;
|
||||
self.ui.focus_outside_canvas = true;
|
||||
}
|
||||
|
||||
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();
|
||||
self.ui.dialog.dialog_active_button_index = 0;
|
||||
self.ui.dialog.purpose = None;
|
||||
self.ui.dialog.is_loading = true;
|
||||
self.ui.dialog.dialog_show = true;
|
||||
self.ui.focus_outside_canvas = true;
|
||||
}
|
||||
|
||||
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;
|
||||
self.ui.dialog.purpose = Some(purpose);
|
||||
self.ui.dialog.is_loading = false;
|
||||
/// Returns true if the current view's editor is in Edit mode.
|
||||
/// Uses current_view_profile_name/current_view_table_name to build the path.
|
||||
pub fn is_canvas_edit_mode(&self) -> bool {
|
||||
if let (Some(profile), Some(table)) =
|
||||
(self.current_view_profile_name.as_ref(), self.current_view_table_name.as_ref())
|
||||
{
|
||||
let path = format!("{}/{}", profile, table);
|
||||
if let Some(editor) = self.form_editor.get(&path) {
|
||||
return matches!(editor.mode(), canvas::AppMode::Edit);
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
|
||||
pub fn hide_dialog(&mut self) {
|
||||
self.ui.dialog.dialog_show = false;
|
||||
self.ui.dialog.dialog_title.clear();
|
||||
self.ui.dialog.dialog_message.clear();
|
||||
self.ui.dialog.dialog_buttons.clear();
|
||||
self.ui.dialog.dialog_active_button_index = 0;
|
||||
self.ui.dialog.purpose = None;
|
||||
self.ui.focus_outside_canvas = false;
|
||||
self.ui.dialog.is_loading = false;
|
||||
pub fn is_canvas_edit_mode_at(&self, path: &str) -> bool {
|
||||
self.form_editor
|
||||
.get(path)
|
||||
.map(|e| matches!(e.mode(), canvas::AppMode::Edit))
|
||||
.unwrap_or(false)
|
||||
}
|
||||
|
||||
pub fn next_dialog_button(&mut self) {
|
||||
if !self.ui.dialog.dialog_buttons.is_empty() {
|
||||
let next_index = (self.ui.dialog.dialog_active_button_index + 1)
|
||||
% self.ui.dialog.dialog_buttons.len();
|
||||
self.ui.dialog.dialog_active_button_index = next_index;
|
||||
// Mutable editor accessor
|
||||
pub fn editor_for_path(&mut self, path: &str) -> Option<&mut FormEditor<FormState>> {
|
||||
self.form_editor.get_mut(path)
|
||||
}
|
||||
|
||||
// Mutable FormState accessor
|
||||
pub fn form_state_for_path(&mut self, path: &str) -> Option<&mut FormState> {
|
||||
self.form_editor
|
||||
.get_mut(path)
|
||||
.map(|e| e.data_provider_mut())
|
||||
}
|
||||
|
||||
// Immutable editor accessor
|
||||
pub fn editor_for_path_ref(&self, path: &str) -> Option<&FormEditor<FormState>> {
|
||||
self.form_editor.get(path)
|
||||
}
|
||||
|
||||
// Immutable FormState accessor
|
||||
pub fn form_state_for_path_ref(&self, path: &str) -> Option<&FormState> {
|
||||
self.form_editor.get(path).map(|e| e.data_provider())
|
||||
}
|
||||
|
||||
pub fn ensure_form_editor<F>(&mut self, path: &str, config: &Config, loader: F)
|
||||
where
|
||||
F: FnOnce() -> FormState,
|
||||
{
|
||||
if !self.form_editor.contains_key(path) {
|
||||
let mut editor = FormEditor::new(loader());
|
||||
editor.set_keymap(config.build_canvas_keymap());
|
||||
self.form_editor.insert(path.to_string(), editor);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn previous_dialog_button(&mut self) {
|
||||
if !self.ui.dialog.dialog_buttons.is_empty() {
|
||||
let len = self.ui.dialog.dialog_buttons.len();
|
||||
let prev_index =
|
||||
(self.ui.dialog.dialog_active_button_index + len - 1) % len;
|
||||
self.ui.dialog.dialog_active_button_index = prev_index;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_active_dialog_button_label(&self) -> Option<&str> {
|
||||
self.ui.dialog
|
||||
.dialog_buttons
|
||||
.get(self.ui.dialog.dialog_active_button_index)
|
||||
.map(|s| s.as_str())
|
||||
}
|
||||
|
||||
pub fn init_form_editor(&mut self, form_state: FormState, config: &Config) {
|
||||
let mut editor = FormEditor::new(form_state);
|
||||
editor.set_keymap(config.build_canvas_keymap()); // inject keymap
|
||||
self.form_editor = Some(editor);
|
||||
}
|
||||
|
||||
/// Replace the current form state and wrap it in a FormEditor with keymap
|
||||
pub fn set_form_state(&mut self, form_state: FormState, config: &Config) {
|
||||
let mut editor = FormEditor::new(form_state);
|
||||
editor.set_keymap(config.build_canvas_keymap());
|
||||
self.form_editor = Some(editor);
|
||||
}
|
||||
|
||||
/// Immutable access to the underlying FormState
|
||||
pub fn form_state(&self) -> Option<&FormState> {
|
||||
self.form_editor.as_ref().map(|e| e.data_provider())
|
||||
}
|
||||
|
||||
/// Mutable access to the underlying FormState
|
||||
pub fn form_state_mut(&mut self) -> Option<&mut FormState> {
|
||||
self.form_editor.as_mut().map(|e| e.data_provider_mut())
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for UiState {
|
||||
@@ -229,17 +172,3 @@ impl Default for UiState {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for DialogState {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
dialog_show: false,
|
||||
dialog_title: String::new(),
|
||||
dialog_message: String::new(),
|
||||
dialog_buttons: Vec::new(),
|
||||
dialog_active_button_index: 0,
|
||||
purpose: None,
|
||||
is_loading: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,8 +1,3 @@
|
||||
// src/state/pages.rs
|
||||
|
||||
pub mod form;
|
||||
pub mod auth;
|
||||
pub mod admin;
|
||||
pub mod intro;
|
||||
pub mod add_table;
|
||||
pub mod add_logic;
|
||||
|
||||
@@ -1,210 +0,0 @@
|
||||
// src/state/pages/add_table.rs
|
||||
|
||||
use canvas::{DataProvider, AppMode};
|
||||
use ratatui::widgets::TableState;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct ColumnDefinition {
|
||||
pub name: String,
|
||||
pub data_type: String,
|
||||
pub selected: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
|
||||
pub struct IndexDefinition {
|
||||
pub name: String,
|
||||
pub selected: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub struct LinkDefinition {
|
||||
pub linked_table_name: String,
|
||||
pub is_required: bool,
|
||||
pub selected: bool,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
|
||||
pub enum AddTableFocus {
|
||||
#[default]
|
||||
InputTableName, // Field 0 for CanvasState
|
||||
InputColumnName, // Field 1 for CanvasState
|
||||
InputColumnType, // Field 2 for CanvasState
|
||||
AddColumnButton,
|
||||
// Result Tables
|
||||
ColumnsTable,
|
||||
IndexesTable,
|
||||
LinksTable,
|
||||
// Inside Tables (Scrolling Focus)
|
||||
InsideColumnsTable,
|
||||
InsideIndexesTable,
|
||||
InsideLinksTable,
|
||||
// Buttons
|
||||
SaveButton,
|
||||
DeleteSelectedButton,
|
||||
CancelButton,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct AddTableState {
|
||||
pub profile_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 indexes: Vec<IndexDefinition>,
|
||||
pub links: Vec<LinkDefinition>,
|
||||
pub current_focus: AddTableFocus,
|
||||
pub last_canvas_field: usize,
|
||||
pub column_table_state: TableState,
|
||||
pub index_table_state: TableState,
|
||||
pub link_table_state: TableState,
|
||||
pub table_name_cursor_pos: usize,
|
||||
pub column_name_cursor_pos: usize,
|
||||
pub column_type_cursor_pos: usize,
|
||||
pub has_unsaved_changes: bool,
|
||||
pub app_mode: AppMode,
|
||||
}
|
||||
|
||||
impl Default for AddTableState {
|
||||
fn default() -> Self {
|
||||
AddTableState {
|
||||
profile_name: "default".to_string(),
|
||||
table_name: String::new(),
|
||||
table_name_input: String::new(),
|
||||
column_name_input: String::new(),
|
||||
column_type_input: String::new(),
|
||||
columns: Vec::new(),
|
||||
indexes: Vec::new(),
|
||||
links: Vec::new(),
|
||||
current_focus: AddTableFocus::InputTableName,
|
||||
last_canvas_field: 2,
|
||||
column_table_state: TableState::default(),
|
||||
index_table_state: TableState::default(),
|
||||
link_table_state: TableState::default(),
|
||||
table_name_cursor_pos: 0,
|
||||
column_name_cursor_pos: 0,
|
||||
column_type_cursor_pos: 0,
|
||||
has_unsaved_changes: false,
|
||||
app_mode: AppMode::Edit,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl AddTableState {
|
||||
pub const INPUT_FIELD_COUNT: usize = 3;
|
||||
|
||||
/// Helper method to add a column from current inputs
|
||||
pub fn add_column_from_inputs(&mut self) -> Option<String> {
|
||||
if self.column_name_input.trim().is_empty() || self.column_type_input.trim().is_empty() {
|
||||
return Some("Both column name and type are required".to_string());
|
||||
}
|
||||
|
||||
// Check for duplicate column names
|
||||
if self.columns.iter().any(|col| col.name == self.column_name_input.trim()) {
|
||||
return Some("Column name already exists".to_string());
|
||||
}
|
||||
|
||||
// Add the column
|
||||
self.columns.push(ColumnDefinition {
|
||||
name: self.column_name_input.trim().to_string(),
|
||||
data_type: self.column_type_input.trim().to_string(),
|
||||
selected: false,
|
||||
});
|
||||
|
||||
// Clear inputs and reset focus to column name for next entry
|
||||
self.column_name_input.clear();
|
||||
self.column_type_input.clear();
|
||||
self.column_name_cursor_pos = 0;
|
||||
self.column_type_cursor_pos = 0;
|
||||
self.current_focus = AddTableFocus::InputColumnName;
|
||||
self.last_canvas_field = 1;
|
||||
self.has_unsaved_changes = true;
|
||||
|
||||
Some(format!("Column '{}' added successfully", self.columns.last().unwrap().name))
|
||||
}
|
||||
|
||||
/// Helper method to delete selected items
|
||||
pub fn delete_selected_items(&mut self) -> Option<String> {
|
||||
let mut deleted_items = Vec::new();
|
||||
|
||||
// Remove selected columns
|
||||
let initial_column_count = self.columns.len();
|
||||
self.columns.retain(|col| {
|
||||
if col.selected {
|
||||
deleted_items.push(format!("column '{}'", col.name));
|
||||
false
|
||||
} else {
|
||||
true
|
||||
}
|
||||
});
|
||||
|
||||
// Remove selected indexes
|
||||
let initial_index_count = self.indexes.len();
|
||||
self.indexes.retain(|idx| {
|
||||
if idx.selected {
|
||||
deleted_items.push(format!("index '{}'", idx.name));
|
||||
false
|
||||
} else {
|
||||
true
|
||||
}
|
||||
});
|
||||
|
||||
// Remove selected links
|
||||
let initial_link_count = self.links.len();
|
||||
self.links.retain(|link| {
|
||||
if link.selected {
|
||||
deleted_items.push(format!("link to '{}'", link.linked_table_name));
|
||||
false
|
||||
} else {
|
||||
true
|
||||
}
|
||||
});
|
||||
|
||||
if deleted_items.is_empty() {
|
||||
Some("No items selected for deletion".to_string())
|
||||
} else {
|
||||
self.has_unsaved_changes = true;
|
||||
Some(format!("Deleted: {}", deleted_items.join(", ")))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl DataProvider for AddTableState {
|
||||
fn field_count(&self) -> usize {
|
||||
3 // Table name, Column name, Column type
|
||||
}
|
||||
|
||||
fn field_name(&self, index: usize) -> &str {
|
||||
match index {
|
||||
0 => "Table name",
|
||||
1 => "Name",
|
||||
2 => "Type",
|
||||
_ => "",
|
||||
}
|
||||
}
|
||||
|
||||
fn field_value(&self, index: usize) -> &str {
|
||||
match index {
|
||||
0 => &self.table_name_input,
|
||||
1 => &self.column_name_input,
|
||||
2 => &self.column_type_input,
|
||||
_ => "",
|
||||
}
|
||||
}
|
||||
|
||||
fn set_field_value(&mut self, index: usize, value: String) {
|
||||
match index {
|
||||
0 => self.table_name_input = value,
|
||||
1 => self.column_name_input = value,
|
||||
2 => self.column_type_input = value,
|
||||
_ => {}
|
||||
}
|
||||
self.has_unsaved_changes = true;
|
||||
}
|
||||
|
||||
fn supports_suggestions(&self, _field_index: usize) -> bool {
|
||||
false // AddTableState doesn’t use suggestions
|
||||
}
|
||||
}
|
||||
@@ -1,182 +0,0 @@
|
||||
// src/state/pages/admin.rs
|
||||
|
||||
use ratatui::widgets::ListState;
|
||||
use crate::state::pages::add_table::AddTableState;
|
||||
use crate::state::pages::add_logic::AddLogicState;
|
||||
|
||||
// Define the focus states for the admin panel panes
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
|
||||
pub enum AdminFocus {
|
||||
#[default] // Default focus is on the profiles list
|
||||
ProfilesPane,
|
||||
InsideProfilesList,
|
||||
Tables,
|
||||
InsideTablesList,
|
||||
Button1,
|
||||
Button2,
|
||||
Button3,
|
||||
}
|
||||
|
||||
#[derive(Default, Clone, Debug)]
|
||||
pub struct AdminState {
|
||||
pub profiles: Vec<String>, // Holds profile names (used by non-admin view)
|
||||
pub profile_list_state: ListState, // Tracks navigation highlight (>) in profiles
|
||||
pub table_list_state: ListState, // Tracks navigation highlight (>) in tables
|
||||
pub selected_profile_index: Option<usize>, // Index with [*] in profiles (persistent)
|
||||
pub selected_table_index: Option<usize>, // Index with [*] in tables (persistent)
|
||||
pub current_focus: AdminFocus, // Tracks which pane is focused
|
||||
pub add_table_state: AddTableState,
|
||||
pub add_logic_state: AddLogicState,
|
||||
}
|
||||
|
||||
impl AdminState {
|
||||
/// Gets the index of the currently selected item.
|
||||
pub fn get_selected_index(&self) -> Option<usize> {
|
||||
self.profile_list_state.selected()
|
||||
}
|
||||
|
||||
/// Gets the name of the currently selected profile.
|
||||
pub fn get_selected_profile_name(&self) -> Option<&String> {
|
||||
self.profile_list_state.selected().and_then(|i| self.profiles.get(i))
|
||||
}
|
||||
|
||||
/// Populates the profile list and updates/resets the selection.
|
||||
pub fn set_profiles(&mut self, new_profiles: Vec<String>) {
|
||||
let current_selection_index = self.profile_list_state.selected();
|
||||
self.profiles = new_profiles;
|
||||
|
||||
if self.profiles.is_empty() {
|
||||
self.profile_list_state.select(None);
|
||||
} else {
|
||||
let new_selection = match current_selection_index {
|
||||
Some(index) => Some(index.min(self.profiles.len() - 1)),
|
||||
None => Some(0),
|
||||
};
|
||||
self.profile_list_state.select(new_selection);
|
||||
}
|
||||
}
|
||||
|
||||
/// Selects the next profile in the list, wrapping around.
|
||||
pub fn next(&mut self) {
|
||||
if self.profiles.is_empty() {
|
||||
self.profile_list_state.select(None);
|
||||
return;
|
||||
}
|
||||
let i = match self.profile_list_state.selected() {
|
||||
Some(i) => if i >= self.profiles.len() - 1 { 0 } else { i + 1 },
|
||||
None => 0,
|
||||
};
|
||||
self.profile_list_state.select(Some(i));
|
||||
}
|
||||
|
||||
/// Selects the previous profile in the list, wrapping around.
|
||||
pub fn previous(&mut self) {
|
||||
if self.profiles.is_empty() {
|
||||
self.profile_list_state.select(None);
|
||||
return;
|
||||
}
|
||||
let i = match self.profile_list_state.selected() {
|
||||
Some(i) => if i == 0 { self.profiles.len() - 1 } else { i - 1 },
|
||||
None => self.profiles.len() - 1,
|
||||
};
|
||||
self.profile_list_state.select(Some(i));
|
||||
}
|
||||
|
||||
/// Gets the index of the currently selected profile.
|
||||
pub fn get_selected_profile_index(&self) -> Option<usize> {
|
||||
self.profile_list_state.selected()
|
||||
}
|
||||
|
||||
/// Gets the index of the currently selected table.
|
||||
pub fn get_selected_table_index(&self) -> Option<usize> {
|
||||
self.table_list_state.selected()
|
||||
}
|
||||
|
||||
/// Selects a profile by index and resets table selection.
|
||||
pub fn select_profile(&mut self, index: Option<usize>) {
|
||||
self.profile_list_state.select(index);
|
||||
self.table_list_state.select(None);
|
||||
}
|
||||
|
||||
/// Selects a table by index.
|
||||
pub fn select_table(&mut self, index: Option<usize>) {
|
||||
self.table_list_state.select(index);
|
||||
}
|
||||
|
||||
/// Selects the next profile, wrapping around.
|
||||
/// `profile_count` should be the total number of profiles available.
|
||||
pub fn next_profile(&mut self, profile_count: usize) {
|
||||
if profile_count == 0 {
|
||||
return;
|
||||
}
|
||||
let i = match self.get_selected_profile_index() {
|
||||
Some(i) => {
|
||||
if i >= profile_count - 1 {
|
||||
0
|
||||
} else {
|
||||
i + 1
|
||||
}
|
||||
}
|
||||
None => 0,
|
||||
};
|
||||
self.select_profile(Some(i)); // Use the helper method
|
||||
}
|
||||
|
||||
/// Selects the previous profile, wrapping around.
|
||||
/// `profile_count` should be the total number of profiles available.
|
||||
pub fn previous_profile(&mut self, profile_count: usize) {
|
||||
if profile_count == 0 {
|
||||
return;
|
||||
}
|
||||
let i = match self.get_selected_profile_index() {
|
||||
Some(i) => {
|
||||
if i == 0 {
|
||||
profile_count - 1
|
||||
} else {
|
||||
i - 1
|
||||
}
|
||||
}
|
||||
None => 0, // Or profile_count - 1 if you prefer wrapping from None
|
||||
};
|
||||
self.select_profile(Some(i)); // Use the helper method
|
||||
}
|
||||
|
||||
/// Selects the next table, wrapping around.
|
||||
/// `table_count` should be the number of tables in the *currently selected* profile.
|
||||
pub fn next_table(&mut self, table_count: usize) {
|
||||
if table_count == 0 {
|
||||
return;
|
||||
}
|
||||
let i = match self.get_selected_table_index() {
|
||||
Some(i) => {
|
||||
if i >= table_count - 1 {
|
||||
0
|
||||
} else {
|
||||
i + 1
|
||||
}
|
||||
}
|
||||
None => 0,
|
||||
};
|
||||
self.select_table(Some(i));
|
||||
}
|
||||
|
||||
/// Selects the previous table, wrapping around.
|
||||
/// `table_count` should be the number of tables in the *currently selected* profile.
|
||||
pub fn previous_table(&mut self, table_count: usize) {
|
||||
if table_count == 0 {
|
||||
return;
|
||||
}
|
||||
let i = match self.get_selected_table_index() {
|
||||
Some(i) => {
|
||||
if i == 0 {
|
||||
table_count - 1
|
||||
} else {
|
||||
i - 1
|
||||
}
|
||||
}
|
||||
None => 0, // Or table_count - 1
|
||||
};
|
||||
self.select_table(Some(i));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,14 +1,37 @@
|
||||
// src/state/pages/auth.rs
|
||||
use canvas::{DataProvider, AppMode};
|
||||
use lazy_static::lazy_static;
|
||||
|
||||
lazy_static! {
|
||||
pub static ref AVAILABLE_ROLES: Vec<String> = vec![
|
||||
"admin".to_string(),
|
||||
"moderator".to_string(),
|
||||
"accountant".to_string(),
|
||||
"viewer".to_string(),
|
||||
];
|
||||
use canvas::{DataProvider, AppMode};
|
||||
|
||||
/// Strongly typed user roles
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum UserRole {
|
||||
Admin,
|
||||
Moderator,
|
||||
Accountant,
|
||||
Viewer,
|
||||
Unknown(String), // fallback for unexpected roles
|
||||
}
|
||||
|
||||
impl UserRole {
|
||||
pub fn from_str(s: &str) -> Self {
|
||||
match s.trim().to_lowercase().as_str() {
|
||||
"admin" => UserRole::Admin,
|
||||
"moderator" => UserRole::Moderator,
|
||||
"accountant" => UserRole::Accountant,
|
||||
"viewer" => UserRole::Viewer,
|
||||
other => UserRole::Unknown(other.to_string()),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn as_str(&self) -> &str {
|
||||
match self {
|
||||
UserRole::Admin => "admin",
|
||||
UserRole::Moderator => "moderator",
|
||||
UserRole::Accountant => "accountant",
|
||||
UserRole::Viewer => "viewer",
|
||||
UserRole::Unknown(s) => s.as_str(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Represents the authenticated session state
|
||||
@@ -16,311 +39,12 @@ lazy_static! {
|
||||
pub struct AuthState {
|
||||
pub auth_token: Option<String>,
|
||||
pub user_id: Option<String>,
|
||||
pub role: Option<String>,
|
||||
pub role: Option<UserRole>,
|
||||
pub decoded_username: Option<String>,
|
||||
}
|
||||
|
||||
/// Represents the state of the Login form UI
|
||||
#[derive(Clone)]
|
||||
pub struct LoginState {
|
||||
pub username: String,
|
||||
pub password: String,
|
||||
pub error_message: Option<String>,
|
||||
pub current_field: usize,
|
||||
pub current_cursor_pos: usize,
|
||||
pub has_unsaved_changes: bool,
|
||||
pub login_request_pending: bool,
|
||||
pub app_mode: AppMode,
|
||||
}
|
||||
|
||||
impl Default for LoginState {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
username: String::new(),
|
||||
password: String::new(),
|
||||
error_message: None,
|
||||
current_field: 0,
|
||||
current_cursor_pos: 0,
|
||||
has_unsaved_changes: false,
|
||||
login_request_pending: false,
|
||||
app_mode: AppMode::Edit,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Represents the state of the Registration form UI
|
||||
#[derive(Clone)]
|
||||
pub struct RegisterState {
|
||||
pub username: String,
|
||||
pub email: String,
|
||||
pub password: String,
|
||||
pub password_confirmation: String,
|
||||
pub role: String,
|
||||
pub error_message: Option<String>,
|
||||
pub current_field: usize,
|
||||
pub current_cursor_pos: usize,
|
||||
pub has_unsaved_changes: bool,
|
||||
pub app_mode: AppMode,
|
||||
// Keep role suggestions for later integration
|
||||
pub role_suggestions: Vec<String>,
|
||||
pub role_suggestions_active: bool,
|
||||
}
|
||||
|
||||
impl Default for RegisterState {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
username: String::new(),
|
||||
email: String::new(),
|
||||
password: String::new(),
|
||||
password_confirmation: String::new(),
|
||||
role: String::new(),
|
||||
error_message: None,
|
||||
current_field: 0,
|
||||
current_cursor_pos: 0,
|
||||
has_unsaved_changes: false,
|
||||
app_mode: AppMode::Edit,
|
||||
role_suggestions: AVAILABLE_ROLES.clone(),
|
||||
role_suggestions_active: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl AuthState {
|
||||
pub fn new() -> Self {
|
||||
Self::default()
|
||||
}
|
||||
}
|
||||
|
||||
impl LoginState {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
app_mode: AppMode::Edit,
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
|
||||
// Legacy method compatibility
|
||||
pub fn current_field(&self) -> usize {
|
||||
self.current_field
|
||||
}
|
||||
|
||||
pub fn current_cursor_pos(&self) -> usize {
|
||||
self.current_cursor_pos
|
||||
}
|
||||
|
||||
pub fn set_current_field(&mut self, index: usize) {
|
||||
if index < 2 {
|
||||
self.current_field = index;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_current_cursor_pos(&mut self, pos: usize) {
|
||||
self.current_cursor_pos = pos;
|
||||
}
|
||||
|
||||
pub fn get_current_input(&self) -> &str {
|
||||
match self.current_field {
|
||||
0 => &self.username,
|
||||
1 => &self.password,
|
||||
_ => "",
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_current_input_mut(&mut self) -> &mut String {
|
||||
match self.current_field {
|
||||
0 => &mut self.username,
|
||||
1 => &mut self.password,
|
||||
_ => panic!("Invalid current_field index in LoginState"),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn current_mode(&self) -> AppMode {
|
||||
self.app_mode
|
||||
}
|
||||
|
||||
// Add missing methods that used to come from CanvasState trait
|
||||
pub fn has_unsaved_changes(&self) -> bool {
|
||||
self.has_unsaved_changes
|
||||
}
|
||||
|
||||
pub fn set_has_unsaved_changes(&mut self, changed: bool) {
|
||||
self.has_unsaved_changes = changed;
|
||||
}
|
||||
}
|
||||
|
||||
impl RegisterState {
|
||||
pub fn new() -> Self {
|
||||
Self {
|
||||
app_mode: AppMode::Edit,
|
||||
role_suggestions: AVAILABLE_ROLES.clone(),
|
||||
role_suggestions_active: false,
|
||||
..Default::default()
|
||||
}
|
||||
}
|
||||
|
||||
// Legacy method compatibility
|
||||
pub fn current_field(&self) -> usize {
|
||||
self.current_field
|
||||
}
|
||||
|
||||
pub fn current_cursor_pos(&self) -> usize {
|
||||
self.current_cursor_pos
|
||||
}
|
||||
|
||||
pub fn set_current_field(&mut self, index: usize) {
|
||||
if index < 5 {
|
||||
self.current_field = index;
|
||||
|
||||
// Auto-activate role suggestions when moving to role field (index 4)
|
||||
if index == 4 {
|
||||
self.activate_role_suggestions();
|
||||
} else {
|
||||
self.deactivate_role_suggestions();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn set_current_cursor_pos(&mut self, pos: usize) {
|
||||
self.current_cursor_pos = pos;
|
||||
}
|
||||
|
||||
pub fn get_current_input(&self) -> &str {
|
||||
match self.current_field {
|
||||
0 => &self.username,
|
||||
1 => &self.email,
|
||||
2 => &self.password,
|
||||
3 => &self.password_confirmation,
|
||||
4 => &self.role,
|
||||
_ => "",
|
||||
}
|
||||
}
|
||||
|
||||
pub fn get_current_input_mut(&mut self) -> &mut String {
|
||||
match self.current_field {
|
||||
0 => &mut self.username,
|
||||
1 => &mut self.email,
|
||||
2 => &mut self.password,
|
||||
3 => &mut self.password_confirmation,
|
||||
4 => &mut self.role,
|
||||
_ => panic!("Invalid current_field index in RegisterState"),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn current_mode(&self) -> AppMode {
|
||||
self.app_mode
|
||||
}
|
||||
|
||||
// Role suggestions management
|
||||
pub fn activate_role_suggestions(&mut self) {
|
||||
self.role_suggestions_active = true;
|
||||
// Filter suggestions based on current input
|
||||
let current_input = self.role.to_lowercase();
|
||||
self.role_suggestions = AVAILABLE_ROLES
|
||||
.iter()
|
||||
.filter(|role| role.to_lowercase().contains(¤t_input))
|
||||
.cloned()
|
||||
.collect();
|
||||
}
|
||||
|
||||
pub fn deactivate_role_suggestions(&mut self) {
|
||||
self.role_suggestions_active = false;
|
||||
}
|
||||
|
||||
pub fn is_role_suggestions_active(&self) -> bool {
|
||||
self.role_suggestions_active
|
||||
}
|
||||
|
||||
pub fn get_role_suggestions(&self) -> &[String] {
|
||||
&self.role_suggestions
|
||||
}
|
||||
|
||||
// Add missing methods that used to come from CanvasState trait
|
||||
pub fn has_unsaved_changes(&self) -> bool {
|
||||
self.has_unsaved_changes
|
||||
}
|
||||
|
||||
pub fn set_has_unsaved_changes(&mut self, changed: bool) {
|
||||
self.has_unsaved_changes = changed;
|
||||
}
|
||||
}
|
||||
|
||||
// Step 2: Implement DataProvider for LoginState
|
||||
impl DataProvider for LoginState {
|
||||
fn field_count(&self) -> usize {
|
||||
2
|
||||
}
|
||||
|
||||
fn field_name(&self, index: usize) -> &str {
|
||||
match index {
|
||||
0 => "Username/Email",
|
||||
1 => "Password",
|
||||
_ => "",
|
||||
}
|
||||
}
|
||||
|
||||
fn field_value(&self, index: usize) -> &str {
|
||||
match index {
|
||||
0 => &self.username,
|
||||
1 => &self.password,
|
||||
_ => "",
|
||||
}
|
||||
}
|
||||
|
||||
fn set_field_value(&mut self, index: usize, value: String) {
|
||||
match index {
|
||||
0 => self.username = value,
|
||||
1 => self.password = value,
|
||||
_ => {}
|
||||
}
|
||||
self.has_unsaved_changes = true;
|
||||
}
|
||||
|
||||
fn supports_suggestions(&self, _field_index: usize) -> bool {
|
||||
false // Login form doesn't support suggestions
|
||||
}
|
||||
}
|
||||
|
||||
// Step 3: Implement DataProvider for RegisterState
|
||||
impl DataProvider for RegisterState {
|
||||
fn field_count(&self) -> usize {
|
||||
5
|
||||
}
|
||||
|
||||
fn field_name(&self, index: usize) -> &str {
|
||||
match index {
|
||||
0 => "Username",
|
||||
1 => "Email (Optional)",
|
||||
2 => "Password (Optional)",
|
||||
3 => "Confirm Password",
|
||||
4 => "Role (Optional)",
|
||||
_ => "",
|
||||
}
|
||||
}
|
||||
|
||||
fn field_value(&self, index: usize) -> &str {
|
||||
match index {
|
||||
0 => &self.username,
|
||||
1 => &self.email,
|
||||
2 => &self.password,
|
||||
3 => &self.password_confirmation,
|
||||
4 => &self.role,
|
||||
_ => "",
|
||||
}
|
||||
}
|
||||
|
||||
fn set_field_value(&mut self, index: usize, value: String) {
|
||||
match index {
|
||||
0 => self.username = value,
|
||||
1 => self.email = value,
|
||||
2 => self.password = value,
|
||||
3 => self.password_confirmation = value,
|
||||
4 => self.role = value,
|
||||
_ => {}
|
||||
}
|
||||
self.has_unsaved_changes = true;
|
||||
}
|
||||
|
||||
fn supports_suggestions(&self, field_index: usize) -> bool {
|
||||
field_index == 4 // only Role field supports suggestions
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,25 +0,0 @@
|
||||
// src/state/pages/intro.rs
|
||||
|
||||
#[derive(Default, Clone, Debug)]
|
||||
pub struct IntroState {
|
||||
pub selected_option: usize,
|
||||
}
|
||||
|
||||
impl IntroState {
|
||||
pub fn new() -> Self {
|
||||
Self::default()
|
||||
}
|
||||
|
||||
pub fn next_option(&mut self) {
|
||||
if self.selected_option < 3 {
|
||||
self.selected_option += 1;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn previous_option(&mut self) {
|
||||
if self.selected_option > 0 {
|
||||
self.selected_option -= 1
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,11 +1,3 @@
|
||||
// src/tui/functions.rs
|
||||
|
||||
pub mod admin;
|
||||
pub mod intro;
|
||||
pub mod login;
|
||||
pub mod form;
|
||||
pub mod common;
|
||||
|
||||
pub use admin::*;
|
||||
pub use intro::*;
|
||||
pub use form::*;
|
||||
|
||||
@@ -1,7 +1,3 @@
|
||||
// src/tui/functions/common.rs
|
||||
|
||||
pub mod form;
|
||||
pub mod login;
|
||||
pub mod logout;
|
||||
pub mod register;
|
||||
pub mod add_table;
|
||||
|
||||
@@ -1,44 +0,0 @@
|
||||
// src/tui/functions/form.rs
|
||||
use crate::state::pages::form::FormState;
|
||||
use crate::services::grpc_client::GrpcClient;
|
||||
use anyhow::{anyhow, Result};
|
||||
|
||||
pub async fn handle_action(
|
||||
action: &str,
|
||||
form_state: &mut FormState,
|
||||
_grpc_client: &mut GrpcClient,
|
||||
ideal_cursor_column: &mut usize,
|
||||
) -> Result<String> {
|
||||
if form_state.has_unsaved_changes() {
|
||||
return Ok(
|
||||
"Unsaved changes. Save (Ctrl+S) or Revert (Ctrl+R) before navigating."
|
||||
.to_string(),
|
||||
);
|
||||
}
|
||||
|
||||
let total_count = form_state.total_count;
|
||||
|
||||
match action {
|
||||
"previous_entry" => {
|
||||
// Only decrement if the current position is greater than the first record.
|
||||
// This prevents wrapping from 1 to total_count.
|
||||
// It also correctly handles moving from "New Entry" (total_count + 1) to the last record.
|
||||
if form_state.current_position > 1 {
|
||||
form_state.current_position -= 1;
|
||||
*ideal_cursor_column = 0;
|
||||
}
|
||||
}
|
||||
"next_entry" => {
|
||||
// Only increment if the current position is not yet at the "New Entry" stage.
|
||||
// The "New Entry" position is total_count + 1.
|
||||
// This allows moving from the last record to "New Entry", but stops there.
|
||||
if form_state.current_position <= total_count {
|
||||
form_state.current_position += 1;
|
||||
*ideal_cursor_column = 0;
|
||||
}
|
||||
}
|
||||
_ => return Err(anyhow!("Unknown form action: {}", action)),
|
||||
}
|
||||
|
||||
Ok(String::new())
|
||||
}
|
||||
@@ -1,30 +0,0 @@
|
||||
// src/tui/functions/intro.rs
|
||||
use crate::state::app::state::AppState;
|
||||
use crate::buffer::state::{AppView, BufferState};
|
||||
|
||||
/// Handles intro screen selection by updating view history and managing focus state.
|
||||
/// 0: Continue (restores last form or default)
|
||||
/// 1: Admin view
|
||||
/// 2: Login view
|
||||
/// 3: Register view (with focus reset)
|
||||
pub fn handle_intro_selection(
|
||||
app_state: &mut AppState,
|
||||
buffer_state: &mut BufferState,
|
||||
index: usize,
|
||||
) {
|
||||
let target_view = match index {
|
||||
0 => AppView::Form,
|
||||
1 => AppView::Admin,
|
||||
2 => AppView::Login,
|
||||
3 => AppView::Register,
|
||||
_ => return,
|
||||
};
|
||||
|
||||
buffer_state.update_history(target_view);
|
||||
|
||||
// Register view requires focus reset
|
||||
if index == 3 {
|
||||
app_state.ui.focus_outside_canvas = false;
|
||||
app_state.focused_button_index = 0;
|
||||
}
|
||||
}
|
||||
@@ -1,15 +0,0 @@
|
||||
// src/tui/functions/login.rs
|
||||
|
||||
use anyhow::{anyhow, Result};
|
||||
|
||||
pub async fn handle_action(action: &str,) -> Result<String> {
|
||||
match action {
|
||||
"previous_entry" => {
|
||||
Ok("Previous entry at tui/functions/login.rs not implemented".into())
|
||||
}
|
||||
"next_entry" => {
|
||||
Ok("Next entry at tui/functions/login.rs not implemented".into())
|
||||
}
|
||||
_ => Err(anyhow!("Unknown login action: {}", action))
|
||||
}
|
||||
}
|
||||
@@ -5,6 +5,7 @@ use crossterm::{
|
||||
terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
|
||||
cursor::{SetCursorStyle, EnableBlinking, Show, Hide, MoveTo},
|
||||
};
|
||||
use crossterm::ExecutableCommand;
|
||||
use ratatui::{backend::CrosstermBackend, Terminal};
|
||||
use std::io::{self, stdout, Write};
|
||||
use anyhow::Result;
|
||||
@@ -81,6 +82,12 @@ impl TerminalCore {
|
||||
)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Move the cursor to a specific (x, y) position on screen.
|
||||
pub fn set_cursor_position(&mut self, x: u16, y: u16) -> io::Result<()> {
|
||||
self.terminal.backend_mut().execute(MoveTo(x, y))?;
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
impl Drop for TerminalCore {
|
||||
|
||||
@@ -1,15 +1,15 @@
|
||||
// src/ui/handlers/render.rs
|
||||
|
||||
use crate::components::{
|
||||
admin::add_logic::render_add_logic,
|
||||
admin::render_add_table,
|
||||
auth::{login::render_login, register::render_register},
|
||||
common::dialog::render_dialog,
|
||||
common::find_file_palette,
|
||||
intro::intro::render_intro,
|
||||
render_background,
|
||||
render_command_line,
|
||||
render_status_line,
|
||||
use crate::components::render_background;
|
||||
use crate::pages::admin_panel::add_logic::ui::render_add_logic;
|
||||
use crate::pages::admin_panel::add_table::ui::render_add_table;
|
||||
use crate::pages::login::render_login;
|
||||
use crate::pages::register::render_register;
|
||||
use crate::pages::intro::render_intro;
|
||||
use crate::bottom_panel::{
|
||||
command_line::render_command_line,
|
||||
status_line::render_status_line,
|
||||
find_file_palette,
|
||||
};
|
||||
use crate::sidebar::{calculate_sidebar_layout, render_sidebar};
|
||||
use crate::buffer::render_buffer_list;
|
||||
@@ -18,30 +18,22 @@ use crate::config::colors::themes::Theme;
|
||||
use crate::modes::general::command_navigation::NavigationState;
|
||||
use crate::buffer::state::BufferState;
|
||||
use crate::state::app::state::AppState;
|
||||
use crate::state::pages::admin::AdminState;
|
||||
use crate::state::pages::auth::AuthState;
|
||||
use crate::state::pages::auth::LoginState;
|
||||
use crate::state::pages::auth::RegisterState;
|
||||
use crate::state::pages::form::FormState;
|
||||
use crate::state::pages::intro::IntroState;
|
||||
use crate::components::render_form;
|
||||
use crate::bottom_panel::layout::{bottom_panel_constraints, render_bottom_panel};
|
||||
use canvas::FormEditor;
|
||||
use ratatui::{
|
||||
layout::{Constraint, Direction, Layout},
|
||||
Frame,
|
||||
};
|
||||
use crate::pages::routing::{Router, Page};
|
||||
use crate::dialog::render_dialog;
|
||||
use crate::pages::forms::render_form_page;
|
||||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
pub fn render_ui(
|
||||
f: &mut Frame,
|
||||
form_state: &mut FormState,
|
||||
auth_state: &mut AuthState,
|
||||
login_state: &LoginState,
|
||||
register_state: &RegisterState,
|
||||
intro_state: &IntroState,
|
||||
admin_state: &mut AdminState,
|
||||
router: &mut Router,
|
||||
buffer_state: &BufferState,
|
||||
theme: &Theme,
|
||||
is_event_handler_edit_mode: bool,
|
||||
event_handler_command_input: &str,
|
||||
event_handler_command_mode_active: bool,
|
||||
event_handler_command_message: &str,
|
||||
@@ -49,41 +41,20 @@ pub fn render_ui(
|
||||
current_dir: &str,
|
||||
current_fps: f64,
|
||||
app_state: &AppState,
|
||||
auth_state: &AuthState,
|
||||
) {
|
||||
render_background(f, f.area(), theme);
|
||||
|
||||
// --- START DYNAMIC LAYOUT LOGIC ---
|
||||
let status_line_height = 1;
|
||||
#[cfg(feature = "ui-debug")]
|
||||
{
|
||||
if let Some(debug_state) = &app_state.debug_state {
|
||||
if debug_state.is_error {
|
||||
status_line_height = 4;
|
||||
}
|
||||
}
|
||||
}
|
||||
// --- END DYNAMIC LAYOUT LOGIC ---
|
||||
|
||||
const PALETTE_OPTIONS_HEIGHT_FOR_LAYOUT: u16 = 15;
|
||||
|
||||
let mut bottom_area_constraints: Vec<Constraint> = vec![Constraint::Length(status_line_height)];
|
||||
let command_palette_area_height = if navigation_state.active {
|
||||
1 + PALETTE_OPTIONS_HEIGHT_FOR_LAYOUT
|
||||
} else if event_handler_command_mode_active {
|
||||
1
|
||||
} else {
|
||||
0
|
||||
};
|
||||
|
||||
if command_palette_area_height > 0 {
|
||||
bottom_area_constraints.push(Constraint::Length(command_palette_area_height));
|
||||
}
|
||||
|
||||
// Layout: optional buffer list + main content + bottom panel
|
||||
let mut main_layout_constraints = vec![Constraint::Min(1)];
|
||||
if app_state.ui.show_buffer_list {
|
||||
main_layout_constraints.insert(0, Constraint::Length(1));
|
||||
}
|
||||
main_layout_constraints.extend(bottom_area_constraints);
|
||||
main_layout_constraints.extend(bottom_panel_constraints(
|
||||
app_state,
|
||||
navigation_state,
|
||||
event_handler_command_mode_active,
|
||||
));
|
||||
|
||||
let root_chunks = Layout::default()
|
||||
.direction(Direction::Vertical)
|
||||
@@ -102,144 +73,103 @@ pub fn render_ui(
|
||||
let main_content_area = root_chunks[chunk_idx];
|
||||
chunk_idx += 1;
|
||||
|
||||
let status_line_area = root_chunks[chunk_idx];
|
||||
chunk_idx += 1;
|
||||
|
||||
let command_render_area = if command_palette_area_height > 0 {
|
||||
if root_chunks.len() > chunk_idx {
|
||||
Some(root_chunks[chunk_idx])
|
||||
} else {
|
||||
None
|
||||
}
|
||||
} else {
|
||||
None
|
||||
};
|
||||
|
||||
if app_state.ui.show_intro {
|
||||
render_intro(f, intro_state, main_content_area, theme);
|
||||
} else if app_state.ui.show_register {
|
||||
render_register(
|
||||
// Page rendering is now fully router-driven
|
||||
match &mut router.current {
|
||||
Page::Intro(state) => render_intro(f, state, main_content_area, theme),
|
||||
Page::Login(page) => render_login(
|
||||
f,
|
||||
main_content_area,
|
||||
theme,
|
||||
register_state,
|
||||
page,
|
||||
app_state,
|
||||
register_state.current_field() < 4, // Now using CanvasState trait method
|
||||
);
|
||||
} else if app_state.ui.show_add_table {
|
||||
render_add_table(
|
||||
),
|
||||
Page::Register(state) => render_register(
|
||||
f,
|
||||
main_content_area,
|
||||
theme,
|
||||
state,
|
||||
app_state,
|
||||
&mut admin_state.add_table_state,
|
||||
is_event_handler_edit_mode,
|
||||
);
|
||||
} else if app_state.ui.show_add_logic {
|
||||
render_add_logic(
|
||||
f,
|
||||
main_content_area,
|
||||
theme,
|
||||
app_state,
|
||||
&mut admin_state.add_logic_state,
|
||||
is_event_handler_edit_mode,
|
||||
);
|
||||
} else if app_state.ui.show_login {
|
||||
render_login(
|
||||
f,
|
||||
main_content_area,
|
||||
theme,
|
||||
login_state,
|
||||
app_state,
|
||||
login_state.current_field() < 2, // Now using CanvasState trait method
|
||||
);
|
||||
} else if app_state.ui.show_admin {
|
||||
crate::components::admin::admin_panel::render_admin_panel(
|
||||
),
|
||||
Page::Admin(state) => crate::pages::admin::main::ui::render_admin_panel(
|
||||
f,
|
||||
app_state,
|
||||
auth_state,
|
||||
admin_state,
|
||||
state,
|
||||
main_content_area,
|
||||
theme,
|
||||
&app_state.profile_tree,
|
||||
&app_state.selected_profile,
|
||||
);
|
||||
} else if app_state.ui.show_form {
|
||||
let (sidebar_area, form_actual_area) =
|
||||
calculate_sidebar_layout(app_state.ui.show_sidebar, main_content_area);
|
||||
if let Some(sidebar_rect) = sidebar_area {
|
||||
render_sidebar(
|
||||
f,
|
||||
sidebar_rect,
|
||||
theme,
|
||||
&app_state.profile_tree,
|
||||
&app_state.selected_profile,
|
||||
);
|
||||
}
|
||||
let available_width = form_actual_area.width;
|
||||
let form_render_area = if available_width >= 80 {
|
||||
Layout::default()
|
||||
.direction(Direction::Horizontal)
|
||||
.constraints([Constraint::Min(0), Constraint::Length(80), Constraint::Min(0)])
|
||||
.split(form_actual_area)[1]
|
||||
} else {
|
||||
Layout::default()
|
||||
.direction(Direction::Horizontal)
|
||||
.constraints([
|
||||
Constraint::Min(0),
|
||||
Constraint::Length(available_width),
|
||||
Constraint::Min(0),
|
||||
])
|
||||
.split(form_actual_area)[1]
|
||||
};
|
||||
|
||||
render_form(
|
||||
),
|
||||
Page::AddLogic(state) => render_add_logic(
|
||||
f,
|
||||
form_render_area,
|
||||
app_state,
|
||||
form_state,
|
||||
app_state.current_view_table_name.as_deref().unwrap_or(""),
|
||||
main_content_area,
|
||||
theme,
|
||||
form_state.total_count,
|
||||
form_state.current_position,
|
||||
);
|
||||
app_state,
|
||||
state,
|
||||
),
|
||||
Page::AddTable(state) => render_add_table(
|
||||
f,
|
||||
main_content_area,
|
||||
theme,
|
||||
app_state,
|
||||
state,
|
||||
),
|
||||
Page::Form(path) => {
|
||||
let (sidebar_area, form_actual_area) =
|
||||
calculate_sidebar_layout(app_state.ui.show_sidebar, main_content_area);
|
||||
if let Some(sidebar_rect) = sidebar_area {
|
||||
render_sidebar(
|
||||
f,
|
||||
sidebar_rect,
|
||||
theme,
|
||||
&app_state.profile_tree,
|
||||
&app_state.selected_profile,
|
||||
);
|
||||
}
|
||||
let available_width = form_actual_area.width;
|
||||
let form_render_area = if available_width >= 80 {
|
||||
Layout::default()
|
||||
.direction(Direction::Horizontal)
|
||||
.constraints([Constraint::Min(0), Constraint::Length(80), Constraint::Min(0)])
|
||||
.split(form_actual_area)[1]
|
||||
} else {
|
||||
Layout::default()
|
||||
.direction(Direction::Horizontal)
|
||||
.constraints([
|
||||
Constraint::Min(0),
|
||||
Constraint::Length(available_width),
|
||||
Constraint::Min(0),
|
||||
])
|
||||
.split(form_actual_area)[1]
|
||||
};
|
||||
|
||||
if let Some(editor) = app_state.editor_for_path_ref(path) {
|
||||
let (total_count, current_position) =
|
||||
if let Some(fs) = app_state.form_state_for_path_ref(path) {
|
||||
(fs.total_count, fs.current_position)
|
||||
} else {
|
||||
(0, 1)
|
||||
};
|
||||
let table_name = path.split('/').nth(1).unwrap_or("");
|
||||
|
||||
render_form_page(
|
||||
f,
|
||||
form_render_area,
|
||||
editor,
|
||||
table_name,
|
||||
theme,
|
||||
total_count,
|
||||
current_position,
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Global overlays (not tied to a page)
|
||||
if let Some(area) = buffer_list_area {
|
||||
render_buffer_list(f, area, theme, buffer_state, app_state);
|
||||
}
|
||||
|
||||
render_status_line(
|
||||
f,
|
||||
status_line_area,
|
||||
current_dir,
|
||||
theme,
|
||||
is_event_handler_edit_mode,
|
||||
current_fps,
|
||||
app_state,
|
||||
);
|
||||
|
||||
if let Some(palette_or_command_area) = command_render_area {
|
||||
if navigation_state.active {
|
||||
find_file_palette::render_find_file_palette(
|
||||
f,
|
||||
palette_or_command_area,
|
||||
theme,
|
||||
navigation_state,
|
||||
);
|
||||
} else if event_handler_command_mode_active {
|
||||
render_command_line(
|
||||
f,
|
||||
palette_or_command_area,
|
||||
event_handler_command_input,
|
||||
true,
|
||||
theme,
|
||||
event_handler_command_message,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// This block now correctly handles drawing popups over any view.
|
||||
if app_state.ui.show_search_palette {
|
||||
if let Some(search_state) = &app_state.search_state {
|
||||
render_search_palette(f, f.area(), theme, search_state);
|
||||
@@ -256,4 +186,19 @@ pub fn render_ui(
|
||||
app_state.ui.dialog.is_loading,
|
||||
);
|
||||
}
|
||||
|
||||
render_bottom_panel(
|
||||
f,
|
||||
&root_chunks,
|
||||
&mut chunk_idx,
|
||||
current_dir,
|
||||
theme,
|
||||
current_fps,
|
||||
app_state,
|
||||
router,
|
||||
navigation_state,
|
||||
event_handler_command_input,
|
||||
event_handler_command_mode_active,
|
||||
event_handler_command_message,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -8,31 +8,38 @@ use crate::config::storage::storage::load_auth_data;
|
||||
use crate::modes::common::commands::CommandHandler;
|
||||
use crate::modes::handlers::event::{EventHandler, EventOutcome};
|
||||
use crate::modes::handlers::mode_manager::{AppMode, ModeManager};
|
||||
use crate::state::pages::form::{FormState, FieldDefinition};
|
||||
use crate::state::pages::auth::AuthState;
|
||||
use crate::state::pages::auth::LoginState;
|
||||
use crate::state::pages::auth::RegisterState;
|
||||
use crate::state::pages::admin::AdminState;
|
||||
use crate::state::pages::admin::AdminFocus;
|
||||
use crate::state::pages::intro::IntroState;
|
||||
use crate::state::pages::auth::UserRole;
|
||||
use crate::pages::login::LoginFormState;
|
||||
use crate::pages::register::RegisterFormState;
|
||||
use crate::pages::admin::AdminState;
|
||||
use crate::pages::admin::AdminFocus;
|
||||
use crate::pages::intro::IntroState;
|
||||
use crate::pages::forms::{FormState, FieldDefinition};
|
||||
use crate::pages::routing::{Router, Page};
|
||||
use crate::buffer::state::BufferState;
|
||||
use crate::buffer::state::AppView;
|
||||
use crate::state::app::state::AppState;
|
||||
use crate::tui::terminal::{EventReader, TerminalCore};
|
||||
use crate::ui::handlers::render::render_ui;
|
||||
use crate::tui::functions::common::login::LoginResult;
|
||||
use crate::tui::functions::common::register::RegisterResult;
|
||||
use crate::pages::login;
|
||||
use crate::pages::register;
|
||||
use crate::pages::login::LoginResult;
|
||||
use crate::pages::login::LoginState;
|
||||
use crate::pages::register::RegisterResult;
|
||||
use crate::ui::handlers::context::DialogPurpose;
|
||||
use crate::tui::functions::common::login;
|
||||
use crate::tui::functions::common::register;
|
||||
use crate::utils::columns::filter_user_columns;
|
||||
use canvas::keymap::KeyEventOutcome;
|
||||
use canvas::CursorManager;
|
||||
use canvas::FormEditor;
|
||||
use anyhow::{Context, Result};
|
||||
use crossterm::cursor::SetCursorStyle;
|
||||
use crossterm::cursor::{SetCursorStyle, MoveTo};
|
||||
use crossterm::event as crossterm_event;
|
||||
use crossterm::ExecutableCommand;
|
||||
use tracing::{error, info, warn};
|
||||
use tokio::sync::mpsc;
|
||||
use std::time::Instant;
|
||||
use std::time::Duration;
|
||||
#[cfg(feature = "ui-debug")]
|
||||
use crate::state::app::state::DebugState;
|
||||
#[cfg(feature = "ui-debug")]
|
||||
@@ -63,10 +70,13 @@ pub async fn run_ui() -> Result<()> {
|
||||
let event_reader = EventReader::new();
|
||||
|
||||
let mut auth_state = AuthState::default();
|
||||
let mut login_state = LoginState::default();
|
||||
let mut register_state = RegisterState::default();
|
||||
let mut login_state = LoginFormState::new();
|
||||
login_state.editor.set_keymap(config.build_canvas_keymap());
|
||||
let mut register_state = RegisterFormState::default();
|
||||
register_state.editor.set_keymap(config.build_canvas_keymap());
|
||||
let mut intro_state = IntroState::default();
|
||||
let mut admin_state = AdminState::default();
|
||||
let mut router = Router::new();
|
||||
let mut buffer_state = BufferState::default();
|
||||
let mut app_state = AppState::new().context("Failed to create initial app state")?;
|
||||
|
||||
@@ -75,7 +85,7 @@ pub async fn run_ui() -> Result<()> {
|
||||
Ok(Some(stored_data)) => {
|
||||
auth_state.auth_token = Some(stored_data.access_token);
|
||||
auth_state.user_id = Some(stored_data.user_id);
|
||||
auth_state.role = Some(stored_data.role);
|
||||
auth_state.role = Some(UserRole::from_str(&stored_data.role));
|
||||
auth_state.decoded_username = Some(stored_data.username);
|
||||
auto_logged_in = true;
|
||||
info!("Auth data loaded from file. User is auto-logged in.");
|
||||
@@ -104,13 +114,15 @@ pub async fn run_ui() -> Result<()> {
|
||||
.collect();
|
||||
|
||||
// Replace local form_state with app_state.form_editor
|
||||
app_state.set_form_state(
|
||||
FormState::new(initial_profile.clone(), initial_table.clone(), initial_field_defs),
|
||||
&config,
|
||||
);
|
||||
let path = format!("{}/{}", initial_profile, initial_table);
|
||||
app_state.ensure_form_editor(&path, &config, || {
|
||||
FormState::new(initial_profile.clone(), initial_table.clone(), initial_field_defs)
|
||||
});
|
||||
buffer_state.update_history(AppView::Form(path.clone()));
|
||||
router.navigate(Page::Form(path.clone()));
|
||||
|
||||
// Fetch initial count using app_state accessor
|
||||
if let Some(form_state) = app_state.form_state_mut() {
|
||||
if let Some(form_state) = app_state.form_state_for_path(&path) {
|
||||
UiService::fetch_and_set_table_count(&mut grpc_client, form_state)
|
||||
.await
|
||||
.context(format!(
|
||||
@@ -128,7 +140,9 @@ pub async fn run_ui() -> Result<()> {
|
||||
}
|
||||
|
||||
if auto_logged_in {
|
||||
buffer_state.history = vec![AppView::Form];
|
||||
let path = format!("{}/{}", initial_profile, initial_table);
|
||||
buffer_state.history = vec![AppView::Form(path.clone())];
|
||||
router.navigate(Page::Form(path));
|
||||
buffer_state.active_index = 0;
|
||||
info!("Initial view set to Form due to auto-login.");
|
||||
}
|
||||
@@ -141,9 +155,11 @@ pub async fn run_ui() -> Result<()> {
|
||||
let mut table_just_switched = false;
|
||||
|
||||
loop {
|
||||
let position_before_event = app_state.form_state()
|
||||
.map(|fs| fs.current_position)
|
||||
.unwrap_or(1);
|
||||
let position_before_event = if let Page::Form(path) = &router.current {
|
||||
app_state.form_state_for_path(path).map(|fs| fs.current_position).unwrap_or(1)
|
||||
} else {
|
||||
1
|
||||
};
|
||||
let mut event_processed = false;
|
||||
|
||||
// --- CHANNEL RECEIVERS ---
|
||||
@@ -168,16 +184,18 @@ pub async fn run_ui() -> Result<()> {
|
||||
// --- ADDED: For live form autocomplete ---
|
||||
match event_handler.autocomplete_result_receiver.try_recv() {
|
||||
Ok(hits) => {
|
||||
if let Some(form_state) = app_state.form_state_mut() {
|
||||
if form_state.autocomplete_active {
|
||||
form_state.autocomplete_suggestions = hits;
|
||||
form_state.autocomplete_loading = false;
|
||||
if !form_state.autocomplete_suggestions.is_empty() {
|
||||
form_state.selected_suggestion_index = Some(0);
|
||||
} else {
|
||||
form_state.selected_suggestion_index = None;
|
||||
if let Page::Form(path) = &router.current {
|
||||
if let Some(form_state) = app_state.form_state_for_path(path) {
|
||||
if form_state.autocomplete_active {
|
||||
form_state.autocomplete_suggestions = hits;
|
||||
form_state.autocomplete_loading = false;
|
||||
if !form_state.autocomplete_suggestions.is_empty() {
|
||||
form_state.selected_suggestion_index = Some(0);
|
||||
} else {
|
||||
form_state.selected_suggestion_index = None;
|
||||
}
|
||||
event_handler.command_message = format!("Found {} suggestions.", form_state.autocomplete_suggestions.len());
|
||||
}
|
||||
event_handler.command_message = format!("Found {} suggestions.", form_state.autocomplete_suggestions.len());
|
||||
}
|
||||
}
|
||||
needs_redraw = true;
|
||||
@@ -195,46 +213,53 @@ pub async fn run_ui() -> Result<()> {
|
||||
let event = event_reader.read_event().context("Failed to read terminal event")?;
|
||||
event_processed = true;
|
||||
|
||||
// Decouple Command Line and palettes from canvas:
|
||||
// Only forward keys to Form canvas when:
|
||||
// - not in command mode
|
||||
// - no search/palette active
|
||||
// - focus is inside the canvas
|
||||
if let crossterm_event::Event::Key(key_event) = &event {
|
||||
if app_state.ui.show_form {
|
||||
if let Some(editor) = app_state.form_editor.as_mut() {
|
||||
match editor.handle_key_event(*key_event) {
|
||||
KeyEventOutcome::Consumed(Some(msg)) => {
|
||||
event_handler.command_message = msg;
|
||||
needs_redraw = true;
|
||||
continue;
|
||||
}
|
||||
KeyEventOutcome::Consumed(None) => {
|
||||
needs_redraw = true;
|
||||
continue;
|
||||
}
|
||||
KeyEventOutcome::Pending => {
|
||||
needs_redraw = true;
|
||||
continue;
|
||||
}
|
||||
KeyEventOutcome::NotMatched => {
|
||||
// fall through to client-level handling
|
||||
let overlay_active = event_handler.command_mode
|
||||
|| app_state.ui.show_search_palette
|
||||
|| event_handler.navigation_state.active;
|
||||
if !overlay_active {
|
||||
if let Page::Form(path) = &router.current {
|
||||
if !app_state.ui.focus_outside_canvas {
|
||||
if let Some(editor) = app_state.editor_for_path(path) {
|
||||
match editor.handle_key_event(*key_event) {
|
||||
KeyEventOutcome::Consumed(Some(msg)) => {
|
||||
event_handler.command_message = msg;
|
||||
needs_redraw = true;
|
||||
continue;
|
||||
}
|
||||
KeyEventOutcome::Consumed(None) => {
|
||||
needs_redraw = true;
|
||||
continue;
|
||||
}
|
||||
KeyEventOutcome::Pending => {
|
||||
needs_redraw = true;
|
||||
continue;
|
||||
}
|
||||
KeyEventOutcome::NotMatched => {
|
||||
// fall through to client-level handling
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Get form state from app_state and pass to handle_event
|
||||
let form_state = app_state.form_state_mut().unwrap();
|
||||
|
||||
// Call handle_event directly
|
||||
let event_outcome_result = event_handler.handle_event(
|
||||
event,
|
||||
&config,
|
||||
&mut terminal,
|
||||
&mut command_handler,
|
||||
&mut auth_state,
|
||||
&mut login_state,
|
||||
&mut register_state,
|
||||
&mut intro_state,
|
||||
&mut admin_state,
|
||||
&mut buffer_state,
|
||||
&mut app_state,
|
||||
&mut router,
|
||||
).await;
|
||||
let mut should_exit = false;
|
||||
match event_outcome_result {
|
||||
@@ -250,20 +275,22 @@ pub async fn run_ui() -> Result<()> {
|
||||
}
|
||||
EventOutcome::DataSaved(save_outcome, message) => {
|
||||
event_handler.command_message = message;
|
||||
// Clone form_state to avoid double borrow
|
||||
let mut temp_form_state = app_state.form_state().unwrap().clone();
|
||||
if let Err(e) = UiService::handle_save_outcome(
|
||||
save_outcome,
|
||||
&mut grpc_client,
|
||||
&mut app_state,
|
||||
&mut temp_form_state,
|
||||
).await {
|
||||
event_handler.command_message =
|
||||
format!("Error handling save outcome: {}", e);
|
||||
}
|
||||
// Update app_state with changes
|
||||
if let Some(form_state) = app_state.form_state_mut() {
|
||||
*form_state = temp_form_state;
|
||||
if let Page::Form(path) = &router.current {
|
||||
if let Some(mut temp_form_state) = app_state.form_state_for_path(path).cloned() {
|
||||
if let Err(e) = UiService::handle_save_outcome(
|
||||
save_outcome,
|
||||
&mut grpc_client,
|
||||
&mut app_state,
|
||||
&mut temp_form_state,
|
||||
).await {
|
||||
event_handler.command_message =
|
||||
format!("Error handling save outcome: {}", e);
|
||||
}
|
||||
// Update app_state with changes
|
||||
if let Some(form_state) = app_state.form_state_for_path(path) {
|
||||
*form_state = temp_form_state;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
EventOutcome::ButtonSelected { .. } => {}
|
||||
@@ -274,7 +301,7 @@ pub async fn run_ui() -> Result<()> {
|
||||
let table_name = parts[1].to_string();
|
||||
|
||||
app_state.set_current_view_table(profile_name, table_name);
|
||||
buffer_state.update_history(AppView::Form);
|
||||
buffer_state.update_history(AppView::Form(path.clone()));
|
||||
event_handler.command_message = format!("Loading table: {}", path);
|
||||
} else {
|
||||
event_handler.command_message = format!("Invalid table path: {}", path);
|
||||
@@ -292,9 +319,19 @@ pub async fn run_ui() -> Result<()> {
|
||||
|
||||
match login_result_receiver.try_recv() {
|
||||
Ok(result) => {
|
||||
if login::handle_login_result(result, &mut app_state, &mut auth_state, &mut login_state) {
|
||||
needs_redraw = true;
|
||||
}
|
||||
// Apply result to the active router Login page if present,
|
||||
// otherwise update the local copy.
|
||||
let updated = if let Page::Login(page) = &mut router.current {
|
||||
login::handle_login_result(
|
||||
result,
|
||||
&mut app_state,
|
||||
&mut auth_state,
|
||||
page,
|
||||
)
|
||||
} else {
|
||||
login::handle_login_result(result, &mut app_state, &mut auth_state, &mut login_state)
|
||||
};
|
||||
if updated { needs_redraw = true; }
|
||||
}
|
||||
Err(mpsc::error::TryRecvError::Empty) => {}
|
||||
Err(mpsc::error::TryRecvError::Disconnected) => {
|
||||
@@ -340,56 +377,89 @@ pub async fn run_ui() -> Result<()> {
|
||||
}
|
||||
|
||||
if let Some(active_view) = buffer_state.get_active_view() {
|
||||
app_state.ui.show_intro = false;
|
||||
app_state.ui.show_login = false;
|
||||
app_state.ui.show_register = false;
|
||||
app_state.ui.show_admin = false;
|
||||
app_state.ui.show_add_table = false;
|
||||
app_state.ui.show_add_logic = false;
|
||||
app_state.ui.show_form = false;
|
||||
match active_view {
|
||||
AppView::Intro => app_state.ui.show_intro = true,
|
||||
AppView::Login => app_state.ui.show_login = true,
|
||||
AppView::Register => app_state.ui.show_register = true,
|
||||
AppView::Intro => {
|
||||
// Keep external intro_state in sync with the live Router state
|
||||
if let Page::Intro(current) = &router.current {
|
||||
intro_state = current.clone();
|
||||
}
|
||||
// Navigate with the up-to-date state
|
||||
router.navigate(Page::Intro(intro_state.clone()));
|
||||
}
|
||||
AppView::Login => {
|
||||
// Do not re-create the page every frame. If we're already on Login,
|
||||
// keep it. If we just switched into Login, create it once and
|
||||
// inject the keymap.
|
||||
if let Page::Login(_) = &router.current {
|
||||
// Already on login page; keep existing state
|
||||
} else {
|
||||
let mut page = LoginFormState::new();
|
||||
page.editor.set_keymap(config.build_canvas_keymap());
|
||||
router.navigate(Page::Login(page));
|
||||
}
|
||||
}
|
||||
AppView::Register => {
|
||||
if let Page::Register(_) = &router.current {
|
||||
// already on register page
|
||||
} else {
|
||||
let mut page = RegisterFormState::new();
|
||||
page.editor.set_keymap(config.build_canvas_keymap());
|
||||
router.navigate(Page::Register(page));
|
||||
}
|
||||
}
|
||||
AppView::Admin => {
|
||||
info!("Active view is Admin, refreshing profile tree...");
|
||||
if let Page::Admin(current) = &router.current {
|
||||
admin_state = current.clone();
|
||||
}
|
||||
info!("Auth role at render: {:?}", auth_state.role);
|
||||
match grpc_client.get_profile_tree().await {
|
||||
Ok(refreshed_tree) => {
|
||||
app_state.profile_tree = refreshed_tree;
|
||||
}
|
||||
Err(e) => {
|
||||
error!("Failed to refresh profile tree for Admin panel: {}", e);
|
||||
event_handler.command_message = format!("Error refreshing admin data: {}", e);
|
||||
event_handler.command_message =
|
||||
format!("Error refreshing admin data: {}", e);
|
||||
}
|
||||
}
|
||||
app_state.ui.show_admin = true;
|
||||
let profile_names = app_state.profile_tree.profiles.iter()
|
||||
.map(|p| p.name.clone())
|
||||
.collect();
|
||||
admin_state.set_profiles(profile_names);
|
||||
|
||||
if admin_state.current_focus == AdminFocus::default() ||
|
||||
!matches!(admin_state.current_focus,
|
||||
AdminFocus::InsideProfilesList |
|
||||
AdminFocus::Tables | AdminFocus::InsideTablesList |
|
||||
AdminFocus::Button1 | AdminFocus::Button2 | AdminFocus::Button3) {
|
||||
if admin_state.current_focus == AdminFocus::default()
|
||||
|| !matches!(admin_state.current_focus,
|
||||
AdminFocus::InsideProfilesList |
|
||||
AdminFocus::Tables | AdminFocus::InsideTablesList |
|
||||
AdminFocus::Button1 | AdminFocus::Button2 | AdminFocus::Button3)
|
||||
{
|
||||
admin_state.current_focus = AdminFocus::ProfilesPane;
|
||||
}
|
||||
if admin_state.profile_list_state.selected().is_none() && !app_state.profile_tree.profiles.is_empty() {
|
||||
if admin_state.profile_list_state.selected().is_none()
|
||||
&& !app_state.profile_tree.profiles.is_empty()
|
||||
{
|
||||
admin_state.profile_list_state.select(Some(0));
|
||||
}
|
||||
|
||||
router.navigate(Page::Admin(admin_state.clone()));
|
||||
}
|
||||
AppView::AddTable => router.navigate(Page::AddTable(admin_state.add_table_state.clone())),
|
||||
AppView::AddLogic => router.navigate(Page::AddLogic(admin_state.add_logic_state.clone())),
|
||||
AppView::Form(path) => {
|
||||
// Keep current_view_* consistent with the active buffer path
|
||||
if let Some((profile, table)) = path.split_once('/') {
|
||||
app_state.set_current_view_table(
|
||||
profile.to_string(),
|
||||
table.to_string(),
|
||||
);
|
||||
}
|
||||
router.navigate(Page::Form(path.clone()));
|
||||
}
|
||||
AppView::AddTable => app_state.ui.show_add_table = true,
|
||||
AppView::AddLogic => app_state.ui.show_add_logic = true,
|
||||
AppView::Form => app_state.ui.show_form = true,
|
||||
AppView::Scratch => {}
|
||||
}
|
||||
}
|
||||
|
||||
// Continue with the rest of the function...
|
||||
// (The rest remains the same, but now CanvasState trait methods are available)
|
||||
|
||||
if app_state.ui.show_form {
|
||||
if let Page::Form(current_path) = &router.current {
|
||||
let current_view_profile = app_state.current_view_profile_name.clone();
|
||||
let current_view_table = app_state.current_view_table_name.clone();
|
||||
|
||||
@@ -415,9 +485,10 @@ pub async fn run_ui() -> Result<()> {
|
||||
{
|
||||
Ok(new_form_state) => {
|
||||
// Set the new form state and fetch count
|
||||
app_state.set_form_state(new_form_state, &config);
|
||||
let path = format!("{}/{}", prof_name, tbl_name);
|
||||
app_state.ensure_form_editor(&path, &config, || new_form_state);
|
||||
|
||||
if let Some(form_state) = app_state.form_state_mut() {
|
||||
if let Some(form_state) = app_state.form_state_for_path(&path) {
|
||||
if let Err(e) = UiService::fetch_and_set_table_count(
|
||||
&mut grpc_client,
|
||||
form_state,
|
||||
@@ -475,18 +546,21 @@ pub async fn run_ui() -> Result<()> {
|
||||
// Now we can use CanvasState methods like get_current_input(), current_field(), etc.
|
||||
|
||||
if let Some((profile_name, table_name)) = app_state.pending_table_structure_fetch.take() {
|
||||
if app_state.ui.show_add_logic {
|
||||
if admin_state.add_logic_state.profile_name == profile_name &&
|
||||
admin_state.add_logic_state.selected_table_name.as_deref() == Some(table_name.as_str()) {
|
||||
if let Page::AddLogic(state) = &mut router.current {
|
||||
if state.profile_name == profile_name
|
||||
&& state.selected_table_name.as_deref() == Some(table_name.as_str())
|
||||
{
|
||||
info!("Fetching table structure for {}.{}", profile_name, table_name);
|
||||
let fetch_message = UiService::initialize_add_logic_table_data(
|
||||
&mut grpc_client,
|
||||
&mut admin_state.add_logic_state,
|
||||
state,
|
||||
&app_state.profile_tree,
|
||||
).await.unwrap_or_else(|e| {
|
||||
error!("Error initializing add_logic_table_data: {}", e);
|
||||
format!("Error fetching table structure: {}", e)
|
||||
});
|
||||
)
|
||||
.await
|
||||
.unwrap_or_else(|e| {
|
||||
error!("Error initializing add_logic_table_data: {}", e);
|
||||
format!("Error fetching table structure: {}", e)
|
||||
});
|
||||
|
||||
if !fetch_message.contains("Error") && !fetch_message.contains("Warning") {
|
||||
info!("{}", fetch_message);
|
||||
@@ -496,10 +570,11 @@ pub async fn run_ui() -> Result<()> {
|
||||
needs_redraw = true;
|
||||
} else {
|
||||
error!(
|
||||
"Mismatch in pending_table_structure_fetch: app_state wants {}.{}, but add_logic_state is for {}.{:?}",
|
||||
profile_name, table_name,
|
||||
admin_state.add_logic_state.profile_name,
|
||||
admin_state.add_logic_state.selected_table_name
|
||||
"Mismatch in pending_table_structure_fetch: app_state wants {}.{}, but AddLogic state is for {}.{:?}",
|
||||
profile_name,
|
||||
table_name,
|
||||
state.profile_name,
|
||||
state.selected_table_name
|
||||
);
|
||||
}
|
||||
} else {
|
||||
@@ -510,21 +585,21 @@ pub async fn run_ui() -> Result<()> {
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(table_name) = admin_state.add_logic_state.script_editor_awaiting_column_autocomplete.clone() {
|
||||
if app_state.ui.show_add_logic {
|
||||
let profile_name = admin_state.add_logic_state.profile_name.clone();
|
||||
if let Page::AddLogic(state) = &mut router.current {
|
||||
if let Some(table_name) = state.script_editor_awaiting_column_autocomplete.clone() {
|
||||
let profile_name = state.profile_name.clone();
|
||||
|
||||
info!("Fetching columns for table selection: {}.{}", profile_name, table_name);
|
||||
match UiService::fetch_columns_for_table(&mut grpc_client, &profile_name, &table_name).await {
|
||||
Ok(columns) => {
|
||||
admin_state.add_logic_state.set_columns_for_table_autocomplete(columns.clone());
|
||||
state.set_columns_for_table_autocomplete(columns.clone());
|
||||
info!("Loaded {} columns for table '{}'", columns.len(), table_name);
|
||||
event_handler.command_message = format!("Columns for '{}' loaded. Select a column.", table_name);
|
||||
}
|
||||
Err(e) => {
|
||||
error!("Failed to fetch columns for {}.{}: {}", profile_name, table_name, e);
|
||||
admin_state.add_logic_state.script_editor_awaiting_column_autocomplete = None;
|
||||
admin_state.add_logic_state.deactivate_script_editor_autocomplete();
|
||||
state.script_editor_awaiting_column_autocomplete = None;
|
||||
state.deactivate_script_editor_autocomplete();
|
||||
event_handler.command_message = format!("Error loading columns for '{}': {}", table_name, e);
|
||||
}
|
||||
}
|
||||
@@ -532,65 +607,81 @@ pub async fn run_ui() -> Result<()> {
|
||||
}
|
||||
}
|
||||
|
||||
let current_position = app_state.form_state()
|
||||
.map(|fs| fs.current_position)
|
||||
.unwrap_or(1);
|
||||
let current_position = if let Page::Form(path) = &router.current {
|
||||
app_state.form_state_for_path(path).map(|fs| fs.current_position).unwrap_or(1)
|
||||
} else {
|
||||
1
|
||||
};
|
||||
let position_changed = current_position != position_before_event;
|
||||
let mut position_logic_needs_redraw = false;
|
||||
|
||||
if app_state.ui.show_form && !table_just_switched {
|
||||
if position_changed && !event_handler.is_edit_mode {
|
||||
position_logic_needs_redraw = true;
|
||||
if let Page::Form(path) = &router.current {
|
||||
if !table_just_switched {
|
||||
if position_changed && !app_state.is_canvas_edit_mode_at(path) {
|
||||
position_logic_needs_redraw = true;
|
||||
|
||||
if let Some(form_state) = app_state.form_state_mut() {
|
||||
if form_state.current_position > form_state.total_count {
|
||||
form_state.reset_to_empty();
|
||||
event_handler.command_message = format!("New entry for {}.{}", form_state.profile_name, form_state.table_name);
|
||||
} else {
|
||||
match UiService::load_table_data_by_position(&mut grpc_client, form_state).await {
|
||||
Ok(load_message) => {
|
||||
if event_handler.command_message.is_empty() || !load_message.starts_with("Error") {
|
||||
event_handler.command_message = load_message;
|
||||
if let Some(form_state) = app_state.form_state_for_path(path) {
|
||||
if form_state.current_position > form_state.total_count {
|
||||
form_state.reset_to_empty();
|
||||
event_handler.command_message = format!(
|
||||
"New entry for {}.{}",
|
||||
form_state.profile_name,
|
||||
form_state.table_name
|
||||
);
|
||||
} else {
|
||||
match UiService::load_table_data_by_position(&mut grpc_client, form_state).await {
|
||||
Ok(load_message) => {
|
||||
if event_handler.command_message.is_empty()
|
||||
|| !load_message.starts_with("Error")
|
||||
{
|
||||
event_handler.command_message = load_message;
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
event_handler.command_message =
|
||||
format!("Error loading data: {}", e);
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
event_handler.command_message = format!("Error loading data: {}", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let current_input_after_load_str = form_state.get_current_input();
|
||||
let current_input_len_after_load = current_input_after_load_str.chars().count();
|
||||
let max_cursor_pos = if current_input_len_after_load > 0 {
|
||||
current_input_len_after_load.saturating_sub(1)
|
||||
} else {
|
||||
0
|
||||
};
|
||||
form_state.current_cursor_pos = event_handler.ideal_cursor_column.min(max_cursor_pos);
|
||||
}
|
||||
} else if !position_changed && !event_handler.is_edit_mode {
|
||||
if let Some(form_state) = app_state.form_state_mut() {
|
||||
let current_input_str = form_state.get_current_input();
|
||||
let current_input_len = current_input_str.chars().count();
|
||||
let max_cursor_pos = if current_input_len > 0 {
|
||||
current_input_len.saturating_sub(1)
|
||||
} else {
|
||||
0
|
||||
};
|
||||
form_state.current_cursor_pos = event_handler.ideal_cursor_column.min(max_cursor_pos);
|
||||
let current_input_after_load_str = form_state.get_current_input();
|
||||
let current_input_len_after_load =
|
||||
current_input_after_load_str.chars().count();
|
||||
let max_cursor_pos = if current_input_len_after_load > 0 {
|
||||
current_input_len_after_load.saturating_sub(1)
|
||||
} else {
|
||||
0
|
||||
};
|
||||
form_state.current_cursor_pos =
|
||||
event_handler.ideal_cursor_column.min(max_cursor_pos);
|
||||
}
|
||||
} else if !position_changed && !app_state.is_canvas_edit_mode_at(path) {
|
||||
if let Some(form_state) = app_state.form_state_for_path(path) {
|
||||
let current_input_str = form_state.get_current_input();
|
||||
let current_input_len = current_input_str.chars().count();
|
||||
let max_cursor_pos = if current_input_len > 0 {
|
||||
current_input_len.saturating_sub(1)
|
||||
} else {
|
||||
0
|
||||
};
|
||||
form_state.current_cursor_pos =
|
||||
event_handler.ideal_cursor_column.min(max_cursor_pos);
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if app_state.ui.show_register {
|
||||
if !event_handler.is_edit_mode {
|
||||
let current_input = register_state.get_current_input();
|
||||
let max_cursor_pos = if !current_input.is_empty() { current_input.len() - 1 } else { 0 };
|
||||
register_state.current_cursor_pos = event_handler.ideal_cursor_column.min(max_cursor_pos);
|
||||
} else if let Page::Register(state) = &mut router.current {
|
||||
if !app_state.is_canvas_edit_mode() {
|
||||
let current_input = state.get_current_input();
|
||||
let max_cursor_pos =
|
||||
if !current_input.is_empty() { current_input.len() - 1 } else { 0 };
|
||||
state.set_current_cursor_pos(event_handler.ideal_cursor_column.min(max_cursor_pos));
|
||||
}
|
||||
} else if app_state.ui.show_login {
|
||||
if !event_handler.is_edit_mode {
|
||||
let current_input = login_state.get_current_input();
|
||||
let max_cursor_pos = if !current_input.is_empty() { current_input.len() - 1 } else { 0 };
|
||||
login_state.current_cursor_pos = event_handler.ideal_cursor_column.min(max_cursor_pos);
|
||||
} else if let Page::Login(state) = &mut router.current {
|
||||
if !app_state.is_canvas_edit_mode() {
|
||||
let current_input = state.get_current_input();
|
||||
let max_cursor_pos =
|
||||
if !current_input.is_empty() { current_input.len() - 1 } else { 0 };
|
||||
state.set_current_cursor_pos(event_handler.ideal_cursor_column.min(max_cursor_pos));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -621,59 +712,55 @@ pub async fn run_ui() -> Result<()> {
|
||||
}
|
||||
|
||||
if event_processed || needs_redraw || position_changed {
|
||||
let current_mode = ModeManager::derive_mode(&app_state, &event_handler, &admin_state);
|
||||
let current_mode = ModeManager::derive_mode(&app_state, &event_handler, &router);
|
||||
|
||||
match current_mode {
|
||||
AppMode::Edit => { terminal.show_cursor()?; }
|
||||
AppMode::Highlight => { terminal.set_cursor_style(SetCursorStyle::SteadyBlock)?; terminal.show_cursor()?; }
|
||||
AppMode::ReadOnly => {
|
||||
if !app_state.ui.focus_outside_canvas { terminal.set_cursor_style(SetCursorStyle::SteadyBlock)?; }
|
||||
else { terminal.set_cursor_style(SetCursorStyle::SteadyUnderScore)?; }
|
||||
terminal.show_cursor().context("Failed to show cursor in ReadOnly mode")?;
|
||||
}
|
||||
AppMode::General => {
|
||||
if app_state.ui.focus_outside_canvas { terminal.set_cursor_style(SetCursorStyle::SteadyUnderScore)?; terminal.show_cursor()?; }
|
||||
else { terminal.hide_cursor()?; }
|
||||
if app_state.ui.focus_outside_canvas {
|
||||
// Outside canvas → app decides
|
||||
terminal.set_cursor_style(SetCursorStyle::SteadyUnderScore)?;
|
||||
terminal.show_cursor()?;
|
||||
} else {
|
||||
// Inside canvas → let canvas handle it
|
||||
if let Page::Form(path) = &router.current {
|
||||
if let Some(editor) = app_state.editor_for_path(path) {
|
||||
let _ = CursorManager::update_for_mode(editor.mode());
|
||||
}
|
||||
}
|
||||
if let Page::Login(page) = &router.current {
|
||||
let _ = CursorManager::update_for_mode(page.editor.mode());
|
||||
}
|
||||
if let Page::Register(page) = &router.current {
|
||||
let _ = CursorManager::update_for_mode(page.editor.mode());
|
||||
}
|
||||
}
|
||||
}
|
||||
AppMode::Command => {
|
||||
// Command line overlay → app decides
|
||||
terminal.set_cursor_style(SetCursorStyle::SteadyBlock)?;
|
||||
terminal.show_cursor()?;
|
||||
}
|
||||
AppMode::Command => { terminal.set_cursor_style(SetCursorStyle::SteadyUnderScore)?; terminal.show_cursor().context("Failed to show cursor in Command mode")?; }
|
||||
}
|
||||
|
||||
// Temporarily work around borrow checker by extracting needed values
|
||||
let current_dir = app_state.current_dir.clone();
|
||||
|
||||
// Since we can't borrow app_state both mutably and immutably,
|
||||
// we'll need to either:
|
||||
// 1. Modify render_ui to take just app_state and access form_state internally, OR
|
||||
// 2. Extract the specific fields render_ui needs from app_state
|
||||
|
||||
// For now, using approach where we temporarily clone what we need
|
||||
let form_state_clone = app_state.form_state().unwrap().clone();
|
||||
|
||||
terminal.draw(|f| {
|
||||
// Use a mutable clone for rendering
|
||||
let mut temp_form_state = form_state_clone.clone();
|
||||
render_ui(
|
||||
f,
|
||||
&mut temp_form_state,
|
||||
&mut auth_state,
|
||||
&login_state,
|
||||
®ister_state,
|
||||
&intro_state,
|
||||
&mut admin_state,
|
||||
&buffer_state,
|
||||
&theme,
|
||||
event_handler.is_edit_mode,
|
||||
&event_handler.command_input,
|
||||
event_handler.command_mode,
|
||||
&event_handler.command_message,
|
||||
&event_handler.navigation_state,
|
||||
¤t_dir,
|
||||
current_fps,
|
||||
&app_state,
|
||||
);
|
||||
|
||||
// If render_ui modified the form_state, we'd need to sync it back
|
||||
// But typically render functions don't modify state, just read it
|
||||
}).context("Terminal draw call failed")?;
|
||||
terminal
|
||||
.draw(|f| {
|
||||
render_ui(
|
||||
f,
|
||||
&mut router,
|
||||
&buffer_state,
|
||||
&theme,
|
||||
&event_handler.command_input,
|
||||
event_handler.command_mode,
|
||||
&event_handler.command_message,
|
||||
&event_handler.navigation_state,
|
||||
¤t_dir,
|
||||
current_fps,
|
||||
&app_state,
|
||||
&auth_state,
|
||||
);
|
||||
})
|
||||
.context("Terminal draw call failed")?;
|
||||
needs_redraw = false;
|
||||
}
|
||||
|
||||
|
||||
0
client/ui.rs
Normal file
0
client/ui.rs
Normal file
1
server/.gitignore
vendored
Normal file
1
server/.gitignore
vendored
Normal file
@@ -0,0 +1 @@
|
||||
docs-prod/
|
||||
@@ -1,14 +0,0 @@
|
||||
[book]
|
||||
authors = ["Priec"]
|
||||
language = "en"
|
||||
src = "src"
|
||||
title = "Server API Documentation"
|
||||
|
||||
[output.html.search]
|
||||
enable = true
|
||||
limit-results = 30
|
||||
teaser-word-count = 30
|
||||
use-boolean-and = true
|
||||
boost-title = 2
|
||||
boost-hierarchy = 1
|
||||
boost-paragraph = 1
|
||||
@@ -1 +0,0 @@
|
||||
This file makes sure that Github Pages doesn't process mdBook's output.
|
||||
@@ -1,216 +0,0 @@
|
||||
<!DOCTYPE HTML>
|
||||
<html lang="en" class="light sidebar-visible" dir="ltr">
|
||||
<head>
|
||||
<!-- Book generated using mdBook -->
|
||||
<meta charset="UTF-8">
|
||||
<title>Page not found - Server API Documentation</title>
|
||||
<base href="/">
|
||||
|
||||
|
||||
<!-- Custom HTML head -->
|
||||
|
||||
<meta name="description" content="">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1">
|
||||
<meta name="theme-color" content="#ffffff">
|
||||
|
||||
<link rel="icon" href="favicon.svg">
|
||||
<link rel="shortcut icon" href="favicon.png">
|
||||
<link rel="stylesheet" href="css/variables.css">
|
||||
<link rel="stylesheet" href="css/general.css">
|
||||
<link rel="stylesheet" href="css/chrome.css">
|
||||
<link rel="stylesheet" href="css/print.css" media="print">
|
||||
|
||||
<!-- Fonts -->
|
||||
<link rel="stylesheet" href="FontAwesome/css/font-awesome.css">
|
||||
<link rel="stylesheet" href="fonts/fonts.css">
|
||||
|
||||
<!-- Highlight.js Stylesheets -->
|
||||
<link rel="stylesheet" id="highlight-css" href="highlight.css">
|
||||
<link rel="stylesheet" id="tomorrow-night-css" href="tomorrow-night.css">
|
||||
<link rel="stylesheet" id="ayu-highlight-css" href="ayu-highlight.css">
|
||||
|
||||
<!-- Custom theme stylesheets -->
|
||||
|
||||
|
||||
<!-- Provide site root and default themes to javascript -->
|
||||
<script>
|
||||
const path_to_root = "";
|
||||
const default_light_theme = "light";
|
||||
const default_dark_theme = "navy";
|
||||
</script>
|
||||
<!-- Start loading toc.js asap -->
|
||||
<script src="toc.js"></script>
|
||||
</head>
|
||||
<body>
|
||||
<div id="body-container">
|
||||
<!-- Work around some values being stored in localStorage wrapped in quotes -->
|
||||
<script>
|
||||
try {
|
||||
let theme = localStorage.getItem('mdbook-theme');
|
||||
let sidebar = localStorage.getItem('mdbook-sidebar');
|
||||
|
||||
if (theme.startsWith('"') && theme.endsWith('"')) {
|
||||
localStorage.setItem('mdbook-theme', theme.slice(1, theme.length - 1));
|
||||
}
|
||||
|
||||
if (sidebar.startsWith('"') && sidebar.endsWith('"')) {
|
||||
localStorage.setItem('mdbook-sidebar', sidebar.slice(1, sidebar.length - 1));
|
||||
}
|
||||
} catch (e) { }
|
||||
</script>
|
||||
|
||||
<!-- Set the theme before any content is loaded, prevents flash -->
|
||||
<script>
|
||||
const default_theme = window.matchMedia("(prefers-color-scheme: dark)").matches ? default_dark_theme : default_light_theme;
|
||||
let theme;
|
||||
try { theme = localStorage.getItem('mdbook-theme'); } catch(e) { }
|
||||
if (theme === null || theme === undefined) { theme = default_theme; }
|
||||
const html = document.documentElement;
|
||||
html.classList.remove('light')
|
||||
html.classList.add(theme);
|
||||
html.classList.add("js");
|
||||
</script>
|
||||
|
||||
<input type="checkbox" id="sidebar-toggle-anchor" class="hidden">
|
||||
|
||||
<!-- Hide / unhide sidebar before it is displayed -->
|
||||
<script>
|
||||
let sidebar = null;
|
||||
const sidebar_toggle = document.getElementById("sidebar-toggle-anchor");
|
||||
if (document.body.clientWidth >= 1080) {
|
||||
try { sidebar = localStorage.getItem('mdbook-sidebar'); } catch(e) { }
|
||||
sidebar = sidebar || 'visible';
|
||||
} else {
|
||||
sidebar = 'hidden';
|
||||
}
|
||||
sidebar_toggle.checked = sidebar === 'visible';
|
||||
html.classList.remove('sidebar-visible');
|
||||
html.classList.add("sidebar-" + sidebar);
|
||||
</script>
|
||||
|
||||
<nav id="sidebar" class="sidebar" aria-label="Table of contents">
|
||||
<!-- populated by js -->
|
||||
<mdbook-sidebar-scrollbox class="sidebar-scrollbox"></mdbook-sidebar-scrollbox>
|
||||
<noscript>
|
||||
<iframe class="sidebar-iframe-outer" src="toc.html"></iframe>
|
||||
</noscript>
|
||||
<div id="sidebar-resize-handle" class="sidebar-resize-handle">
|
||||
<div class="sidebar-resize-indicator"></div>
|
||||
</div>
|
||||
</nav>
|
||||
|
||||
<div id="page-wrapper" class="page-wrapper">
|
||||
|
||||
<div class="page">
|
||||
<div id="menu-bar-hover-placeholder"></div>
|
||||
<div id="menu-bar" class="menu-bar sticky">
|
||||
<div class="left-buttons">
|
||||
<label id="sidebar-toggle" class="icon-button" for="sidebar-toggle-anchor" title="Toggle Table of Contents" aria-label="Toggle Table of Contents" aria-controls="sidebar">
|
||||
<i class="fa fa-bars"></i>
|
||||
</label>
|
||||
<button id="theme-toggle" class="icon-button" type="button" title="Change theme" aria-label="Change theme" aria-haspopup="true" aria-expanded="false" aria-controls="theme-list">
|
||||
<i class="fa fa-paint-brush"></i>
|
||||
</button>
|
||||
<ul id="theme-list" class="theme-popup" aria-label="Themes" role="menu">
|
||||
<li role="none"><button role="menuitem" class="theme" id="default_theme">Auto</button></li>
|
||||
<li role="none"><button role="menuitem" class="theme" id="light">Light</button></li>
|
||||
<li role="none"><button role="menuitem" class="theme" id="rust">Rust</button></li>
|
||||
<li role="none"><button role="menuitem" class="theme" id="coal">Coal</button></li>
|
||||
<li role="none"><button role="menuitem" class="theme" id="navy">Navy</button></li>
|
||||
<li role="none"><button role="menuitem" class="theme" id="ayu">Ayu</button></li>
|
||||
</ul>
|
||||
<button id="search-toggle" class="icon-button" type="button" title="Search. (Shortkey: s)" aria-label="Toggle Searchbar" aria-expanded="false" aria-keyshortcuts="S" aria-controls="searchbar">
|
||||
<i class="fa fa-search"></i>
|
||||
</button>
|
||||
</div>
|
||||
|
||||
<h1 class="menu-title">Server API Documentation</h1>
|
||||
|
||||
<div class="right-buttons">
|
||||
<a href="print.html" title="Print this book" aria-label="Print this book">
|
||||
<i id="print-button" class="fa fa-print"></i>
|
||||
</a>
|
||||
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<div id="search-wrapper" class="hidden">
|
||||
<form id="searchbar-outer" class="searchbar-outer">
|
||||
<input type="search" id="searchbar" name="searchbar" placeholder="Search this book ..." aria-controls="searchresults-outer" aria-describedby="searchresults-header">
|
||||
</form>
|
||||
<div id="searchresults-outer" class="searchresults-outer hidden">
|
||||
<div id="searchresults-header" class="searchresults-header"></div>
|
||||
<ul id="searchresults">
|
||||
</ul>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<!-- Apply ARIA attributes after the sidebar and the sidebar toggle button are added to the DOM -->
|
||||
<script>
|
||||
document.getElementById('sidebar-toggle').setAttribute('aria-expanded', sidebar === 'visible');
|
||||
document.getElementById('sidebar').setAttribute('aria-hidden', sidebar !== 'visible');
|
||||
Array.from(document.querySelectorAll('#sidebar a')).forEach(function(link) {
|
||||
link.setAttribute('tabIndex', sidebar === 'visible' ? 0 : -1);
|
||||
});
|
||||
</script>
|
||||
|
||||
<div id="content" class="content">
|
||||
<main>
|
||||
<h1 id="document-not-found-404"><a class="header" href="#document-not-found-404">Document not found (404)</a></h1>
|
||||
<p>This URL is invalid, sorry. Please use the navigation bar or search to continue.</p>
|
||||
|
||||
</main>
|
||||
|
||||
<nav class="nav-wrapper" aria-label="Page navigation">
|
||||
<!-- Mobile navigation buttons -->
|
||||
|
||||
|
||||
<div style="clear: both"></div>
|
||||
</nav>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<nav class="nav-wide-wrapper" aria-label="Page navigation">
|
||||
|
||||
</nav>
|
||||
|
||||
</div>
|
||||
|
||||
<!-- Livereload script (if served using the cli tool) -->
|
||||
<script>
|
||||
const wsProtocol = location.protocol === 'https:' ? 'wss:' : 'ws:';
|
||||
const wsAddress = wsProtocol + "//" + location.host + "/" + "__livereload";
|
||||
const socket = new WebSocket(wsAddress);
|
||||
socket.onmessage = function (event) {
|
||||
if (event.data === "reload") {
|
||||
socket.close();
|
||||
location.reload();
|
||||
}
|
||||
};
|
||||
|
||||
window.onbeforeunload = function() {
|
||||
socket.close();
|
||||
}
|
||||
</script>
|
||||
|
||||
|
||||
|
||||
<script>
|
||||
window.playground_copyable = true;
|
||||
</script>
|
||||
|
||||
|
||||
<script src="elasticlunr.min.js"></script>
|
||||
<script src="mark.min.js"></script>
|
||||
<script src="searcher.js"></script>
|
||||
|
||||
<script src="clipboard.min.js"></script>
|
||||
<script src="highlight.js"></script>
|
||||
<script src="book.js"></script>
|
||||
|
||||
<!-- Custom JS scripts -->
|
||||
|
||||
|
||||
</div>
|
||||
</body>
|
||||
</html>
|
||||
File diff suppressed because one or more lines are too long
Binary file not shown.
Binary file not shown.
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user