From 51a0e99097f9ecb63eeb7f2ea7c38089977eb4d0 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 31 Oct 2019 03:50:40 +0100 Subject: Implement cursor movement in `TextInput` --- core/src/widget/text_input.rs | 92 +++++++++++++++++++++++++++++++++++++++---- 1 file changed, 85 insertions(+), 7 deletions(-) (limited to 'core/src/widget') diff --git a/core/src/widget/text_input.rs b/core/src/widget/text_input.rs index e0f0744b..95064ab3 100644 --- a/core/src/widget/text_input.rs +++ b/core/src/widget/text_input.rs @@ -1,9 +1,11 @@ use crate::Length; +use std::ops::{Index, RangeTo}; + pub struct TextInput<'a, Message> { pub state: &'a mut State, pub placeholder: String, - pub value: String, + pub value: Value, pub width: Length, pub max_width: Length, pub padding: u16, @@ -12,11 +14,6 @@ pub struct TextInput<'a, Message> { pub on_submit: Option, } -#[derive(Debug, Default)] -pub struct State { - pub is_focused: bool, -} - impl<'a, Message> TextInput<'a, Message> { pub fn new( state: &'a mut State, @@ -30,7 +27,7 @@ impl<'a, Message> TextInput<'a, Message> { Self { state, placeholder: String::from(placeholder), - value: String::from(value), + value: Value::new(value), width: Length::Fill, max_width: Length::Shrink, padding: 0, @@ -84,3 +81,84 @@ where f.debug_struct("TextInput").finish() } } + +#[derive(Debug, Default)] +pub struct State { + pub is_focused: bool, + cursor_position: usize, +} + +impl State { + pub fn new() -> Self { + Self::default() + } + + pub fn focused() -> Self { + Self { + is_focused: true, + ..Self::default() + } + } + + pub fn move_cursor_right(&mut self, value: &Value) { + let current = self.cursor_position(value); + + if current < value.len() { + self.cursor_position = current + 1; + } + } + + pub fn move_cursor_left(&mut self, value: &Value) { + let current = self.cursor_position(value); + + if current > 0 { + self.cursor_position = current - 1; + } + } + + pub fn cursor_position(&self, value: &Value) -> usize { + self.cursor_position.min(value.len()) + } +} + +pub struct Value(Vec); + +impl Value { + pub fn new(string: &str) -> Self { + Self(string.chars().collect()) + } + + pub fn len(&self) -> usize { + self.0.len() + } + + pub fn until(&self, index: usize) -> Self { + Self(self.0[..index].iter().cloned().collect()) + } + + pub fn to_string(&self) -> String { + let mut string = String::new(); + + for c in self.0.iter() { + string.push(*c); + } + + string + } + + pub fn insert(&mut self, index: usize, c: char) { + self.0.insert(index, c); + } + + pub fn remove(&mut self, index: usize) { + self.0.remove(index); + } +} + +impl Index> for Value { + type Output = [char]; + + fn index(&self, index: RangeTo) -> &[char] { + &self.0[index] + } +} -- cgit