diff options
author | 2022-11-18 15:00:10 -0800 | |
---|---|---|
committer | 2022-11-29 12:50:57 -0800 | |
commit | 899bd45700f0fe7a5dee5fb71400820981f154e0 (patch) | |
tree | 6939784c7121a4937bd32e65bfc781c1aca324bd /examples/modal | |
parent | 3814fda9d22875bdbc9e11ccfc6dce1f0e7ca5d3 (diff) | |
download | iced-899bd45700f0fe7a5dee5fb71400820981f154e0.tar.gz iced-899bd45700f0fe7a5dee5fb71400820981f154e0.tar.bz2 iced-899bd45700f0fe7a5dee5fb71400820981f154e0.zip |
Add on_submit & esc for example
Diffstat (limited to 'examples/modal')
-rw-r--r-- | examples/modal/src/main.rs | 39 |
1 files changed, 30 insertions, 9 deletions
diff --git a/examples/modal/src/main.rs b/examples/modal/src/main.rs index 37ac16fd..25483cf3 100644 --- a/examples/modal/src/main.rs +++ b/examples/modal/src/main.rs @@ -25,6 +25,7 @@ enum Message { HideModal, Email(String), Password(String), + Submit, Event(Event), } @@ -53,9 +54,7 @@ impl Application for App { widget::focus_next() } Message::HideModal => { - self.show_modal = false; - self.email.clear(); - self.password.clear(); + self.hide_modal(); Command::none() } Message::Email(email) => { @@ -66,21 +65,33 @@ impl Application for App { self.password = password; Command::none() } - Message::Event(event) => { - if let Event::Keyboard(keyboard::Event::KeyPressed { + Message::Submit => { + 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, modifiers, - }) = event - { + }) => { if modifiers.shift() { widget::focus_previous() } else { widget::focus_next() } - } else { + } + Event::Keyboard(keyboard::Event::KeyPressed { + key_code: keyboard::KeyCode::Escape, + .. + }) => { + self.hide_modal(); Command::none() } - } + _ => Command::none(), + }, } } @@ -127,12 +138,14 @@ impl Application for App { &self.email, Message::Email ) + .on_submit(Message::Submit) .padding(5), ] .spacing(5), column![ text("Password").size(12), text_input("", &self.password, Message::Password) + .on_submit(Message::Submit) .password() .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 { use iced_native::alignment::Alignment; use iced_native::widget::{self, Tree}; |