working inputting characters
This commit is contained in:
@@ -84,7 +84,7 @@ pub async fn execute_edit_action<S: CanvasState>(
|
||||
return Ok("Error: insert_char called without a char key."
|
||||
.to_string());
|
||||
}
|
||||
Ok("".to_string())
|
||||
Ok("working?".to_string())
|
||||
}
|
||||
|
||||
"delete_char_backward" => {
|
||||
|
||||
@@ -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()))
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -12,6 +12,7 @@ pub struct AuthState {
|
||||
pub auth_token: Option<String>,
|
||||
pub user_id: Option<String>,
|
||||
pub role: Option<String>,
|
||||
pub has_unsaved_changes: bool,
|
||||
}
|
||||
|
||||
impl AuthState {
|
||||
@@ -26,6 +27,7 @@ impl AuthState {
|
||||
auth_token: None,
|
||||
user_id: None,
|
||||
role: None,
|
||||
has_unsaved_changes: false,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -36,12 +38,16 @@ impl CanvasState for AuthState {
|
||||
}
|
||||
|
||||
fn current_cursor_pos(&self) -> usize {
|
||||
self.current_cursor_pos
|
||||
let len = match self.current_field {
|
||||
0 => self.username.len(),
|
||||
1 => self.password.len(),
|
||||
_ => 0,
|
||||
};
|
||||
self.current_cursor_pos.min(len)
|
||||
}
|
||||
|
||||
fn has_unsaved_changes(&self) -> bool {
|
||||
// Auth form doesn't need unsaved changes tracking
|
||||
false
|
||||
self.has_unsaved_changes
|
||||
}
|
||||
|
||||
fn inputs(&self) -> Vec<&String> {
|
||||
@@ -52,7 +58,7 @@ impl CanvasState for AuthState {
|
||||
match self.current_field {
|
||||
0 => &self.username,
|
||||
1 => &self.password,
|
||||
_ => "", // Return empty string for invalid index instead of panicking
|
||||
_ => "",
|
||||
}
|
||||
}
|
||||
|
||||
@@ -68,20 +74,31 @@ impl CanvasState for AuthState {
|
||||
vec!["Username/Email", "Password"]
|
||||
}
|
||||
|
||||
// --- Implement the setter methods ---
|
||||
fn set_current_field(&mut self, index: usize) {
|
||||
// AuthState only has 2 fields (0 and 1)
|
||||
if index < 2 {
|
||||
if index < 2 { // AuthState only has 2 fields
|
||||
self.current_field = index;
|
||||
// IMPORTANT: Clamp cursor position to the length of the NEW field
|
||||
let len = match self.current_field {
|
||||
0 => self.username.len(),
|
||||
1 => self.password.len(),
|
||||
_ => 0,
|
||||
};
|
||||
self.current_cursor_pos = self.current_cursor_pos.min(len);
|
||||
}
|
||||
}
|
||||
|
||||
fn set_current_cursor_pos(&mut self, pos: usize) {
|
||||
// Optional: Add validation based on current input length if needed
|
||||
self.current_cursor_pos = pos;
|
||||
let len = match self.current_field {
|
||||
0 => self.username.len(),
|
||||
1 => self.password.len(),
|
||||
_ => 0,
|
||||
};
|
||||
// Ensure stored position is always valid
|
||||
self.current_cursor_pos = pos.min(len);
|
||||
}
|
||||
|
||||
fn set_has_unsaved_changes(&mut self, _changed: bool) {
|
||||
// Auth form doesn't track unsaved changes, so do nothing
|
||||
fn set_has_unsaved_changes(&mut self, changed: bool) {
|
||||
// Allow the generic handler to signal changes
|
||||
self.has_unsaved_changes = changed;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,6 +11,7 @@ use crate::config::binds::config::Config;
|
||||
use crate::ui::handlers::render::render_ui;
|
||||
use crate::state::pages::form::FormState;
|
||||
use crate::state::pages::auth::AuthState;
|
||||
use crate::state::canvas_state::CanvasState;
|
||||
use crate::modes::handlers::event::EventHandler;
|
||||
use crate::state::state::AppState;
|
||||
|
||||
@@ -18,10 +19,10 @@ pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
|
||||
let config = Config::load()?;
|
||||
let mut terminal = TerminalCore::new()?;
|
||||
let mut grpc_client = GrpcClient::new().await?;
|
||||
let auth_client = AuthClient::new().await?;
|
||||
// let auth_client = AuthClient::new().await?; // AuthClient is now inside EventHandler
|
||||
let mut command_handler = CommandHandler::new();
|
||||
let theme = Theme::from_str(&config.colors.theme);
|
||||
let mut auth_state = AuthState::default();
|
||||
let mut auth_state = AuthState::default(); // The single source of truth for AuthState
|
||||
|
||||
// Initialize app_state first
|
||||
let mut app_state = AppState::new()?;
|
||||
@@ -32,7 +33,7 @@ pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
|
||||
// Initialize FormState with dynamic fields
|
||||
let mut form_state = FormState::new(column_names);
|
||||
|
||||
// The rest of your UI initialization remains the same
|
||||
// Initialize EventHandler (which now contains AuthClient)
|
||||
let mut event_handler = EventHandler::new().await?;
|
||||
let event_reader = EventReader::new();
|
||||
|
||||
@@ -43,13 +44,16 @@ pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
|
||||
loop {
|
||||
UiService::update_adresar_count(&mut grpc_client, &mut app_state).await?;
|
||||
|
||||
// Determine edit mode based on EventHandler state
|
||||
let is_edit_mode = event_handler.is_edit_mode;
|
||||
|
||||
terminal.draw(|f| {
|
||||
render_ui(
|
||||
f,
|
||||
&mut form_state,
|
||||
&mut auth_state,
|
||||
&mut auth_state, // Pass the single AuthState instance
|
||||
&theme,
|
||||
event_handler.is_edit_mode,
|
||||
is_edit_mode, // Use determined edit mode
|
||||
app_state.total_count,
|
||||
app_state.current_position,
|
||||
&app_state.current_dir,
|
||||
@@ -71,6 +75,7 @@ pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
|
||||
&mut grpc_client,
|
||||
&mut command_handler,
|
||||
&mut form_state,
|
||||
&mut auth_state, // Pass the single AuthState instance here too
|
||||
&mut app_state,
|
||||
total_count,
|
||||
&mut current_position,
|
||||
@@ -78,7 +83,8 @@ pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
|
||||
|
||||
app_state.current_position = current_position;
|
||||
|
||||
// Handle position changes and update form state
|
||||
// Handle position changes and update form state (Only when form is shown)
|
||||
if app_state.ui.show_form { // Added check
|
||||
if !event_handler.is_edit_mode {
|
||||
let current_input = form_state.get_current_input();
|
||||
let max_cursor_pos = if !current_input.is_empty() {
|
||||
@@ -98,12 +104,12 @@ pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
|
||||
form_state.current_field = 0;
|
||||
} else if app_state.current_position >= 1 && app_state.current_position <= total_count {
|
||||
// Existing entry - load data
|
||||
let current_position = app_state.current_position;
|
||||
let message = UiService::load_adresar_by_position(
|
||||
let current_position_to_load = app_state.current_position; // Use a copy
|
||||
let load_message = UiService::load_adresar_by_position(
|
||||
&mut grpc_client,
|
||||
&mut app_state,
|
||||
&mut app_state, // Pass app_state mutably if needed by the service
|
||||
&mut form_state,
|
||||
current_position
|
||||
current_position_to_load
|
||||
).await?;
|
||||
|
||||
let current_input = form_state.get_current_input();
|
||||
@@ -113,14 +119,38 @@ pub async fn run_ui() -> Result<(), Box<dyn std::error::Error>> {
|
||||
current_input.len()
|
||||
};
|
||||
form_state.current_cursor_pos = event_handler.ideal_cursor_column.min(max_cursor_pos);
|
||||
event_handler.command_message = message;
|
||||
// Don't overwrite message from handle_event if load_message is simple success
|
||||
if !load_message.starts_with("Loaded entry") || message.is_empty() {
|
||||
event_handler.command_message = load_message;
|
||||
}
|
||||
} else {
|
||||
// Invalid position - reset to first entry
|
||||
app_state.current_position = 1;
|
||||
// Invalid position (e.g., 0) - reset to first entry or new entry mode
|
||||
app_state.current_position = 1.min(total_count + 1); // Go to 1 or new entry if empty
|
||||
if app_state.current_position > total_count {
|
||||
form_state.reset_to_empty();
|
||||
form_state.current_field = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if app_state.ui.show_login {
|
||||
// Handle cursor updates for AuthState if needed, similar to FormState
|
||||
if !event_handler.is_edit_mode {
|
||||
let current_input = auth_state.get_current_input();
|
||||
let max_cursor_pos = if !current_input.is_empty() {
|
||||
current_input.len() - 1
|
||||
} else {
|
||||
0
|
||||
};
|
||||
auth_state.current_cursor_pos = event_handler.ideal_cursor_column.min(max_cursor_pos);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
// Only update command message if handle_event provided one
|
||||
if !message.is_empty() {
|
||||
event_handler.command_message = message;
|
||||
}
|
||||
|
||||
if should_exit {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user