Create text_input::Editor to hold editing logic
This commit is contained in:
parent
28382a47d3
commit
6c47a40730
2 changed files with 103 additions and 55 deletions
|
|
@ -4,6 +4,7 @@
|
||||||
//!
|
//!
|
||||||
//! [`TextInput`]: struct.TextInput.html
|
//! [`TextInput`]: struct.TextInput.html
|
||||||
//! [`State`]: struct.State.html
|
//! [`State`]: struct.State.html
|
||||||
|
mod editor;
|
||||||
mod value;
|
mod value;
|
||||||
|
|
||||||
pub mod cursor;
|
pub mod cursor;
|
||||||
|
|
@ -11,6 +12,8 @@ pub mod cursor;
|
||||||
pub use cursor::Cursor;
|
pub use cursor::Cursor;
|
||||||
pub use value::Value;
|
pub use value::Value;
|
||||||
|
|
||||||
|
use editor::Editor;
|
||||||
|
|
||||||
use crate::{
|
use crate::{
|
||||||
input::{
|
input::{
|
||||||
keyboard,
|
keyboard,
|
||||||
|
|
@ -333,18 +336,12 @@ where
|
||||||
&& self.state.is_pasting.is_none()
|
&& self.state.is_pasting.is_none()
|
||||||
&& !c.is_control() =>
|
&& !c.is_control() =>
|
||||||
{
|
{
|
||||||
match self.state.cursor.selection() {
|
let mut editor =
|
||||||
Some((left, right)) => {
|
Editor::new(&mut self.value, &mut self.state.cursor);
|
||||||
self.value.remove_many(left, right);
|
|
||||||
self.state.cursor.move_left(&self.value);
|
|
||||||
}
|
|
||||||
_ => (),
|
|
||||||
}
|
|
||||||
|
|
||||||
self.value.insert(self.state.cursor.end(&self.value), c);
|
editor.insert(c);
|
||||||
self.state.cursor.move_right(&self.value);
|
|
||||||
|
|
||||||
let message = (self.on_change)(self.value.to_string());
|
let message = (self.on_change)(editor.contents());
|
||||||
messages.push(message);
|
messages.push(message);
|
||||||
}
|
}
|
||||||
Event::Keyboard(keyboard::Event::Input {
|
Event::Keyboard(keyboard::Event::Input {
|
||||||
|
|
@ -358,39 +355,21 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
keyboard::KeyCode::Backspace => {
|
keyboard::KeyCode::Backspace => {
|
||||||
match self.state.cursor.selection() {
|
let mut editor =
|
||||||
Some((start, end)) => {
|
Editor::new(&mut self.value, &mut self.state.cursor);
|
||||||
self.value.remove_many(start, end);
|
|
||||||
self.state.cursor.move_left(&self.value);
|
|
||||||
}
|
|
||||||
None => {
|
|
||||||
let start = self.state.cursor.start(&self.value);
|
|
||||||
|
|
||||||
if start > 0 {
|
editor.backspace();
|
||||||
self.state.cursor.move_left(&self.value);
|
|
||||||
|
|
||||||
let _ = self.value.remove(start - 1);
|
let message = (self.on_change)(editor.contents());
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
let message = (self.on_change)(self.value.to_string());
|
|
||||||
messages.push(message);
|
messages.push(message);
|
||||||
}
|
}
|
||||||
keyboard::KeyCode::Delete => {
|
keyboard::KeyCode::Delete => {
|
||||||
match self.state.cursor.selection() {
|
let mut editor =
|
||||||
Some((start, end)) => {
|
Editor::new(&mut self.value, &mut self.state.cursor);
|
||||||
self.value.remove_many(start, end);
|
|
||||||
self.state.cursor.move_left(&self.value);
|
|
||||||
}
|
|
||||||
None => {
|
|
||||||
let end = self.state.cursor.end(&self.value);
|
|
||||||
|
|
||||||
if end < self.value.len() {
|
editor.delete();
|
||||||
let _ = self.value.remove(end);
|
|
||||||
}
|
let message = (self.on_change)(editor.contents());
|
||||||
}
|
|
||||||
}
|
|
||||||
let message = (self.on_change)(self.value.to_string());
|
|
||||||
messages.push(message);
|
messages.push(message);
|
||||||
}
|
}
|
||||||
keyboard::KeyCode::Left => {
|
keyboard::KeyCode::Left => {
|
||||||
|
|
@ -462,28 +441,17 @@ where
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
match self.state.cursor.selection() {
|
let mut editor = Editor::new(
|
||||||
Some((left, right)) => {
|
&mut self.value,
|
||||||
self.value.remove_many(left, right);
|
&mut self.state.cursor,
|
||||||
self.state.cursor.move_left(&self.value);
|
|
||||||
}
|
|
||||||
_ => (),
|
|
||||||
}
|
|
||||||
|
|
||||||
self.value.insert_many(
|
|
||||||
self.state.cursor.end(&self.value),
|
|
||||||
content.clone(),
|
|
||||||
);
|
);
|
||||||
|
|
||||||
self.state.cursor.move_right_by_amount(
|
editor.paste(content.clone());
|
||||||
&self.value,
|
|
||||||
content.len(),
|
|
||||||
);
|
|
||||||
self.state.is_pasting = Some(content);
|
|
||||||
|
|
||||||
let message =
|
let message = (self.on_change)(editor.contents());
|
||||||
(self.on_change)(self.value.to_string());
|
|
||||||
messages.push(message);
|
messages.push(message);
|
||||||
|
|
||||||
|
self.state.is_pasting = Some(content);
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
self.state.is_pasting = None;
|
self.state.is_pasting = None;
|
||||||
|
|
|
||||||
80
native/src/widget/text_input/editor.rs
Normal file
80
native/src/widget/text_input/editor.rs
Normal file
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue