iced-yoda/src/lib.rs

74 lines
1.6 KiB
Rust
Raw Normal View History

#[cfg_attr(target_arch = "wasm32", path = "web.rs")]
#[cfg_attr(not(target_arch = "wasm32"), path = "native.rs")]
mod platform;
2019-10-03 00:01:45 +02:00
pub use platform::*;
2019-10-03 00:01:45 +02:00
2019-11-17 07:09:46 +01:00
pub trait Application: Sized {
type Message: std::fmt::Debug + Send;
fn new() -> (Self, Command<Self::Message>);
2019-10-03 00:01:45 +02:00
fn title(&self) -> String;
2019-11-17 07:09:46 +01:00
fn update(&mut self, message: Self::Message) -> Command<Self::Message>;
2019-10-03 00:01:45 +02:00
fn view(&mut self) -> Element<Self::Message>;
2019-11-17 07:09:46 +01:00
fn run()
2019-10-03 00:01:45 +02:00
where
2019-10-03 01:59:16 +02:00
Self: 'static + Sized,
2019-10-03 00:01:45 +02:00
{
#[cfg(not(target_arch = "wasm32"))]
2019-11-17 07:09:46 +01:00
<Instance<Self> as iced_winit::Application>::run();
#[cfg(target_arch = "wasm32")]
iced_web::Application::run(Instance(self));
2019-10-03 00:01:45 +02:00
}
}
struct Instance<A: Application>(A);
#[cfg(not(target_arch = "wasm32"))]
impl<A> iced_winit::Application for Instance<A>
where
A: Application,
{
type Renderer = iced_wgpu::Renderer;
type Message = A::Message;
2019-11-17 07:09:46 +01:00
fn new() -> (Self, Command<A::Message>) {
let (app, command) = A::new();
(Instance(app), command)
}
fn title(&self) -> String {
self.0.title()
}
2019-11-17 07:09:46 +01:00
fn update(&mut self, message: Self::Message) -> Command<Self::Message> {
self.0.update(message)
}
fn view(&mut self) -> Element<Self::Message> {
self.0.view()
}
}
#[cfg(target_arch = "wasm32")]
impl<A> iced_web::Application for Instance<A>
where
A: Application,
{
type Message = A::Message;
fn update(&mut self, message: Self::Message) {
self.0.update(message);
}
fn view(&mut self) -> Element<Self::Message> {
self.0.view()
}
}