From 22138671b4af21b58a4273a5aa2f7b4c3247c393 Mon Sep 17 00:00:00 2001 From: Ashley Wulber Date: Mon, 29 Jul 2024 16:41:15 -0400 Subject: [PATCH] fix: keep the cursor at the end if it previously when diffing --- src/widget/text_input/input.rs | 30 +++++++++++++++++++++--------- 1 file changed, 21 insertions(+), 9 deletions(-) diff --git a/src/widget/text_input/input.rs b/src/widget/text_input/input.rs index b7fbe9d..fad9a02 100644 --- a/src/widget/text_input/input.rs +++ b/src/widget/text_input/input.rs @@ -542,16 +542,15 @@ where state.is_pasting = None; state.dragging_state = None; } - + let old_value = state + .value + .buffer() + .lines + .iter() + .map(|l| l.text()) + .collect::(); if state.is_secure != self.is_secure - || state - .value - .buffer() - .lines - .iter() - .map(|l| l.text()) - .collect::() - != self.value.to_string() + || old_value != self.value.to_string() || state .label .buffer() @@ -585,6 +584,19 @@ where }); } + // if the previous state was at the end of the text, keep it there + let old_value = Value::new(&old_value); + if state.is_focused.is_some() { + match state.cursor.state(&old_value) { + cursor::State::Index(index) => { + if index == old_value.len() { + state.cursor.move_to(self.value.len()); + } + } + _ => {} + }; + } + let mut children: Vec<_> = self .leading_icon .iter_mut()