From 98009717917324085c358d3f4544d68194014aa4 Mon Sep 17 00:00:00 2001 From: presiyan-ivanov <15377841+presiyan-ivanov@users.noreply.github.com> Date: Sun, 19 Nov 2023 16:06:48 +0200 Subject: [PATCH] Fix char out of bounds error in text_input when text is modified externally (#185) --- src/views/text_input.rs | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/src/views/text_input.rs b/src/views/text_input.rs index 85e6758f..164e891f 100644 --- a/src/views/text_input.rs +++ b/src/views/text_input.rs @@ -395,10 +395,6 @@ impl TextInput { AttrsList::new(attrs) } - fn set_cursor_glyph_idx(&mut self, new_cursor_x: usize) { - self.cursor_glyph_idx = new_cursor_x; - } - fn select_all(&mut self, cx: &mut EventCx) { let text_node = self.text_node.unwrap(); let node_layout = *cx.app_state.taffy.layout(text_node).unwrap(); @@ -735,11 +731,18 @@ impl View for TextInput { _id_path: Option<&[Id]>, event: Event, ) -> EventPropagation { + let buff_len = self.buffer.with_untracked(|buff| buff.len()); + // Workaround for cursor going out of bounds when text buffer is modified externally + // TODO: find a better way to handle this + if self.cursor_glyph_idx > buff_len { + self.cursor_glyph_idx = buff_len; + } + let is_handled = match &event { Event::PointerDown(event) => { if !self.is_focused { // Just gained focus - move cursor to buff end - self.set_cursor_glyph_idx(self.buffer.with_untracked(|buff| buff.len())); + self.cursor_glyph_idx = self.buffer.with_untracked(|buff| buff.len()); } else { // Already focused - move cursor to click pos let layout = cx.get_layout(self.id()).unwrap();