Add on_submit & esc for example

This commit is contained in:
Cory Forsstrom 2022-11-18 15:00:10 -08:00
parent 3814fda9d2
commit 899bd45700

View file

@ -25,6 +25,7 @@ enum Message {
HideModal, HideModal,
Email(String), Email(String),
Password(String), Password(String),
Submit,
Event(Event), Event(Event),
} }
@ -53,9 +54,7 @@ impl Application for App {
widget::focus_next() widget::focus_next()
} }
Message::HideModal => { Message::HideModal => {
self.show_modal = false; self.hide_modal();
self.email.clear();
self.password.clear();
Command::none() Command::none()
} }
Message::Email(email) => { Message::Email(email) => {
@ -66,21 +65,33 @@ impl Application for App {
self.password = password; self.password = password;
Command::none() Command::none()
} }
Message::Event(event) => { Message::Submit => {
if let Event::Keyboard(keyboard::Event::KeyPressed { if !self.email.is_empty() && !self.password.is_empty() {
self.hide_modal();
}
Command::none()
}
Message::Event(event) => match event {
Event::Keyboard(keyboard::Event::KeyPressed {
key_code: keyboard::KeyCode::Tab, key_code: keyboard::KeyCode::Tab,
modifiers, modifiers,
}) = event }) => {
{
if modifiers.shift() { if modifiers.shift() {
widget::focus_previous() widget::focus_previous()
} else { } else {
widget::focus_next() widget::focus_next()
} }
} else { }
Event::Keyboard(keyboard::Event::KeyPressed {
key_code: keyboard::KeyCode::Escape,
..
}) => {
self.hide_modal();
Command::none() Command::none()
} }
} _ => Command::none(),
},
} }
} }
@ -127,12 +138,14 @@ impl Application for App {
&self.email, &self.email,
Message::Email Message::Email
) )
.on_submit(Message::Submit)
.padding(5), .padding(5),
] ]
.spacing(5), .spacing(5),
column![ column![
text("Password").size(12), text("Password").size(12),
text_input("", &self.password, Message::Password) text_input("", &self.password, Message::Password)
.on_submit(Message::Submit)
.password() .password()
.padding(5), .padding(5),
] ]
@ -156,6 +169,14 @@ impl Application for App {
} }
} }
impl App {
fn hide_modal(&mut self) {
self.show_modal = false;
self.email.clear();
self.password.clear();
}
}
mod modal { mod modal {
use iced_native::alignment::Alignment; use iced_native::alignment::Alignment;
use iced_native::widget::{self, Tree}; use iced_native::widget::{self, Tree};