Skip to content

Commit

Permalink
Merge pull request #445 from mtkennerly/bugfix/paste-panic
Browse files Browse the repository at this point in the history
Fix panic on paste in TextInput after programmatic modification of contents
  • Loading branch information
hecrj authored Jul 10, 2020
2 parents 46ce3a1 + 4314ce3 commit 62ec03a
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 8 deletions.
4 changes: 2 additions & 2 deletions native/src/widget/text_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,7 +329,7 @@ where
}
keyboard::KeyCode::Backspace => {
if platform::is_jump_modifier_pressed(modifiers)
&& self.state.cursor.selection().is_none()
&& self.state.cursor.selection(&self.value).is_none()
{
if self.is_secure {
let cursor_pos = self.state.cursor.end(&self.value);
Expand All @@ -349,7 +349,7 @@ where
}
keyboard::KeyCode::Delete => {
if platform::is_jump_modifier_pressed(modifiers)
&& self.state.cursor.selection().is_none()
&& self.state.cursor.selection(&self.value).is_none()
{
if self.is_secure {
let cursor_pos = self.state.cursor.end(&self.value);
Expand Down
4 changes: 2 additions & 2 deletions native/src/widget/text_input/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,8 +166,8 @@ impl Cursor {
end.min(value.len())
}

pub(crate) fn selection(&self) -> Option<(usize, usize)> {
match self.state {
pub(crate) fn selection(&self, value: &Value) -> Option<(usize, usize)> {
match self.state(value) {
State::Selection { start, end } => {
Some((start.min(end), start.max(end)))
}
Expand Down
8 changes: 4 additions & 4 deletions native/src/widget/text_input/editor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ impl<'a> Editor<'a> {
}

pub fn insert(&mut self, character: char) {
match self.cursor.selection() {
match self.cursor.selection(self.value) {
Some((left, right)) => {
self.cursor.move_left(self.value);
self.value.remove_many(left, right);
Expand All @@ -30,7 +30,7 @@ impl<'a> Editor<'a> {
pub fn paste(&mut self, content: Value) {
let length = content.len();

match self.cursor.selection() {
match self.cursor.selection(self.value) {
Some((left, right)) => {
self.cursor.move_left(self.value);
self.value.remove_many(left, right);
Expand All @@ -44,7 +44,7 @@ impl<'a> Editor<'a> {
}

pub fn backspace(&mut self) {
match self.cursor.selection() {
match self.cursor.selection(self.value) {
Some((start, end)) => {
self.cursor.move_left(self.value);
self.value.remove_many(start, end);
Expand All @@ -61,7 +61,7 @@ impl<'a> Editor<'a> {
}

pub fn delete(&mut self) {
match self.cursor.selection() {
match self.cursor.selection(self.value) {
Some(_) => {
self.backspace();
}
Expand Down

0 comments on commit 62ec03a

Please sign in to comment.