diff options
author | 2019-12-08 06:53:54 +0100 | |
---|---|---|
committer | 2019-12-08 06:53:54 +0100 | |
commit | 9b84b6e40336543380312aa1b2b1091791ec25cd (patch) | |
tree | c14ed67cfc38e1a22617716c361e4684a64e7fac /native/src | |
parent | 48145ba51e045f8b0b4788f3a75d20b9d9b7e6ad (diff) | |
parent | f942fc3b68ecbbe136c54922109c7e2e4732735b (diff) | |
download | iced-9b84b6e40336543380312aa1b2b1091791ec25cd.tar.gz iced-9b84b6e40336543380312aa1b2b1091791ec25cd.tar.bz2 iced-9b84b6e40336543380312aa1b2b1091791ec25cd.zip |
Merge branch 'master' into feature/event-subscriptions
Diffstat (limited to 'native/src')
-rw-r--r-- | native/src/input/keyboard.rs | 2 | ||||
-rw-r--r-- | native/src/input/keyboard/event.rs | 7 | ||||
-rw-r--r-- | native/src/input/keyboard/modifiers_state.rs | 15 | ||||
-rw-r--r-- | native/src/renderer/null.rs | 4 | ||||
-rw-r--r-- | native/src/widget/text_input.rs | 283 |
5 files changed, 277 insertions, 34 deletions
diff --git a/native/src/input/keyboard.rs b/native/src/input/keyboard.rs index 57c24484..432e75ba 100644 --- a/native/src/input/keyboard.rs +++ b/native/src/input/keyboard.rs @@ -1,6 +1,8 @@ //! Build keyboard events. mod event; mod key_code; +mod modifiers_state; pub use event::Event; pub use key_code::KeyCode; +pub use modifiers_state::ModifiersState; diff --git a/native/src/input/keyboard/event.rs b/native/src/input/keyboard/event.rs index 8118f112..862f30c4 100644 --- a/native/src/input/keyboard/event.rs +++ b/native/src/input/keyboard/event.rs @@ -1,13 +1,13 @@ -use super::KeyCode; +use super::{KeyCode, ModifiersState}; use crate::input::ButtonState; -#[derive(Debug, Clone, Copy, PartialEq)] /// A keyboard event. /// /// _**Note:** This type is largely incomplete! If you need to track /// additional events, feel free to [open an issue] and share your use case!_ /// /// [open an issue]: https://github.com/hecrj/iced/issues +#[derive(Debug, Clone, Copy, PartialEq)] pub enum Event { /// A keyboard key was pressed or released. Input { @@ -16,6 +16,9 @@ pub enum Event { /// The key identifier key_code: KeyCode, + + /// The state of the modifier keys + modifiers: ModifiersState, }, /// A unicode character was received. diff --git a/native/src/input/keyboard/modifiers_state.rs b/native/src/input/keyboard/modifiers_state.rs new file mode 100644 index 00000000..4e3794b3 --- /dev/null +++ b/native/src/input/keyboard/modifiers_state.rs @@ -0,0 +1,15 @@ +/// The current state of the keyboard modifiers. +#[derive(Debug, Clone, Copy, PartialEq)] +pub struct ModifiersState { + /// Whether a shift key is pressed + pub shift: bool, + + /// Whether a control key is pressed + pub control: bool, + + /// Whether an alt key is pressed + pub alt: bool, + + /// Whether a logo key is pressed (e.g. windows key, command key...) + pub logo: bool, +} diff --git a/native/src/renderer/null.rs b/native/src/renderer/null.rs index da0e5159..02f033a5 100644 --- a/native/src/renderer/null.rs +++ b/native/src/renderer/null.rs @@ -89,6 +89,10 @@ impl text_input::Renderer for Null { 20 } + fn measure_value(&self, _value: &str, _size: u16) -> f32 { + 0.0 + } + fn draw( &mut self, _bounds: Rectangle, diff --git a/native/src/widget/text_input.rs b/native/src/widget/text_input.rs index f97ed424..f0bb9f87 100644 --- a/native/src/widget/text_input.rs +++ b/native/src/widget/text_input.rs @@ -9,6 +9,7 @@ use crate::{ layout, Element, Event, Hasher, Layout, Length, Point, Rectangle, Size, Widget, }; +use unicode_segmentation::UnicodeSegmentation; /// A field that can be filled with text. /// @@ -38,6 +39,7 @@ pub struct TextInput<'a, Message> { state: &'a mut State, placeholder: String, value: Value, + is_secure: bool, width: Length, max_width: Length, padding: u16, @@ -70,6 +72,7 @@ impl<'a, Message> TextInput<'a, Message> { state, placeholder: String::from(placeholder), value: Value::new(value), + is_secure: false, width: Length::Fill, max_width: Length::Shrink, padding: 0, @@ -79,6 +82,14 @@ impl<'a, Message> TextInput<'a, Message> { } } + /// Converts the [`TextInput`] into a secure password input. + /// + /// [`TextInput`]: struct.TextInput.html + pub fn password(mut self) -> Self { + self.is_secure = true; + self + } + /// Sets the width of the [`TextInput`]. /// /// [`TextInput`]: struct.TextInput.html @@ -160,7 +171,7 @@ where layout: Layout<'_>, cursor_position: Point, messages: &mut Vec<Message>, - _renderer: &Renderer, + renderer: &Renderer, ) { match event { Event::Mouse(mouse::Event::Input { @@ -170,8 +181,31 @@ where self.state.is_focused = layout.bounds().contains(cursor_position); - if self.state.cursor_position(&self.value) == 0 { - self.state.move_cursor_to_end(&self.value); + if self.state.is_focused { + let text_layout = layout.children().next().unwrap(); + let target = cursor_position.x - text_layout.bounds().x; + + if target < 0.0 { + self.state.cursor_position = 0; + } else if self.is_secure { + self.state.cursor_position = find_cursor_position( + renderer, + target, + &self.value.secure(), + self.size.unwrap_or(renderer.default_size()), + 0, + self.value.len(), + ); + } else { + self.state.cursor_position = find_cursor_position( + renderer, + target, + &self.value, + self.size.unwrap_or(renderer.default_size()), + 0, + self.value.len(), + ); + } } } Event::Keyboard(keyboard::Event::CharacterReceived(c)) @@ -188,6 +222,7 @@ where Event::Keyboard(keyboard::Event::Input { key_code, state: ButtonState::Pressed, + modifiers, }) if self.state.is_focused => match key_code { keyboard::KeyCode::Enter => { if let Some(on_submit) = self.on_submit.clone() { @@ -219,10 +254,36 @@ where } } keyboard::KeyCode::Left => { - self.state.move_cursor_left(&self.value); + let jump_modifier_pressed = if cfg!(target_os = "macos") { + modifiers.alt + } else { + modifiers.control + }; + + if jump_modifier_pressed && !self.is_secure { + self.state.move_cursor_left_by_words(&self.value); + } else { + self.state.move_cursor_left(&self.value); + } } keyboard::KeyCode::Right => { - self.state.move_cursor_right(&self.value); + let jump_modifier_pressed = if cfg!(target_os = "macos") { + modifiers.alt + } else { + modifiers.control + }; + + if jump_modifier_pressed && !self.is_secure { + self.state.move_cursor_right_by_words(&self.value); + } else { + self.state.move_cursor_right(&self.value); + } + } + keyboard::KeyCode::Home => { + self.state.cursor_position = 0; + } + keyboard::KeyCode::End => { + self.state.move_cursor_to_end(&self.value); } _ => {} }, @@ -239,15 +300,27 @@ where let bounds = layout.bounds(); let text_bounds = layout.children().next().unwrap().bounds(); - renderer.draw( - bounds, - text_bounds, - cursor_position, - self.size.unwrap_or(renderer.default_size()), - &self.placeholder, - &self.value, - &self.state, - ) + if self.is_secure { + renderer.draw( + bounds, + text_bounds, + cursor_position, + self.size.unwrap_or(renderer.default_size()), + &self.placeholder, + &self.value.secure(), + &self.state, + ) + } else { + renderer.draw( + bounds, + text_bounds, + cursor_position, + self.size.unwrap_or(renderer.default_size()), + &self.placeholder, + &self.value, + &self.state, + ) + } } fn hash_layout(&self, state: &mut Hasher) { @@ -275,6 +348,11 @@ pub trait Renderer: crate::Renderer + Sized { /// [`TextInput`]: struct.TextInput.html fn default_size(&self) -> u16; + /// Returns the width of the value of the [`TextInput`]. + /// + /// [`TextInput`]: struct.TextInput.html + fn measure_value(&self, value: &str, size: u16) -> f32; + /// Draws a [`TextInput`]. /// /// It receives: @@ -356,6 +434,17 @@ impl State { self.cursor_position.min(value.len()) } + /// Moves the cursor of a [`TextInput`] to the left. + /// + /// [`TextInput`]: struct.TextInput.html + pub(crate) fn move_cursor_left(&mut self, value: &Value) { + let current = self.cursor_position(value); + + if current > 0 { + self.cursor_position = current - 1; + } + } + /// Moves the cursor of a [`TextInput`] to the right. /// /// [`TextInput`]: struct.TextInput.html @@ -367,15 +456,22 @@ impl State { } } - /// Moves the cursor of a [`TextInput`] to the left. + /// Moves the cursor of a [`TextInput`] to the previous start of a word. /// /// [`TextInput`]: struct.TextInput.html - pub(crate) fn move_cursor_left(&mut self, value: &Value) { + pub(crate) fn move_cursor_left_by_words(&mut self, value: &Value) { let current = self.cursor_position(value); - if current > 0 { - self.cursor_position = current - 1; - } + self.cursor_position = value.previous_start_of_word(current); + } + + /// Moves the cursor of a [`TextInput`] to the next end of a word. + /// + /// [`TextInput`]: struct.TextInput.html + pub(crate) fn move_cursor_right_by_words(&mut self, value: &Value) { + let current = self.cursor_position(value); + + self.cursor_position = value.next_end_of_word(current); } /// Moves the cursor of a [`TextInput`] to the end. @@ -389,51 +485,174 @@ impl State { /// The value of a [`TextInput`]. /// /// [`TextInput`]: struct.TextInput.html -// TODO: Use `unicode-segmentation` +// TODO: Reduce allocations, cache results (?) #[derive(Debug)] -pub struct Value(Vec<char>); +pub struct Value { + graphemes: Vec<String>, +} impl Value { /// Creates a new [`Value`] from a string slice. /// /// [`Value`]: struct.Value.html pub fn new(string: &str) -> Self { - Self(string.chars().collect()) + let graphemes = UnicodeSegmentation::graphemes(string, true) + .map(String::from) + .collect(); + + Self { graphemes } } - /// Returns the total amount of `char` in the [`Value`]. + /// Returns the total amount of graphemes in the [`Value`]. /// /// [`Value`]: struct.Value.html pub fn len(&self) -> usize { - self.0.len() + self.graphemes.len() + } + + /// Returns the position of the previous start of a word from the given + /// grapheme `index`. + /// + /// [`Value`]: struct.Value.html + pub fn previous_start_of_word(&self, index: usize) -> usize { + let previous_string = + &self.graphemes[..index.min(self.graphemes.len())].concat(); + + UnicodeSegmentation::split_word_bound_indices(&previous_string as &str) + .filter(|(_, word)| !word.trim_start().is_empty()) + .next_back() + .map(|(i, previous_word)| { + index + - UnicodeSegmentation::graphemes(previous_word, true) + .count() + - UnicodeSegmentation::graphemes( + &previous_string[i + previous_word.len()..] as &str, + true, + ) + .count() + }) + .unwrap_or(0) + } + + /// Returns the position of the next end of a word from the given grapheme + /// `index`. + /// + /// [`Value`]: struct.Value.html + pub fn next_end_of_word(&self, index: usize) -> usize { + let next_string = &self.graphemes[index..].concat(); + + UnicodeSegmentation::split_word_bound_indices(&next_string as &str) + .filter(|(_, word)| !word.trim_start().is_empty()) + .next() + .map(|(i, next_word)| { + index + + UnicodeSegmentation::graphemes(next_word, true).count() + + UnicodeSegmentation::graphemes( + &next_string[..i] as &str, + true, + ) + .count() + }) + .unwrap_or(self.len()) } - /// Returns a new [`Value`] containing the `char` until the given `index`. + /// Returns a new [`Value`] containing the graphemes until the given `index`. /// /// [`Value`]: struct.Value.html pub fn until(&self, index: usize) -> Self { - Self(self.0[..index.min(self.len())].to_vec()) + let graphemes = self.graphemes[..index.min(self.len())].to_vec(); + + Self { graphemes } } /// Converts the [`Value`] into a `String`. /// /// [`Value`]: struct.Value.html pub fn to_string(&self) -> String { - use std::iter::FromIterator; - String::from_iter(self.0.iter()) + self.graphemes.concat() } - /// Inserts a new `char` at the given `index`. + /// Inserts a new `char` at the given grapheme `index`. /// /// [`Value`]: struct.Value.html pub fn insert(&mut self, index: usize, c: char) { - self.0.insert(index, c); + self.graphemes.insert(index, c.to_string()); + + self.graphemes = + UnicodeSegmentation::graphemes(&self.to_string() as &str, true) + .map(String::from) + .collect(); } - /// Removes the `char` at the given `index`. + /// Removes the grapheme at the given `index`. /// /// [`Value`]: struct.Value.html pub fn remove(&mut self, index: usize) { - let _ = self.0.remove(index); + let _ = self.graphemes.remove(index); + } + + /// Returns a new [`Value`] with all its graphemes replaced with the + /// dot ('•') character. + /// + /// [`Value`]: struct.Value.html + pub fn secure(&self) -> Self { + Self { + graphemes: std::iter::repeat(String::from("•")) + .take(self.graphemes.len()) + .collect(), + } + } +} + +// TODO: Reduce allocations +fn find_cursor_position<Renderer: self::Renderer>( + renderer: &Renderer, + target: f32, + value: &Value, + size: u16, + start: usize, + end: usize, +) -> usize { + if start >= end { + if start == 0 { + return 0; + } + + let prev = value.until(start - 1); + let next = value.until(start); + + let prev_width = renderer.measure_value(&prev.to_string(), size); + let next_width = renderer.measure_value(&next.to_string(), size); + + if next_width - target > target - prev_width { + return start - 1; + } else { + return start; + } + } + + let index = (end - start) / 2; + let subvalue = value.until(start + index); + + let width = renderer.measure_value(&subvalue.to_string(), size); + + if width > target { + find_cursor_position( + renderer, + target, + value, + size, + start, + start + index, + ) + } else { + find_cursor_position( + renderer, + target, + value, + size, + start + index + 1, + end, + ) } } |