From 8a70d10401eb1277718a19f47ff1e2a5c4c7564b Mon Sep 17 00:00:00 2001 From: Drake Tetreault Date: Wed, 29 Dec 2021 09:31:43 -0800 Subject: Allow Sandbox applications to exit. --- examples/exit/Cargo.toml | 8 +++++ examples/exit/README.md | 12 ++++++++ examples/exit/src/main.rs | 75 +++++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 95 insertions(+) create mode 100644 examples/exit/Cargo.toml create mode 100644 examples/exit/README.md create mode 100644 examples/exit/src/main.rs (limited to 'examples') 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 { + 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() + } + } +} -- cgit From ecd0997576378caab549cfe537639355e4a75376 Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Mon, 3 Jan 2022 12:09:07 +0700 Subject: Center contents with `Container` in `exit` example ... also add some `padding` to buttons! --- examples/exit/src/main.rs | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) (limited to 'examples') diff --git a/examples/exit/src/main.rs b/examples/exit/src/main.rs index c3a190d8..c45a8205 100644 --- a/examples/exit/src/main.rs +++ b/examples/exit/src/main.rs @@ -1,5 +1,6 @@ use iced::{ - button, Alignment, Button, Column, Element, Sandbox, Settings, Text, + button, Alignment, Button, Column, Container, Element, Length, Sandbox, + Settings, Text, }; pub fn main() -> iced::Result { @@ -47,9 +48,9 @@ impl Sandbox for Exit { } fn view(&mut self) -> Element { - if self.show_confirm { + let content = if self.show_confirm { Column::new() - .padding(20) + .spacing(10) .align_items(Alignment::Center) .push(Text::new("Are you sure you want to exit?")) .push( @@ -57,19 +58,27 @@ impl Sandbox for Exit { &mut self.confirm_button, Text::new("Yes, exit now"), ) + .padding([10, 20]) .on_press(Message::Confirm), ) - .into() } else { Column::new() - .padding(20) + .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), ) - .into() - } + }; + + Container::new(content) + .width(Length::Fill) + .height(Length::Fill) + .padding(20) + .center_x() + .center_y() + .into() } } -- cgit