From 533e099cf6259fac14580f5c3881777a43b93932 Mon Sep 17 00:00:00 2001 From: Ashley Wulber Date: Mon, 21 Oct 2024 09:12:41 -0400 Subject: [PATCH] fix: text input icon render --- examples/application/src/main.rs | 22 +++++++++++++++++++++- src/widget/text_input/input.rs | 16 ++++++++++++---- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/examples/application/src/main.rs b/examples/application/src/main.rs index 24568f0..29e2bbf 100644 --- a/examples/application/src/main.rs +++ b/examples/application/src/main.rs @@ -54,6 +54,8 @@ fn main() -> Result<(), Box> { pub enum Message { Input1(String), Input2(String), + Ignore, + ToggleHide, } /// The [`App`] stores application-specific state. @@ -62,6 +64,7 @@ pub struct App { nav_model: nav_bar::Model, input_1: String, input_2: String, + hidden: bool, } /// Implement [`cosmic::Application`] to integrate with COSMIC. @@ -101,6 +104,7 @@ impl cosmic::Application for App { nav_model, input_1: String::new(), input_2: String::new(), + hidden: true, }; let command = app.update_title(); @@ -128,6 +132,10 @@ impl cosmic::Application for App { Message::Input2(v) => { self.input_2 = v; } + Message::Ignore => {} + Message::ToggleHide => { + self.hidden = !self.hidden; + } } Task::none() } @@ -144,8 +152,20 @@ impl cosmic::Application for App { let centered = cosmic::widget::container( column![ text, + cosmic::widget::text_input::text_input("", &self.input_1) + .on_input(Message::Input1) + .on_clear(Message::Ignore), + cosmic::widget::text_input::secure_input( + "", + &self.input_1, + Some(Message::ToggleHide), + self.hidden + ) + .on_input(Message::Input1), cosmic::widget::text_input::text_input("", &self.input_1).on_input(Message::Input1), - cosmic::widget::text_input::text_input("", &self.input_2).on_input(Message::Input2), + cosmic::widget::text_input::search_input("", &self.input_2) + .on_input(Message::Input2) + .on_clear(Message::Ignore), ] .width(iced::Length::Fill) .height(iced::Length::Shrink) diff --git a/src/widget/text_input/input.rs b/src/widget/text_input/input.rs index 3c08562..2b8f424 100644 --- a/src/widget/text_input/input.rs +++ b/src/widget/text_input/input.rs @@ -431,7 +431,7 @@ where value: Option<&Value>, style: &renderer::Style, ) { - let text_layout = self.text_layout(layout.clone()); + let text_layout = self.text_layout(layout); draw( renderer, theme, @@ -2008,7 +2008,7 @@ pub fn draw<'a, Message>( let mut children_layout = layout.children(); let bounds = layout.bounds(); - let text_bounds = text_layout.bounds(); + let text_bounds = children_layout.next().unwrap().bounds(); let is_mouse_over = cursor_position.is_over(bounds); @@ -2115,8 +2115,11 @@ pub fn draw<'a, Message>( let mut child_index = 0; let leading_icon_tree = children.get(child_index); // draw the start icon in the text input + let has_start_icon = icon.is_some(); if let (Some(icon), Some(tree)) = (icon, leading_icon_tree) { - let icon_layout = children_layout.next().unwrap(); + let mut children = text_layout.children(); + let _ = children.next().unwrap(); + let icon_layout = children.next().unwrap(); icon.as_widget().draw( tree, @@ -2292,7 +2295,12 @@ pub fn draw<'a, Message>( // draw the end icon in the text input if let (Some(icon), Some(tree)) = (trailing_icon, trailing_icon_tree) { - let icon_layout = children_layout.next().unwrap(); + let mut children = text_layout.children(); + let mut icon_layout = children.next().unwrap(); + if has_start_icon { + icon_layout = children.next().unwrap(); + } + icon_layout = children.next().unwrap(); icon.as_widget().draw( tree,