summaryrefslogtreecommitdiffstats
path: root/native/src/widget/text_input
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
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')
-rw-r--r--native/src/widget/text_input/editor.rs80
1 files changed, 80 insertions, 0 deletions
diff --git a/native/src/widget/text_input/editor.rs b/native/src/widget/text_input/editor.rs
new file mode 100644
index 00000000..de235d52
--- /dev/null
+++ b/native/src/widget/text_input/editor.rs
@@ -0,0 +1,80 @@
+use crate::text_input::{Cursor, Value};
+
+pub struct Editor<'a> {
+ value: &'a mut Value,
+ cursor: &'a mut Cursor,
+}
+
+impl<'a> Editor<'a> {
+ pub fn new(value: &'a mut Value, cursor: &'a mut Cursor) -> Editor<'a> {
+ Editor { value, cursor }
+ }
+
+ pub fn contents(&self) -> String {
+ self.value.to_string()
+ }
+
+ pub fn insert(&mut self, character: char) {
+ match self.cursor.selection() {
+ Some((left, right)) => {
+ self.value.remove_many(left, right);
+ self.cursor.move_left(&self.value);
+ }
+ _ => (),
+ }
+
+ self.value.insert(self.cursor.end(&self.value), character);
+ self.cursor.move_right(&self.value);
+ }
+
+ pub fn paste(&mut self, content: Value) {
+ let length = content.len();
+
+ match self.cursor.selection() {
+ Some((left, right)) => {
+ self.value.remove_many(left, right);
+ self.cursor.move_left(&self.value);
+ }
+ _ => (),
+ }
+
+ self.value
+ .insert_many(self.cursor.end(&self.value), content);
+
+ self.cursor.move_right_by_amount(&self.value, length);
+ }
+
+ pub fn backspace(&mut self) {
+ match self.cursor.selection() {
+ Some((start, end)) => {
+ self.value.remove_many(start, end);
+ self.cursor.move_left(&self.value);
+ }
+ None => {
+ let start = self.cursor.start(&self.value);
+
+ if start > 0 {
+ self.cursor.move_left(&self.value);
+
+ let _ = self.value.remove(start - 1);
+ }
+ }
+ }
+ }
+
+ pub fn delete(&mut self) {
+ match self.cursor.selection() {
+ Some((start, end)) => {
+ self.value.remove_many(start, end);
+ self.cursor.move_left(&self.value);
+ }
+ None => {
+ let end = self.cursor.end(&self.value);
+
+ if end < self.value.len() {
+ let _ = self.value.remove(end);
+ }
+ }
+ }
+ }
+}