iced-yoda/src/lib.rs

60 lines
1.2 KiB
Rust
Raw Normal View History

#[cfg_attr(target_arch = "wasm32", path = "web.rs")]
#[cfg_attr(not(target_arch = "wasm32"), path = "winit.rs")]
mod platform;
2019-10-03 00:01:45 +02:00
pub use platform::*;
2019-10-03 00:01:45 +02:00
pub trait Application {
type Message: std::fmt::Debug;
2019-10-03 00:01:45 +02:00
fn update(&mut self, message: Self::Message);
fn view(&mut self) -> Element<Self::Message>;
fn run(self)
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"))]
iced_winit::Application::run(Instance(self));
#[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 = Renderer;
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()
}
}
#[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()
}
}