edit mode cursor persisting position

This commit is contained in:
filipriec
2025-02-20 13:51:09 +01:00
parent 22d2619a4a
commit 193cdfe802

View File

@@ -13,6 +13,7 @@ pub struct EventHandler {
pub is_edit_mode: bool,
pub edit_mode_cooldown: bool,
pub saved_cursor_column: usize,
pub ideal_cursor_column: usize,
}
impl EventHandler {
@@ -24,6 +25,7 @@ impl EventHandler {
is_edit_mode: false,
edit_mode_cooldown: false,
saved_cursor_column: 0,
ideal_cursor_column: 0,
}
}
@@ -312,16 +314,20 @@ impl EventHandler {
KeyCode::Left => {
// Move cursor left
form_state.current_cursor_pos = form_state.current_cursor_pos.saturating_sub(1);
// Update ideal position to match actual position
self.ideal_cursor_column = form_state.current_cursor_pos;
return Ok((false, "".to_string()));
}
},
KeyCode::Right => {
// Move cursor right
let current_input = form_state.get_current_input();
if form_state.current_cursor_pos < current_input.len() {
form_state.current_cursor_pos += 1;
// Update ideal position to match actual position
self.ideal_cursor_column = form_state.current_cursor_pos;
}
return Ok((false, "".to_string()));
}
},
KeyCode::Char(':') => {
self.command_mode = true;
self.command_input.clear();
@@ -339,22 +345,16 @@ impl EventHandler {
return Ok((false, self.command_message.clone()));
}
}
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();
// Get the new field's content and set cursor to saved column or end of field
// Get the new field's content and set cursor using ideal position
let current_input = form_state.get_current_input();
form_state.current_cursor_pos = self.saved_cursor_column.min(current_input.len());
form_state.current_cursor_pos = self.ideal_cursor_column.min(current_input.len());
},
// Fix Up key handling
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;
@@ -362,15 +362,13 @@ impl EventHandler {
form_state.current_field = form_state.current_field.saturating_sub(1);
}
// Get the new field's content and set cursor to saved column or end of field
// Get the new field's content and set cursor using ideal position
let current_input = form_state.get_current_input();
form_state.current_cursor_pos = self.saved_cursor_column.min(current_input.len());
form_state.current_cursor_pos = self.ideal_cursor_column.min(current_input.len());
},
// Fix Tab key handling
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
@@ -382,15 +380,13 @@ impl EventHandler {
form_state.current_field = (form_state.current_field + 1) % form_state.fields.len();
}
// Use saved column position
// Use ideal column position instead of saved_cursor_column
let current_input = form_state.get_current_input();
form_state.current_cursor_pos = self.saved_cursor_column.min(current_input.len());
form_state.current_cursor_pos = self.ideal_cursor_column.min(current_input.len());
},
// Fix BackTab key handling
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;
@@ -398,20 +394,18 @@ impl EventHandler {
form_state.current_field = form_state.current_field.saturating_sub(1);
}
// Use saved column position
// Use ideal column position instead of saved_cursor_column
let current_input = form_state.get_current_input();
form_state.current_cursor_pos = self.saved_cursor_column.min(current_input.len());
form_state.current_cursor_pos = self.ideal_cursor_column.min(current_input.len());
},
// Fix Enter key handling
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
// Use ideal column position instead of saved_cursor_column
let current_input = form_state.get_current_input();
form_state.current_cursor_pos = self.saved_cursor_column.min(current_input.len());
form_state.current_cursor_pos = self.ideal_cursor_column.min(current_input.len());
},
KeyCode::Char(c) => {
// Save cursor position before mutable borrow
@@ -428,11 +422,11 @@ impl EventHandler {
// Move cursor forward after updating the field
form_state.current_cursor_pos = cursor_pos + 1;
// Update ideal cursor position too
self.ideal_cursor_column = form_state.current_cursor_pos;
form_state.has_unsaved_changes = true; // Mark as unsaved
}
}
// Fix for the Backspace handler
},
KeyCode::Backspace => {
// Only delete if cursor is not at the beginning
if form_state.current_cursor_pos > 0 {
@@ -450,11 +444,12 @@ impl EventHandler {
// Update cursor position after modifying the field
form_state.current_cursor_pos = cursor_pos - 1;
// Update ideal cursor position too
self.ideal_cursor_column = form_state.current_cursor_pos;
form_state.has_unsaved_changes = true; // Mark as unsaved
}
}
}
},
// Fix for the Delete handler
KeyCode::Delete => {
// Save cursor position before mutable borrow