Use Program API in game_of_life example

This commit is contained in:
Héctor Ramón Jiménez 2024-03-17 18:06:37 +01:00
parent 2eb3333623
commit c4be7efce5
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
2 changed files with 22 additions and 36 deletions

View file

@ -5,32 +5,24 @@ mod preset;
use grid::Grid; use grid::Grid;
use preset::Preset; use preset::Preset;
use iced::executor;
use iced::time; use iced::time;
use iced::widget::{ use iced::widget::{
button, checkbox, column, container, pick_list, row, slider, text, button, checkbox, column, container, pick_list, row, slider, text,
}; };
use iced::window; use iced::{Alignment, Command, Element, Length, Subscription, Theme};
use iced::{
Alignment, Application, Command, Element, Length, Settings, Subscription,
Theme,
};
use std::time::Duration; use std::time::Duration;
pub fn main() -> iced::Result { pub fn main() -> iced::Result {
tracing_subscriber::fmt::init(); tracing_subscriber::fmt::init();
GameOfLife::run(Settings { iced::program("Game of Life - Iced", GameOfLife::update, GameOfLife::view)
antialiasing: true, .subscription(GameOfLife::subscription)
window: window::Settings { .theme(|_| Theme::Dark)
position: window::Position::Centered, .antialiasing(true)
..window::Settings::default() .centered()
}, .run()
..Settings::default()
})
} }
#[derive(Default)]
struct GameOfLife { struct GameOfLife {
grid: Grid, grid: Grid,
is_playing: bool, is_playing: bool,
@ -52,24 +44,16 @@ enum Message {
PresetPicked(Preset), PresetPicked(Preset),
} }
impl Application for GameOfLife { impl GameOfLife {
type Message = Message; fn new() -> Self {
type Theme = Theme; Self {
type Executor = executor::Default; grid: Grid::default(),
type Flags = (); is_playing: false,
queued_ticks: 0,
fn new(_flags: ()) -> (Self, Command<Message>) { speed: 5,
( next_speed: None,
Self { version: 0,
speed: 5, }
..Self::default()
},
Command::none(),
)
}
fn title(&self) -> String {
String::from("Game of Life - Iced")
} }
fn update(&mut self, message: Message) -> Command<Message> { fn update(&mut self, message: Message) -> Command<Message> {
@ -154,9 +138,11 @@ impl Application for GameOfLife {
.height(Length::Fill) .height(Length::Fill)
.into() .into()
} }
}
fn theme(&self) -> Theme { impl Default for GameOfLife {
Theme::Dark fn default() -> Self {
Self::new()
} }
} }

View file

@ -152,7 +152,7 @@ impl Todos {
Message::ToggleFullscreen(mode) => { Message::ToggleFullscreen(mode) => {
window::change_mode(window::Id::MAIN, mode) window::change_mode(window::Id::MAIN, mode)
} }
_ => Command::none(), Message::Loaded(_) => Command::none(),
}; };
if !saved { if !saved {