Connect iced_web with iced properly

This commit is contained in:
Héctor Ramón Jiménez 2019-10-23 02:33:07 +02:00
parent 871eb41430
commit c7ef9d0da7
5 changed files with 42 additions and 22 deletions

View file

@ -1,13 +1,8 @@
pub use iced_wgpu::{Primitive, Renderer};
pub use iced_winit::{
button, slider, text, winit, Align, Background, Checkbox, Color, Image,
Justify, Length, Radio, Slider, Text,
};
#[cfg_attr(target_arch = "wasm32", path = "web.rs")]
#[cfg_attr(not(target_arch = "wasm32"), path = "winit.rs")]
mod platform;
pub type Element<'a, Message> = iced_winit::Element<'a, Message, Renderer>;
pub type Row<'a, Message> = iced_winit::Row<'a, Message, Renderer>;
pub type Column<'a, Message> = iced_winit::Column<'a, Message, Renderer>;
pub type Button<'a, Message> = iced_winit::Button<'a, Message, Renderer>;
pub use platform::*;
pub trait Application {
type Message;
@ -20,12 +15,17 @@ pub trait Application {
where
Self: 'static + Sized,
{
iced_winit::Application::run(Instance(self))
#[cfg(not(target_arch = "wasm32"))]
iced_winit::Application::run(Instance(self));
#[cfg(target_arch = "wasm32")]
iced_web::Application::run(Instance(self));
}
}
struct Instance<A: Application>(A);
#[cfg(not(target_arch = "wasm32"))]
impl<A> iced_winit::Application for Instance<A>
where
A: Application,
@ -41,3 +41,19 @@ where
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()
}
}

1
src/web.rs Normal file
View file

@ -0,0 +1 @@
pub use iced_web::*;

11
src/winit.rs Normal file
View file

@ -0,0 +1,11 @@
pub use iced_wgpu::{Primitive, Renderer};
pub use iced_winit::{
button, slider, text, winit, Align, Background, Checkbox, Color, Image,
Justify, Length, Radio, Slider, Text,
};
pub type Element<'a, Message> = iced_winit::Element<'a, Message, Renderer>;
pub type Row<'a, Message> = iced_winit::Row<'a, Message, Renderer>;
pub type Column<'a, Message> = iced_winit::Column<'a, Message, Renderer>;
pub type Button<'a, Message> = iced_winit::Button<'a, Message, Renderer>;

View file

@ -17,8 +17,7 @@ maintenance = { status = "actively-developed" }
[dependencies]
iced_core = { version = "0.1.0-alpha", path = "../core" }
dodrio = "0.1.0"
futures-preview = "=0.3.0-alpha.18"
wasm-bindgen = "0.2.50"
wasm-bindgen = "0.2.51"
[dependencies.web-sys]
version = "0.3.27"

View file

@ -1,5 +1,4 @@
use dodrio::bumpalo;
use futures::Future;
use std::cell::RefCell;
mod bus;
@ -8,16 +7,13 @@ pub mod widget;
pub use bus::Bus;
pub use element::Element;
pub use iced_core::{Align, Color, Justify, Length};
pub use iced_core::{Align, Background, Color, Justify, Length};
pub use widget::*;
pub trait Application {
type Message;
fn update(
&mut self,
message: Self::Message,
) -> Option<Box<dyn Future<Output = Self::Message>>>;
fn update(&mut self, message: Self::Message);
fn view(&mut self) -> Element<Self::Message>;
@ -48,10 +44,7 @@ impl<Message> Instance<Message> {
}
fn update(&mut self, message: Message) {
let mut ui = self.ui.borrow_mut();
// TODO: Resolve futures and publish resulting messages
let _ = ui.update(message);
self.ui.borrow_mut().update(message);
}
}