diff options
author | 2019-12-07 07:18:15 +0100 | |
---|---|---|
committer | 2019-12-07 07:18:15 +0100 | |
commit | 34bdfe941613d4bb51848eec1dc0f99f2a59ff21 (patch) | |
tree | 935dee0352e4af8f463224f60d825dea83fcd33b /native | |
parent | 749722fca6eb9c85901e80060583559317f2af79 (diff) | |
download | iced-34bdfe941613d4bb51848eec1dc0f99f2a59ff21.tar.gz iced-34bdfe941613d4bb51848eec1dc0f99f2a59ff21.tar.bz2 iced-34bdfe941613d4bb51848eec1dc0f99f2a59ff21.zip |
Implement `TextInput::password` for secure data
Diffstat (limited to 'native')
-rw-r--r-- | native/src/widget/text_input.rs | 65 |
1 files changed, 54 insertions, 11 deletions
diff --git a/native/src/widget/text_input.rs b/native/src/widget/text_input.rs index c57b80aa..985d1b80 100644 --- a/native/src/widget/text_input.rs +++ b/native/src/widget/text_input.rs @@ -39,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, @@ -71,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, @@ -80,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 @@ -177,6 +187,15 @@ where 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, @@ -235,14 +254,14 @@ where } } keyboard::KeyCode::Left => { - if modifiers.control { + if modifiers.control && !self.is_secure { self.state.move_cursor_left_by_words(&self.value); } else { self.state.move_cursor_left(&self.value); } } keyboard::KeyCode::Right => { - if modifiers.control { + if modifiers.control && !self.is_secure { self.state.move_cursor_right_by_words(&self.value); } else { self.state.move_cursor_right(&self.value); @@ -269,15 +288,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) { @@ -547,6 +578,18 @@ impl Value { pub fn remove(&mut self, index: usize) { 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 |