Compare commits

..

6 Commits

Author SHA1 Message Date
filipriec
d1d33b5752 project redesign 2025-03-23 13:50:47 +01:00
filipriec
c6c6c5ed81 restored rendering 2025-03-23 12:59:36 +01:00
filipriec
4ddcb34205 nothing 2025-03-23 12:56:27 +01:00
filipriec
83393a20e2 fix of the error 2025-03-23 12:44:33 +01:00
filipriec
13d501e6d7 not working 2025-03-23 12:30:00 +01:00
filipriec
993febd204 admin panel keyindings 2025-03-23 11:28:39 +01:00
13 changed files with 229 additions and 172 deletions

View File

@@ -8,6 +8,10 @@ next_option = ["j", "Right"]
previous_option = ["k", "Left"] previous_option = ["k", "Left"]
select = ["Enter"] select = ["Enter"]
[keybindings.admin]
move_up = ["k"]
move_down = ["j"]
[keybindings.common] [keybindings.common]
save = ["ctrl+s"] save = ["ctrl+s"]
quit = ["ctrl+q"] quit = ["ctrl+q"]

View File

@@ -28,6 +28,8 @@ pub struct ModeKeybindings {
#[serde(default)] #[serde(default)]
pub intro: HashMap<String, Vec<String>>, pub intro: HashMap<String, Vec<String>>,
#[serde(default)] #[serde(default)]
pub admin: HashMap<String, Vec<String>>,
#[serde(default)]
pub read_only: HashMap<String, Vec<String>>, pub read_only: HashMap<String, Vec<String>>,
#[serde(default)] #[serde(default)]
pub edit: HashMap<String, Vec<String>>, pub edit: HashMap<String, Vec<String>>,
@@ -58,7 +60,9 @@ impl Config {
/// Gets an action for a key in Admin mode, checking common and global bindings /// Gets an action for a key in Admin mode, checking common and global bindings
pub fn get_admin_action(&self, key: KeyCode, modifiers: KeyModifiers) -> Option<&str> { pub fn get_admin_action(&self, key: KeyCode, modifiers: KeyModifiers) -> Option<&str> {
self.get_action_for_key_in_mode(&self.keybindings.common, key, modifiers) self.get_action_for_key_in_mode(&self.keybindings.admin, 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.intro, key, modifiers))
.or_else(|| self.get_action_for_key_in_mode(&self.keybindings.global, key, modifiers)) .or_else(|| self.get_action_for_key_in_mode(&self.keybindings.global, key, modifiers))
} }

View File

@@ -0,0 +1,4 @@
// src/client/modes/canvas.rs
pub mod edit;
pub mod common;
pub mod read_only;

View File

@@ -1,4 +1,4 @@
// src/modes/handlers/common.rs // src/modes/canvas/common.rs
use crate::tui::terminal::grpc_client::GrpcClient; use crate::tui::terminal::grpc_client::GrpcClient;
use crate::ui::handlers::form::FormState; use crate::ui::handlers::form::FormState;

View File

@@ -1,4 +1,4 @@
// src/modes/handlers/edit.rs // src/modes/canvas/edit.rs
use crossterm::event::{KeyEvent, KeyCode, KeyModifiers}; use crossterm::event::{KeyEvent, KeyCode, KeyModifiers};
use crate::tui::terminal::{ use crate::tui::terminal::{
@@ -6,7 +6,7 @@ use crate::tui::terminal::{
}; };
use crate::config::binds::config::Config; use crate::config::binds::config::Config;
use crate::ui::handlers::form::FormState; use crate::ui::handlers::form::FormState;
use super::common; use crate::modes::canvas::common;
pub async fn handle_edit_event_internal( pub async fn handle_edit_event_internal(
key: KeyEvent, key: KeyEvent,

View File

@@ -1,6 +1,3 @@
// src/client/modes/handlers.rs // src/client/modes/handlers.rs
pub mod event; pub mod event;
pub mod edit;
pub mod common;
pub mod command_mode; pub mod command_mode;
pub mod read_only;

View File

@@ -4,7 +4,9 @@ use crossterm::event::{KeyEvent, KeyCode, KeyModifiers};
use crate::tui::terminal::grpc_client::GrpcClient; use crate::tui::terminal::grpc_client::GrpcClient;
use crate::config::binds::config::Config; use crate::config::binds::config::Config;
use crate::ui::handlers::form::FormState; use crate::ui::handlers::form::FormState;
use super::common; use crate::modes::{
canvas::{common},
};
pub async fn handle_command_event( pub async fn handle_command_event(
key: KeyEvent, key: KeyEvent,

View File

@@ -9,9 +9,11 @@ use crate::tui::terminal::{
use crate::config::binds::config::Config; use crate::config::binds::config::Config;
use crate::ui::handlers::form::FormState; use crate::ui::handlers::form::FormState;
use crate::ui::handlers::rat_state::UiStateHandler; use crate::ui::handlers::rat_state::UiStateHandler;
use crate::modes::handlers::{edit, command_mode, read_only}; use crate::modes::{
handlers::{command_mode},
canvas::{edit, read_only, common},
};
use crate::config::binds::key_sequences::KeySequenceTracker; use crate::config::binds::key_sequences::KeySequenceTracker;
use super::common;
pub struct EventHandler { pub struct EventHandler {
pub command_mode: bool, pub command_mode: bool,
@@ -66,174 +68,192 @@ impl EventHandler {
return Ok((false, String::new())); return Ok((false, String::new()));
} }
if let Event::Key(key) = event { if let Event::Key(key) = event {
let key_code = key.code; let key_code = key.code;
let modifiers = key.modifiers; let modifiers = key.modifiers;
if UiStateHandler::toggle_sidebar( // Handle admin panel mode
&mut app_state.ui, if app_state.ui.show_admin {
config, if let Some(action) = config.get_admin_action(key_code, modifiers) {
key_code, match action {
modifiers, "move_up" => {
) { // Handle up movement using app_state directly
return Ok((false, format!("Sidebar {}", app_state.admin_selected_item = app_state.admin_selected_item.saturating_sub(1);
if app_state.ui.show_sidebar { "shown" } else { "hidden" } }
))); "move_down" => {
} // Handle down movement using app_state
app_state.admin_selected_item = app_state.admin_selected_item.saturating_add(1);
}
_ => {}
}
return Ok((false, format!("Admin: {}", action)));
}
return Ok((false, String::new()));
}
if UiStateHandler::toggle_sidebar(
&mut app_state.ui,
config,
key_code,
modifiers,
) {
return Ok((false, format!("Sidebar {}",
if app_state.ui.show_sidebar { "shown" } else { "hidden" }
)));
}
// Handle edit mode first to allow normal character input // Handle edit mode first to allow normal character input
if self.is_edit_mode { if self.is_edit_mode {
if config.is_exit_edit_mode(key_code, modifiers) { if config.is_exit_edit_mode(key_code, modifiers) {
if form_state.has_unsaved_changes { if form_state.has_unsaved_changes {
self.command_message = "Unsaved changes! Use :w to save or :q! to discard".to_string(); self.command_message = "Unsaved changes! Use :w to save or :q! to discard".to_string();
return Ok((false, self.command_message.clone()));
}
self.is_edit_mode = false;
self.edit_mode_cooldown = true;
self.command_message = "Read-only mode".to_string();
terminal.set_cursor_style(SetCursorStyle::SteadyBlock)?;
let current_input = form_state.get_current_input();
if !current_input.is_empty() && form_state.current_cursor_pos >= current_input.len() {
form_state.current_cursor_pos = current_input.len() - 1;
self.ideal_cursor_column = form_state.current_cursor_pos;
}
return Ok((false, self.command_message.clone())); return Ok((false, self.command_message.clone()));
} }
self.is_edit_mode = false;
self.edit_mode_cooldown = true;
self.command_message = "Read-only mode".to_string();
terminal.set_cursor_style(SetCursorStyle::SteadyBlock)?;
let current_input = form_state.get_current_input(); let result = edit::handle_edit_event_internal(
if !current_input.is_empty() && form_state.current_cursor_pos >= current_input.len() { key,
form_state.current_cursor_pos = current_input.len() - 1; config,
self.ideal_cursor_column = form_state.current_cursor_pos; form_state,
} &mut self.ideal_cursor_column,
return Ok((false, self.command_message.clone())); &mut self.command_message,
&mut app_state.ui.is_saved,
current_position,
total_count,
grpc_client,
).await?;
self.key_sequence_tracker.reset();
return Ok((false, result));
} }
let result = edit::handle_edit_event_internal( // Global command mode activation
key, let context_action = config.get_action_for_current_context(
config, self.is_edit_mode,
form_state, self.command_mode,
&mut self.ideal_cursor_column, app_state.ui.show_intro,
&mut self.command_message, app_state.ui.show_admin,
&mut app_state.ui.is_saved, key_code,
current_position, modifiers
total_count, );
grpc_client,
).await?;
self.key_sequence_tracker.reset(); if let Some("enter_command_mode") = context_action {
return Ok((false, result));
}
// Global command mode activation
let context_action = config.get_action_for_current_context(
self.is_edit_mode,
self.command_mode,
app_state.ui.show_intro,
app_state.ui.show_admin,
key_code,
modifiers
);
if let Some("enter_command_mode") = context_action {
self.command_mode = true;
self.command_input.clear();
self.command_message.clear();
return Ok((false, String::new()));
}
if let Some(action) = config.get_action_for_key_in_mode(
&config.keybindings.common,
key_code,
modifiers
) {
match action {
"save" => {
let message = common::save(
form_state,
grpc_client,
&mut app_state.ui.is_saved,
current_position,
total_count,
).await?;
return Ok((false, message));
},
"force_quit" => {
let (should_exit, message) = command_handler.handle_command("force_quit", terminal).await?;
return Ok((should_exit, message));
},
"save_and_quit" => {
let (should_exit, message) = command_handler.handle_command("save_and_quit", terminal).await?;
return Ok((should_exit, message));
},
"revert" => {
let message = common::revert(
form_state,
grpc_client,
current_position,
total_count,
).await?;
return Ok((false, message));
},
_ => {}
}
}
if self.command_mode {
let (should_exit, message, exit_command_mode) = command_mode::handle_command_event(
key,
config,
form_state,
&mut self.command_input,
&mut self.command_message,
grpc_client,
&mut app_state.ui.is_saved,
current_position,
total_count,
).await?;
if exit_command_mode {
self.command_mode = false;
}
return Ok((should_exit, message));
}
if let Some(action) = config.get_read_only_action_for_key(key_code, modifiers) {
if action == "enter_command_mode" {
self.command_mode = true; self.command_mode = true;
self.command_input.clear(); self.command_input.clear();
self.command_message.clear(); self.command_message.clear();
return Ok((false, String::new())); return Ok((false, String::new()));
} }
}
if config.is_enter_edit_mode_before(key_code, modifiers) { if let Some(action) = config.get_action_for_key_in_mode(
self.is_edit_mode = true; &config.keybindings.common,
self.edit_mode_cooldown = true; key_code,
self.command_message = "Edit mode".to_string(); modifiers
terminal.set_cursor_style(SetCursorStyle::BlinkingBar)?; ) {
return Ok((false, self.command_message.clone())); match action {
} "save" => {
let message = common::save(
if config.is_enter_edit_mode_after(key_code, modifiers) { form_state,
let current_input = form_state.get_current_input(); grpc_client,
if !current_input.is_empty() && form_state.current_cursor_pos < current_input.len() { &mut app_state.ui.is_saved,
form_state.current_cursor_pos += 1; current_position,
self.ideal_cursor_column = form_state.current_cursor_pos; total_count,
).await?;
return Ok((false, message));
},
"force_quit" => {
let (should_exit, message) = command_handler.handle_command("force_quit", terminal).await?;
return Ok((should_exit, message));
},
"save_and_quit" => {
let (should_exit, message) = command_handler.handle_command("save_and_quit", terminal).await?;
return Ok((should_exit, message));
},
"revert" => {
let message = common::revert(
form_state,
grpc_client,
current_position,
total_count,
).await?;
return Ok((false, message));
},
_ => {}
}
} }
self.is_edit_mode = true;
self.edit_mode_cooldown = true;
self.command_message = "Edit mode (after cursor)".to_string();
terminal.set_cursor_style(SetCursorStyle::BlinkingBar)?;
return Ok((false, self.command_message.clone()));
}
return read_only::handle_read_only_event( if self.command_mode {
key, let (should_exit, message, exit_command_mode) = command_mode::handle_command_event(
config, key,
form_state, config,
&mut self.key_sequence_tracker, form_state,
current_position, &mut self.command_input,
total_count, &mut self.command_message,
grpc_client, grpc_client,
&mut self.command_message, &mut app_state.ui.is_saved,
&mut self.edit_mode_cooldown, current_position,
&mut self.ideal_cursor_column, total_count,
).await; ).await?;
}
if exit_command_mode {
self.command_mode = false;
}
return Ok((should_exit, message));
}
if let Some(action) = config.get_read_only_action_for_key(key_code, modifiers) {
if action == "enter_command_mode" {
self.command_mode = true;
self.command_input.clear();
self.command_message.clear();
return Ok((false, String::new()));
}
}
if config.is_enter_edit_mode_before(key_code, modifiers) {
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((false, self.command_message.clone()));
}
if config.is_enter_edit_mode_after(key_code, modifiers) {
let current_input = form_state.get_current_input();
if !current_input.is_empty() && form_state.current_cursor_pos < current_input.len() {
form_state.current_cursor_pos += 1;
self.ideal_cursor_column = form_state.current_cursor_pos;
}
self.is_edit_mode = true;
self.edit_mode_cooldown = true;
self.command_message = "Edit mode (after cursor)".to_string();
terminal.set_cursor_style(SetCursorStyle::BlinkingBar)?;
return Ok((false, self.command_message.clone()));
}
return read_only::handle_read_only_event(
key,
config,
form_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;
}
self.edit_mode_cooldown = false; self.edit_mode_cooldown = false;
Ok((false, self.command_message.clone())) Ok((false, self.command_message.clone()))

View File

@@ -1,4 +1,6 @@
// src/client/modes/mod.rs // src/client/modes/mod.rs
pub mod handlers; pub mod handlers;
pub mod canvas;
pub use handlers::*; pub use handlers::*;
pub use canvas::*;

View File

@@ -17,6 +17,8 @@ pub struct AppState {
pub current_position: u64, pub current_position: u64,
pub profile_tree: ProfileTreeResponse, pub profile_tree: ProfileTreeResponse,
pub selected_profile: Option<String>, pub selected_profile: Option<String>,
pub admin_selected_item: usize, // Tracks selection in admin panel
pub admin_profiles: Vec<String>, // Stores admin panel data
// UI preferences // UI preferences
pub ui: UiState, pub ui: UiState,
@@ -33,6 +35,8 @@ impl AppState {
current_position: 0, current_position: 0,
profile_tree: ProfileTreeResponse::default(), profile_tree: ProfileTreeResponse::default(),
selected_profile: None, selected_profile: None,
admin_selected_item: 0,
admin_profiles: Vec::new(),
ui: UiState::default(), ui: UiState::default(),
}) })
} }

View File

@@ -27,7 +27,6 @@ pub fn render_ui(
command_message: &str, command_message: &str,
app_state: &AppState, app_state: &AppState,
intro_state: &intro::IntroState, intro_state: &intro::IntroState,
admin_panel_state: &mut AdminPanelState,
) { ) {
render_background(f, f.area(), theme); render_background(f, f.area(), theme);
@@ -44,7 +43,26 @@ pub fn render_ui(
if app_state.ui.show_intro { if app_state.ui.show_intro {
intro_state.render(f, main_content_area, theme); intro_state.render(f, main_content_area, theme);
} else if app_state.ui.show_admin { } else if app_state.ui.show_admin {
admin_panel_state.render( // Create temporary AdminPanelState for rendering
let mut admin_state = AdminPanelState::new(
if app_state.admin_profiles.is_empty() {
// Fallback if admin_profiles is empty
app_state.profile_tree.profiles
.iter()
.map(|p| p.name.clone())
.collect()
} else {
app_state.admin_profiles.clone()
}
);
// Set the selected item
if !admin_state.profiles.is_empty() {
let safe_index = app_state.admin_selected_item.min(admin_state.profiles.len() - 1);
admin_state.list_state.select(Some(safe_index));
}
admin_state.render(
f, f,
main_content_area, main_content_area,
theme, theme,

View File

@@ -28,11 +28,14 @@ pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
app_state.profile_tree = profile_tree; app_state.profile_tree = profile_tree;
// Now create admin panel with profiles from app_state // Now create admin panel with profiles from app_state
let profiles = app_state.profile_tree.profiles if intro_state.selected_option == 1 {
.iter() app_state.ui.show_admin = true;
.map(|p| p.name.clone()) app_state.admin_profiles = app_state.profile_tree.profiles
.collect(); .iter()
let mut admin_panel_state = AdminPanelState::new(profiles); .map(|p| p.name.clone())
.collect();
app_state.admin_selected_item = 0;
}
// Fetch table structure at startup (one-time) // Fetch table structure at startup (one-time)
let table_structure = grpc_client.get_table_structure().await?; let table_structure = grpc_client.get_table_structure().await?;
@@ -75,7 +78,6 @@ pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
&event_handler.command_message, &event_handler.command_message,
&app_state, &app_state,
&intro_state, &intro_state,
&mut admin_panel_state,
); );
})?; })?;