From 3cc605b70f543313cb665465ac169d0c85c446ab Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Sat, 9 Sep 2023 12:36:00 +0200 Subject: Implement `Icon` support for `TextInput` --- widget/src/text_input.rs | 27 +++++++++++++++++++++++---- 1 file changed, 23 insertions(+), 4 deletions(-) (limited to 'widget/src/text_input.rs') diff --git a/widget/src/text_input.rs b/widget/src/text_input.rs index f36b5616..aa35b5e4 100644 --- a/widget/src/text_input.rs +++ b/widget/src/text_input.rs @@ -511,7 +511,20 @@ where ); if let Some(icon) = icon { - let icon_width = 0.0; // TODO + let icon_text = Text { + line_height, + content: &icon.code_point.to_string(), + font: icon.font, + size: icon.size.unwrap_or_else(|| renderer.default_size()), + bounds: Size::new(f32::INFINITY, text_bounds.height), + horizontal_alignment: alignment::Horizontal::Center, + vertical_alignment: alignment::Vertical::Center, + shaping: text::Shaping::Advanced, + }; + + renderer.update_paragraph(&mut state.icon, icon_text); + + let icon_width = state.icon.min_width(); let mut text_node = layout::Node::new( text_bounds - Size::new(icon_width + icon.spacing, 0.0), @@ -1053,10 +1066,14 @@ pub fn draw( appearance.background, ); - if let Some(_icon) = icon { - let _icon_layout = children_layout.next().unwrap(); + if icon.is_some() { + let icon_layout = children_layout.next().unwrap(); - // TODO + renderer.fill_paragraph( + &state.icon, + icon_layout.bounds().center(), + appearance.icon_color, + ); } let text = value.to_string(); @@ -1206,6 +1223,7 @@ pub fn mouse_interaction( pub struct State { value: P, placeholder: P, + icon: P, is_focused: Option, is_dragging: bool, is_pasting: Option, @@ -1233,6 +1251,7 @@ impl State

{ Self { value: P::default(), placeholder: P::default(), + icon: P::default(), is_focused: None, is_dragging: false, is_pasting: None, -- cgit