iced-yoda/examples/events/src/main.rs

114 lines
2.8 KiB
Rust
Raw Normal View History

2019-12-05 06:10:47 +01:00
use iced::{
alignment, button, executor, Alignment, Application, Button, Checkbox,
Column, Command, Container, Element, Length, Settings, Subscription, Text,
2019-12-05 06:10:47 +01:00
};
use iced_native::{window, Event};
2019-12-05 06:10:47 +01:00
pub fn main() -> iced::Result {
Events::run(Settings {
exit_on_close_request: false,
..Settings::default()
})
2019-12-05 06:10:47 +01:00
}
2019-12-09 22:39:28 +01:00
#[derive(Debug, Default)]
struct Events {
last: Vec<iced_native::Event>,
2019-12-05 06:10:47 +01:00
enabled: bool,
exit: button::State,
should_exit: bool,
2019-12-05 06:10:47 +01:00
}
#[derive(Debug, Clone)]
enum Message {
2019-12-09 22:39:28 +01:00
EventOccurred(iced_native::Event),
2019-12-05 06:10:47 +01:00
Toggled(bool),
Exit,
2019-12-05 06:10:47 +01:00
}
2019-12-09 22:39:28 +01:00
impl Application for Events {
type Executor = executor::Default;
2019-12-05 06:10:47 +01:00
type Message = Message;
type Flags = ();
2019-12-05 06:10:47 +01:00
fn new(_flags: ()) -> (Events, Command<Message>) {
2019-12-09 22:39:28 +01:00
(Events::default(), Command::none())
2019-12-05 06:10:47 +01:00
}
fn title(&self) -> String {
2019-12-09 22:39:28 +01:00
String::from("Events - Iced")
2019-12-05 06:10:47 +01:00
}
fn update(&mut self, message: Message) -> Command<Message> {
2019-12-05 06:10:47 +01:00
match message {
Message::EventOccurred(event) if self.enabled => {
2019-12-09 22:39:28 +01:00
self.last.push(event);
if self.last.len() > 5 {
let _ = self.last.remove(0);
}
2019-12-05 06:10:47 +01:00
}
Message::EventOccurred(event) => {
if let Event::Window(window::Event::CloseRequested) = event {
self.should_exit = true;
}
}
2019-12-05 06:10:47 +01:00
Message::Toggled(enabled) => {
self.enabled = enabled;
}
Message::Exit => {
self.should_exit = true;
}
2019-12-05 06:10:47 +01:00
};
Command::none()
}
fn subscription(&self) -> Subscription<Message> {
iced_native::subscription::events().map(Message::EventOccurred)
}
fn should_exit(&self) -> bool {
self.should_exit
2019-12-05 06:10:47 +01:00
}
fn view(&mut self) -> Element<Message> {
2019-12-09 22:39:28 +01:00
let events = self.last.iter().fold(
Column::new().spacing(10),
2019-12-09 22:39:28 +01:00
|column, event| {
column.push(Text::new(format!("{:?}", event)).size(40))
2019-12-09 22:39:28 +01:00
},
);
2019-12-05 06:10:47 +01:00
let toggle = Checkbox::new(
self.enabled,
"Listen to runtime events",
Message::Toggled,
);
2019-12-05 06:10:47 +01:00
let exit = Button::new(
&mut self.exit,
Text::new("Exit")
.width(Length::Fill)
.horizontal_alignment(alignment::Horizontal::Center),
)
.width(Length::Units(100))
.padding(10)
.on_press(Message::Exit);
2019-12-05 06:10:47 +01:00
let content = Column::new()
.align_items(Alignment::Center)
2019-12-05 06:10:47 +01:00
.spacing(20)
2019-12-09 22:39:28 +01:00
.push(events)
.push(toggle)
.push(exit);
2019-12-05 06:10:47 +01:00
Container::new(content)
.width(Length::Fill)
.height(Length::Fill)
.center_x()
.center_y()
.into()
}
}