Compare commits

...

15 Commits

Author SHA1 Message Date
filipriec
9917195fc4 disabling modes where they shouldnt be enabled BIG UPDATE 2025-03-23 20:07:47 +01:00
filipriec
fbcea1b270 trying to make the intro and admin with general keybindings 2025-03-23 19:17:42 +01:00
filipriec
87b07db26a completely broken intro or admin 2025-03-23 15:35:57 +01:00
filipriec
4481560025 HUGE CHANGES TO MODESA 2025-03-23 15:11:43 +01:00
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
filipriec
49fe2aa793 edit mode is now perfectly working 2025-03-23 10:55:05 +01:00
filipriec
87a572783a i think its a step in the right direction, needs to export other functions now 2025-03-23 10:03:59 +01:00
filipriec
ca8dea53fd VERY SUSPICIOUS BREAKING FUNCTIONALITY CHECK LATER 2025-03-23 00:49:19 +01:00
filipriec
fef2f12c9a :disabled in the edit mode, cant type it tho, needs fix 2025-03-23 00:28:51 +01:00
filipriec
1a529a70bf gamechanging, commands works only on their windows properly well 2025-03-22 23:32:33 +01:00
15 changed files with 342 additions and 129 deletions

View File

@@ -1,6 +1,18 @@
# config.toml
[keybindings]
enter_command_mode = [":", "ctrl+;"]
[keybindings.general]
move_up = ["k", "Up"]
move_down = ["j", "Down"]
next_option = ["l", "Right"]
previous_option = ["h", "Left"]
select = ["Enter"]
toggle_sidebar = ["ctrl+t"]
next_field = ["Tab"]
prev_field = ["Shift+Tab"]
[keybindings.common]
save = ["ctrl+s"]
quit = ["ctrl+q"]
@@ -33,7 +45,6 @@ move_line_start = ["0"]
move_line_end = ["$"]
move_first_line = ["gg"]
move_last_line = ["x"]
enter_command_mode = [":", "ctrl+;"]
[keybindings.edit]
exit_edit_mode = ["esc","ctrl+e"]

View File

@@ -1,4 +1,4 @@
// client/src/config/config.rs
// src/config/binds/config.rs
use serde::Deserialize;
use std::collections::HashMap;
@@ -25,6 +25,8 @@ pub struct Config {
#[derive(Debug, Deserialize)]
pub struct ModeKeybindings {
#[serde(default)]
pub general: HashMap<String, Vec<String>>,
#[serde(default)]
pub read_only: HashMap<String, Vec<String>>,
#[serde(default)]
@@ -33,7 +35,6 @@ pub struct ModeKeybindings {
pub command: HashMap<String, Vec<String>>,
#[serde(default)]
pub common: HashMap<String, Vec<String>>,
// Store top-level keybindings that aren't in a specific mode section
#[serde(flatten)]
pub global: HashMap<String, Vec<String>>,
}
@@ -49,6 +50,17 @@ impl Config {
Ok(config)
}
pub fn get_general_action(&self, key: KeyCode, modifiers: KeyModifiers) -> Option<&str> {
self.get_action_for_key_in_mode(&self.keybindings.general, key, modifiers)
.or_else(|| self.get_action_for_key_in_mode(&self.keybindings.global, key, modifiers))
}
/// Common actions for Edit/Read-only modes
pub fn get_common_action(&self, key: KeyCode, modifiers: KeyModifiers) -> Option<&str> {
self.get_action_for_key_in_mode(&self.keybindings.common, key, modifiers)
}
/// Gets an action for a key in Read-Only mode, also checking common keybindings.
pub fn get_read_only_action_for_key(&self, key: KeyCode, modifiers: KeyModifiers) -> Option<&str> {
self.get_action_for_key_in_mode(&self.keybindings.read_only, key, modifiers)
@@ -70,6 +82,25 @@ impl Config {
.or_else(|| self.get_action_for_key_in_mode(&self.keybindings.global, key, modifiers))
}
/// Context-aware keybinding resolution
pub fn get_action_for_current_context(
&self,
is_edit_mode: bool,
command_mode: bool,
key: KeyCode,
modifiers: KeyModifiers
) -> Option<&str> {
match (command_mode, is_edit_mode) {
(true, _) => self.get_command_action_for_key(key, modifiers),
(_, true) => self.get_edit_action_for_key(key, modifiers)
.or_else(|| self.get_common_action(key, modifiers)),
_ => self.get_read_only_action_for_key(key, modifiers)
.or_else(|| self.get_common_action(key, modifiers))
// Add global bindings check for read-only mode
.or_else(|| self.get_action_for_key_in_mode(&self.keybindings.global, key, modifiers)),
}
}
/// Helper function to get an action for a key in a specific mode.
pub fn get_action_for_key_in_mode<'a>(
&self,

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::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 crate::tui::terminal::{
@@ -6,7 +6,7 @@ use crate::tui::terminal::{
};
use crate::config::binds::config::Config;
use crate::ui::handlers::form::FormState;
use super::common;
use crate::modes::canvas::common;
pub async fn handle_edit_event_internal(
key: KeyEvent,
@@ -19,6 +19,24 @@ pub async fn handle_edit_event_internal(
total_count: u64,
grpc_client: &mut GrpcClient,
) -> Result<String, Box<dyn std::error::Error>> {
if let Some("enter_command_mode") = config.get_action_for_key_in_mode(&config.keybindings.global, key.code, key.modifiers) {
// Ignore in edit mode and process as normal input
handle_edit_specific_input(key, form_state, ideal_cursor_column);
return Ok(command_message.clone());
}
// Check common actions first
if let Some(action) = config.get_action_for_key_in_mode(&config.keybindings.common, key.code, key.modifiers) {
return execute_common_action(
action,
form_state,
grpc_client,
is_saved,
current_position,
total_count,
).await;
}
if let Some(action) = config.get_edit_action_for_key(key.code, key.modifiers) {
return execute_edit_action(
action,
@@ -40,6 +58,48 @@ pub async fn handle_edit_event_internal(
Ok(command_message.clone())
}
async fn execute_common_action(
action: &str,
form_state: &mut FormState,
grpc_client: &mut GrpcClient,
is_saved: &mut bool,
current_position: &mut u64,
total_count: u64,
) -> Result<String, Box<dyn std::error::Error>> {
match action {
"save" => {
common::save(
form_state,
grpc_client,
is_saved,
current_position,
total_count,
).await
},
"revert" => {
common::revert(
form_state,
grpc_client,
current_position,
total_count,
).await
},
"move_up" | "move_down" => {
// Reuse edit mode's existing logic
execute_edit_action(
action,
form_state,
&mut 0, // Dummy ideal_cursor_column (not used here)
grpc_client,
is_saved,
current_position,
total_count,
).await
},
_ => Ok(format!("Common action not handled: {}", action)),
}
}
fn handle_edit_specific_input(
key: KeyEvent,
form_state: &mut FormState,

View File

@@ -0,0 +1,2 @@
// src/client/modes/general.rs
pub mod navigation;

View File

View File

@@ -1,6 +1,3 @@
// src/client/modes/handlers.rs
pub mod event;
pub mod edit;
pub mod common;
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::config::binds::config::Config;
use crate::ui::handlers::form::FormState;
use super::common;
use crate::modes::{
canvas::{common},
};
pub async fn handle_command_event(
key: KeyEvent,

View File

@@ -1,5 +1,5 @@
// src/modes/handlers/event.rs
use crossterm::event::{Event, KeyCode};
use crossterm::event::Event;
use crossterm::cursor::SetCursorStyle;
use crate::tui::terminal::{
core::TerminalCore,
@@ -9,9 +9,11 @@ use crate::tui::terminal::{
use crate::config::binds::config::Config;
use crate::ui::handlers::form::FormState;
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 super::common;
pub struct EventHandler {
pub command_mode: bool,
@@ -47,32 +49,88 @@ impl EventHandler {
app_state: &mut crate::state::state::AppState,
total_count: u64,
current_position: &mut u64,
intro_state: &mut crate::components::intro::intro::IntroState,
) -> Result<(bool, String), Box<dyn std::error::Error>> {
if app_state.ui.show_intro {
if let Event::Key(key) = event {
match key.code {
KeyCode::Left => intro_state.previous_option(),
KeyCode::Right => intro_state.next_option(),
KeyCode::Enter => {
if intro_state.selected_option == 0 {
app_state.ui.show_intro = false;
} else {
app_state.ui.show_intro = false;
app_state.ui.show_admin = true;
let key_code = key.code;
let modifiers = key.modifiers;
// Handle general mode (replaces intro and admin)
if app_state.ui.show_intro || app_state.ui.show_admin {
if let Some(action) = config.get_general_action(key_code, modifiers) {
match action {
"move_up" => {
app_state.general.selected_item = app_state.general.selected_item.saturating_sub(1);
return Ok((false, String::new()));
}
"move_down" => {
app_state.general.selected_item = app_state.general.selected_item.saturating_add(1);
return Ok((false, String::new()));
}
"next_option" => {
app_state.general.current_option = app_state.general.current_option.saturating_add(1);
return Ok((false, String::new()));
}
"previous_option" => {
app_state.general.current_option = app_state.general.current_option.saturating_sub(1);
return Ok((false, String::new()));
}
"select" => {
if app_state.ui.show_intro {
app_state.ui.show_intro = false;
} else if app_state.ui.show_admin {
app_state.ui.show_admin = false;
}
return Ok((false, "Selected".to_string()));
}
"toggle_sidebar" => {
app_state.ui.show_sidebar = !app_state.ui.show_sidebar;
return Ok((false, format!("Sidebar {}",
if app_state.ui.show_sidebar { "shown" } else { "hidden" }
)));
}
"next_field" => {
// Increment field navigation
if form_state.fields.len() > 0 {
form_state.current_field = (form_state.current_field + 1) % form_state.fields.len();
}
return Ok((false, String::new()));
}
"prev_field" => {
// Decrement field navigation
if form_state.fields.len() > 0 {
form_state.current_field = if form_state.current_field == 0 {
form_state.fields.len() - 1
} else {
form_state.current_field - 1
};
}
return Ok((false, String::new()));
}
"enter_command_mode" => {
self.command_mode = true;
self.command_input.clear();
self.command_message.clear();
return Ok((false, String::new()));
}
_ => {}
}
}
if let Some("enter_command_mode") = config.get_action_for_key_in_mode(
&config.keybindings.global,
key_code,
modifiers
) {
self.command_mode = true;
return Ok((false, String::new()));
}
if let Event::Key(key) = event {
let key_code = key.code;
let modifiers = key.modifiers;
// If no general action matched, return to stay in general mode
return Ok((false, String::new()));
}
// The rest of the function handles other modes as before
// Handle toggling sidebar which is common across modes
if UiStateHandler::toggle_sidebar(
&mut app_state.ui,
config,
@@ -84,6 +142,60 @@ impl EventHandler {
)));
}
// Handle edit mode first to allow normal character input
if self.is_edit_mode {
if config.is_exit_edit_mode(key_code, modifiers) {
if form_state.has_unsaved_changes {
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()));
}
let result = edit::handle_edit_event_internal(
key,
config,
form_state,
&mut self.ideal_cursor_column,
&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));
}
// Global command mode activation
let context_action = config.get_action_for_current_context(
self.is_edit_mode,
self.command_mode,
key_code,
modifiers
);
// Block command mode entry from edit mode
if let Some("enter_command_mode") = context_action {
if !self.is_edit_mode {
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,
@@ -141,40 +253,6 @@ impl EventHandler {
return Ok((should_exit, message));
}
if self.is_edit_mode {
if config.is_exit_edit_mode(key_code, modifiers) {
if form_state.has_unsaved_changes {
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()));
}
let result = edit::handle_edit_event_internal(
key,
config,
form_state,
&mut self.ideal_cursor_column,
&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));
} else {
if let Some(action) = config.get_read_only_action_for_key(key_code, modifiers) {
if action == "enter_command_mode" {
self.command_mode = true;
@@ -218,7 +296,6 @@ impl EventHandler {
&mut self.ideal_cursor_column,
).await;
}
}
self.edit_mode_cooldown = false;
Ok((false, self.command_message.clone()))

View File

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

View File

@@ -10,6 +10,11 @@ pub struct UiState {
pub show_admin: bool,
}
pub struct GeneralState {
pub selected_item: usize,
pub current_option: usize,
}
pub struct AppState {
// Core editor state
pub current_dir: String,
@@ -20,6 +25,7 @@ pub struct AppState {
// UI preferences
pub ui: UiState,
pub general: GeneralState,
}
impl AppState {
@@ -34,6 +40,10 @@ impl AppState {
profile_tree: ProfileTreeResponse::default(),
selected_profile: None,
ui: UiState::default(),
general: GeneralState {
selected_item: 0,
current_option: 0,
},
})
}

View File

@@ -27,7 +27,6 @@ pub fn render_ui(
command_message: &str,
app_state: &AppState,
intro_state: &intro::IntroState,
admin_panel_state: &mut AdminPanelState,
) {
render_background(f, f.area(), theme);
@@ -44,7 +43,25 @@ pub fn render_ui(
if app_state.ui.show_intro {
intro_state.render(f, main_content_area, theme);
} 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.profile_tree.profiles.is_empty() {
// Fallback if admin_profiles is empty
app_state.profile_tree.profiles
.iter()
.map(|p| p.name.clone())
.collect()
} else {
app_state.profile_tree.profiles.iter().map(|p| p.name.clone()).collect()
}
);
// Set the selected item
if !admin_state.profiles.is_empty() {
app_state.general.selected_item.min(admin_state.profiles.len().saturating_sub(1));
}
admin_state.render(
f,
main_content_area,
theme,

View File

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