From f76ae9b019dffa65b1dba052466d1fad39306fcd Mon Sep 17 00:00:00 2001 From: Piotr <114903054+elevenhsoft@users.noreply.github.com> Date: Fri, 17 May 2024 00:08:03 +0200 Subject: [PATCH] add input_validation example --- examples/input_validation/Cargo.toml | 8 +++ examples/input_validation/src/main.rs | 83 +++++++++++++++++++++++++++ 2 files changed, 91 insertions(+) create mode 100644 examples/input_validation/Cargo.toml create mode 100644 examples/input_validation/src/main.rs diff --git a/examples/input_validation/Cargo.toml b/examples/input_validation/Cargo.toml new file mode 100644 index 00000000..732bfe32 --- /dev/null +++ b/examples/input_validation/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "input_validation" +version = "0.1.0" +edition = "2021" + +[dependencies] +libcosmic = { path = "../..", features = ["wayland", "wgpu"] } +regex = "1.10.4" diff --git a/examples/input_validation/src/main.rs b/examples/input_validation/src/main.rs new file mode 100644 index 00000000..d98db1a7 --- /dev/null +++ b/examples/input_validation/src/main.rs @@ -0,0 +1,83 @@ +use cosmic::app::{Application, Command, Core, Settings}; +use cosmic::iced; +use cosmic::iced::executor; +use cosmic::iced_core::Size; +use cosmic::widget::{column, text_input, validate_input}; +use cosmic::Element; +use regex::Regex; + +#[derive(Debug, Clone)] +pub enum Message { + InputName(String), + InputEmail(String), +} + +struct Window { + core: Core, + name: String, + email: String, +} + +impl Application for Window { + type Executor = executor::Default; + type Flags = (); + type Message = Message; + const APP_ID: &'static str = "com.system76.InputValidation"; + + fn core(&self) -> &Core { + &self.core + } + + fn core_mut(&mut self) -> &mut Core { + &mut self.core + } + + fn init(core: Core, _flags: Self::Flags) -> (Self, Command) { + ( + Self { + core, + name: String::new(), + email: String::new(), + }, + Command::none(), + ) + } + + fn update( + &mut self, + message: Self::Message, + ) -> iced::Command> { + match message { + Message::InputName(name) => self.name = name, + Message::InputEmail(email) => self.email = email, + } + Command::none() + } + + fn view(&self) -> Element { + let emailregex = Regex::new(r#"(?:[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*|"(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21\x23-\x5b\x5d-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])*")@(?:(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?|\[(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?|[a-z0-9-]*[a-z0-9]:(?:[\x01-\x08\x0b\x0c\x0e-\x1f\x21-\x5a\x53-\x7f]|\\[\x01-\x09\x0b\x0c\x0e-\x7f])+)\])"#).unwrap(); + let validation_condition = emailregex.is_match(&self.email); + + let name_input = text_input("Name", &self.name).on_input(Message::InputName); + let email_input = validate_input("E-mail", &self.email, validation_condition) + .on_input(Message::InputEmail); + + column() + .push(name_input) + .push(email_input) + .spacing(10) + .into() + } +} + +fn main() -> std::io::Result<()> { + let mut settings = Settings::default(); + settings = settings.size(Size { + width: 480., + height: 480., + }); + + let _ = cosmic::app::run::(settings, ()); + + Ok(()) +}