Purify Animation API and introduce application::timed

This commit is contained in:
Héctor Ramón Jiménez 2025-04-29 03:03:32 +02:00
parent 4334923add
commit 29a19fcde1
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
9 changed files with 281 additions and 104 deletions

View file

@ -21,31 +21,36 @@ use std::time::Instant;
pub fn main() -> iced::Result {
tracing_subscriber::fmt::init();
iced::application(
SolarSystem::default,
iced::application::timed(
SolarSystem::new,
SolarSystem::update,
SolarSystem::subscription,
SolarSystem::view,
)
.subscription(SolarSystem::subscription)
.theme(SolarSystem::theme)
.run()
}
#[derive(Default)]
struct SolarSystem {
state: State,
}
#[derive(Debug, Clone, Copy)]
enum Message {
Tick(Instant),
Tick,
}
impl SolarSystem {
fn update(&mut self, message: Message) {
fn new() -> Self {
Self {
state: State::new(),
}
}
fn update(&mut self, message: Message, now: Instant) {
match message {
Message::Tick(instant) => {
self.state.update(instant);
Message::Tick => {
self.state.update(now);
}
}
}
@ -59,7 +64,7 @@ impl SolarSystem {
}
fn subscription(&self) -> Subscription<Message> {
window::frames().map(Message::Tick)
window::frames().map(|_| Message::Tick)
}
}
@ -105,10 +110,7 @@ impl State {
}
pub fn update(&mut self, now: Instant) {
if self.start > now {
self.start = now;
}
self.start = self.start.min(now);
self.now = now;
self.system_cache.clear();
}
@ -206,9 +208,3 @@ impl<Message> canvas::Program<Message> for State {
vec![background, system]
}
}
impl Default for State {
fn default() -> Self {
Self::new()
}
}