summaryrefslogtreecommitdiffstats
path: root/native/src/widget/text_input.rs
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón Jiménez <hector0193@gmail.com>2020-03-24 20:51:22 +0100
committerLibravatar Héctor Ramón Jiménez <hector0193@gmail.com>2020-03-24 20:51:22 +0100
commit6c47a40730938fb59aa7fb738b460dd37f756766 (patch)
tree6d379676d180a53458ebc54179a47a7b07fae279 /native/src/widget/text_input.rs
parent28382a47d3abd4f79064b610f1a2eca478a08595 (diff)
downloadiced-6c47a40730938fb59aa7fb738b460dd37f756766.tar.gz
iced-6c47a40730938fb59aa7fb738b460dd37f756766.tar.bz2
iced-6c47a40730938fb59aa7fb738b460dd37f756766.zip
Create `text_input::Editor` to hold editing logic
Diffstat (limited to 'native/src/widget/text_input.rs')
-rw-r--r--native/src/widget/text_input.rs78
1 files changed, 23 insertions, 55 deletions
diff --git a/native/src/widget/text_input.rs b/native/src/widget/text_input.rs
index 7e77b76a..9fc7c6e6 100644
--- a/native/src/widget/text_input.rs
+++ b/native/src/widget/text_input.rs
@@ -4,6 +4,7 @@
//!
//! [`TextInput`]: struct.TextInput.html
//! [`State`]: struct.State.html
+mod editor;
mod value;
pub mod cursor;
@@ -11,6 +12,8 @@ pub mod cursor;
pub use cursor::Cursor;
pub use value::Value;
+use editor::Editor;
+
use crate::{
input::{
keyboard,
@@ -333,18 +336,12 @@ where
&& self.state.is_pasting.is_none()
&& !c.is_control() =>
{
- match self.state.cursor.selection() {
- Some((left, right)) => {
- self.value.remove_many(left, right);
- self.state.cursor.move_left(&self.value);
- }
- _ => (),
- }
+ let mut editor =
+ Editor::new(&mut self.value, &mut self.state.cursor);
- self.value.insert(self.state.cursor.end(&self.value), c);
- self.state.cursor.move_right(&self.value);
+ editor.insert(c);
- let message = (self.on_change)(self.value.to_string());
+ let message = (self.on_change)(editor.contents());
messages.push(message);
}
Event::Keyboard(keyboard::Event::Input {
@@ -358,39 +355,21 @@ where
}
}
keyboard::KeyCode::Backspace => {
- match self.state.cursor.selection() {
- Some((start, end)) => {
- self.value.remove_many(start, end);
- self.state.cursor.move_left(&self.value);
- }
- None => {
- let start = self.state.cursor.start(&self.value);
+ let mut editor =
+ Editor::new(&mut self.value, &mut self.state.cursor);
- if start > 0 {
- self.state.cursor.move_left(&self.value);
+ editor.backspace();
- let _ = self.value.remove(start - 1);
- }
- }
- }
- let message = (self.on_change)(self.value.to_string());
+ let message = (self.on_change)(editor.contents());
messages.push(message);
}
keyboard::KeyCode::Delete => {
- match self.state.cursor.selection() {
- Some((start, end)) => {
- self.value.remove_many(start, end);
- self.state.cursor.move_left(&self.value);
- }
- None => {
- let end = self.state.cursor.end(&self.value);
+ let mut editor =
+ Editor::new(&mut self.value, &mut self.state.cursor);
- if end < self.value.len() {
- let _ = self.value.remove(end);
- }
- }
- }
- let message = (self.on_change)(self.value.to_string());
+ editor.delete();
+
+ let message = (self.on_change)(editor.contents());
messages.push(message);
}
keyboard::KeyCode::Left => {
@@ -462,28 +441,17 @@ where
}
};
- match self.state.cursor.selection() {
- Some((left, right)) => {
- self.value.remove_many(left, right);
- self.state.cursor.move_left(&self.value);
- }
- _ => (),
- }
-
- self.value.insert_many(
- self.state.cursor.end(&self.value),
- content.clone(),
+ let mut editor = Editor::new(
+ &mut self.value,
+ &mut self.state.cursor,
);
- self.state.cursor.move_right_by_amount(
- &self.value,
- content.len(),
- );
- self.state.is_pasting = Some(content);
+ editor.paste(content.clone());
- let message =
- (self.on_change)(self.value.to_string());
+ let message = (self.on_change)(editor.contents());
messages.push(message);
+
+ self.state.is_pasting = Some(content);
}
} else {
self.state.is_pasting = None;