summaryrefslogtreecommitdiffstats
path: root/widget/src/text_input.rs
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón Jiménez <hector0193@gmail.com>2023-08-30 06:43:45 +0200
committerLibravatar Héctor Ramón Jiménez <hector0193@gmail.com>2023-08-30 06:43:45 +0200
commitbcd9fdb521ce4cf21e9d1ee28157ed068ae1428c (patch)
tree71b3140cea08241c307fec75936415aae9340490 /widget/src/text_input.rs
parenta026e917d3364e58fd827995261158d8cb356ce9 (diff)
downloadiced-bcd9fdb521ce4cf21e9d1ee28157ed068ae1428c.tar.gz
iced-bcd9fdb521ce4cf21e9d1ee28157ed068ae1428c.tar.bz2
iced-bcd9fdb521ce4cf21e9d1ee28157ed068ae1428c.zip
Simplify new logic in `TextInput`
Diffstat (limited to 'widget/src/text_input.rs')
-rw-r--r--widget/src/text_input.rs58
1 files changed, 25 insertions, 33 deletions
diff --git a/widget/src/text_input.rs b/widget/src/text_input.rs
index 75800a34..f36b5616 100644
--- a/widget/src/text_input.rs
+++ b/widget/src/text_input.rs
@@ -497,26 +497,18 @@ where
shaping: text::Shaping::Advanced,
};
- renderer
- .update_paragraph(&mut state.placeholder_paragraph, placeholder_text);
-
- if is_secure {
- renderer.update_paragraph(
- &mut state.paragraph,
- Text {
- content: &value.secure().to_string(),
- ..placeholder_text
- },
- );
- } else {
- renderer.update_paragraph(
- &mut state.paragraph,
- Text {
- content: &value.to_string(),
- ..placeholder_text
- },
- );
- }
+ renderer.update_paragraph(&mut state.placeholder, placeholder_text);
+
+ let secure_value = is_secure.then(|| value.secure());
+ let value = secure_value.as_ref().unwrap_or(value);
+
+ renderer.update_paragraph(
+ &mut state.value,
+ Text {
+ content: &value.to_string(),
+ ..placeholder_text
+ },
+ );
if let Some(icon) = icon {
let icon_width = 0.0; // TODO
@@ -1078,7 +1070,7 @@ pub fn draw<Renderer>(
cursor::State::Index(position) => {
let (text_value_width, offset) =
measure_cursor_and_scroll_offset(
- &state.paragraph,
+ &state.value,
text_bounds,
position,
);
@@ -1116,14 +1108,14 @@ pub fn draw<Renderer>(
let (left_position, left_offset) =
measure_cursor_and_scroll_offset(
- &state.paragraph,
+ &state.value,
text_bounds,
left,
);
let (right_position, right_offset) =
measure_cursor_and_scroll_offset(
- &state.paragraph,
+ &state.value,
text_bounds,
right,
);
@@ -1157,7 +1149,7 @@ pub fn draw<Renderer>(
(None, 0.0)
};
- let text_width = state.paragraph.min_width();
+ let text_width = state.value.min_width();
let render = |renderer: &mut Renderer| {
if let Some((cursor, color)) = cursor {
@@ -1168,9 +1160,9 @@ pub fn draw<Renderer>(
renderer.fill_paragraph(
if text.is_empty() {
- &state.placeholder_paragraph
+ &state.placeholder
} else {
- &state.paragraph
+ &state.value
},
Point::new(text_bounds.x, text_bounds.center_y()),
if text.is_empty() {
@@ -1212,8 +1204,8 @@ pub fn mouse_interaction(
/// The state of a [`TextInput`].
#[derive(Debug, Default, Clone)]
pub struct State<P: text::Paragraph> {
- paragraph: P,
- placeholder_paragraph: P,
+ value: P,
+ placeholder: P,
is_focused: Option<Focus>,
is_dragging: bool,
is_pasting: Option<Value>,
@@ -1239,8 +1231,8 @@ impl<P: text::Paragraph> State<P> {
/// Creates a new [`State`], representing a focused [`TextInput`].
pub fn focused() -> Self {
Self {
- paragraph: P::default(),
- placeholder_paragraph: P::default(),
+ value: P::default(),
+ placeholder: P::default(),
is_focused: None,
is_dragging: false,
is_pasting: None,
@@ -1357,7 +1349,7 @@ fn offset<P: text::Paragraph>(
};
let (_, offset) = measure_cursor_and_scroll_offset(
- &state.paragraph,
+ &state.value,
text_bounds,
focus_position,
);
@@ -1394,7 +1386,7 @@ fn find_cursor_position<P: text::Paragraph>(
let value = value.to_string();
let char_offset = state
- .paragraph
+ .value
.hit_test(Point::new(x + offset, text_bounds.height / 2.0))
.map(text::Hit::cursor)?;
@@ -1424,7 +1416,7 @@ fn replace_paragraph<Renderer>(
let mut children_layout = layout.children();
let text_bounds = children_layout.next().unwrap().bounds();
- state.paragraph = renderer.create_paragraph(Text {
+ state.value = renderer.create_paragraph(Text {
font,
line_height,
content: &value.to_string(),