Introduce Program API

This commit is contained in:
Héctor Ramón Jiménez 2024-03-16 05:33:47 +01:00
parent 0524e9b457
commit c22269bff3
No known key found for this signature in database
GPG key ID: 7CC46565708259A7
43 changed files with 1141 additions and 831 deletions

View file

@ -6,8 +6,6 @@
//! Inspired by the example found in the MDN docs[1].
//!
//! [1]: https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API/Tutorial/Basic_animations#An_animated_solar_system
use iced::application;
use iced::executor;
use iced::mouse;
use iced::widget::canvas;
use iced::widget::canvas::gradient;
@ -15,8 +13,8 @@ use iced::widget::canvas::stroke::{self, Stroke};
use iced::widget::canvas::Path;
use iced::window;
use iced::{
Application, Color, Command, Element, Length, Point, Rectangle, Renderer,
Settings, Size, Subscription, Theme, Vector,
Color, Element, Length, Point, Rectangle, Renderer, Size, Subscription,
Theme, Vector,
};
use std::time::Instant;
@ -24,12 +22,14 @@ use std::time::Instant;
pub fn main() -> iced::Result {
tracing_subscriber::fmt::init();
SolarSystem::run(Settings {
antialiasing: true,
..Settings::default()
})
iced::sandbox(SolarSystem::update, SolarSystem::view)
.subscription(SolarSystem::subscription)
.theme(SolarSystem::theme)
.title("Solar System - Iced")
.run()
}
#[derive(Default)]
struct SolarSystem {
state: State,
}
@ -39,33 +39,13 @@ enum Message {
Tick(Instant),
}
impl Application for SolarSystem {
type Executor = executor::Default;
type Message = Message;
type Theme = Theme;
type Flags = ();
fn new(_flags: ()) -> (Self, Command<Message>) {
(
SolarSystem {
state: State::new(),
},
Command::none(),
)
}
fn title(&self) -> String {
String::from("Solar system - Iced")
}
fn update(&mut self, message: Message) -> Command<Message> {
impl SolarSystem {
fn update(&mut self, message: Message) {
match message {
Message::Tick(instant) => {
self.state.update(instant);
}
}
Command::none()
}
fn view(&self) -> Element<Message> {
@ -76,14 +56,7 @@ impl Application for SolarSystem {
}
fn theme(&self) -> Theme {
Theme::Dark
}
fn style(&self, _theme: &Theme) -> application::Appearance {
application::Appearance {
background_color: Color::BLACK,
text_color: Color::WHITE,
}
Theme::Moonfly
}
fn subscription(&self) -> Subscription<Message> {
@ -224,3 +197,9 @@ impl<Message> canvas::Program<Message> for State {
vec![background, system]
}
}
impl Default for State {
fn default() -> Self {
Self::new()
}
}