diff options
author | 2021-12-29 09:31:43 -0800 | |
---|---|---|
committer | 2021-12-29 09:34:37 -0800 | |
commit | 8a70d10401eb1277718a19f47ff1e2a5c4c7564b (patch) | |
tree | 3f9735bbddef9e293e78fc2452b9896752975c9a | |
parent | 5466d6a11d0a4bee01e954b7c727b3fd67cea02a (diff) | |
download | iced-8a70d10401eb1277718a19f47ff1e2a5c4c7564b.tar.gz iced-8a70d10401eb1277718a19f47ff1e2a5c4c7564b.tar.bz2 iced-8a70d10401eb1277718a19f47ff1e2a5c4c7564b.zip |
Allow Sandbox applications to exit.
-rw-r--r-- | Cargo.toml | 1 | ||||
-rw-r--r-- | examples/exit/Cargo.toml | 8 | ||||
-rw-r--r-- | examples/exit/README.md | 12 | ||||
-rw-r--r-- | examples/exit/src/main.rs | 75 | ||||
-rw-r--r-- | src/sandbox.rs | 11 |
5 files changed, 107 insertions, 0 deletions
@@ -71,6 +71,7 @@ members = [ "examples/custom_widget", "examples/download_progress", "examples/events", + "examples/exit", "examples/game_of_life", "examples/geometry", "examples/integration_opengl", diff --git a/examples/exit/Cargo.toml b/examples/exit/Cargo.toml new file mode 100644 index 00000000..34d0789a --- /dev/null +++ b/examples/exit/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "exit" +version = "0.1.0" +edition = "2021" +publish = false + +[dependencies] +iced = { path = "../.." } diff --git a/examples/exit/README.md b/examples/exit/README.md new file mode 100644 index 00000000..969c9182 --- /dev/null +++ b/examples/exit/README.md @@ -0,0 +1,12 @@ +## Exit + +How to exit an application based on user input. + +The __[`main`]__ file contains all the code of the example. + +You can run it with `cargo run`: +``` +cargo run --package exit +``` + +[`main`]: src/main.rs diff --git a/examples/exit/src/main.rs b/examples/exit/src/main.rs new file mode 100644 index 00000000..c3a190d8 --- /dev/null +++ b/examples/exit/src/main.rs @@ -0,0 +1,75 @@ +use iced::{ + button, Alignment, Button, Column, Element, Sandbox, Settings, Text, +}; + +pub fn main() -> iced::Result { + Exit::run(Settings::default()) +} + +#[derive(Default)] +struct Exit { + show_confirm: bool, + exit: bool, + confirm_button: button::State, + exit_button: button::State, +} + +#[derive(Debug, Clone, Copy)] +enum Message { + Confirm, + Exit, +} + +impl Sandbox for Exit { + type Message = Message; + + fn new() -> Self { + Self::default() + } + + fn title(&self) -> String { + String::from("Exit - Iced") + } + + fn should_exit(&self) -> bool { + self.exit + } + + fn update(&mut self, message: Message) { + match message { + Message::Confirm => { + self.exit = true; + } + Message::Exit => { + self.show_confirm = true; + } + } + } + + fn view(&mut self) -> Element<Message> { + if self.show_confirm { + Column::new() + .padding(20) + .align_items(Alignment::Center) + .push(Text::new("Are you sure you want to exit?")) + .push( + Button::new( + &mut self.confirm_button, + Text::new("Yes, exit now"), + ) + .on_press(Message::Confirm), + ) + .into() + } else { + Column::new() + .padding(20) + .align_items(Alignment::Center) + .push(Text::new("Click the button to exit")) + .push( + Button::new(&mut self.exit_button, Text::new("Exit")) + .on_press(Message::Exit), + ) + .into() + } + } +} diff --git a/src/sandbox.rs b/src/sandbox.rs index 1b23196f..aabfb9c7 100644 --- a/src/sandbox.rs +++ b/src/sandbox.rs @@ -131,6 +131,13 @@ pub trait Sandbox { 1.0 } + /// Returns whether the [`Sandbox`] should be terminated. + /// + /// By default, it returns `false`. + fn should_exit(&self) -> bool { + false + } + /// Runs the [`Sandbox`]. /// /// On native platforms, this method will take control of the current thread @@ -182,4 +189,8 @@ where fn scale_factor(&self) -> f64 { T::scale_factor(self) } + + fn should_exit(&self) -> bool { + T::should_exit(self) + } } |