Compare commits
35 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
26b899df16 | ||
|
|
afc8e1a1e5 | ||
|
|
b6c4d3308d | ||
|
|
af4567aa3d | ||
|
|
415bc2044d | ||
|
|
91ad2b0caf | ||
|
|
bc6471fa54 | ||
|
|
0704668d8d | ||
|
|
2e9f8815d2 | ||
|
|
f4689125e0 | ||
|
|
bbba67a253 | ||
|
|
800b857e53 | ||
|
|
921059bed8 | ||
|
|
e8b585dc07 | ||
|
|
afce27184a | ||
|
|
8d41beef0b | ||
|
|
5e482cd77b | ||
|
|
09068fd4e5 | ||
|
|
d8c2b9089b | ||
|
|
bb577bc276 | ||
|
|
6267e3d593 | ||
|
|
c091a39802 | ||
|
|
f94006dd08 | ||
|
|
f42790980d | ||
|
|
779683de4b | ||
|
|
aa8887318f | ||
|
|
3ad8dc6490 | ||
|
|
20f9fae141 | ||
|
|
ec062bbf24 | ||
|
|
8745c9ea2f | ||
|
|
0917654361 | ||
|
|
d892d1cfa0 | ||
|
|
e31138c250 | ||
|
|
7cbe5ce8be | ||
|
|
1c31d4cd1c |
23
Cargo.lock
generated
23
Cargo.lock
generated
@@ -421,7 +421,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "client"
|
||||
version = "0.3.5"
|
||||
version = "0.3.13"
|
||||
dependencies = [
|
||||
"async-trait",
|
||||
"common",
|
||||
@@ -432,6 +432,7 @@ dependencies = [
|
||||
"prost",
|
||||
"ratatui",
|
||||
"serde",
|
||||
"time",
|
||||
"tokio",
|
||||
"toml",
|
||||
"tonic",
|
||||
@@ -461,7 +462,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "common"
|
||||
version = "0.3.5"
|
||||
version = "0.3.13"
|
||||
dependencies = [
|
||||
"prost",
|
||||
"serde",
|
||||
@@ -713,9 +714,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "deranged"
|
||||
version = "0.3.11"
|
||||
version = "0.4.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "b42b6fa04a440b495c8b04d0e71b707c585f83cb9cb28cf8cd0d976c315e31b4"
|
||||
checksum = "9c9e6a11ca8224451684bc0d7d5a7adbf8f2fd6887261a1cfc3c0432f9d4068e"
|
||||
dependencies = [
|
||||
"powerfmt",
|
||||
]
|
||||
@@ -2592,7 +2593,7 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "server"
|
||||
version = "0.3.5"
|
||||
version = "0.3.13"
|
||||
dependencies = [
|
||||
"bcrypt",
|
||||
"chrono",
|
||||
@@ -3212,9 +3213,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "time"
|
||||
version = "0.3.39"
|
||||
version = "0.3.41"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "dad298b01a40a23aac4580b67e3dbedb7cc8402f3592d7f49469de2ea4aecdd8"
|
||||
checksum = "8a7619e19bc266e0f9c5e6686659d394bc57973859340060a69221e57dbc0c40"
|
||||
dependencies = [
|
||||
"deranged",
|
||||
"itoa",
|
||||
@@ -3229,15 +3230,15 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "time-core"
|
||||
version = "0.1.3"
|
||||
version = "0.1.4"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "765c97a5b985b7c11d7bc27fa927dc4fe6af3a6dfb021d28deb60d3bf51e76ef"
|
||||
checksum = "c9e9a38711f559d9e3ce1cdb06dd7c5b8ea546bc90052da6d06bb76da74bb07c"
|
||||
|
||||
[[package]]
|
||||
name = "time-macros"
|
||||
version = "0.2.20"
|
||||
version = "0.2.22"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "e8093bc3e81c3bc5f7879de09619d06c9a5a5e45ca44dfeeb7225bae38005c5c"
|
||||
checksum = "3526739392ec93fd8b359c8e98514cb3e8e021beb4e5f597b00a0221f8ed8a49"
|
||||
dependencies = [
|
||||
"num-conv",
|
||||
"time-core",
|
||||
|
||||
@@ -5,7 +5,7 @@ resolver = "2"
|
||||
[workspace.package]
|
||||
# TODO: idk how to do the name, fix later
|
||||
# name = "Multieko2"
|
||||
version = "0.3.5"
|
||||
version = "0.3.13"
|
||||
edition = "2021"
|
||||
license = "GPL-3.0-or-later"
|
||||
authors = ["Filip Priečinský <filippriec@gmail.com>"]
|
||||
|
||||
@@ -1,5 +1,7 @@
|
||||
# Hey
|
||||
|
||||
This is only work in progress, until release 1.0.0 this is for development use cases only.
|
||||
|
||||
I run development like this:
|
||||
|
||||
Server:
|
||||
|
||||
@@ -15,6 +15,7 @@ lazy_static = "1.5.0"
|
||||
prost = "0.13.5"
|
||||
ratatui = "0.29.0"
|
||||
serde = { version = "1.0.218", features = ["derive"] }
|
||||
time = "0.3.41"
|
||||
tokio = { version = "1.43.0", features = ["full", "macros"] }
|
||||
toml = "0.8.20"
|
||||
tonic = "0.12.3"
|
||||
|
||||
@@ -2,6 +2,8 @@
|
||||
[keybindings]
|
||||
|
||||
enter_command_mode = [":", "ctrl+;"]
|
||||
next_buffer = ["ctrl+l"]
|
||||
previous_buffer = ["ctrl+h"]
|
||||
|
||||
[keybindings.general]
|
||||
move_up = ["k", "Up"]
|
||||
@@ -10,6 +12,7 @@ next_option = ["l", "Right"]
|
||||
previous_option = ["h", "Left"]
|
||||
select = ["Enter"]
|
||||
toggle_sidebar = ["ctrl+t"]
|
||||
toggle_buffer_list = ["ctrl+b"]
|
||||
next_field = ["Tab"]
|
||||
prev_field = ["Shift+Tab"]
|
||||
|
||||
@@ -24,6 +27,7 @@ save_and_quit = ["ctrl+shift+s"]
|
||||
move_up = ["Up"]
|
||||
move_down = ["Down"]
|
||||
toggle_sidebar = ["ctrl+t"]
|
||||
toggle_buffer_list = ["ctrl+b"]
|
||||
|
||||
# MODE SPECIFIC
|
||||
# READ ONLY MODE
|
||||
@@ -45,9 +49,18 @@ move_line_start = ["0"]
|
||||
move_line_end = ["$"]
|
||||
move_first_line = ["gg"]
|
||||
move_last_line = ["x"]
|
||||
enter_highlight_mode = ["v"]
|
||||
enter_highlight_mode_linewise = ["ctrl+v"]
|
||||
|
||||
[keybindings.highlight]
|
||||
exit_highlight_mode = ["esc"]
|
||||
enter_highlight_mode_linewise = ["ctrl+v"]
|
||||
|
||||
[keybindings.edit]
|
||||
exit_edit_mode = ["esc","ctrl+e"]
|
||||
# BIG CHANGES NOW EXIT HANDLES EITHER IF THOSE
|
||||
# exit_edit_mode = ["esc","ctrl+e"]
|
||||
# exit_suggestion_mode = ["esc"]
|
||||
exit = ["esc", "ctrl+e"]
|
||||
delete_char_forward = ["delete"]
|
||||
delete_char_backward = ["backspace"]
|
||||
next_field = ["enter"]
|
||||
@@ -57,7 +70,6 @@ move_right = ["right"]
|
||||
suggestion_down = ["ctrl+n", "tab"]
|
||||
suggestion_up = ["ctrl+p", "shift+tab"]
|
||||
select_suggestion = ["enter"]
|
||||
exit_suggestion_mode = ["esc"]
|
||||
|
||||
[keybindings.command]
|
||||
exit_command_mode = ["ctrl+g", "esc"]
|
||||
|
||||
@@ -1,24 +1,35 @@
|
||||
// src/components/admin/admin_panel.rs
|
||||
|
||||
use crate::config::colors::themes::Theme;
|
||||
use crate::state::pages::admin::AdminState; // Import the persistent state
|
||||
use crate::state::pages::auth::AuthState;
|
||||
use crate::state::pages::admin::AdminState;
|
||||
use common::proto::multieko2::table_definition::ProfileTreeResponse;
|
||||
use ratatui::{
|
||||
layout::{Alignment, Constraint, Direction, Layout, Rect},
|
||||
style::Style, // Added Modifier
|
||||
style::Style,
|
||||
text::{Line, Span, Text},
|
||||
widgets::{Block, BorderType, Borders, List, ListItem, Paragraph, Wrap},
|
||||
Frame,
|
||||
};
|
||||
|
||||
// Renamed from AdminPanelState::render and made a standalone function
|
||||
/// Renders the view specific to admin users.
|
||||
fn render_admin_panel_admin(f: &mut Frame, content_chunks: &[Rect], theme: &Theme) {
|
||||
// Admin-specific view placeholder
|
||||
let admin_message = Paragraph::new("Admin-specific view. Profile selection not applicable.")
|
||||
.style(Style::default().fg(theme.fg))
|
||||
.alignment(Alignment::Center);
|
||||
// Render only in the right pane for now, leaving left empty
|
||||
f.render_widget(admin_message, content_chunks[1]);
|
||||
}
|
||||
|
||||
pub fn render_admin_panel(
|
||||
f: &mut Frame,
|
||||
admin_state: &AdminState, // Accept the persistent state (immutable borrow is enough for rendering)
|
||||
auth_state: &AuthState,
|
||||
admin_state: &AdminState,
|
||||
area: Rect,
|
||||
theme: &Theme,
|
||||
profile_tree: &ProfileTreeResponse,
|
||||
selected_profile: &Option<String>, // The globally selected profile for the checkmark
|
||||
selected_profile: &Option<String>,
|
||||
) {
|
||||
let block = Block::default()
|
||||
.borders(Borders::ALL)
|
||||
@@ -45,14 +56,36 @@ 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, &content_chunks, theme);
|
||||
}
|
||||
}
|
||||
|
||||
/// Renders the view for non-admin users (profile list and details).
|
||||
fn render_admin_panel_non_admin(
|
||||
f: &mut Frame,
|
||||
admin_state: &AdminState,
|
||||
content_chunks: &[Rect],
|
||||
theme: &Theme,
|
||||
profile_tree: &ProfileTreeResponse,
|
||||
selected_profile: &Option<String>,
|
||||
) {
|
||||
// Profile list - Use data from admin_state
|
||||
let items: Vec<ListItem> = admin_state
|
||||
.profiles // Use profiles from the persistent state
|
||||
.profiles
|
||||
.iter()
|
||||
.map(|p| {
|
||||
ListItem::new(Line::from(vec![
|
||||
Span::styled(
|
||||
// Check against the globally selected profile for the checkmark
|
||||
if Some(p) == selected_profile.as_ref() { "✓ " } else { " " },
|
||||
Style::default().fg(theme.accent),
|
||||
),
|
||||
@@ -65,15 +98,12 @@ pub fn render_admin_panel(
|
||||
.block(Block::default().title("Profiles"))
|
||||
.highlight_style(Style::default().bg(theme.highlight).fg(theme.bg));
|
||||
|
||||
// Render statefully using a CLONE of the list_state from persistent AdminState
|
||||
// Cloning is necessary because render_stateful_widget needs `&mut ListState`
|
||||
// but we only have `&AdminState`. This is a common pattern in Ratatui.
|
||||
let mut list_state_clone = admin_state.list_state.clone();
|
||||
f.render_stateful_widget(list, content_chunks[0], &mut list_state_clone);
|
||||
|
||||
// Profile details - Use selection info from admin_state
|
||||
if let Some(profile) = admin_state
|
||||
.get_selected_index() // Use the method from persistent state
|
||||
.get_selected_index()
|
||||
.and_then(|i| profile_tree.profiles.get(i))
|
||||
{
|
||||
let mut text = Text::default();
|
||||
@@ -101,8 +131,7 @@ pub fn render_admin_panel(
|
||||
|
||||
let details_widget = Paragraph::new(text)
|
||||
.block(Block::default().title("Details"))
|
||||
.wrap(Wrap { trim: true }); // Add wrapping
|
||||
.wrap(Wrap { trim: true });
|
||||
f.render_widget(details_widget, content_chunks[1]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -12,6 +12,7 @@ use ratatui::{
|
||||
widgets::{Block, BorderType, Borders, Paragraph},
|
||||
Frame,
|
||||
};
|
||||
use crate::state::app::highlight::HighlightState;
|
||||
|
||||
pub fn render_login(
|
||||
f: &mut Frame,
|
||||
@@ -20,6 +21,7 @@ pub fn render_login(
|
||||
login_state: &LoginState,
|
||||
app_state: &AppState,
|
||||
is_edit_mode: bool,
|
||||
highlight_state: &HighlightState,
|
||||
) {
|
||||
// Main container
|
||||
let block = Block::default()
|
||||
@@ -56,6 +58,7 @@ pub fn render_login(
|
||||
&[&login_state.username, &login_state.password],
|
||||
theme,
|
||||
is_edit_mode,
|
||||
highlight_state,
|
||||
);
|
||||
|
||||
// --- ERROR MESSAGE ---
|
||||
|
||||
@@ -14,6 +14,7 @@ use ratatui::{
|
||||
widgets::{Block, BorderType, Borders, Paragraph},
|
||||
Frame,
|
||||
};
|
||||
use crate::state::app::highlight::HighlightState;
|
||||
|
||||
pub fn render_register(
|
||||
f: &mut Frame,
|
||||
@@ -22,6 +23,7 @@ pub fn render_register(
|
||||
state: &RegisterState, // Use RegisterState
|
||||
app_state: &AppState,
|
||||
is_edit_mode: bool,
|
||||
highlight_state: &HighlightState,
|
||||
) {
|
||||
let block = Block::default()
|
||||
.borders(Borders::ALL)
|
||||
@@ -64,6 +66,7 @@ pub fn render_register(
|
||||
&state.inputs().iter().map(|s| *s).collect::<Vec<&String>>(), // Pass inputs directly
|
||||
theme,
|
||||
is_edit_mode,
|
||||
highlight_state,
|
||||
);
|
||||
|
||||
// --- HELP TEXT ---
|
||||
|
||||
@@ -1,11 +1,11 @@
|
||||
// src/client/components/handlers/status_line.rs
|
||||
use ratatui::{
|
||||
widgets::Paragraph,
|
||||
style::Style,
|
||||
layout::Rect,
|
||||
Frame,
|
||||
text::{Line, Span},
|
||||
widgets::Paragraph,
|
||||
};
|
||||
use unicode_width::UnicodeWidthStr;
|
||||
use crate::config::colors::themes::Theme;
|
||||
use std::path::Path;
|
||||
|
||||
@@ -15,17 +15,11 @@ pub fn render_status_line(
|
||||
current_dir: &str,
|
||||
theme: &Theme,
|
||||
is_edit_mode: bool,
|
||||
current_fps: f64,
|
||||
) {
|
||||
// Program name and version
|
||||
let program_info = format!("multieko2 v{}", env!("CARGO_PKG_VERSION"));
|
||||
let mode_text = if is_edit_mode { "[EDIT]" } else { "[READ-ONLY]" };
|
||||
|
||||
let mode_text = if is_edit_mode {
|
||||
"[EDIT]"
|
||||
} else {
|
||||
"[READ-ONLY]"
|
||||
};
|
||||
|
||||
// Shorten the current directory path
|
||||
let home_dir = dirs::home_dir().map(|p| p.to_string_lossy().into_owned()).unwrap_or_default();
|
||||
let display_dir = if current_dir.starts_with(&home_dir) {
|
||||
current_dir.replacen(&home_dir, "~", 1)
|
||||
@@ -33,47 +27,51 @@ pub fn render_status_line(
|
||||
current_dir.to_string()
|
||||
};
|
||||
|
||||
// Create the full status line text
|
||||
let full_text = format!("{} | {} | {}", mode_text, display_dir, program_info);
|
||||
|
||||
// Check if the full text fits in the available width
|
||||
let available_width = area.width as usize;
|
||||
let mut display_text = if full_text.len() <= available_width {
|
||||
// If it fits, use the full text
|
||||
full_text
|
||||
let mode_width = UnicodeWidthStr::width(mode_text);
|
||||
let program_info_width = UnicodeWidthStr::width(program_info.as_str());
|
||||
let fps_text = format!("{:.0} FPS", current_fps);
|
||||
let fps_width = UnicodeWidthStr::width(fps_text.as_str());
|
||||
let separator = " | ";
|
||||
let separator_width = UnicodeWidthStr::width(separator);
|
||||
|
||||
let fixed_width_with_fps = mode_width + separator_width + separator_width +
|
||||
program_info_width + separator_width + fps_width;
|
||||
let show_fps = fixed_width_with_fps < available_width;
|
||||
|
||||
let remaining_width_for_dir = available_width.saturating_sub(
|
||||
mode_width + separator_width + separator_width + program_info_width +
|
||||
if show_fps { separator_width + fps_width } else { 0 }
|
||||
);
|
||||
|
||||
let dir_display_text = if UnicodeWidthStr::width(display_dir.as_str()) <= remaining_width_for_dir {
|
||||
display_dir
|
||||
} else {
|
||||
// If it doesn't fit, prioritize mode and program info, and show only the directory name
|
||||
let dir_name = Path::new(current_dir)
|
||||
.file_name()
|
||||
.and_then(|n| n.to_str())
|
||||
.unwrap_or(current_dir);
|
||||
format!("{} | {} | {}", mode_text, dir_name, program_info)
|
||||
if UnicodeWidthStr::width(dir_name) <= remaining_width_for_dir {
|
||||
dir_name.to_string()
|
||||
} else {
|
||||
dir_name.chars().take(remaining_width_for_dir).collect()
|
||||
}
|
||||
};
|
||||
|
||||
// If even the shortened version overflows, truncate it
|
||||
if display_text.len() > available_width {
|
||||
display_text = display_text.chars().take(available_width).collect();
|
||||
}
|
||||
|
||||
// Create the status line text using Line and Span
|
||||
let status_line = Line::from(vec![
|
||||
let mut spans = vec![
|
||||
Span::styled(mode_text, Style::default().fg(theme.accent)),
|
||||
Span::styled(" | ", Style::default().fg(theme.border)),
|
||||
Span::styled(
|
||||
display_text.split(" | ").nth(1).unwrap_or(""), // Directory part
|
||||
Style::default().fg(theme.fg),
|
||||
),
|
||||
Span::styled(dir_display_text, Style::default().fg(theme.fg)),
|
||||
Span::styled(" | ", Style::default().fg(theme.border)),
|
||||
Span::styled(
|
||||
program_info,
|
||||
Style::default()
|
||||
.fg(theme.secondary)
|
||||
.add_modifier(ratatui::style::Modifier::BOLD),
|
||||
),
|
||||
]);
|
||||
Span::styled(program_info, Style::default().fg(theme.secondary)),
|
||||
];
|
||||
|
||||
// Render the status line
|
||||
let paragraph = Paragraph::new(status_line)
|
||||
if show_fps {
|
||||
spans.push(Span::styled(" | ", Style::default().fg(theme.border)));
|
||||
spans.push(Span::styled(fps_text, Style::default().fg(theme.secondary)));
|
||||
}
|
||||
|
||||
let paragraph = Paragraph::new(Line::from(spans))
|
||||
.style(Style::default().bg(theme.bg));
|
||||
|
||||
f.render_widget(paragraph, area);
|
||||
|
||||
@@ -7,6 +7,7 @@ use ratatui::{
|
||||
};
|
||||
use crate::config::colors::themes::Theme;
|
||||
use crate::state::pages::canvas_state::CanvasState;
|
||||
use crate::state::app::highlight::HighlightState;
|
||||
use crate::components::handlers::canvas::render_canvas;
|
||||
|
||||
pub fn render_form(
|
||||
@@ -18,6 +19,7 @@ pub fn render_form(
|
||||
inputs: &[&String],
|
||||
theme: &Theme,
|
||||
is_edit_mode: bool,
|
||||
highlight_state: &HighlightState,
|
||||
total_count: u64,
|
||||
current_position: u64,
|
||||
) {
|
||||
@@ -62,5 +64,6 @@ pub fn render_form(
|
||||
inputs,
|
||||
theme,
|
||||
is_edit_mode,
|
||||
highlight_state,
|
||||
);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
// src/components/handlers.rs
|
||||
pub mod canvas;
|
||||
pub mod sidebar;
|
||||
pub mod buffer_list;
|
||||
|
||||
pub use canvas::*;
|
||||
pub use sidebar::*;
|
||||
pub use buffer_list::*;
|
||||
|
||||
76
client/src/components/handlers/buffer_list.rs
Normal file
76
client/src/components/handlers/buffer_list.rs
Normal file
@@ -0,0 +1,76 @@
|
||||
// src/components/handlers/buffer_list.rs
|
||||
|
||||
use crate::config::colors::themes::Theme;
|
||||
use crate::state::app::buffer::BufferState;
|
||||
use ratatui::{
|
||||
layout::{Alignment, Rect},
|
||||
style::{Style, Stylize},
|
||||
text::{Line, Span},
|
||||
widgets::Paragraph,
|
||||
Frame,
|
||||
};
|
||||
use unicode_width::UnicodeWidthStr;
|
||||
use crate::functions::common::buffer::get_view_layer;
|
||||
|
||||
pub fn render_buffer_list(
|
||||
f: &mut Frame,
|
||||
area: Rect,
|
||||
theme: &Theme,
|
||||
buffer_state: &BufferState,
|
||||
) {
|
||||
// --- Style Definitions ---
|
||||
let active_style = Style::default()
|
||||
.fg(theme.bg)
|
||||
.bg(theme.highlight);
|
||||
|
||||
let inactive_style = Style::default()
|
||||
.fg(theme.fg)
|
||||
.bg(theme.bg);
|
||||
|
||||
// --- Determine Active Layer ---
|
||||
let active_layer = match buffer_state.history.get(buffer_state.active_index) {
|
||||
Some(view) => get_view_layer(view),
|
||||
None => 1,
|
||||
};
|
||||
|
||||
// --- Create Spans ---
|
||||
let mut spans = Vec::new();
|
||||
let mut current_width = 0;
|
||||
|
||||
for (original_index, view) in buffer_state.history.iter().enumerate() {
|
||||
// Filter: Only process views matching the active layer
|
||||
if get_view_layer(view) != active_layer {
|
||||
continue;
|
||||
}
|
||||
|
||||
let is_active = original_index == buffer_state.active_index;
|
||||
let buffer_name = view.display_name();
|
||||
let buffer_text = format!(" {} ", buffer_name);
|
||||
let text_width = UnicodeWidthStr::width(buffer_text.as_str());
|
||||
|
||||
// Calculate width needed for this buffer (separator + text)
|
||||
let needed_width = text_width;
|
||||
if current_width + needed_width > area.width as usize {
|
||||
break;
|
||||
}
|
||||
|
||||
// Add the buffer text itself
|
||||
let text_style = if is_active { active_style } else { inactive_style };
|
||||
spans.push(Span::styled(buffer_text, text_style));
|
||||
current_width += text_width;
|
||||
}
|
||||
|
||||
// --- Filler Span ---
|
||||
let remaining_width = area.width.saturating_sub(current_width as u16);
|
||||
if !spans.is_empty() || remaining_width > 0 {
|
||||
spans.push(Span::styled(
|
||||
" ".repeat(remaining_width as usize),
|
||||
inactive_style,
|
||||
));
|
||||
}
|
||||
|
||||
// --- Render ---
|
||||
let buffer_line = Line::from(spans);
|
||||
let paragraph = Paragraph::new(buffer_line).alignment(Alignment::Left);
|
||||
f.render_widget(paragraph, area);
|
||||
}
|
||||
@@ -2,31 +2,33 @@
|
||||
use ratatui::{
|
||||
widgets::{Paragraph, Block, Borders},
|
||||
layout::{Layout, Constraint, Direction, Rect},
|
||||
style::Style,
|
||||
style::{Style, Modifier},
|
||||
text::{Line, Span},
|
||||
Frame,
|
||||
prelude::Alignment,
|
||||
};
|
||||
use crate::config::colors::themes::Theme;
|
||||
use crate::state::pages::canvas_state::CanvasState;
|
||||
use crate::state::app::highlight::HighlightState; // Ensure correct import path
|
||||
use std::cmp::{min, max};
|
||||
|
||||
pub fn render_canvas(
|
||||
f: &mut Frame,
|
||||
area: Rect,
|
||||
form_state: &impl CanvasState,
|
||||
fields: &[&str],
|
||||
current_field: &usize,
|
||||
current_field_idx: &usize,
|
||||
inputs: &[&String],
|
||||
theme: &Theme,
|
||||
is_edit_mode: bool,
|
||||
highlight_state: &HighlightState, // Using the enum state
|
||||
) -> Option<Rect> {
|
||||
// Split area into columns
|
||||
// ... (setup code remains the same) ...
|
||||
let columns = Layout::default()
|
||||
.direction(Direction::Horizontal)
|
||||
.constraints([Constraint::Percentage(30), Constraint::Percentage(70)])
|
||||
.split(area);
|
||||
|
||||
// Input container styling
|
||||
let border_style = if form_state.has_unsaved_changes() {
|
||||
Style::default().fg(theme.warning)
|
||||
} else if is_edit_mode {
|
||||
@@ -39,7 +41,6 @@ pub fn render_canvas(
|
||||
.border_style(border_style)
|
||||
.style(Style::default().bg(theme.bg));
|
||||
|
||||
// Input block dimensions
|
||||
let input_block = Rect {
|
||||
x: columns[1].x,
|
||||
y: columns[1].y,
|
||||
@@ -49,7 +50,6 @@ pub fn render_canvas(
|
||||
|
||||
f.render_widget(&input_container, input_block);
|
||||
|
||||
// Input rows layout
|
||||
let input_area = input_container.inner(input_block);
|
||||
let input_rows = Layout::default()
|
||||
.direction(Direction::Vertical)
|
||||
@@ -72,17 +72,113 @@ pub fn render_canvas(
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
// Render inputs and cursor
|
||||
for (i, input) in inputs.iter().enumerate() {
|
||||
let is_active = i == *current_field;
|
||||
let input_display = Paragraph::new(input.as_str())
|
||||
.alignment(Alignment::Left)
|
||||
.style(if is_active {
|
||||
Style::default().fg(theme.highlight)
|
||||
} else {
|
||||
Style::default().fg(theme.fg)
|
||||
});
|
||||
let is_active = i == *current_field_idx;
|
||||
let current_cursor_pos = form_state.current_cursor_pos();
|
||||
let text = input.as_str();
|
||||
let text_len = text.chars().count();
|
||||
|
||||
let line: Line;
|
||||
|
||||
// --- Use match on the highlight_state enum ---
|
||||
match highlight_state {
|
||||
HighlightState::Off => {
|
||||
// Not in highlight mode, render normally
|
||||
line = Line::from(Span::styled(
|
||||
text,
|
||||
if is_active { Style::default().fg(theme.highlight) } else { Style::default().fg(theme.fg) }
|
||||
));
|
||||
}
|
||||
HighlightState::Characterwise { anchor } => {
|
||||
// --- Character-wise Highlight Logic ---
|
||||
let (anchor_field, anchor_char) = *anchor;
|
||||
let start_field = min(anchor_field, *current_field_idx);
|
||||
let end_field = max(anchor_field, *current_field_idx);
|
||||
|
||||
// Use start_char and end_char consistently
|
||||
let (start_char, end_char) = if anchor_field == *current_field_idx {
|
||||
(min(anchor_char, current_cursor_pos), max(anchor_char, current_cursor_pos))
|
||||
} else if anchor_field < *current_field_idx {
|
||||
(anchor_char, current_cursor_pos)
|
||||
} else {
|
||||
(current_cursor_pos, anchor_char)
|
||||
};
|
||||
|
||||
let highlight_style = Style::default().fg(theme.highlight).bg(theme.highlight_bg).add_modifier(Modifier::BOLD);
|
||||
let normal_style_in_highlight = Style::default().fg(theme.highlight);
|
||||
let normal_style_outside = Style::default().fg(theme.fg);
|
||||
|
||||
if i >= start_field && i <= end_field {
|
||||
// This line is within the character-wise highlight range
|
||||
if start_field == end_field { // Case 1: Single Line Highlight
|
||||
// Use start_char and end_char here
|
||||
let clamped_start = start_char.min(text_len);
|
||||
let clamped_end = end_char.min(text_len); // Use text_len for slicing logic
|
||||
|
||||
let before: String = text.chars().take(clamped_start).collect();
|
||||
let highlighted: String = text.chars().skip(clamped_start).take(clamped_end.saturating_sub(clamped_start) + 1).collect();
|
||||
// Define 'after' here
|
||||
let after: String = text.chars().skip(clamped_end + 1).collect();
|
||||
|
||||
line = Line::from(vec![
|
||||
Span::styled(before, normal_style_in_highlight),
|
||||
Span::styled(highlighted, highlight_style),
|
||||
Span::styled(after, normal_style_in_highlight), // Use defined 'after'
|
||||
]);
|
||||
} else if i == start_field { // Case 2: Multi-Line Highlight - Start Line
|
||||
// Use start_char here
|
||||
let safe_start = start_char.min(text_len);
|
||||
let before: String = text.chars().take(safe_start).collect();
|
||||
let highlighted: String = text.chars().skip(safe_start).collect();
|
||||
line = Line::from(vec![
|
||||
Span::styled(before, normal_style_in_highlight),
|
||||
Span::styled(highlighted, highlight_style),
|
||||
]);
|
||||
} else if i == end_field { // Case 3: Multi-Line Highlight - End Line (Corrected index)
|
||||
// Use end_char here
|
||||
let safe_end_inclusive = if text_len > 0 { end_char.min(text_len - 1) } else { 0 };
|
||||
let highlighted: String = text.chars().take(safe_end_inclusive + 1).collect();
|
||||
let after: String = text.chars().skip(safe_end_inclusive + 1).collect();
|
||||
line = Line::from(vec![
|
||||
Span::styled(highlighted, highlight_style),
|
||||
Span::styled(after, normal_style_in_highlight),
|
||||
]);
|
||||
} else { // Case 4: Multi-Line Highlight - Middle Line (Corrected index)
|
||||
line = Line::from(Span::styled(text, highlight_style)); // Highlight whole line
|
||||
}
|
||||
} else { // Case 5: Line Outside Character-wise Highlight Range
|
||||
line = Line::from(Span::styled(
|
||||
text,
|
||||
// Use normal styling (active or inactive)
|
||||
if is_active { normal_style_in_highlight } else { normal_style_outside }
|
||||
));
|
||||
}
|
||||
}
|
||||
HighlightState::Linewise { anchor_line } => {
|
||||
// --- Linewise Highlight Logic ---
|
||||
let start_field = min(*anchor_line, *current_field_idx);
|
||||
let end_field = max(*anchor_line, *current_field_idx);
|
||||
let highlight_style = Style::default().fg(theme.highlight).bg(theme.highlight_bg).add_modifier(Modifier::BOLD);
|
||||
let normal_style_in_highlight = Style::default().fg(theme.highlight);
|
||||
let normal_style_outside = Style::default().fg(theme.fg);
|
||||
|
||||
if i >= start_field && i <= end_field {
|
||||
// Highlight the entire line
|
||||
line = Line::from(Span::styled(text, highlight_style));
|
||||
} else {
|
||||
// Line outside linewise highlight range
|
||||
line = Line::from(Span::styled(
|
||||
text,
|
||||
// Use normal styling (active or inactive)
|
||||
if is_active { normal_style_in_highlight } else { normal_style_outside }
|
||||
));
|
||||
}
|
||||
}
|
||||
} // End match highlight_state
|
||||
|
||||
let input_display = Paragraph::new(line).alignment(Alignment::Left);
|
||||
f.render_widget(input_display, input_rows[i]);
|
||||
|
||||
if is_active {
|
||||
@@ -95,3 +191,4 @@ pub fn render_canvas(
|
||||
|
||||
active_field_input_rect
|
||||
}
|
||||
|
||||
|
||||
@@ -8,103 +8,80 @@ use ratatui::{
|
||||
Frame,
|
||||
};
|
||||
use crate::config::colors::themes::Theme;
|
||||
use crate::state::pages::intro::IntroState;
|
||||
|
||||
pub struct IntroState {
|
||||
pub selected_option: usize,
|
||||
pub fn render_intro(f: &mut Frame, intro_state: &IntroState, area: Rect, theme: &Theme) {
|
||||
let block = Block::default()
|
||||
.borders(Borders::ALL)
|
||||
.border_type(BorderType::Rounded)
|
||||
.border_style(Style::default().fg(theme.accent))
|
||||
.style(Style::default().bg(theme.bg));
|
||||
|
||||
let inner_area = block.inner(area);
|
||||
f.render_widget(block, area);
|
||||
|
||||
// Center layout
|
||||
let chunks = Layout::default()
|
||||
.direction(Direction::Vertical)
|
||||
.constraints([
|
||||
Constraint::Percentage(40),
|
||||
Constraint::Length(5),
|
||||
Constraint::Percentage(40),
|
||||
])
|
||||
.split(inner_area);
|
||||
|
||||
// Title
|
||||
let title = Line::from(vec![
|
||||
Span::styled("multieko2", Style::default().fg(theme.highlight)),
|
||||
Span::styled(" v", Style::default().fg(theme.fg)),
|
||||
Span::styled(env!("CARGO_PKG_VERSION"), Style::default().fg(theme.secondary)),
|
||||
]);
|
||||
let title_para = Paragraph::new(title)
|
||||
.alignment(Alignment::Center);
|
||||
f.render_widget(title_para, chunks[1]);
|
||||
|
||||
// Buttons - now with 4 options
|
||||
let button_area = Layout::default()
|
||||
.direction(Direction::Horizontal)
|
||||
.constraints([
|
||||
Constraint::Percentage(25),
|
||||
Constraint::Percentage(25),
|
||||
Constraint::Percentage(25),
|
||||
Constraint::Percentage(25),
|
||||
])
|
||||
.split(chunks[1].inner(Margin {
|
||||
horizontal: 1,
|
||||
vertical: 1
|
||||
}));
|
||||
|
||||
let buttons = ["Continue", "Admin", "Login", "Register"];
|
||||
for (i, &text) in buttons.iter().enumerate() {
|
||||
render_button(f, button_area[i], text, intro_state.selected_option == i, theme);
|
||||
}
|
||||
}
|
||||
|
||||
impl IntroState {
|
||||
pub fn new() -> Self {
|
||||
Self { selected_option: 0 }
|
||||
}
|
||||
fn render_button(f: &mut Frame, area: Rect, text: &str, selected: bool, theme: &Theme) {
|
||||
let button_style = Style::default()
|
||||
.fg(if selected { theme.highlight } else { theme.fg })
|
||||
.bg(theme.bg)
|
||||
.add_modifier(if selected {
|
||||
ratatui::style::Modifier::BOLD
|
||||
} else {
|
||||
ratatui::style::Modifier::empty()
|
||||
});
|
||||
|
||||
pub fn render(&self, f: &mut Frame, area: Rect, theme: &Theme) {
|
||||
let block = Block::default()
|
||||
let border_style = Style::default()
|
||||
.fg(if selected { theme.accent } else { theme.border });
|
||||
|
||||
let button = Paragraph::new(text)
|
||||
.style(button_style)
|
||||
.alignment(Alignment::Center)
|
||||
.block(
|
||||
Block::default()
|
||||
.borders(Borders::ALL)
|
||||
.border_type(BorderType::Rounded)
|
||||
.border_style(Style::default().fg(theme.accent))
|
||||
.style(Style::default().bg(theme.bg));
|
||||
.border_type(BorderType::Double)
|
||||
.border_style(border_style),
|
||||
);
|
||||
|
||||
let inner_area = block.inner(area);
|
||||
f.render_widget(block, area);
|
||||
|
||||
// Center layout
|
||||
let chunks = Layout::default()
|
||||
.direction(Direction::Vertical)
|
||||
.constraints([
|
||||
Constraint::Percentage(40),
|
||||
Constraint::Length(5),
|
||||
Constraint::Percentage(40),
|
||||
])
|
||||
.split(inner_area);
|
||||
|
||||
// Title
|
||||
let title = Line::from(vec![
|
||||
Span::styled("multieko2", Style::default().fg(theme.highlight)),
|
||||
Span::styled(" v", Style::default().fg(theme.fg)),
|
||||
Span::styled(env!("CARGO_PKG_VERSION"), Style::default().fg(theme.secondary)),
|
||||
]);
|
||||
let title_para = Paragraph::new(title)
|
||||
.alignment(Alignment::Center);
|
||||
f.render_widget(title_para, chunks[1]);
|
||||
|
||||
// Buttons - now with 4 options
|
||||
let button_area = Layout::default()
|
||||
.direction(Direction::Horizontal)
|
||||
.constraints([
|
||||
Constraint::Percentage(25),
|
||||
Constraint::Percentage(25),
|
||||
Constraint::Percentage(25),
|
||||
Constraint::Percentage(25),
|
||||
])
|
||||
.split(chunks[1].inner(Margin {
|
||||
horizontal: 1,
|
||||
vertical: 1
|
||||
}));
|
||||
|
||||
let buttons = ["Continue", "Admin", "Login", "Register"];
|
||||
for (i, &text) in buttons.iter().enumerate() {
|
||||
self.render_button(
|
||||
f,
|
||||
button_area[i],
|
||||
text,
|
||||
self.selected_option == i,
|
||||
theme,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
fn render_button(&self, f: &mut Frame, area: Rect, text: &str, selected: bool, theme: &Theme) {
|
||||
let button_style = Style::default()
|
||||
.fg(if selected { theme.highlight } else { theme.fg })
|
||||
.bg(theme.bg)
|
||||
.add_modifier(if selected {
|
||||
ratatui::style::Modifier::BOLD
|
||||
} else {
|
||||
ratatui::style::Modifier::empty()
|
||||
});
|
||||
|
||||
let border_style = Style::default()
|
||||
.fg(if selected { theme.accent } else { theme.border });
|
||||
|
||||
let button = Paragraph::new(text)
|
||||
.style(button_style)
|
||||
.alignment(Alignment::Center)
|
||||
.block(
|
||||
Block::default()
|
||||
.borders(Borders::ALL)
|
||||
.border_type(BorderType::Double)
|
||||
.border_style(border_style),
|
||||
);
|
||||
|
||||
f.render_widget(button, area);
|
||||
}
|
||||
|
||||
pub fn next_option(&mut self) {
|
||||
self.selected_option = (self.selected_option + 1) % 4;
|
||||
}
|
||||
|
||||
pub fn previous_option(&mut self) {
|
||||
self.selected_option = if self.selected_option == 0 { 3 } else { self.selected_option - 1 };
|
||||
}
|
||||
f.render_widget(button, area);
|
||||
}
|
||||
|
||||
@@ -32,6 +32,8 @@ pub struct ModeKeybindings {
|
||||
#[serde(default)]
|
||||
pub edit: HashMap<String, Vec<String>>,
|
||||
#[serde(default)]
|
||||
pub highlight: HashMap<String, Vec<String>>,
|
||||
#[serde(default)]
|
||||
pub command: HashMap<String, Vec<String>>,
|
||||
#[serde(default)]
|
||||
pub common: HashMap<String, Vec<String>>,
|
||||
@@ -75,6 +77,14 @@ impl Config {
|
||||
.or_else(|| self.get_action_for_key_in_mode(&self.keybindings.global, key, modifiers))
|
||||
}
|
||||
|
||||
/// Gets an action for a key in Highlight mode, also checking common/global keybindings.
|
||||
pub fn get_highlight_action_for_key(&self, key: KeyCode, modifiers: KeyModifiers) -> Option<&str> {
|
||||
self.get_action_for_key_in_mode(&self.keybindings.highlight, key, modifiers)
|
||||
.or_else(|| self.get_action_for_key_in_mode(&self.keybindings.common, key, modifiers))
|
||||
.or_else(|| self.get_action_for_key_in_mode(&self.keybindings.read_only, key, modifiers))
|
||||
.or_else(|| self.get_action_for_key_in_mode(&self.keybindings.global, key, modifiers))
|
||||
}
|
||||
|
||||
/// Gets an action for a key in Command mode, also checking common keybindings.
|
||||
pub fn get_command_action_for_key(&self, key: KeyCode, modifiers: KeyModifiers) -> Option<&str> {
|
||||
self.get_action_for_key_in_mode(&self.keybindings.command, key, modifiers)
|
||||
|
||||
@@ -10,6 +10,7 @@ pub struct Theme {
|
||||
pub highlight: Color,
|
||||
pub warning: Color,
|
||||
pub border: Color,
|
||||
pub highlight_bg: Color,
|
||||
}
|
||||
|
||||
impl Theme {
|
||||
@@ -31,6 +32,7 @@ impl Theme {
|
||||
highlight: Color::Rgb(152, 251, 152), // Pastel green
|
||||
warning: Color::Rgb(255, 182, 193), // Pastel pink
|
||||
border: Color::Rgb(220, 220, 220), // Light gray border
|
||||
highlight_bg: Color::Rgb(70, 70, 70), // Darker grey for highlight background
|
||||
}
|
||||
}
|
||||
|
||||
@@ -44,6 +46,7 @@ impl Theme {
|
||||
highlight: Color::Rgb(50, 205, 50), // Bright green
|
||||
warning: Color::Rgb(255, 99, 71), // Bright red
|
||||
border: Color::Rgb(100, 100, 100), // Medium gray border
|
||||
highlight_bg: Color::Rgb(180, 180, 180), // Lighter grey for highlight background
|
||||
}
|
||||
}
|
||||
|
||||
@@ -57,6 +60,7 @@ impl Theme {
|
||||
highlight: Color::Rgb(0, 128, 0), // Green
|
||||
warning: Color::Rgb(255, 0, 0), // Red
|
||||
border: Color::Rgb(0, 0, 0), // Black border
|
||||
highlight_bg: Color::Rgb(180, 180, 180), // Lighter grey for highlight background
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,2 +1,5 @@
|
||||
// src/functions/common.rs
|
||||
|
||||
pub mod buffer;
|
||||
|
||||
pub use buffer::*;
|
||||
|
||||
35
client/src/functions/common/buffer.rs
Normal file
35
client/src/functions/common/buffer.rs
Normal file
@@ -0,0 +1,35 @@
|
||||
// src/functions/common/buffer.rs
|
||||
|
||||
use crate::state::app::buffer::BufferState;
|
||||
use crate::state::app::buffer::AppView;
|
||||
|
||||
pub fn get_view_layer(view: &AppView) -> u8 {
|
||||
match view {
|
||||
AppView::Intro => 1,
|
||||
AppView::Login | AppView::Register | AppView::Admin => 2,
|
||||
AppView::Form(_) | AppView::Scratch => 3,
|
||||
}
|
||||
}
|
||||
|
||||
/// Switches the active buffer index.
|
||||
pub fn switch_buffer(buffer_state: &mut BufferState, next: bool) -> bool {
|
||||
if buffer_state.history.len() <= 1 {
|
||||
return false;
|
||||
}
|
||||
|
||||
let len = buffer_state.history.len();
|
||||
let current_index = buffer_state.active_index;
|
||||
let new_index = if next {
|
||||
(current_index + 1) % len
|
||||
} else {
|
||||
(current_index + len - 1) % len
|
||||
};
|
||||
|
||||
if new_index != current_index {
|
||||
buffer_state.active_index = new_index;
|
||||
true
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,14 +1,19 @@
|
||||
// src/modes/canvas/edit.rs
|
||||
|
||||
use crate::config::binds::config::Config;
|
||||
use crate::services::grpc_client::GrpcClient;
|
||||
use crate::state::pages::{auth::{LoginState, RegisterState}};
|
||||
use crate::state::pages::canvas_state::CanvasState;
|
||||
use crate::state::pages::{auth::{LoginState, RegisterState}, canvas_state::CanvasState};
|
||||
use crate::state::pages::form::FormState;
|
||||
use crate::functions::modes::edit::{auth_e, form_e};
|
||||
use crate::modes::handlers::event::EventOutcome;
|
||||
use crate::functions::modes::edit::{auth_e, form_e};
|
||||
use crate::state::app::state::AppState;
|
||||
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
|
||||
// Removed duplicate/unused imports
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum EditEventOutcome {
|
||||
Message(String), // Return a message, stay in Edit mode
|
||||
ExitEditMode, // Signal to exit Edit mode
|
||||
}
|
||||
|
||||
pub async fn handle_edit_event(
|
||||
key: KeyEvent,
|
||||
@@ -17,12 +22,12 @@ pub async fn handle_edit_event(
|
||||
login_state: &mut LoginState,
|
||||
register_state: &mut RegisterState,
|
||||
ideal_cursor_column: &mut usize,
|
||||
command_message: &mut String,
|
||||
// command_message: &mut String, // Removed as messages are returned
|
||||
current_position: &mut u64,
|
||||
total_count: u64,
|
||||
grpc_client: &mut GrpcClient,
|
||||
app_state: &AppState,
|
||||
) -> Result<String, Box<dyn std::error::Error>> {
|
||||
) -> Result<EditEventOutcome, Box<dyn std::error::Error>> {
|
||||
|
||||
// Global command mode check
|
||||
if let Some("enter_command_mode") = config.get_action_for_key_in_mode(
|
||||
@@ -30,8 +35,7 @@ pub async fn handle_edit_event(
|
||||
key.code,
|
||||
key.modifiers
|
||||
) {
|
||||
*command_message = "Switching to Command Mode...".to_string();
|
||||
return Ok(command_message.clone());
|
||||
return Ok(EditEventOutcome::Message("Switching to Command Mode...".to_string()));
|
||||
}
|
||||
|
||||
// Common actions (save/revert)
|
||||
@@ -41,14 +45,15 @@ pub async fn handle_edit_event(
|
||||
key.modifiers
|
||||
) {
|
||||
if matches!(action, "save" | "revert") {
|
||||
let message = if app_state.ui.show_login {
|
||||
// Ensure all branches result in Result<String, Error> before the final Ok(...) wrap
|
||||
let message_string: String = if app_state.ui.show_login {
|
||||
auth_e::execute_common_action(
|
||||
action,
|
||||
login_state,
|
||||
grpc_client,
|
||||
current_position,
|
||||
total_count
|
||||
).await
|
||||
).await? // Results in String on success
|
||||
} else if app_state.ui.show_register {
|
||||
auth_e::execute_common_action(
|
||||
action,
|
||||
@@ -56,46 +61,66 @@ pub async fn handle_edit_event(
|
||||
grpc_client,
|
||||
current_position,
|
||||
total_count
|
||||
).await
|
||||
).await? // Results in String on success
|
||||
} else {
|
||||
form_e::execute_common_action(
|
||||
let outcome = form_e::execute_common_action(
|
||||
action,
|
||||
form_state, // Concrete FormState
|
||||
form_state,
|
||||
grpc_client,
|
||||
current_position,
|
||||
total_count
|
||||
).await
|
||||
.map(|outcome| match outcome {
|
||||
).await?; // This returns EventOutcome on success
|
||||
|
||||
// Extract the message string from the EventOutcome
|
||||
match outcome {
|
||||
EventOutcome::Ok(msg) => msg,
|
||||
EventOutcome::Exit(msg) => format!("Exit requested: {}", msg),
|
||||
EventOutcome::DataSaved(save_outcome, msg) => format!("Data saved ({:?}): {}", save_outcome, msg),
|
||||
EventOutcome::ButtonSelected { context, index } => {
|
||||
"Unexpected action in edit mode".to_string()
|
||||
}
|
||||
})
|
||||
}?;
|
||||
return Ok(message);
|
||||
EventOutcome::DataSaved(_, msg) => msg,
|
||||
_ => format!("Unexpected outcome from common action: {:?}", outcome),
|
||||
}
|
||||
};
|
||||
// Wrap the resulting String message
|
||||
return Ok(EditEventOutcome::Message(message_string));
|
||||
}
|
||||
}
|
||||
|
||||
// Edit-specific actions
|
||||
if let Some(action) = config.get_edit_action_for_key(key.code, key.modifiers) {
|
||||
// --- Special Handling for Tab/Shift+Tab in Role Field ---
|
||||
if action == "exit" {
|
||||
if app_state.ui.show_register && register_state.in_suggestion_mode {
|
||||
// Call the action, get Result<String, Error>
|
||||
let msg = auth_e::execute_edit_action(
|
||||
"exit_suggestion_mode",
|
||||
key,
|
||||
register_state,
|
||||
ideal_cursor_column,
|
||||
grpc_client,
|
||||
current_position,
|
||||
total_count,
|
||||
).await?; // Results in String on success
|
||||
// Wrap the String message
|
||||
return Ok(EditEventOutcome::Message(msg));
|
||||
} else {
|
||||
// Signal exit
|
||||
return Ok(EditEventOutcome::ExitEditMode);
|
||||
}
|
||||
}
|
||||
|
||||
// Special handling for role field suggestions
|
||||
if app_state.ui.show_register && register_state.current_field() == 4 {
|
||||
if !register_state.in_suggestion_mode && key.code == KeyCode::Tab && key.modifiers == KeyModifiers::NONE {
|
||||
register_state.update_role_suggestions();
|
||||
if !register_state.role_suggestions.is_empty() {
|
||||
register_state.in_suggestion_mode = true;
|
||||
register_state.selected_suggestion_index = Some(0); // Select first suggestion
|
||||
return Ok("Suggestions shown".to_string());
|
||||
} else {
|
||||
return Ok("No suggestions available".to_string());
|
||||
register_state.selected_suggestion_index = Some(0);
|
||||
return Ok(EditEventOutcome::Message("Suggestions shown".to_string()));
|
||||
} else { // Added else here for clarity
|
||||
return Ok(EditEventOutcome::Message("No suggestions available".to_string()));
|
||||
}
|
||||
}
|
||||
}
|
||||
// --- End Special Handling ---
|
||||
|
||||
return if app_state.ui.show_login {
|
||||
// Execute other edit actions
|
||||
let msg = if app_state.ui.show_login {
|
||||
auth_e::execute_edit_action(
|
||||
action,
|
||||
key,
|
||||
@@ -104,7 +129,7 @@ pub async fn handle_edit_event(
|
||||
grpc_client,
|
||||
current_position,
|
||||
total_count
|
||||
).await
|
||||
).await? // Results in String
|
||||
} else if app_state.ui.show_register {
|
||||
auth_e::execute_edit_action(
|
||||
action,
|
||||
@@ -114,7 +139,7 @@ pub async fn handle_edit_event(
|
||||
grpc_client,
|
||||
current_position,
|
||||
total_count
|
||||
).await
|
||||
).await? // Results in String
|
||||
} else {
|
||||
form_e::execute_edit_action(
|
||||
action,
|
||||
@@ -124,22 +149,22 @@ pub async fn handle_edit_event(
|
||||
grpc_client,
|
||||
current_position,
|
||||
total_count
|
||||
).await
|
||||
).await? // Results in String
|
||||
};
|
||||
// Wrap the resulting String message
|
||||
return Ok(EditEventOutcome::Message(msg));
|
||||
}
|
||||
|
||||
// Character insertion
|
||||
if let KeyCode::Char(_) = key.code {
|
||||
// If in suggestion mode, exit it before inserting char
|
||||
if app_state.ui.show_register && register_state.in_suggestion_mode {
|
||||
register_state.in_suggestion_mode = false;
|
||||
register_state.show_role_suggestions = false;
|
||||
register_state.selected_suggestion_index = None;
|
||||
}
|
||||
let is_role_field = app_state.ui.show_register && register_state.current_field() == 4;
|
||||
// --- End Autocomplete Trigger ---
|
||||
|
||||
return if app_state.ui.show_login {
|
||||
|
||||
// Execute insert_char action
|
||||
let msg = if app_state.ui.show_login {
|
||||
auth_e::execute_edit_action(
|
||||
"insert_char",
|
||||
key,
|
||||
@@ -148,7 +173,7 @@ pub async fn handle_edit_event(
|
||||
grpc_client,
|
||||
current_position,
|
||||
total_count
|
||||
).await
|
||||
).await? // Results in String
|
||||
} else if app_state.ui.show_register {
|
||||
auth_e::execute_edit_action(
|
||||
"insert_char",
|
||||
@@ -158,7 +183,7 @@ pub async fn handle_edit_event(
|
||||
grpc_client,
|
||||
current_position,
|
||||
total_count
|
||||
).await
|
||||
).await? // Results in String
|
||||
} else {
|
||||
form_e::execute_edit_action(
|
||||
"insert_char",
|
||||
@@ -168,23 +193,23 @@ pub async fn handle_edit_event(
|
||||
grpc_client,
|
||||
current_position,
|
||||
total_count
|
||||
).await
|
||||
).await? // Results in String
|
||||
};
|
||||
// Wrap the resulting String message
|
||||
return Ok(EditEventOutcome::Message(msg));
|
||||
}
|
||||
|
||||
// Handle Backspace/Delete for Autocomplete Trigger
|
||||
// Handle Backspace/Delete
|
||||
if matches!(key.code, KeyCode::Backspace | KeyCode::Delete) {
|
||||
// If in suggestion mode, exit it before deleting char
|
||||
if app_state.ui.show_register && register_state.in_suggestion_mode {
|
||||
register_state.in_suggestion_mode = false;
|
||||
register_state.show_role_suggestions = false;
|
||||
register_state.selected_suggestion_index = None;
|
||||
}
|
||||
let is_role_field = app_state.ui.show_register && register_state.current_field() == 4;
|
||||
let action_str = if key.code == KeyCode::Backspace { "backspace" } else { "delete_char" };
|
||||
|
||||
// Execute the action first
|
||||
let result = if app_state.ui.show_register {
|
||||
let action_str = if key.code == KeyCode::Backspace { "backspace" } else { "delete_char" };
|
||||
// Ensure both branches result in a String *before* wrapping
|
||||
let result_msg: String = if app_state.ui.show_register {
|
||||
auth_e::execute_edit_action(
|
||||
action_str,
|
||||
key,
|
||||
@@ -193,14 +218,17 @@ pub async fn handle_edit_event(
|
||||
grpc_client,
|
||||
current_position,
|
||||
total_count
|
||||
).await
|
||||
).await? // Results in String
|
||||
} else {
|
||||
// Handle for login/form if needed, assuming auth_e covers RegisterState
|
||||
Ok("Action not applicable here".to_string()) // Placeholder
|
||||
}?;
|
||||
// Return String directly, not Ok(String)
|
||||
"Action not applicable here".to_string()
|
||||
}; // Semicolon here ends the if/else expression
|
||||
|
||||
return Ok(result);
|
||||
// Wrap the resulting String message
|
||||
return Ok(EditEventOutcome::Message(result_msg));
|
||||
}
|
||||
|
||||
Ok(command_message.clone())
|
||||
// Default return if no other handler matched
|
||||
Ok(EditEventOutcome::Message("".to_string()))
|
||||
}
|
||||
|
||||
|
||||
@@ -4,6 +4,7 @@ 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::state::app::buffer::BufferState;
|
||||
use crate::state::pages::auth::AuthState;
|
||||
use crate::state::pages::auth::LoginState;
|
||||
use crate::state::pages::auth::RegisterState;
|
||||
@@ -20,6 +21,7 @@ pub async fn handle_dialog_event(
|
||||
auth_state: &mut AuthState,
|
||||
login_state: &mut LoginState,
|
||||
register_state: &mut RegisterState,
|
||||
buffer_state: &mut BufferState,
|
||||
) -> Option<Result<EventOutcome, Box<dyn std::error::Error>>> {
|
||||
if let Event::Key(key) = event {
|
||||
// Always allow Esc to dismiss
|
||||
@@ -62,7 +64,7 @@ pub async fn handle_dialog_event(
|
||||
match selected_index {
|
||||
0 => { // "Menu" button selected
|
||||
app_state.hide_dialog();
|
||||
let message = login::back_to_main(login_state, app_state).await;
|
||||
let message = login::back_to_main(login_state, app_state, buffer_state).await;
|
||||
return Some(Ok(EventOutcome::Ok(message)));
|
||||
}
|
||||
1 => {
|
||||
@@ -91,8 +93,7 @@ pub async fn handle_dialog_event(
|
||||
match selected_index {
|
||||
0 => { // "OK" button for RegisterSuccess
|
||||
app_state.hide_dialog();
|
||||
// Go back to intro after successful registration dialog
|
||||
let message = register::back_to_main(register_state, app_state).await;
|
||||
let message = register::back_to_login(register_state, app_state, buffer_state).await;
|
||||
return Some(Ok(EventOutcome::Ok(message)));
|
||||
}
|
||||
_ => { // Default for RegisterSuccess
|
||||
|
||||
@@ -6,6 +6,7 @@ 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::state::pages::canvas_state::CanvasState;
|
||||
use crate::ui::handlers::context::UiContext;
|
||||
@@ -18,6 +19,7 @@ pub async fn handle_navigation_event(
|
||||
app_state: &mut AppState,
|
||||
login_state: &mut LoginState,
|
||||
register_state: &mut RegisterState,
|
||||
intro_state: &mut IntroState,
|
||||
admin_state: &mut AdminState,
|
||||
command_mode: &mut bool,
|
||||
command_input: &mut String,
|
||||
@@ -26,27 +28,21 @@ 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, admin_state);
|
||||
move_up(app_state, login_state, register_state, intro_state, admin_state);
|
||||
return Ok(EventOutcome::Ok(String::new()));
|
||||
}
|
||||
"move_down" => {
|
||||
move_down(app_state, admin_state);
|
||||
move_down(app_state, intro_state, admin_state);
|
||||
return Ok(EventOutcome::Ok(String::new()));
|
||||
}
|
||||
"next_option" => {
|
||||
next_option(app_state);
|
||||
next_option(app_state, intro_state);
|
||||
return Ok(EventOutcome::Ok(String::new()));
|
||||
}
|
||||
"previous_option" => {
|
||||
previous_option(app_state);
|
||||
previous_option(app_state, intro_state);
|
||||
return Ok(EventOutcome::Ok(String::new()));
|
||||
}
|
||||
"toggle_sidebar" => {
|
||||
toggle_sidebar(app_state);
|
||||
return Ok(EventOutcome::Ok(format!("Sidebar {}",
|
||||
if app_state.ui.show_sidebar { "shown" } else { "hidden" }
|
||||
)));
|
||||
}
|
||||
"next_field" => {
|
||||
next_field(form_state);
|
||||
return Ok(EventOutcome::Ok(String::new()));
|
||||
@@ -61,7 +57,7 @@ pub async fn handle_navigation_event(
|
||||
}
|
||||
"select" => {
|
||||
let (context, index) = if app_state.ui.show_intro {
|
||||
(UiContext::Intro, app_state.ui.intro_state.selected_option)
|
||||
(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 {
|
||||
@@ -81,7 +77,7 @@ 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, admin_state: &mut AdminState) {
|
||||
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;
|
||||
@@ -96,28 +92,28 @@ pub fn move_up(app_state: &mut AppState, login_state: &mut LoginState, register_
|
||||
app_state.focused_button_index = app_state.focused_button_index.saturating_sub(1);
|
||||
}
|
||||
} else if app_state.ui.show_intro {
|
||||
app_state.ui.intro_state.previous_option();
|
||||
intro_state.previous_option();
|
||||
} else if app_state.ui.show_admin {
|
||||
admin_state.previous();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn move_down(app_state: &mut AppState, admin_state: &mut AdminState) {
|
||||
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;
|
||||
}
|
||||
} else if app_state.ui.show_intro {
|
||||
app_state.ui.intro_state.next_option();
|
||||
intro_state.next_option();
|
||||
} else if app_state.ui.show_admin {
|
||||
admin_state.next();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn next_option(app_state: &mut AppState) { // Remove option_count parameter
|
||||
pub fn next_option(app_state: &mut AppState, intro_state: &mut IntroState) {
|
||||
if app_state.ui.show_intro {
|
||||
app_state.ui.intro_state.next_option();
|
||||
intro_state.next_option();
|
||||
} else {
|
||||
// Get option count from state instead of parameter
|
||||
let option_count = app_state.profile_tree.profiles.len();
|
||||
@@ -125,9 +121,9 @@ pub fn next_option(app_state: &mut AppState) { // Remove option_count parameter
|
||||
}
|
||||
}
|
||||
|
||||
pub fn previous_option(app_state: &mut AppState) {
|
||||
pub fn previous_option(app_state: &mut AppState, intro_state: &mut IntroState) {
|
||||
if app_state.ui.show_intro {
|
||||
app_state.ui.intro_state.previous_option();
|
||||
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 {
|
||||
@@ -138,10 +134,6 @@ pub fn previous_option(app_state: &mut AppState) {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn toggle_sidebar(app_state: &mut AppState) {
|
||||
app_state.ui.show_sidebar = !app_state.ui.show_sidebar;
|
||||
}
|
||||
|
||||
pub fn next_field(form_state: &mut FormState) {
|
||||
if !form_state.fields.is_empty() {
|
||||
form_state.current_field = (form_state.current_field + 1) % form_state.fields.len();
|
||||
|
||||
@@ -1,30 +1,45 @@
|
||||
// src/modes/handlers/event.rs
|
||||
use crossterm::event::Event;
|
||||
use crossterm::cursor::SetCursorStyle;
|
||||
use crate::tui::terminal::core::TerminalCore;
|
||||
use crate::services::grpc_client::GrpcClient;
|
||||
use crate::services::auth::AuthClient;
|
||||
use crate::modes::common::commands::CommandHandler;
|
||||
use crate::config::binds::config::Config;
|
||||
use crate::state::pages::form::FormState;
|
||||
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::app::state::AppState;
|
||||
use crate::state::pages::canvas_state::CanvasState;
|
||||
use crate::ui::handlers::rat_state::UiStateHandler;
|
||||
use crate::ui::handlers::context::UiContext;
|
||||
use crate::tui::functions::{intro, admin};
|
||||
use crate::tui::functions::common::{login, register};
|
||||
use crate::functions::common::buffer;
|
||||
use crate::tui::{
|
||||
terminal::core::TerminalCore,
|
||||
functions::{
|
||||
common::{
|
||||
form::SaveOutcome,
|
||||
login,
|
||||
register,
|
||||
},
|
||||
},
|
||||
{intro, admin},
|
||||
};
|
||||
use crate::state::{
|
||||
app::{
|
||||
highlight::HighlightState,
|
||||
state::AppState,
|
||||
buffer::{AppView, BufferState},
|
||||
},
|
||||
pages::{
|
||||
auth::{AuthState, LoginState, RegisterState},
|
||||
admin::AdminState,
|
||||
canvas_state::CanvasState,
|
||||
form::FormState,
|
||||
intro::IntroState,
|
||||
},
|
||||
};
|
||||
use crate::modes::{
|
||||
common::command_mode,
|
||||
common::{command_mode, commands::CommandHandler},
|
||||
handlers::mode_manager::{ModeManager, AppMode},
|
||||
canvas::{edit, read_only, common_mode},
|
||||
highlight::highlight,
|
||||
general::{navigation, dialog},
|
||||
};
|
||||
use crate::config::binds::key_sequences::KeySequenceTracker;
|
||||
use crate::modes::handlers::mode_manager::{ModeManager, AppMode};
|
||||
use crate::tui::functions::common::form::SaveOutcome;
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum EventOutcome {
|
||||
@@ -39,6 +54,7 @@ pub struct EventHandler {
|
||||
pub command_input: String,
|
||||
pub command_message: String,
|
||||
pub is_edit_mode: bool,
|
||||
pub highlight_state: HighlightState,
|
||||
pub edit_mode_cooldown: bool,
|
||||
pub ideal_cursor_column: usize,
|
||||
pub key_sequence_tracker: KeySequenceTracker,
|
||||
@@ -52,6 +68,7 @@ impl EventHandler {
|
||||
command_input: String::new(),
|
||||
command_message: String::new(),
|
||||
is_edit_mode: false,
|
||||
highlight_state: HighlightState::Off,
|
||||
edit_mode_cooldown: false,
|
||||
ideal_cursor_column: 0,
|
||||
key_sequence_tracker: KeySequenceTracker::new(800),
|
||||
@@ -70,7 +87,9 @@ impl EventHandler {
|
||||
auth_state: &mut AuthState,
|
||||
login_state: &mut LoginState,
|
||||
register_state: &mut RegisterState,
|
||||
intro_state: &mut IntroState,
|
||||
admin_state: &mut AdminState,
|
||||
buffer_state: &mut BufferState,
|
||||
app_state: &mut AppState,
|
||||
total_count: u64,
|
||||
current_position: &mut u64,
|
||||
@@ -78,16 +97,28 @@ impl EventHandler {
|
||||
let current_mode = ModeManager::derive_mode(app_state, self);
|
||||
app_state.update_mode(current_mode);
|
||||
|
||||
// --- DIALOG MODALITY ---
|
||||
let current_view = {
|
||||
let ui = &app_state.ui;
|
||||
if ui.show_intro { AppView::Intro }
|
||||
else if ui.show_login { AppView::Login }
|
||||
else if ui.show_register { AppView::Register }
|
||||
else if ui.show_admin { AppView::Admin }
|
||||
else if ui.show_form {
|
||||
let form_name = app_state.selected_profile.clone().unwrap_or_else(|| "Data Form".to_string());
|
||||
AppView::Form(form_name)
|
||||
}
|
||||
else { AppView::Scratch }
|
||||
};
|
||||
buffer_state.update_history(current_view);
|
||||
|
||||
if app_state.ui.dialog.dialog_show {
|
||||
if let Some(dialog_result) = dialog::handle_dialog_event(
|
||||
&event, config, app_state, auth_state, login_state, register_state
|
||||
&event, config, app_state, auth_state, login_state, register_state, buffer_state
|
||||
).await {
|
||||
return dialog_result;
|
||||
}
|
||||
return Ok(EventOutcome::Ok(String::new()));
|
||||
}
|
||||
// --- END DIALOG MODALITY CHECK ---
|
||||
|
||||
if let Event::Key(key) = event {
|
||||
let key_code = key.code;
|
||||
@@ -99,6 +130,32 @@ impl EventHandler {
|
||||
);
|
||||
return Ok(EventOutcome::Ok(message));
|
||||
}
|
||||
if UiStateHandler::toggle_buffer_list(&mut app_state.ui, config, key_code, modifiers) {
|
||||
let message = format!("Buffer {}",
|
||||
if app_state.ui.show_buffer_list { "shown" } else { "hidden" }
|
||||
);
|
||||
return Ok(EventOutcome::Ok(message));
|
||||
}
|
||||
|
||||
if !matches!(current_mode, AppMode::Edit | AppMode::Command) {
|
||||
if let Some(action) = config.get_action_for_key_in_mode(
|
||||
&config.keybindings.global, key_code, modifiers
|
||||
) {
|
||||
match action {
|
||||
"next_buffer" => {
|
||||
if buffer::switch_buffer(buffer_state, true) {
|
||||
return Ok(EventOutcome::Ok("Switched to next buffer".to_string()));
|
||||
}
|
||||
}
|
||||
"previous_buffer" => {
|
||||
if buffer::switch_buffer(buffer_state, false) {
|
||||
return Ok(EventOutcome::Ok("Switched to previous buffer".to_string()));
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
match current_mode {
|
||||
AppMode::General => {
|
||||
@@ -109,6 +166,7 @@ impl EventHandler {
|
||||
app_state,
|
||||
login_state,
|
||||
register_state,
|
||||
intro_state,
|
||||
admin_state,
|
||||
&mut self.command_mode,
|
||||
&mut self.command_input,
|
||||
@@ -116,10 +174,10 @@ impl EventHandler {
|
||||
).await;
|
||||
match nav_outcome {
|
||||
Ok(EventOutcome::ButtonSelected { context, index }) => {
|
||||
let mut message = String::from("Selected"); // Default message
|
||||
let mut message = String::from("Selected");
|
||||
match context {
|
||||
UiContext::Intro => {
|
||||
intro::handle_intro_selection(app_state, index);
|
||||
intro::handle_intro_selection(app_state, buffer_state, index);
|
||||
if app_state.ui.show_admin {
|
||||
let profile_names = app_state.profile_tree.profiles.iter()
|
||||
.map(|p| p.name.clone())
|
||||
@@ -131,44 +189,70 @@ impl EventHandler {
|
||||
UiContext::Login => {
|
||||
message = match index {
|
||||
0 => login::save(auth_state, login_state, &mut self.auth_client, app_state).await?,
|
||||
1 => login::back_to_main(login_state, app_state).await,
|
||||
1 => login::back_to_main(login_state, app_state, buffer_state).await,
|
||||
_ => "Invalid Login Option".to_string(),
|
||||
};
|
||||
}
|
||||
UiContext::Register => {
|
||||
message = match index {
|
||||
0 => register::save(register_state, &mut self.auth_client, app_state).await?,
|
||||
1 => register::back_to_main(register_state, app_state).await,
|
||||
1 => register::back_to_login(register_state, app_state, buffer_state).await,
|
||||
_ => "Invalid Login Option".to_string(),
|
||||
};
|
||||
}
|
||||
UiContext::Admin => {
|
||||
admin::handle_admin_selection(app_state, admin_state);
|
||||
|
||||
message = format!("Admin Option {} selected", index);
|
||||
}
|
||||
UiContext::Dialog => {
|
||||
message = "Internal error: Unexpected dialog state".to_string();
|
||||
}
|
||||
}
|
||||
return Ok(EventOutcome::Ok(message)); // Return Ok with message
|
||||
return Ok(EventOutcome::Ok(message));
|
||||
}
|
||||
other => return other, // Pass through Ok, Err, DataSaved directly
|
||||
other => return other,
|
||||
}
|
||||
},
|
||||
|
||||
AppMode::ReadOnly => {
|
||||
if config.is_enter_edit_mode_before(key_code, modifiers) &&
|
||||
ModeManager::can_enter_edit_mode(current_mode) {
|
||||
// Check for Linewise highlight first
|
||||
if config.get_read_only_action_for_key(key_code, modifiers) == Some("enter_highlight_mode_linewise")
|
||||
&& ModeManager::can_enter_highlight_mode(current_mode)
|
||||
{
|
||||
let current_field_index = if app_state.ui.show_login { login_state.current_field() }
|
||||
else if app_state.ui.show_register { register_state.current_field() }
|
||||
else { form_state.current_field() };
|
||||
self.highlight_state = HighlightState::Linewise { anchor_line: current_field_index };
|
||||
self.command_message = "-- LINE HIGHLIGHT --".to_string();
|
||||
return Ok(EventOutcome::Ok(self.command_message.clone()));
|
||||
}
|
||||
// Check for Character-wise highlight
|
||||
else if config.get_read_only_action_for_key(key_code, modifiers) == Some("enter_highlight_mode")
|
||||
&& ModeManager::can_enter_highlight_mode(current_mode)
|
||||
{
|
||||
let current_field_index = if app_state.ui.show_login { login_state.current_field() }
|
||||
else if app_state.ui.show_register { register_state.current_field() }
|
||||
else { form_state.current_field() };
|
||||
let current_cursor_pos = if app_state.ui.show_login { login_state.current_cursor_pos() }
|
||||
else if app_state.ui.show_register { register_state.current_cursor_pos() }
|
||||
else { form_state.current_cursor_pos() };
|
||||
let anchor = (current_field_index, current_cursor_pos);
|
||||
self.highlight_state = HighlightState::Characterwise { anchor };
|
||||
self.command_message = "-- HIGHLIGHT --".to_string();
|
||||
return Ok(EventOutcome::Ok(self.command_message.clone()));
|
||||
}
|
||||
// Check for entering edit mode (before cursor)
|
||||
else if config.get_read_only_action_for_key(key_code, modifiers) == Some("enter_edit_mode_before")
|
||||
&& ModeManager::can_enter_edit_mode(current_mode) {
|
||||
self.is_edit_mode = true;
|
||||
self.edit_mode_cooldown = true;
|
||||
self.command_message = "Edit mode".to_string();
|
||||
terminal.set_cursor_style(SetCursorStyle::BlinkingBar)?;
|
||||
return Ok(EventOutcome::Ok(self.command_message.clone()));
|
||||
}
|
||||
|
||||
if config.is_enter_edit_mode_after(key_code, modifiers) &&
|
||||
ModeManager::can_enter_edit_mode(current_mode) {
|
||||
// Check for entering edit mode (after cursor)
|
||||
else if config.get_read_only_action_for_key(key_code, modifiers) == Some("enter_edit_mode_after")
|
||||
&& ModeManager::can_enter_edit_mode(current_mode) {
|
||||
let current_input = if app_state.ui.show_login || app_state.ui.show_register{
|
||||
login_state.get_current_input()
|
||||
} else {
|
||||
@@ -195,21 +279,17 @@ impl EventHandler {
|
||||
terminal.set_cursor_style(SetCursorStyle::BlinkingBar)?;
|
||||
return Ok(EventOutcome::Ok(self.command_message.clone()));
|
||||
}
|
||||
|
||||
if let Some(action) = config.get_read_only_action_for_key(key_code, modifiers) {
|
||||
if action == "enter_command_mode" && ModeManager::can_enter_command_mode(current_mode) {
|
||||
// Check for entering command mode
|
||||
else if config.get_read_only_action_for_key(key_code, modifiers) == Some("enter_command_mode")
|
||||
&& ModeManager::can_enter_command_mode(current_mode) {
|
||||
self.command_mode = true;
|
||||
self.command_input.clear();
|
||||
self.command_message.clear();
|
||||
return Ok(EventOutcome::Ok(String::new()));
|
||||
}
|
||||
}
|
||||
|
||||
if let Some(action) = config.get_action_for_key_in_mode(
|
||||
&config.keybindings.common,
|
||||
key_code,
|
||||
modifiers
|
||||
) {
|
||||
// Check for common actions (save, quit, etc.) only if no mode change happened
|
||||
if let Some(action) = config.get_common_action(key_code, modifiers) {
|
||||
match action {
|
||||
"save" | "force_quit" | "save_and_quit" | "revert" => {
|
||||
return common_mode::handle_core_action(
|
||||
@@ -230,6 +310,7 @@ impl EventHandler {
|
||||
}
|
||||
}
|
||||
|
||||
// If no mode change or specific common action handled, delegate to read_only handler
|
||||
let (_should_exit, message) = read_only::handle_read_only_event(
|
||||
app_state,
|
||||
key,
|
||||
@@ -245,59 +326,50 @@ impl EventHandler {
|
||||
&mut self.edit_mode_cooldown,
|
||||
&mut self.ideal_cursor_column,
|
||||
).await?;
|
||||
// Note: handle_read_only_event should ignore mode entry keys internally now
|
||||
return Ok(EventOutcome::Ok(message));
|
||||
},
|
||||
}, // End AppMode::ReadOnly
|
||||
|
||||
AppMode::Highlight => {
|
||||
// --- Handle Highlight Mode Specific Keys ---
|
||||
// 1. Check for Exit first
|
||||
if config.get_highlight_action_for_key(key_code, modifiers) == Some("exit_highlight_mode") {
|
||||
self.highlight_state = HighlightState::Off;
|
||||
self.command_message = "Exited highlight mode".to_string();
|
||||
terminal.set_cursor_style(SetCursorStyle::SteadyBlock)?;
|
||||
return Ok(EventOutcome::Ok(self.command_message.clone()));
|
||||
}
|
||||
// 2. Check for Switch to Linewise
|
||||
else if config.get_highlight_action_for_key(key_code, modifiers) == Some("enter_highlight_mode_linewise") {
|
||||
// Only switch if currently characterwise
|
||||
if let HighlightState::Characterwise { anchor } = self.highlight_state {
|
||||
self.highlight_state = HighlightState::Linewise { anchor_line: anchor.0 };
|
||||
self.command_message = "-- LINE HIGHLIGHT --".to_string();
|
||||
return Ok(EventOutcome::Ok(self.command_message.clone()));
|
||||
}
|
||||
return Ok(EventOutcome::Ok("".to_string()));
|
||||
}
|
||||
|
||||
let (_should_exit, message) = read_only::handle_read_only_event(
|
||||
app_state, key, config, form_state, login_state,
|
||||
register_state, &mut self.key_sequence_tracker,
|
||||
current_position, total_count, grpc_client,
|
||||
&mut self.command_message, &mut self.edit_mode_cooldown,
|
||||
&mut self.ideal_cursor_column,
|
||||
)
|
||||
.await?;
|
||||
return Ok(EventOutcome::Ok(message));
|
||||
}
|
||||
|
||||
AppMode::Edit => {
|
||||
if config.is_exit_edit_mode(key_code, modifiers) {
|
||||
self.is_edit_mode = false;
|
||||
self.edit_mode_cooldown = true;
|
||||
|
||||
let has_changes = if app_state.ui.show_login || app_state.ui.show_register{
|
||||
login_state.has_unsaved_changes()
|
||||
} else {
|
||||
form_state.has_unsaved_changes()
|
||||
};
|
||||
|
||||
self.command_message = if has_changes {
|
||||
"Exited edit mode (unsaved changes remain)".to_string()
|
||||
} else {
|
||||
"Read-only mode".to_string()
|
||||
};
|
||||
|
||||
terminal.set_cursor_style(SetCursorStyle::SteadyBlock)?;
|
||||
|
||||
let current_input = if app_state.ui.show_login || app_state.ui.show_register{
|
||||
login_state.get_current_input()
|
||||
} else {
|
||||
form_state.get_current_input()
|
||||
};
|
||||
let current_cursor_pos = if app_state.ui.show_login || app_state.ui.show_register{
|
||||
login_state.current_cursor_pos()
|
||||
} else {
|
||||
form_state.current_cursor_pos()
|
||||
};
|
||||
|
||||
if !current_input.is_empty() && current_cursor_pos >= current_input.len() {
|
||||
let new_pos = current_input.len() - 1;
|
||||
if app_state.ui.show_login || app_state.ui.show_register{
|
||||
login_state.set_current_cursor_pos(new_pos);
|
||||
self.ideal_cursor_column = login_state.current_cursor_pos();
|
||||
} else {
|
||||
form_state.set_current_cursor_pos(new_pos);
|
||||
self.ideal_cursor_column = form_state.current_cursor_pos();
|
||||
}
|
||||
}
|
||||
return Ok(EventOutcome::Ok(self.command_message.clone()));
|
||||
}
|
||||
|
||||
if let Some(action) = config.get_action_for_key_in_mode(
|
||||
&config.keybindings.common,
|
||||
key_code,
|
||||
modifiers
|
||||
) {
|
||||
// First, check for common actions (save, revert, etc.) that apply in Edit mode
|
||||
// These might take precedence or have different behavior than the edit handler
|
||||
if let Some(action) = config.get_common_action(key_code, modifiers) {
|
||||
// Handle common actions like save, revert, force_quit, save_and_quit
|
||||
// Ensure these actions return EventOutcome directly if they might exit the app
|
||||
match action {
|
||||
"save" | "force_quit" | "save_and_quit" | "revert" => {
|
||||
// This call likely returns EventOutcome, handle it directly
|
||||
return common_mode::handle_core_action(
|
||||
action,
|
||||
form_state,
|
||||
@@ -312,27 +384,72 @@ impl EventHandler {
|
||||
total_count,
|
||||
).await;
|
||||
},
|
||||
// Handle other common actions if necessary
|
||||
_ => {}
|
||||
}
|
||||
// If a common action was handled but didn't return/exit,
|
||||
// we might want to stop further processing for this key event.
|
||||
// Depending on the action, you might return Ok(EventOutcome::Ok(...)) here.
|
||||
// For now, assume common actions either exit or don't prevent further processing.
|
||||
}
|
||||
|
||||
let message = edit::handle_edit_event(
|
||||
// If no common action took precedence, delegate to the edit-specific handler
|
||||
let edit_result = edit::handle_edit_event(
|
||||
key,
|
||||
config,
|
||||
form_state,
|
||||
login_state,
|
||||
register_state,
|
||||
&mut self.ideal_cursor_column,
|
||||
&mut self.command_message,
|
||||
current_position,
|
||||
total_count,
|
||||
grpc_client,
|
||||
app_state,
|
||||
).await?;
|
||||
).await;
|
||||
|
||||
self.key_sequence_tracker.reset();
|
||||
return Ok(EventOutcome::Ok(message));
|
||||
},
|
||||
match edit_result {
|
||||
Ok(edit::EditEventOutcome::ExitEditMode) => {
|
||||
// The edit handler signaled to exit the mode
|
||||
self.is_edit_mode = false;
|
||||
self.edit_mode_cooldown = true;
|
||||
let has_changes = if app_state.ui.show_login { login_state.has_unsaved_changes() }
|
||||
else if app_state.ui.show_register { register_state.has_unsaved_changes() }
|
||||
else { form_state.has_unsaved_changes() };
|
||||
self.command_message = if has_changes {
|
||||
"Exited edit mode (unsaved changes remain)".to_string()
|
||||
} else {
|
||||
"Read-only mode".to_string()
|
||||
};
|
||||
terminal.set_cursor_style(SetCursorStyle::SteadyBlock)?;
|
||||
// Adjust cursor position if needed
|
||||
let current_input = if app_state.ui.show_login { login_state.get_current_input() }
|
||||
else if app_state.ui.show_register { register_state.get_current_input() }
|
||||
else { form_state.get_current_input() };
|
||||
let current_cursor_pos = if app_state.ui.show_login { login_state.current_cursor_pos() }
|
||||
else if app_state.ui.show_register { register_state.current_cursor_pos() }
|
||||
else { form_state.current_cursor_pos() };
|
||||
if !current_input.is_empty() && current_cursor_pos >= current_input.len() {
|
||||
let new_pos = current_input.len() - 1;
|
||||
let target_state: &mut dyn CanvasState = if app_state.ui.show_login { login_state } else if app_state.ui.show_register { register_state } else { form_state };
|
||||
target_state.set_current_cursor_pos(new_pos);
|
||||
self.ideal_cursor_column = new_pos;
|
||||
}
|
||||
return Ok(EventOutcome::Ok(self.command_message.clone()));
|
||||
}
|
||||
Ok(edit::EditEventOutcome::Message(msg)) => {
|
||||
// Stay in edit mode, update message if not empty
|
||||
if !msg.is_empty() {
|
||||
self.command_message = msg;
|
||||
}
|
||||
self.key_sequence_tracker.reset(); // Reset sequence tracker on successful edit action
|
||||
return Ok(EventOutcome::Ok(self.command_message.clone()));
|
||||
}
|
||||
Err(e) => {
|
||||
// Handle error from the edit handler
|
||||
return Err(e);
|
||||
}
|
||||
}
|
||||
}, // End AppMode::Edit
|
||||
|
||||
AppMode::Command => {
|
||||
let outcome = command_mode::handle_command_event(
|
||||
|
||||
@@ -1,12 +1,14 @@
|
||||
// src/modes/handlers/mode_manager.rs
|
||||
use crate::state::app::state::AppState;
|
||||
use crate::modes::handlers::event::EventHandler;
|
||||
use crate::state::app::highlight::HighlightState;
|
||||
|
||||
#[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, // Cnavas highlight/visual mode
|
||||
Command, // Command mode overlay
|
||||
}
|
||||
|
||||
@@ -19,6 +21,10 @@ impl ModeManager {
|
||||
return AppMode::Command;
|
||||
}
|
||||
|
||||
if !matches!(event_handler.highlight_state, HighlightState::Off) {
|
||||
return AppMode::Highlight;
|
||||
}
|
||||
|
||||
if app_state.ui.focus_outside_canvas {
|
||||
return AppMode::General;
|
||||
}
|
||||
@@ -50,6 +56,10 @@ impl ModeManager {
|
||||
}
|
||||
|
||||
pub fn can_enter_read_only_mode(current_mode: AppMode) -> bool {
|
||||
matches!(current_mode, AppMode::Edit | AppMode::Command)
|
||||
matches!(current_mode, AppMode::Edit | AppMode::Command | AppMode::Highlight)
|
||||
}
|
||||
|
||||
pub fn can_enter_highlight_mode(current_mode: AppMode) -> bool {
|
||||
matches!(current_mode, AppMode::ReadOnly)
|
||||
}
|
||||
}
|
||||
|
||||
2
client/src/modes/highlight.rs
Normal file
2
client/src/modes/highlight.rs
Normal file
@@ -0,0 +1,2 @@
|
||||
// src/client/modes/highlight.rs
|
||||
pub mod highlight;
|
||||
62
client/src/modes/highlight/highlight.rs
Normal file
62
client/src/modes/highlight/highlight.rs
Normal file
@@ -0,0 +1,62 @@
|
||||
// src/modes/highlight/highlight.rs
|
||||
// (This file is intentionally simple for now, reusing ReadOnly logic)
|
||||
|
||||
use crate::config::binds::config::Config;
|
||||
use crate::config::binds::key_sequences::KeySequenceTracker;
|
||||
use crate::services::grpc_client::GrpcClient;
|
||||
use crate::state::app::state::AppState;
|
||||
use crate::state::pages::auth::{LoginState, RegisterState};
|
||||
use crate::state::pages::form::FormState;
|
||||
use crate::modes::handlers::event::EventOutcome;
|
||||
use crate::modes::read_only; // Import the ReadOnly handler
|
||||
use crossterm::event::KeyEvent;
|
||||
|
||||
/// Handles events when in Highlight mode.
|
||||
/// Currently, it mostly delegates to the read_only handler for movement.
|
||||
/// Exiting highlight mode is handled directly in the main event handler.
|
||||
pub async fn handle_highlight_event(
|
||||
app_state: &mut AppState,
|
||||
key: KeyEvent,
|
||||
config: &Config,
|
||||
form_state: &mut FormState,
|
||||
login_state: &mut LoginState,
|
||||
register_state: &mut RegisterState,
|
||||
key_sequence_tracker: &mut KeySequenceTracker,
|
||||
current_position: &mut u64,
|
||||
total_count: u64,
|
||||
grpc_client: &mut GrpcClient,
|
||||
command_message: &mut String,
|
||||
edit_mode_cooldown: &mut bool,
|
||||
ideal_cursor_column: &mut usize,
|
||||
) -> Result<EventOutcome, Box<dyn std::error::Error>> {
|
||||
// Delegate movement and other actions to the read_only handler
|
||||
// The rendering logic will use the highlight_anchor to draw the selection
|
||||
let (should_exit, message) = read_only::handle_read_only_event(
|
||||
app_state,
|
||||
key,
|
||||
config,
|
||||
form_state,
|
||||
login_state,
|
||||
register_state,
|
||||
key_sequence_tracker,
|
||||
current_position,
|
||||
total_count,
|
||||
grpc_client,
|
||||
command_message, // Pass the message buffer
|
||||
edit_mode_cooldown,
|
||||
ideal_cursor_column,
|
||||
)
|
||||
.await?;
|
||||
|
||||
// ReadOnly handler doesn't return EventOutcome directly, adapt if needed
|
||||
// For now, assume Ok outcome unless ReadOnly signals an exit (which we ignore here)
|
||||
if should_exit {
|
||||
// This exit is likely for the whole app, let the main loop handle it
|
||||
// We just return the message from read_only
|
||||
Ok(EventOutcome::Ok(message))
|
||||
} else {
|
||||
Ok(EventOutcome::Ok(message))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -3,8 +3,10 @@ pub mod handlers;
|
||||
pub mod canvas;
|
||||
pub mod general;
|
||||
pub mod common;
|
||||
pub mod highlight;
|
||||
|
||||
pub use handlers::*;
|
||||
pub use canvas::*;
|
||||
pub use general::*;
|
||||
pub use common::*;
|
||||
pub use highlight::*;
|
||||
|
||||
@@ -1,3 +1,5 @@
|
||||
// src/state/app.rs
|
||||
|
||||
pub mod state;
|
||||
pub mod buffer;
|
||||
pub mod highlight;
|
||||
|
||||
92
client/src/state/app/buffer.rs
Normal file
92
client/src/state/app/buffer.rs
Normal file
@@ -0,0 +1,92 @@
|
||||
// src/state/app/buffer.rs
|
||||
|
||||
/// Represents the distinct views or "buffers" the user can navigate.
|
||||
#[derive(Debug, Clone, PartialEq, Eq)]
|
||||
pub enum AppView {
|
||||
Intro,
|
||||
Login,
|
||||
Register,
|
||||
Admin,
|
||||
Form(String),
|
||||
Scratch,
|
||||
}
|
||||
|
||||
impl AppView {
|
||||
/// Returns the display name for the view.
|
||||
pub fn display_name(&self) -> &str {
|
||||
match self {
|
||||
AppView::Intro => "Intro",
|
||||
AppView::Login => "Login",
|
||||
AppView::Register => "Register",
|
||||
AppView::Admin => "Admin Panel",
|
||||
AppView::Form(name) => name.as_str(),
|
||||
AppView::Scratch => "*scratch*",
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Holds the state related to buffer management (navigation history).
|
||||
#[derive(Debug, Clone)]
|
||||
pub struct BufferState {
|
||||
pub history: Vec<AppView>,
|
||||
pub active_index: usize,
|
||||
}
|
||||
|
||||
impl Default for BufferState {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
history: vec![AppView::Intro], // Start with Intro view
|
||||
active_index: 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl BufferState {
|
||||
/// Updates the buffer history and active index.
|
||||
/// If the view already exists, it sets it as active.
|
||||
/// Otherwise, it adds the new view and makes it active.
|
||||
pub fn update_history(&mut self, view: AppView) {
|
||||
let existing_pos = self.history.iter().position(|v| v == &view);
|
||||
|
||||
match existing_pos {
|
||||
Some(pos) => {
|
||||
self.active_index = pos;
|
||||
}
|
||||
None => {
|
||||
self.history.push(view.clone());
|
||||
self.active_index = self.history.len() - 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Gets the currently active view.
|
||||
pub fn get_active_view(&self) -> Option<&AppView> {
|
||||
self.history.get(self.active_index)
|
||||
}
|
||||
|
||||
/// Removes the currently active buffer from the history, unless it's the Intro buffer.
|
||||
/// Sets the new active buffer to the one preceding the closed one.
|
||||
/// # Returns
|
||||
/// * `true` if a non-Intro buffer was closed.
|
||||
/// * `false` if the active buffer was Intro or only Intro remained.
|
||||
pub fn close_active_buffer(&mut self) -> bool {
|
||||
let current_index = self.active_index;
|
||||
|
||||
// Rule 1: Cannot close Intro buffer.
|
||||
if matches!(self.history.get(current_index), Some(AppView::Intro)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Rule 2: Cannot close if only Intro would remain (or already remains).
|
||||
// This check implicitly covers the case where len <= 1.
|
||||
if self.history.len() <= 1 {
|
||||
return false;
|
||||
}
|
||||
|
||||
self.history.remove(current_index);
|
||||
self.active_index = current_index.saturating_sub(1).min(self.history.len() - 1);
|
||||
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
20
client/src/state/app/highlight.rs
Normal file
20
client/src/state/app/highlight.rs
Normal file
@@ -0,0 +1,20 @@
|
||||
// src/state/app/highlight.rs
|
||||
|
||||
/// Represents the different states of text highlighting.
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum HighlightState {
|
||||
/// Highlighting is inactive.
|
||||
Off,
|
||||
/// Highlighting character by character. Stores the anchor point (line index, char index).
|
||||
Characterwise { anchor: (usize, usize) },
|
||||
/// Highlighting line by line. Stores the anchor line index.
|
||||
Linewise { anchor_line: usize },
|
||||
}
|
||||
|
||||
impl Default for HighlightState {
|
||||
/// The default state is no highlighting.
|
||||
fn default() -> Self {
|
||||
HighlightState::Off
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,7 +2,6 @@
|
||||
|
||||
use std::env;
|
||||
use common::proto::multieko2::table_definition::ProfileTreeResponse;
|
||||
use crate::components::IntroState;
|
||||
use crate::modes::handlers::mode_manager::AppMode;
|
||||
use crate::ui::handlers::context::DialogPurpose;
|
||||
|
||||
@@ -17,12 +16,12 @@ pub struct DialogState {
|
||||
|
||||
pub struct UiState {
|
||||
pub show_sidebar: bool,
|
||||
pub show_buffer_list: bool,
|
||||
pub show_intro: bool,
|
||||
pub show_admin: bool,
|
||||
pub show_form: bool,
|
||||
pub show_login: bool,
|
||||
pub show_register: bool,
|
||||
pub intro_state: IntroState,
|
||||
pub focus_outside_canvas: bool,
|
||||
pub dialog: DialogState,
|
||||
}
|
||||
@@ -132,13 +131,13 @@ impl AppState {
|
||||
impl Default for UiState {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
show_sidebar: true,
|
||||
show_sidebar: false,
|
||||
show_intro: true,
|
||||
show_admin: false,
|
||||
show_form: false,
|
||||
show_login: false,
|
||||
show_register: false,
|
||||
intro_state: IntroState::new(),
|
||||
show_buffer_list: true,
|
||||
focus_outside_canvas: false,
|
||||
dialog: DialogState::default(),
|
||||
}
|
||||
|
||||
@@ -3,4 +3,5 @@
|
||||
pub mod form;
|
||||
pub mod auth;
|
||||
pub mod admin;
|
||||
pub mod intro;
|
||||
pub mod canvas_state;
|
||||
|
||||
@@ -2,12 +2,13 @@
|
||||
use crate::config::colors::themes::Theme;
|
||||
use ratatui::layout::Rect;
|
||||
use ratatui::Frame;
|
||||
use crate::state::app::highlight::HighlightState;
|
||||
use crate::state::pages::canvas_state::CanvasState;
|
||||
|
||||
pub struct FormState {
|
||||
pub id: i64,
|
||||
pub fields: Vec<String>, // Use Vec<String> for dynamic field names
|
||||
pub values: Vec<String>, // Store field values dynamically
|
||||
pub fields: Vec<String>,
|
||||
pub values: Vec<String>,
|
||||
pub current_field: usize,
|
||||
pub has_unsaved_changes: bool,
|
||||
pub current_cursor_pos: usize,
|
||||
@@ -33,6 +34,7 @@ impl FormState {
|
||||
area: Rect,
|
||||
theme: &Theme,
|
||||
is_edit_mode: bool,
|
||||
highlight_state: &HighlightState,
|
||||
total_count: u64,
|
||||
current_position: u64,
|
||||
) {
|
||||
@@ -48,6 +50,7 @@ impl FormState {
|
||||
&values,
|
||||
theme,
|
||||
is_edit_mode,
|
||||
highlight_state,
|
||||
total_count,
|
||||
current_position,
|
||||
);
|
||||
|
||||
25
client/src/state/pages/intro.rs
Normal file
25
client/src/state/pages/intro.rs
Normal file
@@ -0,0 +1,25 @@
|
||||
// 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
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -4,6 +4,7 @@ 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::state::app::buffer::{AppView, BufferState};
|
||||
use crate::state::pages::canvas_state::CanvasState;
|
||||
use crate::ui::handlers::context::DialogPurpose;
|
||||
|
||||
@@ -87,6 +88,7 @@ pub async fn revert(
|
||||
pub async fn back_to_main(
|
||||
login_state: &mut LoginState,
|
||||
app_state: &mut AppState,
|
||||
buffer_state: &mut BufferState,
|
||||
) -> String {
|
||||
// Clear the input fields
|
||||
login_state.username.clear();
|
||||
@@ -97,9 +99,9 @@ pub async fn back_to_main(
|
||||
// Ensure dialog is hidden if revert is called
|
||||
app_state.hide_dialog(); // Uncomment if needed
|
||||
|
||||
// Navigation logic (currently disabled in original code)
|
||||
app_state.ui.show_login = false;
|
||||
app_state.ui.show_intro = true;
|
||||
// Navigation logic
|
||||
buffer_state.close_active_buffer();
|
||||
buffer_state.update_history(AppView::Intro);
|
||||
|
||||
// Reset focus state
|
||||
app_state.ui.focus_outside_canvas = false;
|
||||
|
||||
@@ -1,14 +1,13 @@
|
||||
// src/tui/functions/common/register.rs
|
||||
|
||||
use crate::{
|
||||
services::auth::AuthClient,
|
||||
state::{
|
||||
pages::auth::RegisterState,
|
||||
app::state::AppState,
|
||||
pages::canvas_state::CanvasState,
|
||||
},
|
||||
ui::handlers::context::DialogPurpose,
|
||||
use crate::services::auth::AuthClient;
|
||||
use crate::state::{
|
||||
pages::auth::RegisterState,
|
||||
app::state::AppState,
|
||||
pages::canvas_state::CanvasState,
|
||||
};
|
||||
use crate::ui::handlers::context::DialogPurpose;
|
||||
use crate::state::app::buffer::{AppView, BufferState};
|
||||
|
||||
/// Attempts to register the user using the provided details via gRPC.
|
||||
/// Updates RegisterState and AppState on success or failure.
|
||||
@@ -131,9 +130,10 @@ pub async fn revert(
|
||||
}
|
||||
|
||||
/// Clears the form and returns to the intro screen.
|
||||
pub async fn back_to_main(
|
||||
pub async fn back_to_login(
|
||||
register_state: &mut RegisterState,
|
||||
app_state: &mut AppState,
|
||||
buffer_state: &mut BufferState,
|
||||
) -> String {
|
||||
// Clear fields first
|
||||
let _ = revert(register_state, app_state).await;
|
||||
@@ -142,8 +142,8 @@ pub async fn back_to_main(
|
||||
app_state.hide_dialog();
|
||||
|
||||
// Navigation logic
|
||||
app_state.ui.show_register = false;
|
||||
app_state.ui.show_intro = true;
|
||||
buffer_state.close_active_buffer();
|
||||
buffer_state.update_history(AppView::Login);
|
||||
|
||||
// Reset focus state
|
||||
app_state.ui.focus_outside_canvas = false;
|
||||
|
||||
@@ -1,30 +1,33 @@
|
||||
// src/tui/functions/intro.rs
|
||||
use crate::state::app::state::AppState;
|
||||
use crate::state::app::buffer::{AppView, BufferState};
|
||||
|
||||
pub fn handle_intro_selection(app_state: &mut AppState, index: usize) { // Add index parameter
|
||||
match index { // Use index directly
|
||||
0 => { // Continue
|
||||
app_state.ui.show_form = true;
|
||||
app_state.ui.show_admin = false;
|
||||
app_state.ui.show_login = false;
|
||||
/// 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 => {
|
||||
let form_name = app_state.selected_profile.clone().unwrap_or_else(|| "Data Form".to_string());
|
||||
AppView::Form(form_name)
|
||||
}
|
||||
1 => { // Admin
|
||||
app_state.ui.show_form = false;
|
||||
app_state.ui.show_admin = true;
|
||||
app_state.ui.show_login = false;
|
||||
}
|
||||
2 => { // Login
|
||||
app_state.ui.show_form = false;
|
||||
app_state.ui.show_admin = false;
|
||||
app_state.ui.show_login = true;
|
||||
}
|
||||
3 => { // Register
|
||||
app_state.ui.show_intro = false;
|
||||
app_state.ui.show_register = true;
|
||||
app_state.ui.focus_outside_canvas = false;
|
||||
app_state.focused_button_index = 0;
|
||||
}
|
||||
_ => {}
|
||||
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;
|
||||
}
|
||||
app_state.ui.show_intro = false;
|
||||
}
|
||||
|
||||
|
||||
@@ -21,4 +21,18 @@ impl UiStateHandler {
|
||||
false
|
||||
}
|
||||
|
||||
pub fn toggle_buffer_list(
|
||||
ui_state: &mut UiState,
|
||||
config: &Config,
|
||||
key: KeyCode,
|
||||
modifiers: KeyModifiers,
|
||||
) -> bool {
|
||||
if let Some(action) = config.get_common_action(key, modifiers) {
|
||||
if action == "toggle_buffer_list" {
|
||||
ui_state.show_buffer_list = !ui_state.show_buffer_list;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
false
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2,8 +2,10 @@
|
||||
|
||||
use crate::components::{
|
||||
render_background,
|
||||
render_buffer_list,
|
||||
render_command_line,
|
||||
render_status_line,
|
||||
intro::intro::render_intro,
|
||||
handlers::sidebar::{self, calculate_sidebar_layout},
|
||||
form::form::render_form,
|
||||
auth::{login::render_login, register::render_register},
|
||||
@@ -15,8 +17,11 @@ use crate::state::pages::form::FormState;
|
||||
use crate::state::pages::auth::AuthState;
|
||||
use crate::state::pages::auth::LoginState;
|
||||
use crate::state::pages::auth::RegisterState;
|
||||
use crate::state::pages::intro::IntroState;
|
||||
use crate::state::app::buffer::BufferState;
|
||||
use crate::state::app::state::AppState;
|
||||
use crate::state::pages::admin::AdminState;
|
||||
use crate::state::app::highlight::HighlightState;
|
||||
|
||||
pub fn render_ui(
|
||||
f: &mut Frame,
|
||||
@@ -24,31 +29,63 @@ pub fn render_ui(
|
||||
auth_state: &mut AuthState,
|
||||
login_state: &LoginState,
|
||||
register_state: &RegisterState,
|
||||
intro_state: &IntroState,
|
||||
admin_state: &mut AdminState,
|
||||
buffer_state: &BufferState,
|
||||
theme: &Theme,
|
||||
is_edit_mode: bool,
|
||||
highlight_state: &HighlightState,
|
||||
total_count: u64,
|
||||
current_position: u64,
|
||||
current_dir: &str,
|
||||
command_input: &str,
|
||||
command_mode: bool,
|
||||
command_message: &str,
|
||||
current_fps: f64,
|
||||
app_state: &AppState,
|
||||
) {
|
||||
render_background(f, f.area(), theme);
|
||||
|
||||
// Adjust layout based on whether buffer list is shown
|
||||
let constraints = if app_state.ui.show_buffer_list {
|
||||
vec![
|
||||
Constraint::Length(1), // Buffer list
|
||||
Constraint::Min(1), // Main content
|
||||
Constraint::Length(1), // Status line
|
||||
Constraint::Length(1), // Command line
|
||||
]
|
||||
} else {
|
||||
vec![
|
||||
Constraint::Min(1), // Main content
|
||||
Constraint::Length(1), // Status line (no buffer list)
|
||||
Constraint::Length(1), // Command line
|
||||
]
|
||||
};
|
||||
|
||||
let root = Layout::default()
|
||||
.direction(Direction::Vertical)
|
||||
.constraints([
|
||||
Constraint::Min(1),
|
||||
Constraint::Length(1),
|
||||
Constraint::Length(1),
|
||||
])
|
||||
.constraints(constraints)
|
||||
.split(f.area());
|
||||
|
||||
let main_content_area = root[0];
|
||||
let mut buffer_list_area = None;
|
||||
let main_content_area;
|
||||
let status_line_area;
|
||||
let command_line_area;
|
||||
|
||||
// Assign areas based on layout
|
||||
if app_state.ui.show_buffer_list {
|
||||
buffer_list_area = Some(root[0]);
|
||||
main_content_area = root[1];
|
||||
status_line_area = root[2];
|
||||
command_line_area = root[3];
|
||||
} else {
|
||||
main_content_area = root[0];
|
||||
status_line_area = root[1];
|
||||
command_line_area = root[2];
|
||||
}
|
||||
|
||||
if app_state.ui.show_intro {
|
||||
app_state.ui.intro_state.render(f, main_content_area, theme);
|
||||
render_intro(f, intro_state, main_content_area, theme);
|
||||
} else if app_state.ui.show_register {
|
||||
render_register(
|
||||
f,
|
||||
@@ -56,20 +93,23 @@ pub fn render_ui(
|
||||
theme,
|
||||
register_state,
|
||||
app_state,
|
||||
register_state.current_field < 4
|
||||
register_state.current_field < 4,
|
||||
highlight_state,
|
||||
);
|
||||
}else if app_state.ui.show_login {
|
||||
} else if app_state.ui.show_login {
|
||||
render_login(
|
||||
f,
|
||||
main_content_area,
|
||||
theme,
|
||||
f,
|
||||
main_content_area,
|
||||
theme,
|
||||
login_state,
|
||||
app_state,
|
||||
login_state.current_field < 2
|
||||
login_state.current_field < 2,
|
||||
highlight_state,
|
||||
);
|
||||
} else if app_state.ui.show_admin {
|
||||
crate::components::admin::admin_panel::render_admin_panel(
|
||||
crate::components::admin::admin_panel::render_admin_panel(
|
||||
f,
|
||||
auth_state,
|
||||
admin_state,
|
||||
main_content_area,
|
||||
theme,
|
||||
@@ -129,13 +169,18 @@ pub fn render_ui(
|
||||
&values,
|
||||
theme,
|
||||
is_edit_mode,
|
||||
highlight_state,
|
||||
total_count,
|
||||
current_position,
|
||||
);
|
||||
} else{
|
||||
|
||||
}
|
||||
|
||||
render_status_line(f, root[1], current_dir, theme, is_edit_mode);
|
||||
render_command_line(f, root[2], command_input, command_mode, theme, command_message);
|
||||
// Render buffer list if enabled and area is available
|
||||
if let Some(area) = buffer_list_area {
|
||||
if app_state.ui.show_buffer_list {
|
||||
render_buffer_list(f, area, theme, buffer_state);
|
||||
}
|
||||
}
|
||||
render_status_line(f, status_line_area, current_dir, theme, is_edit_mode, current_fps);
|
||||
render_command_line(f, command_line_area, command_input, command_mode, theme, command_message);
|
||||
}
|
||||
|
||||
@@ -2,58 +2,83 @@
|
||||
|
||||
use crate::config::binds::config::Config;
|
||||
use crate::config::colors::themes::Theme;
|
||||
use crate::modes::common::commands::CommandHandler;
|
||||
use crate::modes::handlers::event::{EventHandler, EventOutcome}; // Import EventOutcome
|
||||
use crate::modes::handlers::mode_manager::{AppMode, ModeManager};
|
||||
use crate::services::grpc_client::GrpcClient;
|
||||
use crate::services::ui_service::UiService;
|
||||
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::canvas_state::CanvasState;
|
||||
use crate::state::pages::form::FormState;
|
||||
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::intro::IntroState;
|
||||
use crate::state::app::buffer::BufferState;
|
||||
use crate::state::app::buffer::AppView;
|
||||
use crate::state::app::state::AppState;
|
||||
// Import SaveOutcome
|
||||
use crate::tui::terminal::{EventReader, TerminalCore};
|
||||
use crate::ui::handlers::render::render_ui;
|
||||
use std::time::Instant;
|
||||
use crossterm::cursor::SetCursorStyle;
|
||||
|
||||
pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let config = Config::load()?;
|
||||
let theme = Theme::from_str(&config.colors.theme);
|
||||
let mut terminal = TerminalCore::new()?;
|
||||
let mut grpc_client = GrpcClient::new().await?;
|
||||
let mut command_handler = CommandHandler::new();
|
||||
let theme = Theme::from_str(&config.colors.theme);
|
||||
let mut auth_state = AuthState::default();
|
||||
let mut register_state = RegisterState::default();
|
||||
let mut login_state = LoginState::default();
|
||||
let mut admin_state = AdminState::default();
|
||||
|
||||
// Initialize app_state first
|
||||
let mut event_handler = EventHandler::new().await?;
|
||||
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 intro_state = IntroState::default();
|
||||
let mut admin_state = AdminState::default();
|
||||
let mut buffer_state = BufferState::default();
|
||||
let mut app_state = AppState::new()?;
|
||||
|
||||
// Initialize app state with profile tree and table structure
|
||||
let column_names =
|
||||
UiService::initialize_app_state(&mut grpc_client, &mut app_state)
|
||||
.await?;
|
||||
|
||||
// Initialize FormState with dynamic fields
|
||||
let mut form_state = FormState::new(column_names);
|
||||
|
||||
// Initialize EventHandler (which now contains AuthClient)
|
||||
let mut event_handler = EventHandler::new().await?;
|
||||
let event_reader = EventReader::new();
|
||||
|
||||
// Fetch the total count of Adresar entries
|
||||
UiService::initialize_adresar_count(&mut grpc_client, &mut app_state)
|
||||
.await?;
|
||||
form_state.reset_to_empty();
|
||||
|
||||
// --- FPS Calculation State ---
|
||||
let mut last_frame_time = Instant::now();
|
||||
let mut current_fps = 0.0;
|
||||
|
||||
loop {
|
||||
// Determine edit mode based on EventHandler state
|
||||
let is_edit_mode = event_handler.is_edit_mode;
|
||||
|
||||
// --- Synchronize UI View from Active Buffer ---
|
||||
if let Some(active_view) = buffer_state.get_active_view() {
|
||||
// Reset all flags first
|
||||
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_form = false;
|
||||
match active_view {
|
||||
AppView::Intro => app_state.ui.show_intro = true,
|
||||
AppView::Login => app_state.ui.show_login = true,
|
||||
AppView::Register => app_state.ui.show_register = true,
|
||||
AppView::Admin => app_state.ui.show_admin = true,
|
||||
AppView::Form(_) => app_state.ui.show_form = true,
|
||||
AppView::Scratch => {} // Or show a scratchpad component
|
||||
}
|
||||
}
|
||||
// --- End Synchronization ---
|
||||
|
||||
terminal.draw(|f| {
|
||||
render_ui(
|
||||
f,
|
||||
@@ -61,15 +86,19 @@ pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
|
||||
&mut auth_state,
|
||||
&login_state,
|
||||
®ister_state,
|
||||
&intro_state,
|
||||
&mut admin_state,
|
||||
&buffer_state,
|
||||
&theme,
|
||||
is_edit_mode,
|
||||
&event_handler.highlight_state,
|
||||
app_state.total_count,
|
||||
app_state.current_position,
|
||||
&app_state.current_dir,
|
||||
&event_handler.command_input,
|
||||
event_handler.command_mode,
|
||||
&event_handler.command_message,
|
||||
current_fps,
|
||||
&app_state,
|
||||
);
|
||||
})?;
|
||||
@@ -80,6 +109,10 @@ pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
|
||||
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)?;
|
||||
@@ -121,7 +154,9 @@ pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
|
||||
&mut auth_state,
|
||||
&mut login_state,
|
||||
&mut register_state,
|
||||
&mut intro_state,
|
||||
&mut admin_state,
|
||||
&mut buffer_state,
|
||||
&mut app_state,
|
||||
total_count,
|
||||
&mut current_position,
|
||||
@@ -278,6 +313,14 @@ pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
|
||||
// terminal.cleanup()?; // Optional: Drop handles this
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
// --- FPS Calculation ---
|
||||
let now = Instant::now();
|
||||
let frame_duration = now.duration_since(last_frame_time);
|
||||
last_frame_time = now;
|
||||
if frame_duration.as_secs_f64() > 1e-6 {
|
||||
current_fps = 1.0 / frame_duration.as_secs_f64();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user