summaryrefslogtreecommitdiffstats
path: root/native
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón Jiménez <hector0193@gmail.com>2020-07-10 23:59:49 +0200
committerLibravatar Héctor Ramón Jiménez <hector0193@gmail.com>2020-07-11 00:00:14 +0200
commita1210c9dae3ff7cf1d7afa08eadc76b4d7aaa7b9 (patch)
treef10e89002dd39db548edf61b0c8bf697a3c1fecd /native
parent855c0faa590e1bee3cfad257e36564ad078ed706 (diff)
downloadiced-a1210c9dae3ff7cf1d7afa08eadc76b4d7aaa7b9.tar.gz
iced-a1210c9dae3ff7cf1d7afa08eadc76b4d7aaa7b9.tar.bz2
iced-a1210c9dae3ff7cf1d7afa08eadc76b4d7aaa7b9.zip
Improve safety of `Cursor::selection`
Diffstat (limited to 'native')
-rw-r--r--native/src/widget/text_input.rs4
-rw-r--r--native/src/widget/text_input/cursor.rs4
-rw-r--r--native/src/widget/text_input/editor.rs26
3 files changed, 14 insertions, 20 deletions
diff --git a/native/src/widget/text_input.rs b/native/src/widget/text_input.rs
index 3f415101..230fe7dc 100644
--- a/native/src/widget/text_input.rs
+++ b/native/src/widget/text_input.rs
@@ -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);
@@ -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);
diff --git a/native/src/widget/text_input/cursor.rs b/native/src/widget/text_input/cursor.rs
index 16e7a01b..aa03bb74 100644
--- a/native/src/widget/text_input/cursor.rs
+++ b/native/src/widget/text_input/cursor.rs
@@ -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)))
}
diff --git a/native/src/widget/text_input/editor.rs b/native/src/widget/text_input/editor.rs
index 5cda4f1d..b836f105 100644
--- a/native/src/widget/text_input/editor.rs
+++ b/native/src/widget/text_input/editor.rs
@@ -15,12 +15,10 @@ impl<'a> Editor<'a> {
}
pub fn insert(&mut self, character: char) {
- match self.cursor.selection() {
+ match self.cursor.selection(self.value) {
Some((left, right)) => {
- if left < self.value.len() {
- self.cursor.move_left(self.value);
- self.value.remove_many(left, right.min(self.value.len()));
- }
+ self.cursor.move_left(self.value);
+ self.value.remove_many(left, right.min(self.value.len()));
}
_ => (),
}
@@ -32,12 +30,10 @@ 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)) => {
- if left < self.value.len() {
- self.cursor.move_left(self.value);
- self.value.remove_many(left, right.min(self.value.len()));
- }
+ self.cursor.move_left(self.value);
+ self.value.remove_many(left, right.min(self.value.len()));
}
_ => (),
}
@@ -48,12 +44,10 @@ impl<'a> Editor<'a> {
}
pub fn backspace(&mut self) {
- match self.cursor.selection() {
+ match self.cursor.selection(self.value) {
Some((start, end)) => {
- if start < self.value.len() {
- self.cursor.move_left(self.value);
- self.value.remove_many(start, end.min(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);
@@ -67,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();
}