summaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón <hector0193@gmail.com>2022-01-03 16:23:01 +0700
committerLibravatar GitHub <noreply@github.com>2022-01-03 16:23:01 +0700
commit7ab584ce96dcce3d70928bd3cb07eebe87b56481 (patch)
tree12a6a841f773926c2e5e1ea6ad5277351cdfc609 /examples
parent4db0f4a570a1fb3398062c78770c542d9efb8a64 (diff)
parentecd0997576378caab549cfe537639355e4a75376 (diff)
downloadiced-7ab584ce96dcce3d70928bd3cb07eebe87b56481.tar.gz
iced-7ab584ce96dcce3d70928bd3cb07eebe87b56481.tar.bz2
iced-7ab584ce96dcce3d70928bd3cb07eebe87b56481.zip
Merge pull request #1175 from EkardNT/sandbox-exit
Allow Sandbox applications to exit.
Diffstat (limited to 'examples')
-rw-r--r--examples/exit/Cargo.toml8
-rw-r--r--examples/exit/README.md12
-rw-r--r--examples/exit/src/main.rs84
3 files changed, 104 insertions, 0 deletions
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..c45a8205
--- /dev/null
+++ b/examples/exit/src/main.rs
@@ -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()
+ }
+}