Compare commits
23 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
355aff3032 | ||
|
|
3bb771187a | ||
|
|
aa3ff18f9c | ||
|
|
1fc9a0e1ff | ||
|
|
cdac78c1bc | ||
|
|
bdb6cd4069 | ||
|
|
ec5802b3a2 | ||
|
|
1eb2edc1df | ||
|
|
2da009eede | ||
|
|
b2fd44df49 | ||
|
|
78e8cce08b | ||
|
|
2cf4cd6748 | ||
|
|
7caa4d8c3c | ||
|
|
9917195fc4 | ||
|
|
fbcea1b270 | ||
|
|
87b07db26a | ||
|
|
4481560025 | ||
|
|
d1d33b5752 | ||
|
|
c6c6c5ed81 | ||
|
|
4ddcb34205 | ||
|
|
83393a20e2 | ||
|
|
13d501e6d7 | ||
|
|
993febd204 |
@@ -3,10 +3,15 @@
|
||||
|
||||
enter_command_mode = [":", "ctrl+;"]
|
||||
|
||||
[keybindings.intro]
|
||||
next_option = ["j", "Right"]
|
||||
previous_option = ["k", "Left"]
|
||||
[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"]
|
||||
|
||||
@@ -26,7 +26,7 @@ pub struct Config {
|
||||
#[derive(Debug, Deserialize)]
|
||||
pub struct ModeKeybindings {
|
||||
#[serde(default)]
|
||||
pub intro: HashMap<String, Vec<String>>,
|
||||
pub general: HashMap<String, Vec<String>>,
|
||||
#[serde(default)]
|
||||
pub read_only: HashMap<String, Vec<String>>,
|
||||
#[serde(default)]
|
||||
@@ -50,16 +50,15 @@ impl Config {
|
||||
Ok(config)
|
||||
}
|
||||
|
||||
/// Gets an action for a key in Intro mode, only checking intro and global bindings
|
||||
pub fn get_intro_action(&self, key: KeyCode, modifiers: KeyModifiers) -> Option<&str> {
|
||||
self.get_action_for_key_in_mode(&self.keybindings.intro, key, modifiers)
|
||||
|
||||
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))
|
||||
}
|
||||
|
||||
/// 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> {
|
||||
/// 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)
|
||||
.or_else(|| self.get_action_for_key_in_mode(&self.keybindings.global, key, modifiers))
|
||||
}
|
||||
|
||||
/// Gets an action for a key in Read-Only mode, also checking common keybindings.
|
||||
@@ -88,17 +87,17 @@ impl Config {
|
||||
&self,
|
||||
is_edit_mode: bool,
|
||||
command_mode: bool,
|
||||
show_intro: bool,
|
||||
show_admin: bool,
|
||||
key: KeyCode,
|
||||
modifiers: KeyModifiers
|
||||
) -> Option<&str> {
|
||||
match (show_intro, show_admin, command_mode, is_edit_mode) {
|
||||
(true, _, _, _) => self.get_intro_action(key, modifiers),
|
||||
(_, true, _, _) => self.get_admin_action(key, modifiers),
|
||||
(_, _, true, _) => self.get_command_action_for_key(key, modifiers),
|
||||
(_, _, _, true) => self.get_edit_action_for_key(key, modifiers),
|
||||
_ => self.get_read_only_action_for_key(key, modifiers),
|
||||
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)),
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
4
client/src/modes/canvas.rs
Normal file
4
client/src/modes/canvas.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
// src/client/modes/canvas.rs
|
||||
pub mod edit;
|
||||
pub mod common;
|
||||
pub mod read_only;
|
||||
@@ -1,9 +1,76 @@
|
||||
// src/modes/handlers/common.rs
|
||||
// src/modes/canvas/common.rs
|
||||
|
||||
use crossterm::event::{KeyEvent};
|
||||
use crate::config::binds::config::Config;
|
||||
use crate::tui::terminal::grpc_client::GrpcClient;
|
||||
use crate::tui::terminal::core::TerminalCore;
|
||||
use crate::tui::terminal::commands::CommandHandler;
|
||||
use crate::ui::handlers::form::FormState;
|
||||
use crate::state::state::AppState;
|
||||
use common::proto::multieko2::adresar::{PostAdresarRequest, PutAdresarRequest};
|
||||
|
||||
/// Main handler for common core actions
|
||||
pub async fn handle_core_action(
|
||||
action: &str,
|
||||
form_state: &mut FormState,
|
||||
grpc_client: &mut GrpcClient,
|
||||
command_handler: &mut CommandHandler,
|
||||
terminal: &mut TerminalCore,
|
||||
app_state: &mut AppState,
|
||||
current_position: &mut u64,
|
||||
total_count: u64,
|
||||
) -> Result<(bool, String), Box<dyn std::error::Error>> {
|
||||
match action {
|
||||
"save" => {
|
||||
let message = save(
|
||||
form_state,
|
||||
grpc_client,
|
||||
&mut app_state.ui.is_saved,
|
||||
current_position,
|
||||
total_count,
|
||||
).await?;
|
||||
Ok((false, message))
|
||||
},
|
||||
"force_quit" => {
|
||||
let (should_exit, message) = command_handler.handle_command("force_quit", terminal).await?;
|
||||
Ok((should_exit, message))
|
||||
},
|
||||
"save_and_quit" => {
|
||||
let (should_exit, message) = command_handler.handle_command("save_and_quit", terminal).await?;
|
||||
Ok((should_exit, message))
|
||||
},
|
||||
"revert" => {
|
||||
let message = revert(
|
||||
form_state,
|
||||
grpc_client,
|
||||
current_position,
|
||||
total_count,
|
||||
).await?;
|
||||
Ok((false, message))
|
||||
},
|
||||
// We should never hit this case with proper filtering
|
||||
_ => Ok((false, format!("Core action not handled: {}", action))),
|
||||
}
|
||||
}
|
||||
|
||||
/// Helper function to check if a key event should trigger a core action
|
||||
pub fn is_core_action(config: &Config, key_code: crossterm::event::KeyCode, modifiers: crossterm::event::KeyModifiers) -> Option<String> {
|
||||
// Check for core application actions (save, quit, etc.)
|
||||
if let Some(action) = config.get_action_for_key_in_mode(
|
||||
&config.keybindings.common,
|
||||
key_code,
|
||||
modifiers
|
||||
) {
|
||||
match action {
|
||||
"save" | "force_quit" | "save_and_quit" | "revert" => {
|
||||
return Some(action.to_string())
|
||||
},
|
||||
_ => {} // Other actions are handled by their respective mode handlers
|
||||
}
|
||||
}
|
||||
None
|
||||
}
|
||||
|
||||
/// Shared logic for saving the current form state
|
||||
pub async fn save(
|
||||
form_state: &mut FormState,
|
||||
@@ -1,12 +1,13 @@
|
||||
// src/modes/handlers/edit.rs
|
||||
// src/modes/canvas/edit.rs
|
||||
|
||||
// TODO THIS is freaking bloated with functions it never uses REFACTOR 200 LOC can be gone
|
||||
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_edit_event_internal(
|
||||
key: KeyEvent,
|
||||
3
client/src/modes/common.rs
Normal file
3
client/src/modes/common.rs
Normal file
@@ -0,0 +1,3 @@
|
||||
// src/client/modes/common.rs
|
||||
pub mod command_mode;
|
||||
pub mod highlight;
|
||||
@@ -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,
|
||||
2
client/src/modes/general.rs
Normal file
2
client/src/modes/general.rs
Normal file
@@ -0,0 +1,2 @@
|
||||
// src/client/modes/general.rs
|
||||
pub mod navigation;
|
||||
175
client/src/modes/general/navigation.rs
Normal file
175
client/src/modes/general/navigation.rs
Normal file
@@ -0,0 +1,175 @@
|
||||
// src/modes/general/navigation.rs
|
||||
|
||||
use crossterm::event::KeyEvent;
|
||||
use crate::config::binds::config::Config;
|
||||
use crate::state::state::AppState;
|
||||
use crate::ui::handlers::form::FormState;
|
||||
|
||||
pub async fn handle_navigation_event(
|
||||
key: KeyEvent,
|
||||
config: &Config,
|
||||
form_state: &mut FormState,
|
||||
app_state: &mut AppState,
|
||||
command_mode: &mut bool,
|
||||
command_input: &mut String,
|
||||
command_message: &mut String,
|
||||
) -> Result<(bool, String), Box<dyn std::error::Error>> {
|
||||
if let Some(action) = config.get_general_action(key.code, key.modifiers) {
|
||||
match action {
|
||||
"move_up" => {
|
||||
move_up(app_state);
|
||||
return Ok((false, String::new()));
|
||||
}
|
||||
"move_down" => {
|
||||
let item_count = if app_state.ui.show_intro {
|
||||
2 // Intro options count
|
||||
} else {
|
||||
app_state.profile_tree.profiles.len() // Admin panel items
|
||||
};
|
||||
move_down(app_state, item_count);
|
||||
return Ok((false, String::new()));
|
||||
}
|
||||
"next_option" => {
|
||||
next_option(app_state, 2); // Intro has 2 options
|
||||
return Ok((false, String::new()));
|
||||
}
|
||||
"previous_option" => {
|
||||
previous_option(app_state);
|
||||
return Ok((false, String::new()));
|
||||
}
|
||||
"select" => {
|
||||
select(app_state);
|
||||
return Ok((false, "Selected".to_string()));
|
||||
}
|
||||
"toggle_sidebar" => {
|
||||
toggle_sidebar(app_state);
|
||||
return Ok((false, format!("Sidebar {}",
|
||||
if app_state.ui.show_sidebar { "shown" } else { "hidden" }
|
||||
)));
|
||||
}
|
||||
"next_field" => {
|
||||
next_field(form_state);
|
||||
return Ok((false, String::new()));
|
||||
}
|
||||
"prev_field" => {
|
||||
prev_field(form_state);
|
||||
return Ok((false, String::new()));
|
||||
}
|
||||
"enter_command_mode" => {
|
||||
handle_enter_command_mode(command_mode, command_input, command_message);
|
||||
return Ok((false, String::new()));
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
Ok((false, String::new()))
|
||||
}
|
||||
|
||||
pub fn move_up(app_state: &mut AppState) {
|
||||
if app_state.ui.show_intro {
|
||||
app_state.ui.intro_state.previous_option();
|
||||
} else if app_state.ui.show_admin {
|
||||
// Assuming profile_tree.profiles is the list we're navigating
|
||||
let profile_count = app_state.profile_tree.profiles.len();
|
||||
if profile_count == 0 {
|
||||
return;
|
||||
}
|
||||
|
||||
// Use general state for tracking selection in admin panel
|
||||
if app_state.general.selected_item == 0 {
|
||||
app_state.general.selected_item = profile_count - 1;
|
||||
} else {
|
||||
app_state.general.selected_item = app_state.general.selected_item.saturating_sub(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn move_down(app_state: &mut AppState, item_count: usize) {
|
||||
if app_state.ui.show_intro {
|
||||
app_state.ui.intro_state.next_option();
|
||||
} else if app_state.ui.show_admin {
|
||||
// Assuming profile_tree.profiles is the list we're navigating
|
||||
let profile_count = app_state.profile_tree.profiles.len();
|
||||
if profile_count == 0 {
|
||||
return;
|
||||
}
|
||||
|
||||
app_state.general.selected_item = (app_state.general.selected_item + 1) % profile_count;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn next_option(app_state: &mut AppState, option_count: usize) {
|
||||
if app_state.ui.show_intro {
|
||||
app_state.ui.intro_state.next_option();
|
||||
} else {
|
||||
// For other screens that might have options
|
||||
app_state.general.current_option = (app_state.general.current_option + 1) % option_count;
|
||||
}
|
||||
}
|
||||
|
||||
pub fn previous_option(app_state: &mut AppState) {
|
||||
if app_state.ui.show_intro {
|
||||
app_state.ui.intro_state.previous_option();
|
||||
} else {
|
||||
// For other screens that might have options
|
||||
if app_state.general.current_option == 0 {
|
||||
// We'd need the option count here, but since it's not passed we can't wrap around correctly
|
||||
// For now, just stay at 0
|
||||
} else {
|
||||
app_state.general.current_option -= 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn select(app_state: &mut AppState) {
|
||||
if app_state.ui.show_intro {
|
||||
// Handle selection in intro screen
|
||||
if app_state.ui.intro_state.selected_option == 0 {
|
||||
// First option selected - show form
|
||||
app_state.ui.show_form = true;
|
||||
app_state.ui.show_admin = false;
|
||||
} else {
|
||||
// Second option selected - show admin
|
||||
app_state.ui.show_form = false;
|
||||
app_state.ui.show_admin = true;
|
||||
}
|
||||
app_state.ui.show_intro = false;
|
||||
} else if app_state.ui.show_admin {
|
||||
// Handle selection in admin panel
|
||||
let profiles = &app_state.profile_tree.profiles;
|
||||
if !profiles.is_empty() && app_state.general.selected_item < profiles.len() {
|
||||
// Set the selected profile
|
||||
app_state.selected_profile = Some(profiles[app_state.general.selected_item].name.clone());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
}
|
||||
|
||||
pub fn prev_field(form_state: &mut FormState) {
|
||||
if !form_state.fields.is_empty() {
|
||||
if form_state.current_field == 0 {
|
||||
form_state.current_field = form_state.fields.len() - 1;
|
||||
} else {
|
||||
form_state.current_field -= 1;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn handle_enter_command_mode(
|
||||
command_mode: &mut bool,
|
||||
command_input: &mut String,
|
||||
command_message: &mut String
|
||||
) {
|
||||
*command_mode = true;
|
||||
command_input.clear();
|
||||
command_message.clear();
|
||||
}
|
||||
@@ -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;
|
||||
pub mod mode_manager;
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
// src/modes/handlers/event.rs
|
||||
use crossterm::event::Event;
|
||||
use crossterm::event::{Event, KeyEvent};
|
||||
use crossterm::cursor::SetCursorStyle;
|
||||
use crate::tui::terminal::{
|
||||
core::TerminalCore,
|
||||
@@ -9,9 +9,13 @@ 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::{
|
||||
common::{command_mode},
|
||||
canvas::{edit, read_only, common},
|
||||
general::navigation,
|
||||
};
|
||||
use crate::config::binds::key_sequences::KeySequenceTracker;
|
||||
use super::common;
|
||||
use crate::modes::handlers::mode_manager::{ModeManager, AppMode};
|
||||
|
||||
pub struct EventHandler {
|
||||
pub command_mode: bool,
|
||||
@@ -47,195 +51,197 @@ 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 {
|
||||
let action = config.get_intro_action(key.code, key.modifiers);
|
||||
|
||||
match action {
|
||||
Some("previous_option") => intro_state.previous_option(),
|
||||
Some("next_option") => intro_state.next_option(),
|
||||
Some("select") => {
|
||||
app_state.ui.show_intro = false;
|
||||
app_state.ui.show_admin = intro_state.selected_option == 1;
|
||||
},
|
||||
_ => {} // Ignore all other keys
|
||||
}
|
||||
}
|
||||
return Ok((false, String::new()));
|
||||
}
|
||||
// Determine current mode based on app state and event handler state
|
||||
let current_mode = ModeManager::derive_mode(app_state, self);
|
||||
app_state.update_mode(current_mode);
|
||||
|
||||
if let Event::Key(key) = event {
|
||||
let key_code = key.code;
|
||||
let modifiers = key.modifiers;
|
||||
|
||||
if UiStateHandler::toggle_sidebar(
|
||||
&mut app_state.ui,
|
||||
config,
|
||||
key_code,
|
||||
modifiers,
|
||||
) {
|
||||
// Handle common actions across all modes
|
||||
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
|
||||
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();
|
||||
// Mode-specific handling
|
||||
match current_mode {
|
||||
AppMode::General => {
|
||||
return navigation::handle_navigation_event(
|
||||
key,
|
||||
config,
|
||||
form_state,
|
||||
app_state,
|
||||
&mut self.command_mode,
|
||||
&mut self.command_input,
|
||||
&mut self.command_message,
|
||||
).await;
|
||||
},
|
||||
|
||||
AppMode::ReadOnly => {
|
||||
// Check for mode transitions first
|
||||
if config.is_enter_edit_mode_before(key_code, modifiers) &&
|
||||
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((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;
|
||||
if config.is_enter_edit_mode_after(key_code, modifiers) &&
|
||||
ModeManager::can_enter_edit_mode(current_mode) {
|
||||
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 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?;
|
||||
// Check for entering command mode
|
||||
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) {
|
||||
self.command_mode = true;
|
||||
self.command_input.clear();
|
||||
self.command_message.clear();
|
||||
return Ok((false, String::new()));
|
||||
}
|
||||
}
|
||||
|
||||
self.key_sequence_tracker.reset();
|
||||
return Ok((false, result));
|
||||
}
|
||||
// Check for core application actions (save, quit, etc.)
|
||||
// ONLY handle a limited subset of core actions here
|
||||
if let Some(action) = config.get_action_for_key_in_mode(
|
||||
&config.keybindings.common,
|
||||
key_code,
|
||||
modifiers
|
||||
) {
|
||||
match action {
|
||||
"save" | "force_quit" | "save_and_quit" | "revert" => {
|
||||
return common::handle_core_action(
|
||||
action,
|
||||
form_state,
|
||||
grpc_client,
|
||||
command_handler,
|
||||
terminal,
|
||||
app_state,
|
||||
current_position,
|
||||
total_count,
|
||||
).await;
|
||||
},
|
||||
_ => {} // For other actions, let the mode-specific handler take care of it
|
||||
}
|
||||
}
|
||||
|
||||
// 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
|
||||
);
|
||||
// Let read_only mode handle its own actions (including navigation from common bindings)
|
||||
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;
|
||||
},
|
||||
|
||||
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()));
|
||||
}
|
||||
AppMode::Edit => {
|
||||
// Check for exiting 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)?;
|
||||
|
||||
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));
|
||||
},
|
||||
_ => {}
|
||||
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()));
|
||||
}
|
||||
|
||||
// Check for core application actions (save, quit, etc.)
|
||||
// ONLY handle a limited subset of core actions here
|
||||
if let Some(action) = config.get_action_for_key_in_mode(
|
||||
&config.keybindings.common,
|
||||
key_code,
|
||||
modifiers
|
||||
) {
|
||||
match action {
|
||||
"save" | "force_quit" | "save_and_quit" | "revert" => {
|
||||
return common::handle_core_action(
|
||||
action,
|
||||
form_state,
|
||||
grpc_client,
|
||||
command_handler,
|
||||
terminal,
|
||||
app_state,
|
||||
current_position,
|
||||
total_count,
|
||||
).await;
|
||||
},
|
||||
_ => {} // For other actions, let the mode-specific handler take care of it
|
||||
}
|
||||
}
|
||||
|
||||
// Let edit mode handle its own actions (including navigation from common bindings)
|
||||
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));
|
||||
},
|
||||
|
||||
AppMode::Command => {
|
||||
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 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_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;
|
||||
}
|
||||
|
||||
// Non-key events or if no specific handler was matched
|
||||
self.edit_mode_cooldown = false;
|
||||
Ok((false, self.command_message.clone()))
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
50
client/src/modes/handlers/mode_manager.rs
Normal file
50
client/src/modes/handlers/mode_manager.rs
Normal file
@@ -0,0 +1,50 @@
|
||||
// src/modes/handlers/mode_manager.rs
|
||||
use crate::state::state::AppState;
|
||||
use crate::modes::handlers::event::EventHandler;
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
|
||||
pub enum AppMode {
|
||||
General, // For intro and admin screens
|
||||
ReadOnly, // Canvas read-only mode
|
||||
Edit, // Canvas edit mode
|
||||
Command, // Command mode overlay
|
||||
}
|
||||
|
||||
pub struct ModeManager;
|
||||
|
||||
impl ModeManager {
|
||||
// Determine current mode based on app state
|
||||
pub fn derive_mode(app_state: &AppState, event_handler: &EventHandler) -> AppMode {
|
||||
// Command mode takes precedence if active
|
||||
if event_handler.command_mode {
|
||||
return AppMode::Command;
|
||||
}
|
||||
|
||||
// Check UI state flags
|
||||
if app_state.ui.show_intro || app_state.ui.show_admin {
|
||||
AppMode::General
|
||||
} else if app_state.ui.show_form {
|
||||
if event_handler.is_edit_mode {
|
||||
AppMode::Edit
|
||||
} else {
|
||||
AppMode::ReadOnly
|
||||
}
|
||||
} else {
|
||||
// Fallback
|
||||
AppMode::General
|
||||
}
|
||||
}
|
||||
|
||||
// Mode transition rules
|
||||
pub fn can_enter_command_mode(current_mode: AppMode) -> bool {
|
||||
!matches!(current_mode, AppMode::Edit) // Can't enter from Edit mode
|
||||
}
|
||||
|
||||
pub fn can_enter_edit_mode(current_mode: AppMode) -> bool {
|
||||
matches!(current_mode, AppMode::ReadOnly) // Only from ReadOnly
|
||||
}
|
||||
|
||||
pub fn can_enter_read_only_mode(current_mode: AppMode) -> bool {
|
||||
matches!(current_mode, AppMode::Edit | AppMode::Command)
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,10 @@
|
||||
// src/client/modes/mod.rs
|
||||
pub mod handlers;
|
||||
pub mod canvas;
|
||||
pub mod general;
|
||||
pub mod common;
|
||||
|
||||
pub use handlers::*;
|
||||
pub use canvas::*;
|
||||
pub use general::*;
|
||||
pub use common::*;
|
||||
|
||||
@@ -2,12 +2,21 @@
|
||||
|
||||
use std::env;
|
||||
use common::proto::multieko2::table_definition::ProfileTreeResponse;
|
||||
use crate::components::IntroState;
|
||||
use crate::modes::handlers::mode_manager::AppMode;
|
||||
|
||||
pub struct UiState {
|
||||
pub show_sidebar: bool,
|
||||
pub is_saved: bool,
|
||||
pub show_intro: bool,
|
||||
pub show_admin: bool,
|
||||
pub show_form: bool,
|
||||
pub intro_state: IntroState,
|
||||
}
|
||||
|
||||
pub struct GeneralState {
|
||||
pub selected_item: usize,
|
||||
pub current_option: usize,
|
||||
}
|
||||
|
||||
pub struct AppState {
|
||||
@@ -17,9 +26,11 @@ pub struct AppState {
|
||||
pub current_position: u64,
|
||||
pub profile_tree: ProfileTreeResponse,
|
||||
pub selected_profile: Option<String>,
|
||||
pub current_mode: AppMode,
|
||||
|
||||
// UI preferences
|
||||
pub ui: UiState,
|
||||
pub general: GeneralState,
|
||||
}
|
||||
|
||||
impl AppState {
|
||||
@@ -33,7 +44,12 @@ impl AppState {
|
||||
current_position: 0,
|
||||
profile_tree: ProfileTreeResponse::default(),
|
||||
selected_profile: None,
|
||||
current_mode: AppMode::General,
|
||||
ui: UiState::default(),
|
||||
general: GeneralState {
|
||||
selected_item: 0,
|
||||
current_option: 0,
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
@@ -45,6 +61,10 @@ impl AppState {
|
||||
pub fn update_current_position(&mut self, current_position: u64) {
|
||||
self.current_position = current_position;
|
||||
}
|
||||
|
||||
pub fn update_mode(&mut self, mode: AppMode) {
|
||||
self.current_mode = mode;
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for UiState {
|
||||
@@ -54,6 +74,8 @@ impl Default for UiState {
|
||||
is_saved: false,
|
||||
show_intro: true,
|
||||
show_admin: false,
|
||||
show_form: false,
|
||||
intro_state: IntroState::new(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,8 +26,7 @@ pub fn render_ui(
|
||||
command_mode: bool,
|
||||
command_message: &str,
|
||||
app_state: &AppState,
|
||||
intro_state: &intro::IntroState,
|
||||
admin_panel_state: &mut AdminPanelState,
|
||||
// intro_state parameter removed
|
||||
) {
|
||||
render_background(f, f.area(), theme);
|
||||
|
||||
@@ -42,16 +41,34 @@ pub fn render_ui(
|
||||
|
||||
let main_content_area = root[0];
|
||||
if app_state.ui.show_intro {
|
||||
intro_state.render(f, main_content_area, theme);
|
||||
// Use app_state's intro_state directly
|
||||
app_state.ui.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(
|
||||
app_state.profile_tree.profiles
|
||||
.iter()
|
||||
.map(|p| p.name.clone())
|
||||
.collect()
|
||||
);
|
||||
|
||||
// Set the selected item - FIXED
|
||||
if !admin_state.profiles.is_empty() {
|
||||
let selected_index = std::cmp::min(
|
||||
app_state.general.selected_item,
|
||||
admin_state.profiles.len() - 1
|
||||
);
|
||||
admin_state.list_state.select(Some(selected_index));
|
||||
}
|
||||
|
||||
admin_state.render(
|
||||
f,
|
||||
main_content_area,
|
||||
theme,
|
||||
&app_state.profile_tree,
|
||||
&app_state.selected_profile,
|
||||
);
|
||||
} else {
|
||||
} else if app_state.ui.show_form {
|
||||
let (sidebar_area, form_area) = calculate_sidebar_layout(
|
||||
app_state.ui.show_sidebar,
|
||||
main_content_area
|
||||
@@ -59,11 +76,11 @@ pub fn render_ui(
|
||||
|
||||
if let Some(sidebar_rect) = sidebar_area {
|
||||
sidebar::render_sidebar(
|
||||
f,
|
||||
sidebar_rect,
|
||||
theme,
|
||||
&app_state.profile_tree,
|
||||
&app_state.selected_profile // Remove trailing comma
|
||||
f,
|
||||
sidebar_rect,
|
||||
theme,
|
||||
&app_state.profile_tree,
|
||||
&app_state.selected_profile
|
||||
);
|
||||
}
|
||||
|
||||
@@ -107,6 +124,8 @@ pub fn render_ui(
|
||||
total_count,
|
||||
current_position,
|
||||
);
|
||||
} else{
|
||||
|
||||
}
|
||||
|
||||
render_status_line(f, root[1], current_dir, theme, is_edit_mode);
|
||||
|
||||
@@ -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?;
|
||||
@@ -74,8 +74,6 @@ pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
|
||||
event_handler.command_mode,
|
||||
&event_handler.command_message,
|
||||
&app_state,
|
||||
&intro_state,
|
||||
&mut admin_panel_state,
|
||||
);
|
||||
})?;
|
||||
|
||||
@@ -93,7 +91,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;
|
||||
|
||||
Reference in New Issue
Block a user