Skip to content

Allow Sandbox applications to exit. #1175

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 3, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ members = [
"examples/custom_widget",
"examples/download_progress",
"examples/events",
"examples/exit",
"examples/game_of_life",
"examples/geometry",
"examples/integration_opengl",
Expand Down
8 changes: 8 additions & 0 deletions examples/exit/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "exit"
version = "0.1.0"
edition = "2021"
publish = false

[dependencies]
iced = { path = "../.." }
12 changes: 12 additions & 0 deletions examples/exit/README.md
Original file line number Diff line number Diff line change
@@ -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
84 changes: 84 additions & 0 deletions examples/exit/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
use iced::{
button, Alignment, Button, Column, Container, Element, Length, 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> {
let content = if self.show_confirm {
Column::new()
.spacing(10)
.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"),
)
.padding([10, 20])
.on_press(Message::Confirm),
)
} else {
Column::new()
.spacing(10)
.align_items(Alignment::Center)
.push(Text::new("Click the button to exit"))
.push(
Button::new(&mut self.exit_button, Text::new("Exit"))
.padding([10, 20])
.on_press(Message::Exit),
)
};

Container::new(content)
.width(Length::Fill)
.height(Length::Fill)
.padding(20)
.center_x()
.center_y()
.into()
}
}
11 changes: 11 additions & 0 deletions src/sandbox.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -182,4 +189,8 @@ where
fn scale_factor(&self) -> f64 {
T::scale_factor(self)
}

fn should_exit(&self) -> bool {
T::should_exit(self)
}
}