From 362dc2631f04e5ed194faf15b71b5579831c864e Mon Sep 17 00:00:00 2001 From: Piotr <114903054+elevenhsoft@users.noreply.github.com> Date: Fri, 17 May 2024 00:07:44 +0200 Subject: [PATCH] feat: add validate_input widget --- src/widget/text_input/input.rs | 37 +++++++++++++++++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-) diff --git a/src/widget/text_input/input.rs b/src/widget/text_input/input.rs index 0ddbd567..596051be 100644 --- a/src/widget/text_input/input.rs +++ b/src/widget/text_input/input.rs @@ -160,6 +160,20 @@ where .padding([spacing, spacing, spacing, spacing]) } +/// Creates a new validate input [`TextInput`]. +/// +/// [`TextInput`]: widget::TextInput +pub fn validate_input<'a, Message>( + placeholder: impl Into>, + value: impl Into>, + validate_condition: bool, +) -> TextInput<'a, Message> +where + Message: Clone + 'static, +{ + TextInput::new(placeholder, value).validation_condition(validate_condition) +} + #[cfg(feature = "wayland")] pub(crate) const SUPPORTED_TEXT_MIME_TYPES: &[&str; 6] = &[ "text/plain;charset=utf-8", @@ -185,6 +199,7 @@ pub struct TextInput<'a, Message> { is_secure: bool, is_editable: bool, is_read_only: bool, + is_validated: Option, font: Option<::Font>, width: Length, padding: Padding, @@ -228,6 +243,7 @@ where is_secure: false, is_editable: false, is_read_only: false, + is_validated: None, font: None, width: Length::Fill, padding: [spacing, spacing, spacing, spacing].into(), @@ -298,6 +314,11 @@ where self } + fn validation_condition(mut self, validation_condition: bool) -> Self { + self.is_validated = Some(validation_condition); + self + } + /// Sets the message that should be produced when some text is typed into /// the [`TextInput`]. /// @@ -415,6 +436,7 @@ where self.font, self.on_input.is_none(), self.is_secure, + self.is_validated, self.leading_icon.as_ref(), self.trailing_icon.as_ref(), &self.style, @@ -804,6 +826,7 @@ where self.font, self.on_input.is_none(), self.is_secure, + self.is_validated, self.leading_icon.as_ref(), self.trailing_icon.as_ref(), &self.style, @@ -1959,6 +1982,7 @@ pub fn draw<'a, Message>( font: Option<::Font>, is_disabled: bool, is_secure: bool, + is_validated: Option, icon: Option<&Element<'a, Message, crate::Theme, crate::Renderer>>, trailing_icon: Option<&Element<'a, Message, crate::Theme, crate::Renderer>>, style: &::Style, @@ -2020,6 +2044,7 @@ pub fn draw<'a, Message>( let mut icon_color = appearance.icon_color.unwrap_or(renderer_style.icon_color); let mut text_color = appearance.text_color.unwrap_or(renderer_style.text_color); + let mut border_color = appearance.border_color; // TODO: iced will not render alpha itself on text or icon colors. if is_disabled { @@ -2028,6 +2053,16 @@ pub fn draw<'a, Message>( text_color = text_color.blend_alpha(background, 0.5); } + if is_validated.is_some() { + if !is_validated.unwrap() { + border_color = Color::from_rgb( + 0.95, + 0.30, + 0.30 + ); + } + } + // draw background and its border if let Some(border_offset) = appearance.border_offset { let offset_bounds = Rectangle { @@ -2056,7 +2091,7 @@ pub fn draw<'a, Message>( bounds: offset_bounds, border: Border { width: appearance.border_width, - color: appearance.border_color, + color: border_color, radius: appearance.border_radius, }, shadow: Shadow {