Simplify new logic in TextInput

This commit is contained in:
Héctor Ramón Jiménez 2023-08-30 06:43:45 +02:00
parent a026e917d3
commit bcd9fdb521
No known key found for this signature in database
GPG key ID: 140CC052C94F138E

View file

@ -497,26 +497,18 @@ where
shaping: text::Shaping::Advanced, shaping: text::Shaping::Advanced,
}; };
renderer renderer.update_paragraph(&mut state.placeholder, placeholder_text);
.update_paragraph(&mut state.placeholder_paragraph, placeholder_text);
if is_secure { let secure_value = is_secure.then(|| value.secure());
renderer.update_paragraph( let value = secure_value.as_ref().unwrap_or(value);
&mut state.paragraph,
Text { renderer.update_paragraph(
content: &value.secure().to_string(), &mut state.value,
..placeholder_text Text {
}, content: &value.to_string(),
); ..placeholder_text
} else { },
renderer.update_paragraph( );
&mut state.paragraph,
Text {
content: &value.to_string(),
..placeholder_text
},
);
}
if let Some(icon) = icon { if let Some(icon) = icon {
let icon_width = 0.0; // TODO let icon_width = 0.0; // TODO
@ -1078,7 +1070,7 @@ pub fn draw<Renderer>(
cursor::State::Index(position) => { cursor::State::Index(position) => {
let (text_value_width, offset) = let (text_value_width, offset) =
measure_cursor_and_scroll_offset( measure_cursor_and_scroll_offset(
&state.paragraph, &state.value,
text_bounds, text_bounds,
position, position,
); );
@ -1116,14 +1108,14 @@ pub fn draw<Renderer>(
let (left_position, left_offset) = let (left_position, left_offset) =
measure_cursor_and_scroll_offset( measure_cursor_and_scroll_offset(
&state.paragraph, &state.value,
text_bounds, text_bounds,
left, left,
); );
let (right_position, right_offset) = let (right_position, right_offset) =
measure_cursor_and_scroll_offset( measure_cursor_and_scroll_offset(
&state.paragraph, &state.value,
text_bounds, text_bounds,
right, right,
); );
@ -1157,7 +1149,7 @@ pub fn draw<Renderer>(
(None, 0.0) (None, 0.0)
}; };
let text_width = state.paragraph.min_width(); let text_width = state.value.min_width();
let render = |renderer: &mut Renderer| { let render = |renderer: &mut Renderer| {
if let Some((cursor, color)) = cursor { if let Some((cursor, color)) = cursor {
@ -1168,9 +1160,9 @@ pub fn draw<Renderer>(
renderer.fill_paragraph( renderer.fill_paragraph(
if text.is_empty() { if text.is_empty() {
&state.placeholder_paragraph &state.placeholder
} else { } else {
&state.paragraph &state.value
}, },
Point::new(text_bounds.x, text_bounds.center_y()), Point::new(text_bounds.x, text_bounds.center_y()),
if text.is_empty() { if text.is_empty() {
@ -1212,8 +1204,8 @@ pub fn mouse_interaction(
/// The state of a [`TextInput`]. /// The state of a [`TextInput`].
#[derive(Debug, Default, Clone)] #[derive(Debug, Default, Clone)]
pub struct State<P: text::Paragraph> { pub struct State<P: text::Paragraph> {
paragraph: P, value: P,
placeholder_paragraph: P, placeholder: P,
is_focused: Option<Focus>, is_focused: Option<Focus>,
is_dragging: bool, is_dragging: bool,
is_pasting: Option<Value>, is_pasting: Option<Value>,
@ -1239,8 +1231,8 @@ impl<P: text::Paragraph> State<P> {
/// Creates a new [`State`], representing a focused [`TextInput`]. /// Creates a new [`State`], representing a focused [`TextInput`].
pub fn focused() -> Self { pub fn focused() -> Self {
Self { Self {
paragraph: P::default(), value: P::default(),
placeholder_paragraph: P::default(), placeholder: P::default(),
is_focused: None, is_focused: None,
is_dragging: false, is_dragging: false,
is_pasting: None, is_pasting: None,
@ -1357,7 +1349,7 @@ fn offset<P: text::Paragraph>(
}; };
let (_, offset) = measure_cursor_and_scroll_offset( let (_, offset) = measure_cursor_and_scroll_offset(
&state.paragraph, &state.value,
text_bounds, text_bounds,
focus_position, focus_position,
); );
@ -1394,7 +1386,7 @@ fn find_cursor_position<P: text::Paragraph>(
let value = value.to_string(); let value = value.to_string();
let char_offset = state let char_offset = state
.paragraph .value
.hit_test(Point::new(x + offset, text_bounds.height / 2.0)) .hit_test(Point::new(x + offset, text_bounds.height / 2.0))
.map(text::Hit::cursor)?; .map(text::Hit::cursor)?;
@ -1424,7 +1416,7 @@ fn replace_paragraph<Renderer>(
let mut children_layout = layout.children(); let mut children_layout = layout.children();
let text_bounds = children_layout.next().unwrap().bounds(); let text_bounds = children_layout.next().unwrap().bounds();
state.paragraph = renderer.create_paragraph(Text { state.value = renderer.create_paragraph(Text {
font, font,
line_height, line_height,
content: &value.to_string(), content: &value.to_string(),