diff --git a/src/client/ui/handlers/event.rs b/src/client/ui/handlers/event.rs index 1941501..9670624 100644 --- a/src/client/ui/handlers/event.rs +++ b/src/client/ui/handlers/event.rs @@ -12,6 +12,7 @@ pub struct EventHandler { pub command_message: String, pub is_edit_mode: bool, pub edit_mode_cooldown: bool, + pub saved_cursor_column: usize, } impl EventHandler { @@ -22,6 +23,7 @@ impl EventHandler { command_message: String::new(), is_edit_mode: false, edit_mode_cooldown: false, + saved_cursor_column: 0, } } @@ -40,10 +42,8 @@ impl EventHandler { self.is_edit_mode = true; self.edit_mode_cooldown = true; self.command_message = "Edit mode".to_string(); - app_terminal.set_cursor_style(SetCursorStyle::BlinkingBar)?; // Add this line - // Initialize cursor position when entering edit mode - let current_input = form_state.get_current_input(); - form_state.current_cursor_pos = current_input.len(); + app_terminal.set_cursor_style(SetCursorStyle::BlinkingBar)?; + // Don't change cursor position - current_cursor_pos should be kept from read-only mode return Ok((false, self.command_message.clone())); } else if self.is_edit_mode && config.is_exit_edit_mode(key.code, key.modifiers) { // Prevent exiting edit mode if there are unsaved changes @@ -327,21 +327,6 @@ impl EventHandler { self.command_input.clear(); self.command_message.clear(); } - KeyCode::Tab => { - if key.modifiers.contains(KeyModifiers::SHIFT) { - if form_state.current_field == 0 { - // Wrap to the last field when at the top - form_state.current_field = form_state.fields.len() - 1; - } else { - form_state.current_field = form_state.current_field.saturating_sub(1); - } - } else { - form_state.current_field = (form_state.current_field + 1) % form_state.fields.len(); - } - // Reset cursor position to end of field when changing fields - let current_input = form_state.get_current_input(); - form_state.current_cursor_pos = current_input.len(); - } KeyCode::Esc => { if config.is_exit_edit_mode(key.code, key.modifiers) { if form_state.has_unsaved_changes { @@ -354,39 +339,79 @@ impl EventHandler { return Ok((false, self.command_message.clone())); } } - KeyCode::BackTab => { - if form_state.current_field == 0 { - // Wrap to the last field when at the top - form_state.current_field = form_state.fields.len() - 1; - } else { - form_state.current_field = form_state.current_field.saturating_sub(1); - } - // Reset cursor position to end of field when changing fields - let current_input = form_state.get_current_input(); - form_state.current_cursor_pos = current_input.len(); - }, + KeyCode::Down => { + // Save current horizontal position before changing fields + self.saved_cursor_column = form_state.current_cursor_pos; + form_state.current_field = (form_state.current_field + 1) % form_state.fields.len(); - // Reset cursor position to end of field when changing fields + + // Get the new field's content and set cursor to saved column or end of field let current_input = form_state.get_current_input(); - form_state.current_cursor_pos = current_input.len(); + form_state.current_cursor_pos = self.saved_cursor_column.min(current_input.len()); }, + KeyCode::Up => { + // Save current horizontal position before changing fields + self.saved_cursor_column = form_state.current_cursor_pos; + if form_state.current_field == 0 { // Wrap to the last field when at the top form_state.current_field = form_state.fields.len() - 1; } else { form_state.current_field = form_state.current_field.saturating_sub(1); } - // Reset cursor position to end of field when changing fields + + // Get the new field's content and set cursor to saved column or end of field let current_input = form_state.get_current_input(); - form_state.current_cursor_pos = current_input.len(); + form_state.current_cursor_pos = self.saved_cursor_column.min(current_input.len()); }, - KeyCode::Enter => { - form_state.current_field = (form_state.current_field + 1) % form_state.fields.len(); - // Reset cursor position to end of field when changing fields + + KeyCode::Tab => { + // Save current horizontal position before changing fields + self.saved_cursor_column = form_state.current_cursor_pos; + + if key.modifiers.contains(KeyModifiers::SHIFT) { + if form_state.current_field == 0 { + // Wrap to the last field when at the top + form_state.current_field = form_state.fields.len() - 1; + } else { + form_state.current_field = form_state.current_field.saturating_sub(1); + } + } else { + form_state.current_field = (form_state.current_field + 1) % form_state.fields.len(); + } + + // Use saved column position let current_input = form_state.get_current_input(); - form_state.current_cursor_pos = current_input.len(); + form_state.current_cursor_pos = self.saved_cursor_column.min(current_input.len()); + }, + + KeyCode::BackTab => { + // Save current horizontal position before changing fields + self.saved_cursor_column = form_state.current_cursor_pos; + + if form_state.current_field == 0 { + // Wrap to the last field when at the top + form_state.current_field = form_state.fields.len() - 1; + } else { + form_state.current_field = form_state.current_field.saturating_sub(1); + } + + // Use saved column position + let current_input = form_state.get_current_input(); + form_state.current_cursor_pos = self.saved_cursor_column.min(current_input.len()); + }, + + KeyCode::Enter => { + // Save current horizontal position before changing fields + self.saved_cursor_column = form_state.current_cursor_pos; + + form_state.current_field = (form_state.current_field + 1) % form_state.fields.len(); + + // Use saved column position + let current_input = form_state.get_current_input(); + form_state.current_cursor_pos = self.saved_cursor_column.min(current_input.len()); }, KeyCode::Char(c) => { // Save cursor position before mutable borrow