diff options
author | 2020-07-10 01:12:26 -0400 | |
---|---|---|
committer | 2020-07-10 07:14:26 -0400 | |
commit | 855c0faa590e1bee3cfad257e36564ad078ed706 (patch) | |
tree | 5813127d8a6772b6921217875e6ad315dcb9b3b6 /native | |
parent | 46ce3a1f0004ddc527ba3de1ffe3dac3f41a06c3 (diff) | |
download | iced-855c0faa590e1bee3cfad257e36564ad078ed706.tar.gz iced-855c0faa590e1bee3cfad257e36564ad078ed706.tar.bz2 iced-855c0faa590e1bee3cfad257e36564ad078ed706.zip |
Fix panic on paste in TextInput after programmatic modification of contents
Diffstat (limited to 'native')
-rw-r--r-- | native/src/widget/text_input/editor.rs | 18 |
1 files changed, 12 insertions, 6 deletions
diff --git a/native/src/widget/text_input/editor.rs b/native/src/widget/text_input/editor.rs index c9b9795d..5cda4f1d 100644 --- a/native/src/widget/text_input/editor.rs +++ b/native/src/widget/text_input/editor.rs @@ -17,8 +17,10 @@ impl<'a> Editor<'a> { pub fn insert(&mut self, character: char) { match self.cursor.selection() { Some((left, right)) => { - self.cursor.move_left(self.value); - self.value.remove_many(left, right); + if left < self.value.len() { + self.cursor.move_left(self.value); + self.value.remove_many(left, right.min(self.value.len())); + } } _ => (), } @@ -32,8 +34,10 @@ impl<'a> Editor<'a> { match self.cursor.selection() { Some((left, right)) => { - self.cursor.move_left(self.value); - self.value.remove_many(left, right); + if left < self.value.len() { + self.cursor.move_left(self.value); + self.value.remove_many(left, right.min(self.value.len())); + } } _ => (), } @@ -46,8 +50,10 @@ impl<'a> Editor<'a> { pub fn backspace(&mut self) { match self.cursor.selection() { Some((start, end)) => { - self.cursor.move_left(self.value); - self.value.remove_many(start, end); + if start < self.value.len() { + self.cursor.move_left(self.value); + self.value.remove_many(start, end.min(self.value.len())); + } } None => { let start = self.cursor.start(self.value); |