From ba56a561b254c9a5f3d23cb54d23dc311759ab4c Mon Sep 17 00:00:00 2001 From: Héctor Ramón Jiménez Date: Thu, 21 Nov 2019 18:00:27 +0100 Subject: Implement `iced::Sandbox` trait for simple apps --- src/application.rs | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 src/application.rs (limited to 'src/application.rs') diff --git a/src/application.rs b/src/application.rs new file mode 100644 index 00000000..ba8da446 --- /dev/null +++ b/src/application.rs @@ -0,0 +1,69 @@ +use crate::{Command, Element}; + +pub trait Application: Sized { + type Message: std::fmt::Debug + Send; + + fn new() -> (Self, Command); + + fn title(&self) -> String; + + fn update(&mut self, message: Self::Message) -> Command; + + fn view(&mut self) -> Element; + + fn run() + where + Self: 'static + Sized, + { + #[cfg(not(target_arch = "wasm32"))] + as iced_winit::Application>::run(); + + #[cfg(target_arch = "wasm32")] + iced_web::Application::run(Instance(self)); + } +} + +struct Instance(A); + +#[cfg(not(target_arch = "wasm32"))] +impl iced_winit::Application for Instance +where + A: Application, +{ + type Renderer = iced_wgpu::Renderer; + type Message = A::Message; + + fn new() -> (Self, Command) { + let (app, command) = A::new(); + + (Instance(app), command) + } + + fn title(&self) -> String { + self.0.title() + } + + fn update(&mut self, message: Self::Message) -> Command { + self.0.update(message) + } + + fn view(&mut self) -> Element { + self.0.view() + } +} + +#[cfg(target_arch = "wasm32")] +impl iced_web::Application for Instance +where + A: Application, +{ + type Message = A::Message; + + fn update(&mut self, message: Self::Message) { + self.0.update(message); + } + + fn view(&mut self) -> Element { + self.0.view() + } +} -- cgit