feat: add validate_input widget

This commit is contained in:
Piotr 2024-05-17 00:07:44 +02:00
parent 54eb5487b4
commit 362dc2631f

View file

@ -160,6 +160,20 @@ where
.padding([spacing, spacing, spacing, spacing]) .padding([spacing, spacing, spacing, spacing])
} }
/// Creates a new validate input [`TextInput`].
///
/// [`TextInput`]: widget::TextInput
pub fn validate_input<'a, Message>(
placeholder: impl Into<Cow<'a, str>>,
value: impl Into<Cow<'a, str>>,
validate_condition: bool,
) -> TextInput<'a, Message>
where
Message: Clone + 'static,
{
TextInput::new(placeholder, value).validation_condition(validate_condition)
}
#[cfg(feature = "wayland")] #[cfg(feature = "wayland")]
pub(crate) const SUPPORTED_TEXT_MIME_TYPES: &[&str; 6] = &[ pub(crate) const SUPPORTED_TEXT_MIME_TYPES: &[&str; 6] = &[
"text/plain;charset=utf-8", "text/plain;charset=utf-8",
@ -185,6 +199,7 @@ pub struct TextInput<'a, Message> {
is_secure: bool, is_secure: bool,
is_editable: bool, is_editable: bool,
is_read_only: bool, is_read_only: bool,
is_validated: Option<bool>,
font: Option<<crate::Renderer as iced_core::text::Renderer>::Font>, font: Option<<crate::Renderer as iced_core::text::Renderer>::Font>,
width: Length, width: Length,
padding: Padding, padding: Padding,
@ -228,6 +243,7 @@ where
is_secure: false, is_secure: false,
is_editable: false, is_editable: false,
is_read_only: false, is_read_only: false,
is_validated: None,
font: None, font: None,
width: Length::Fill, width: Length::Fill,
padding: [spacing, spacing, spacing, spacing].into(), padding: [spacing, spacing, spacing, spacing].into(),
@ -298,6 +314,11 @@ where
self 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 /// Sets the message that should be produced when some text is typed into
/// the [`TextInput`]. /// the [`TextInput`].
/// ///
@ -415,6 +436,7 @@ where
self.font, self.font,
self.on_input.is_none(), self.on_input.is_none(),
self.is_secure, self.is_secure,
self.is_validated,
self.leading_icon.as_ref(), self.leading_icon.as_ref(),
self.trailing_icon.as_ref(), self.trailing_icon.as_ref(),
&self.style, &self.style,
@ -804,6 +826,7 @@ where
self.font, self.font,
self.on_input.is_none(), self.on_input.is_none(),
self.is_secure, self.is_secure,
self.is_validated,
self.leading_icon.as_ref(), self.leading_icon.as_ref(),
self.trailing_icon.as_ref(), self.trailing_icon.as_ref(),
&self.style, &self.style,
@ -1959,6 +1982,7 @@ pub fn draw<'a, Message>(
font: Option<<crate::Renderer as iced_core::text::Renderer>::Font>, font: Option<<crate::Renderer as iced_core::text::Renderer>::Font>,
is_disabled: bool, is_disabled: bool,
is_secure: bool, is_secure: bool,
is_validated: Option<bool>,
icon: Option<&Element<'a, Message, crate::Theme, crate::Renderer>>, icon: Option<&Element<'a, Message, crate::Theme, crate::Renderer>>,
trailing_icon: Option<&Element<'a, Message, crate::Theme, crate::Renderer>>, trailing_icon: Option<&Element<'a, Message, crate::Theme, crate::Renderer>>,
style: &<crate::Theme as StyleSheet>::Style, style: &<crate::Theme as StyleSheet>::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 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 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. // TODO: iced will not render alpha itself on text or icon colors.
if is_disabled { if is_disabled {
@ -2028,6 +2053,16 @@ pub fn draw<'a, Message>(
text_color = text_color.blend_alpha(background, 0.5); 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 // draw background and its border
if let Some(border_offset) = appearance.border_offset { if let Some(border_offset) = appearance.border_offset {
let offset_bounds = Rectangle { let offset_bounds = Rectangle {
@ -2056,7 +2091,7 @@ pub fn draw<'a, Message>(
bounds: offset_bounds, bounds: offset_bounds,
border: Border { border: Border {
width: appearance.border_width, width: appearance.border_width,
color: appearance.border_color, color: border_color,
radius: appearance.border_radius, radius: appearance.border_radius,
}, },
shadow: Shadow { shadow: Shadow {