Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix panic on paste in TextInput after programmatic modification of contents #445

Merged
merged 3 commits into from
Jul 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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