Implement TextInput::password for secure data
This commit is contained in:
parent
749722fca6
commit
34bdfe9416
1 changed files with 54 additions and 11 deletions
|
|
@ -39,6 +39,7 @@ pub struct TextInput<'a, Message> {
|
||||||
state: &'a mut State,
|
state: &'a mut State,
|
||||||
placeholder: String,
|
placeholder: String,
|
||||||
value: Value,
|
value: Value,
|
||||||
|
is_secure: bool,
|
||||||
width: Length,
|
width: Length,
|
||||||
max_width: Length,
|
max_width: Length,
|
||||||
padding: u16,
|
padding: u16,
|
||||||
|
|
@ -71,6 +72,7 @@ impl<'a, Message> TextInput<'a, Message> {
|
||||||
state,
|
state,
|
||||||
placeholder: String::from(placeholder),
|
placeholder: String::from(placeholder),
|
||||||
value: Value::new(value),
|
value: Value::new(value),
|
||||||
|
is_secure: false,
|
||||||
width: Length::Fill,
|
width: Length::Fill,
|
||||||
max_width: Length::Shrink,
|
max_width: Length::Shrink,
|
||||||
padding: 0,
|
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`].
|
/// Sets the width of the [`TextInput`].
|
||||||
///
|
///
|
||||||
/// [`TextInput`]: struct.TextInput.html
|
/// [`TextInput`]: struct.TextInput.html
|
||||||
|
|
@ -177,6 +187,15 @@ where
|
||||||
|
|
||||||
if target < 0.0 {
|
if target < 0.0 {
|
||||||
self.state.cursor_position = 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 {
|
} else {
|
||||||
self.state.cursor_position = find_cursor_position(
|
self.state.cursor_position = find_cursor_position(
|
||||||
renderer,
|
renderer,
|
||||||
|
|
@ -235,14 +254,14 @@ where
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
keyboard::KeyCode::Left => {
|
keyboard::KeyCode::Left => {
|
||||||
if modifiers.control {
|
if modifiers.control && !self.is_secure {
|
||||||
self.state.move_cursor_left_by_words(&self.value);
|
self.state.move_cursor_left_by_words(&self.value);
|
||||||
} else {
|
} else {
|
||||||
self.state.move_cursor_left(&self.value);
|
self.state.move_cursor_left(&self.value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
keyboard::KeyCode::Right => {
|
keyboard::KeyCode::Right => {
|
||||||
if modifiers.control {
|
if modifiers.control && !self.is_secure {
|
||||||
self.state.move_cursor_right_by_words(&self.value);
|
self.state.move_cursor_right_by_words(&self.value);
|
||||||
} else {
|
} else {
|
||||||
self.state.move_cursor_right(&self.value);
|
self.state.move_cursor_right(&self.value);
|
||||||
|
|
@ -269,15 +288,27 @@ where
|
||||||
let bounds = layout.bounds();
|
let bounds = layout.bounds();
|
||||||
let text_bounds = layout.children().next().unwrap().bounds();
|
let text_bounds = layout.children().next().unwrap().bounds();
|
||||||
|
|
||||||
renderer.draw(
|
if self.is_secure {
|
||||||
bounds,
|
renderer.draw(
|
||||||
text_bounds,
|
bounds,
|
||||||
cursor_position,
|
text_bounds,
|
||||||
self.size.unwrap_or(renderer.default_size()),
|
cursor_position,
|
||||||
&self.placeholder,
|
self.size.unwrap_or(renderer.default_size()),
|
||||||
&self.value,
|
&self.placeholder,
|
||||||
&self.state,
|
&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) {
|
fn hash_layout(&self, state: &mut Hasher) {
|
||||||
|
|
@ -547,6 +578,18 @@ impl Value {
|
||||||
pub fn remove(&mut self, index: usize) {
|
pub fn remove(&mut self, index: usize) {
|
||||||
let _ = self.graphemes.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
|
// TODO: Reduce allocations
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue