Showcase graceful exiting in `events` example
This commit is contained in:
parent
67db13ff7c
commit
8f952452ce
|
@ -1,22 +1,30 @@
|
||||||
use iced::{
|
use iced::{
|
||||||
executor, Align, Application, Checkbox, Clipboard, Column, Command,
|
button, executor, Align, Application, Button, Checkbox, Clipboard, Column,
|
||||||
Container, Element, Length, Settings, Subscription, Text,
|
Command, Container, Element, HorizontalAlignment, Length, Settings,
|
||||||
|
Subscription, Text,
|
||||||
};
|
};
|
||||||
|
use iced_native::{window, Event};
|
||||||
|
|
||||||
pub fn main() -> iced::Result {
|
pub fn main() -> iced::Result {
|
||||||
Events::run(Settings::default())
|
Events::run(Settings {
|
||||||
|
exit_on_close_request: false,
|
||||||
|
..Settings::default()
|
||||||
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Default)]
|
#[derive(Debug, Default)]
|
||||||
struct Events {
|
struct Events {
|
||||||
last: Vec<iced_native::Event>,
|
last: Vec<iced_native::Event>,
|
||||||
enabled: bool,
|
enabled: bool,
|
||||||
|
exit: button::State,
|
||||||
|
should_exit: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
enum Message {
|
enum Message {
|
||||||
EventOccurred(iced_native::Event),
|
EventOccurred(iced_native::Event),
|
||||||
Toggled(bool),
|
Toggled(bool),
|
||||||
|
Exit,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Application for Events {
|
impl Application for Events {
|
||||||
|
@ -38,27 +46,35 @@ impl Application for Events {
|
||||||
_clipboard: &mut Clipboard,
|
_clipboard: &mut Clipboard,
|
||||||
) -> Command<Message> {
|
) -> Command<Message> {
|
||||||
match message {
|
match message {
|
||||||
Message::EventOccurred(event) => {
|
Message::EventOccurred(event) if self.enabled => {
|
||||||
self.last.push(event);
|
self.last.push(event);
|
||||||
|
|
||||||
if self.last.len() > 5 {
|
if self.last.len() > 5 {
|
||||||
let _ = self.last.remove(0);
|
let _ = self.last.remove(0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Message::EventOccurred(event) => {
|
||||||
|
if let Event::Window(window::Event::CloseRequested) = event {
|
||||||
|
self.should_exit = true;
|
||||||
|
}
|
||||||
|
}
|
||||||
Message::Toggled(enabled) => {
|
Message::Toggled(enabled) => {
|
||||||
self.enabled = enabled;
|
self.enabled = enabled;
|
||||||
}
|
}
|
||||||
|
Message::Exit => {
|
||||||
|
self.should_exit = true;
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
Command::none()
|
Command::none()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn subscription(&self) -> Subscription<Message> {
|
fn subscription(&self) -> Subscription<Message> {
|
||||||
if self.enabled {
|
|
||||||
iced_native::subscription::events().map(Message::EventOccurred)
|
iced_native::subscription::events().map(Message::EventOccurred)
|
||||||
} else {
|
|
||||||
Subscription::none()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn should_exit(&self) -> bool {
|
||||||
|
self.should_exit
|
||||||
}
|
}
|
||||||
|
|
||||||
fn view(&mut self) -> Element<Message> {
|
fn view(&mut self) -> Element<Message> {
|
||||||
|
@ -75,11 +91,22 @@ impl Application for Events {
|
||||||
Message::Toggled,
|
Message::Toggled,
|
||||||
);
|
);
|
||||||
|
|
||||||
|
let exit = Button::new(
|
||||||
|
&mut self.exit,
|
||||||
|
Text::new("Exit")
|
||||||
|
.width(Length::Fill)
|
||||||
|
.horizontal_alignment(HorizontalAlignment::Center),
|
||||||
|
)
|
||||||
|
.width(Length::Units(100))
|
||||||
|
.padding(10)
|
||||||
|
.on_press(Message::Exit);
|
||||||
|
|
||||||
let content = Column::new()
|
let content = Column::new()
|
||||||
.align_items(Align::Center)
|
.align_items(Align::Center)
|
||||||
.spacing(20)
|
.spacing(20)
|
||||||
.push(events)
|
.push(events)
|
||||||
.push(toggle);
|
.push(toggle)
|
||||||
|
.push(exit);
|
||||||
|
|
||||||
Container::new(content)
|
Container::new(content)
|
||||||
.width(Length::Fill)
|
.width(Length::Fill)
|
||||||
|
|
Loading…
Reference in New Issue