working inputting characters

This commit is contained in:
filipriec
2025-04-05 13:34:38 +02:00
parent 1eaa716f42
commit 7fd1c1e41d
4 changed files with 159 additions and 78 deletions

View File

@@ -10,6 +10,7 @@ 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::canvas_state::CanvasState;
use crate::ui::handlers::rat_state::UiStateHandler;
use crate::modes::{
common::{command_mode},
@@ -27,7 +28,7 @@ pub struct EventHandler {
pub edit_mode_cooldown: bool,
pub ideal_cursor_column: usize,
pub key_sequence_tracker: KeySequenceTracker,
pub auth_state: AuthState,
// pub auth_state: AuthState, // Removed
pub auth_client: AuthClient,
}
@@ -41,7 +42,7 @@ impl EventHandler {
edit_mode_cooldown: false,
ideal_cursor_column: 0,
key_sequence_tracker: KeySequenceTracker::new(800),
auth_state: AuthState::new(),
// auth_state: AuthState::new(), // Removed
auth_client: AuthClient::new().await?,
})
}
@@ -54,6 +55,7 @@ impl EventHandler {
grpc_client: &mut GrpcClient,
command_handler: &mut CommandHandler,
form_state: &mut FormState,
auth_state: &mut AuthState, // Added
app_state: &mut crate::state::state::AppState,
total_count: u64,
current_position: &mut u64,
@@ -100,10 +102,25 @@ impl EventHandler {
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;
let current_input = if app_state.ui.show_login {
auth_state.get_current_input()
} else {
form_state.get_current_input()
};
let current_cursor_pos = if app_state.ui.show_login {
auth_state.current_cursor_pos()
} else {
form_state.current_cursor_pos()
};
if !current_input.is_empty() && current_cursor_pos < current_input.len() {
if app_state.ui.show_login {
auth_state.set_current_cursor_pos(current_cursor_pos + 1);
self.ideal_cursor_column = auth_state.current_cursor_pos();
} else {
form_state.set_current_cursor_pos(current_cursor_pos + 1);
self.ideal_cursor_column = form_state.current_cursor_pos();
}
}
self.is_edit_mode = true;
self.edit_mode_cooldown = true;
@@ -123,7 +140,6 @@ impl EventHandler {
}
// 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,
@@ -134,7 +150,7 @@ impl EventHandler {
return common_mode::handle_core_action(
action,
form_state,
&mut self.auth_state,
auth_state, // Changed
grpc_client,
&mut self.auth_client,
terminal,
@@ -143,17 +159,17 @@ impl EventHandler {
total_count,
).await;
},
_ => {} // For other actions, let the mode-specific handler take care of it
_ => {}
}
}
// Let read_only mode handle its own actions (including navigation from common bindings)
// Let read_only mode handle its own actions
return read_only::handle_read_only_event(
&app_state,
key,
config,
form_state,
&mut self.auth_state,
auth_state, // Changed
&mut self.key_sequence_tracker,
current_position,
total_count,
@@ -167,7 +183,13 @@ impl EventHandler {
AppMode::Edit => {
// Check for exiting edit mode
if config.is_exit_edit_mode(key_code, modifiers) {
if form_state.has_unsaved_changes {
let has_changes = if app_state.ui.show_login {
auth_state.has_unsaved_changes()
} else {
form_state.has_unsaved_changes()
};
if has_changes {
self.command_message = "Unsaved changes! Use :w to save or :q! to discard".to_string();
return Ok((false, self.command_message.clone()));
}
@@ -176,16 +198,31 @@ impl EventHandler {
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;
let current_input = if app_state.ui.show_login {
auth_state.get_current_input()
} else {
form_state.get_current_input()
};
let current_cursor_pos = if app_state.ui.show_login {
auth_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 {
auth_state.set_current_cursor_pos(new_pos);
self.ideal_cursor_column = auth_state.current_cursor_pos();
} else {
form_state.set_current_cursor_pos(new_pos);
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,
@@ -196,7 +233,7 @@ impl EventHandler {
return common_mode::handle_core_action(
action,
form_state,
&mut self.auth_state,
auth_state, // Changed
grpc_client,
&mut self.auth_client,
terminal,
@@ -205,17 +242,17 @@ impl EventHandler {
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 edit mode handle its own actions
let result = edit::handle_edit_event(
app_state.ui.show_login,
key,
config,
form_state,
&mut self.auth_state,
auth_state, // Changed
&mut self.ideal_cursor_column,
&mut self.command_message,
&mut app_state.ui.is_saved,
@@ -224,8 +261,6 @@ impl EventHandler {
grpc_client,
).await?;
self.key_sequence_tracker.reset();
return Ok((false, result));
},
@@ -257,6 +292,5 @@ impl EventHandler {
self.edit_mode_cooldown = false;
Ok((false, self.command_message.clone()))
}
}