From 9b35d232880929701fe25e34e94bb63cf83d5d28 Mon Sep 17 00:00:00 2001 From: Samuel Guerra Date: Wed, 31 May 2023 01:16:08 -0300 Subject: [PATCH] Fixed caret not in char boundary panic. --- TODO/_current.md | 1 + zero-ui-core/src/text/segmenting.rs | 12 +++++++++++- 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/TODO/_current.md b/TODO/_current.md index e9d483e74..b38a5ba76 100644 --- a/TODO/_current.md +++ b/TODO/_current.md @@ -4,6 +4,7 @@ - Caret vertical position incorrect when not aligned to the top. - `ShapedSegment::rect` return wrong value. - `ShapedLine::rect` return wrong value for mid lines? + - After TextInput size increases to fit text caret cannot be moved with arrows. - Caret stops blinking while moving with cursor, not resetting timer? - Caret animation does not start visible (on focus). diff --git a/zero-ui-core/src/text/segmenting.rs b/zero-ui-core/src/text/segmenting.rs index d9472bb4b..a36892ef8 100644 --- a/zero-ui-core/src/text/segmenting.rs +++ b/zero-ui-core/src/text/segmenting.rs @@ -448,7 +448,17 @@ impl SegmentedText { .rev(); iter.next().unwrap_or(0) } else { - let s = &self.text.as_str()[..=from]; + let s = self.text.as_str(); + + // from + 1_char, so that the `from` is the first yield in reverse if it is a valid grapheme boundary + let inclusive_from = s[from..] + .char_indices() + .skip(1) + .next() + .map(|(b, _)| from + b) + .unwrap_or_else(|| s.len()); + + let s = &self.text.as_str()[..inclusive_from]; let mut iter = unicode_segmentation::UnicodeSegmentation::grapheme_indices(s, true) .map(|(i, _)| i) .rev();