diff options
author | 2023-03-09 19:05:38 +0100 | |
---|---|---|
committer | 2023-03-09 19:05:38 +0100 | |
commit | caf2836b1b15bff6e8a2ea72441d67f297eb8707 (patch) | |
tree | 0ffa0d1d604780999892b88de85ee93e3ed7d539 /widget/src/text_input/editor.rs | |
parent | 11b2c3bbe31a43e73a61b9bd9f022233f302ae27 (diff) | |
parent | 424ac8177309440bbd8efe0dd9f7622cb10807ce (diff) | |
download | iced-caf2836b1b15bff6e8a2ea72441d67f297eb8707.tar.gz iced-caf2836b1b15bff6e8a2ea72441d67f297eb8707.tar.bz2 iced-caf2836b1b15bff6e8a2ea72441d67f297eb8707.zip |
Merge pull request #1748 from iced-rs/feature/software-renderer
Software renderer, runtime renderer fallback, and core consolidation
Diffstat (limited to 'widget/src/text_input/editor.rs')
-rw-r--r-- | widget/src/text_input/editor.rs | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/widget/src/text_input/editor.rs b/widget/src/text_input/editor.rs new file mode 100644 index 00000000..f1fd641f --- /dev/null +++ b/widget/src/text_input/editor.rs @@ -0,0 +1,70 @@ +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) { + if let Some((left, right)) = self.cursor.selection(self.value) { + self.cursor.move_left(self.value); + self.value.remove_many(left, right); + } + + 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(); + if let Some((left, right)) = self.cursor.selection(self.value) { + self.cursor.move_left(self.value); + self.value.remove_many(left, right); + } + + 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(self.value) { + Some((start, end)) => { + self.cursor.move_left(self.value); + self.value.remove_many(start, end); + } + None => { + let start = self.cursor.start(self.value); + + if start > 0 { + self.cursor.move_left(self.value); + self.value.remove(start - 1); + } + } + } + } + + pub fn delete(&mut self) { + match self.cursor.selection(self.value) { + Some(_) => { + self.backspace(); + } + None => { + let end = self.cursor.end(self.value); + + if end < self.value.len() { + self.value.remove(end); + } + } + } + } +} |