Implement iced::Sandbox trait for simple apps
This commit is contained in:
parent
428509c84a
commit
ba56a561b2
4 changed files with 131 additions and 86 deletions
69
src/application.rs
Normal file
69
src/application.rs
Normal file
|
|
@ -0,0 +1,69 @@
|
|||
use crate::{Command, Element};
|
||||
|
||||
pub trait Application: Sized {
|
||||
type Message: std::fmt::Debug + Send;
|
||||
|
||||
fn new() -> (Self, Command<Self::Message>);
|
||||
|
||||
fn title(&self) -> String;
|
||||
|
||||
fn update(&mut self, message: Self::Message) -> Command<Self::Message>;
|
||||
|
||||
fn view(&mut self) -> Element<Self::Message>;
|
||||
|
||||
fn run()
|
||||
where
|
||||
Self: 'static + Sized,
|
||||
{
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
<Instance<Self> as iced_winit::Application>::run();
|
||||
|
||||
#[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,
|
||||
{
|
||||
type Renderer = iced_wgpu::Renderer;
|
||||
type Message = A::Message;
|
||||
|
||||
fn new() -> (Self, Command<A::Message>) {
|
||||
let (app, command) = A::new();
|
||||
|
||||
(Instance(app), command)
|
||||
}
|
||||
|
||||
fn title(&self) -> String {
|
||||
self.0.title()
|
||||
}
|
||||
|
||||
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()
|
||||
}
|
||||
}
|
||||
72
src/lib.rs
72
src/lib.rs
|
|
@ -1,73 +1,9 @@
|
|||
mod application;
|
||||
#[cfg_attr(target_arch = "wasm32", path = "web.rs")]
|
||||
#[cfg_attr(not(target_arch = "wasm32"), path = "native.rs")]
|
||||
mod platform;
|
||||
mod sandbox;
|
||||
|
||||
pub use application::Application;
|
||||
pub use platform::*;
|
||||
|
||||
pub trait Application: Sized {
|
||||
type Message: std::fmt::Debug + Send;
|
||||
|
||||
fn new() -> (Self, Command<Self::Message>);
|
||||
|
||||
fn title(&self) -> String;
|
||||
|
||||
fn update(&mut self, message: Self::Message) -> Command<Self::Message>;
|
||||
|
||||
fn view(&mut self) -> Element<Self::Message>;
|
||||
|
||||
fn run()
|
||||
where
|
||||
Self: 'static + Sized,
|
||||
{
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
<Instance<Self> as iced_winit::Application>::run();
|
||||
|
||||
#[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,
|
||||
{
|
||||
type Renderer = iced_wgpu::Renderer;
|
||||
type Message = A::Message;
|
||||
|
||||
fn new() -> (Self, Command<A::Message>) {
|
||||
let (app, command) = A::new();
|
||||
|
||||
(Instance(app), command)
|
||||
}
|
||||
|
||||
fn title(&self) -> String {
|
||||
self.0.title()
|
||||
}
|
||||
|
||||
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()
|
||||
}
|
||||
}
|
||||
pub use sandbox::Sandbox;
|
||||
|
|
|
|||
45
src/sandbox.rs
Normal file
45
src/sandbox.rs
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
use crate::{Application, Command, Element};
|
||||
|
||||
pub trait Sandbox {
|
||||
type Message: std::fmt::Debug + Send;
|
||||
|
||||
fn new() -> Self;
|
||||
|
||||
fn title(&self) -> String;
|
||||
|
||||
fn update(&mut self, message: Self::Message);
|
||||
|
||||
fn view(&mut self) -> Element<Self::Message>;
|
||||
|
||||
fn run()
|
||||
where
|
||||
Self: 'static + Sized,
|
||||
{
|
||||
<Self as Application>::run()
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Application for T
|
||||
where
|
||||
T: Sandbox,
|
||||
{
|
||||
type Message = T::Message;
|
||||
|
||||
fn new() -> (Self, Command<T::Message>) {
|
||||
(T::new(), Command::none())
|
||||
}
|
||||
|
||||
fn title(&self) -> String {
|
||||
T::title(self)
|
||||
}
|
||||
|
||||
fn update(&mut self, message: T::Message) -> Command<T::Message> {
|
||||
T::update(self, message);
|
||||
|
||||
Command::none()
|
||||
}
|
||||
|
||||
fn view(&mut self) -> Element<T::Message> {
|
||||
T::view(self)
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue