Implement `WasmBindgen` executor and reorganize
This commit is contained in:
parent
90690702e1
commit
04086a90c9
|
@ -27,3 +27,6 @@ features = ["rt-core"]
|
||||||
[dependencies.async-std]
|
[dependencies.async-std]
|
||||||
version = "1.0"
|
version = "1.0"
|
||||||
optional = true
|
optional = true
|
||||||
|
|
||||||
|
[target.'cfg(target_arch = "wasm32")'.dependencies]
|
||||||
|
wasm-bindgen-futures = "0.4"
|
||||||
|
|
|
@ -4,23 +4,29 @@ mod null;
|
||||||
#[cfg(feature = "thread-pool")]
|
#[cfg(feature = "thread-pool")]
|
||||||
mod thread_pool;
|
mod thread_pool;
|
||||||
|
|
||||||
#[cfg(feature = "thread-pool")]
|
|
||||||
pub use thread_pool::ThreadPool;
|
|
||||||
|
|
||||||
#[cfg(feature = "tokio")]
|
#[cfg(feature = "tokio")]
|
||||||
mod tokio;
|
mod tokio;
|
||||||
|
|
||||||
#[cfg(feature = "async-std")]
|
#[cfg(feature = "async-std")]
|
||||||
mod async_std;
|
mod async_std;
|
||||||
|
|
||||||
|
#[cfg(target_arch = "wasm32")]
|
||||||
|
mod wasm_bindgen;
|
||||||
|
|
||||||
pub use null::Null;
|
pub use null::Null;
|
||||||
|
|
||||||
|
#[cfg(feature = "thread-pool")]
|
||||||
|
pub use thread_pool::ThreadPool;
|
||||||
|
|
||||||
#[cfg(feature = "tokio")]
|
#[cfg(feature = "tokio")]
|
||||||
pub use self::tokio::Tokio;
|
pub use self::tokio::Tokio;
|
||||||
|
|
||||||
#[cfg(feature = "async-std")]
|
#[cfg(feature = "async-std")]
|
||||||
pub use self::async_std::AsyncStd;
|
pub use self::async_std::AsyncStd;
|
||||||
|
|
||||||
|
#[cfg(target_arch = "wasm32")]
|
||||||
|
pub use wasm_bindgen::WasmBindgen;
|
||||||
|
|
||||||
use futures::Future;
|
use futures::Future;
|
||||||
|
|
||||||
pub trait Executor: Sized {
|
pub trait Executor: Sized {
|
||||||
|
|
|
@ -0,0 +1,17 @@
|
||||||
|
use crate::Executor;
|
||||||
|
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct WasmBindgen;
|
||||||
|
|
||||||
|
impl Executor for WasmBindgen {
|
||||||
|
fn new() -> Result<Self, futures::io::Error> {
|
||||||
|
Ok(Self)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn spawn(
|
||||||
|
&self,
|
||||||
|
future: impl futures::Future<Output = ()> + Send + 'static,
|
||||||
|
) {
|
||||||
|
wasm_bindgen_futures::spawn_local(future);
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,9 @@
|
||||||
|
/// A generic widget.
|
||||||
|
///
|
||||||
|
/// This is an alias of an `iced_native` element with a default `Renderer`.
|
||||||
|
#[cfg(not(target_arch = "wasm32"))]
|
||||||
|
pub type Element<'a, Message> =
|
||||||
|
iced_winit::Element<'a, Message, iced_wgpu::Renderer>;
|
||||||
|
|
||||||
|
#[cfg(target_arch = "wasm32")]
|
||||||
|
pub use iced_web::Element;
|
|
@ -0,0 +1,54 @@
|
||||||
|
//! Choose your preferred executor to power your application.
|
||||||
|
pub use crate::common::{executor::Null, Executor};
|
||||||
|
|
||||||
|
pub use platform::Default;
|
||||||
|
|
||||||
|
#[cfg(not(target_arch = "wasm32"))]
|
||||||
|
mod platform {
|
||||||
|
use iced_winit::{executor::ThreadPool, futures, Executor};
|
||||||
|
|
||||||
|
/// A default cross-platform executor.
|
||||||
|
///
|
||||||
|
/// - On native platforms, it will use a `iced_futures::executor::ThreadPool`.
|
||||||
|
/// - On the Web, it will use `iced_futures::executor::WasmBindgen`.
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct Default(ThreadPool);
|
||||||
|
|
||||||
|
impl Executor for Default {
|
||||||
|
fn new() -> Result<Self, futures::io::Error> {
|
||||||
|
Ok(Default(ThreadPool::new()?))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn spawn(
|
||||||
|
&self,
|
||||||
|
future: impl futures::Future<Output = ()> + Send + 'static,
|
||||||
|
) {
|
||||||
|
self.0.spawn(future);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(target_arch = "wasm32")]
|
||||||
|
mod platform {
|
||||||
|
use iced_web::{executor::WasmBindgen, futures, Executor};
|
||||||
|
|
||||||
|
/// A default cross-platform executor.
|
||||||
|
///
|
||||||
|
/// - On native platforms, it will use a `iced_futures::executor::ThreadPool`.
|
||||||
|
/// - On the Web, it will use `iced_futures::executor::WasmBindgen`.
|
||||||
|
#[derive(Debug)]
|
||||||
|
pub struct Default(WasmBindgen);
|
||||||
|
|
||||||
|
impl Executor for Default {
|
||||||
|
fn new() -> Result<Self, futures::io::Error> {
|
||||||
|
Ok(Default(WasmBindgen::new()?))
|
||||||
|
}
|
||||||
|
|
||||||
|
fn spawn(
|
||||||
|
&self,
|
||||||
|
future: impl futures::Future<Output = ()> + Send + 'static,
|
||||||
|
) {
|
||||||
|
self.0.spawn(future);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
30
src/lib.rs
30
src/lib.rs
|
@ -180,26 +180,30 @@
|
||||||
#![deny(unsafe_code)]
|
#![deny(unsafe_code)]
|
||||||
#![deny(rust_2018_idioms)]
|
#![deny(rust_2018_idioms)]
|
||||||
mod application;
|
mod application;
|
||||||
|
mod element;
|
||||||
mod sandbox;
|
mod sandbox;
|
||||||
|
|
||||||
#[cfg(not(target_arch = "wasm32"))]
|
pub mod executor;
|
||||||
mod native;
|
|
||||||
|
|
||||||
#[cfg(not(target_arch = "wasm32"))]
|
|
||||||
pub use native::*;
|
|
||||||
|
|
||||||
#[cfg(target_arch = "wasm32")]
|
|
||||||
mod web;
|
|
||||||
|
|
||||||
#[cfg(target_arch = "wasm32")]
|
|
||||||
pub use web::*;
|
|
||||||
|
|
||||||
pub mod settings;
|
pub mod settings;
|
||||||
|
pub mod widget;
|
||||||
pub mod window;
|
pub mod window;
|
||||||
|
|
||||||
#[doc(no_inline)]
|
#[doc(no_inline)]
|
||||||
pub use executor::Executor;
|
pub use widget::*;
|
||||||
|
|
||||||
pub use application::Application;
|
pub use application::Application;
|
||||||
|
pub use element::Element;
|
||||||
|
pub use executor::Executor;
|
||||||
pub use sandbox::Sandbox;
|
pub use sandbox::Sandbox;
|
||||||
pub use settings::Settings;
|
pub use settings::Settings;
|
||||||
|
|
||||||
|
#[cfg(not(target_arch = "wasm32"))]
|
||||||
|
use iced_winit as common;
|
||||||
|
|
||||||
|
#[cfg(target_arch = "wasm32")]
|
||||||
|
use iced_web as common;
|
||||||
|
|
||||||
|
pub use common::{
|
||||||
|
Align, Background, Color, Command, Font, HorizontalAlignment, Length,
|
||||||
|
Space, Subscription, Vector, VerticalAlignment,
|
||||||
|
};
|
||||||
|
|
|
@ -1,68 +0,0 @@
|
||||||
pub use iced_winit::{
|
|
||||||
Align, Background, Color, Command, Font, HorizontalAlignment, Length,
|
|
||||||
Space, Subscription, Vector, VerticalAlignment,
|
|
||||||
};
|
|
||||||
|
|
||||||
pub mod executor;
|
|
||||||
|
|
||||||
pub mod widget {
|
|
||||||
//! Display information and interactive controls in your application.
|
|
||||||
//!
|
|
||||||
//! # Re-exports
|
|
||||||
//! For convenience, the contents of this module are available at the root
|
|
||||||
//! module. Therefore, you can directly type:
|
|
||||||
//!
|
|
||||||
//! ```
|
|
||||||
//! use iced::{button, Button};
|
|
||||||
//! ```
|
|
||||||
//!
|
|
||||||
//! # Stateful widgets
|
|
||||||
//! Some widgets need to keep track of __local state__.
|
|
||||||
//!
|
|
||||||
//! These widgets have their own module with a `State` type. For instance, a
|
|
||||||
//! [`TextInput`] has some [`text_input::State`].
|
|
||||||
//!
|
|
||||||
//! [`TextInput`]: text_input/struct.TextInput.html
|
|
||||||
//! [`text_input::State`]: text_input/struct.State.html
|
|
||||||
pub use iced_wgpu::widget::*;
|
|
||||||
|
|
||||||
pub mod image {
|
|
||||||
//! Display images in your user interface.
|
|
||||||
pub use iced_winit::image::{Handle, Image};
|
|
||||||
}
|
|
||||||
|
|
||||||
pub mod svg {
|
|
||||||
//! Display vector graphics in your user interface.
|
|
||||||
pub use iced_winit::svg::{Handle, Svg};
|
|
||||||
}
|
|
||||||
|
|
||||||
pub use iced_winit::Text;
|
|
||||||
|
|
||||||
#[doc(no_inline)]
|
|
||||||
pub use {
|
|
||||||
button::Button, checkbox::Checkbox, container::Container, image::Image,
|
|
||||||
progress_bar::ProgressBar, radio::Radio, scrollable::Scrollable,
|
|
||||||
slider::Slider, svg::Svg, text_input::TextInput,
|
|
||||||
};
|
|
||||||
|
|
||||||
/// A container that distributes its contents vertically.
|
|
||||||
///
|
|
||||||
/// This is an alias of an `iced_native` column with a default `Renderer`.
|
|
||||||
pub type Column<'a, Message> =
|
|
||||||
iced_winit::Column<'a, Message, iced_wgpu::Renderer>;
|
|
||||||
|
|
||||||
/// A container that distributes its contents horizontally.
|
|
||||||
///
|
|
||||||
/// This is an alias of an `iced_native` row with a default `Renderer`.
|
|
||||||
pub type Row<'a, Message> =
|
|
||||||
iced_winit::Row<'a, Message, iced_wgpu::Renderer>;
|
|
||||||
}
|
|
||||||
|
|
||||||
#[doc(no_inline)]
|
|
||||||
pub use widget::*;
|
|
||||||
|
|
||||||
/// A generic widget.
|
|
||||||
///
|
|
||||||
/// This is an alias of an `iced_native` element with a default `Renderer`.
|
|
||||||
pub type Element<'a, Message> =
|
|
||||||
iced_winit::Element<'a, Message, iced_wgpu::Renderer>;
|
|
|
@ -1,23 +0,0 @@
|
||||||
//! Choose your preferred executor to power your application.
|
|
||||||
pub use iced_winit::{executor::Null, Executor};
|
|
||||||
use iced_winit::{executor::ThreadPool, futures};
|
|
||||||
|
|
||||||
/// The default cross-platform executor.
|
|
||||||
///
|
|
||||||
/// - On native platforms, it will use a `ThreadPool`.
|
|
||||||
/// - On the Web, it will use `wasm-bindgen-futures::spawn_local`.
|
|
||||||
#[derive(Debug)]
|
|
||||||
pub struct Default(ThreadPool);
|
|
||||||
|
|
||||||
impl Executor for Default {
|
|
||||||
fn new() -> Result<Self, futures::io::Error> {
|
|
||||||
Ok(Default(ThreadPool::new()?))
|
|
||||||
}
|
|
||||||
|
|
||||||
fn spawn(
|
|
||||||
&self,
|
|
||||||
future: impl futures::Future<Output = ()> + Send + 'static,
|
|
||||||
) {
|
|
||||||
self.0.spawn(future);
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1 +0,0 @@
|
||||||
pub use iced_web::*;
|
|
|
@ -0,0 +1,60 @@
|
||||||
|
//! Display information and interactive controls in your application.
|
||||||
|
//!
|
||||||
|
//! # Re-exports
|
||||||
|
//! For convenience, the contents of this module are available at the root
|
||||||
|
//! module. Therefore, you can directly type:
|
||||||
|
//!
|
||||||
|
//! ```
|
||||||
|
//! use iced::{button, Button};
|
||||||
|
//! ```
|
||||||
|
//!
|
||||||
|
//! # Stateful widgets
|
||||||
|
//! Some widgets need to keep track of __local state__.
|
||||||
|
//!
|
||||||
|
//! These widgets have their own module with a `State` type. For instance, a
|
||||||
|
//! [`TextInput`] has some [`text_input::State`].
|
||||||
|
//!
|
||||||
|
//! [`TextInput`]: text_input/struct.TextInput.html
|
||||||
|
//! [`text_input::State`]: text_input/struct.State.html
|
||||||
|
#[cfg(not(target_arch = "wasm32"))]
|
||||||
|
mod platform {
|
||||||
|
pub use iced_wgpu::widget::*;
|
||||||
|
|
||||||
|
pub mod image {
|
||||||
|
//! Display images in your user interface.
|
||||||
|
pub use iced_winit::image::{Handle, Image};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub mod svg {
|
||||||
|
//! Display vector graphics in your user interface.
|
||||||
|
pub use iced_winit::svg::{Handle, Svg};
|
||||||
|
}
|
||||||
|
|
||||||
|
pub use iced_winit::Text;
|
||||||
|
|
||||||
|
#[doc(no_inline)]
|
||||||
|
pub use {
|
||||||
|
button::Button, checkbox::Checkbox, container::Container, image::Image,
|
||||||
|
progress_bar::ProgressBar, radio::Radio, scrollable::Scrollable,
|
||||||
|
slider::Slider, svg::Svg, text_input::TextInput,
|
||||||
|
};
|
||||||
|
|
||||||
|
/// A container that distributes its contents vertically.
|
||||||
|
///
|
||||||
|
/// This is an alias of an `iced_native` column with a default `Renderer`.
|
||||||
|
pub type Column<'a, Message> =
|
||||||
|
iced_winit::Column<'a, Message, iced_wgpu::Renderer>;
|
||||||
|
|
||||||
|
/// A container that distributes its contents horizontally.
|
||||||
|
///
|
||||||
|
/// This is an alias of an `iced_native` row with a default `Renderer`.
|
||||||
|
pub type Row<'a, Message> =
|
||||||
|
iced_winit::Row<'a, Message, iced_wgpu::Renderer>;
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(target_arch = "wasm32")]
|
||||||
|
mod platform {
|
||||||
|
pub use iced_web::widget::*;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub use platform::*;
|
|
@ -72,14 +72,19 @@ pub use dodrio;
|
||||||
pub use element::Element;
|
pub use element::Element;
|
||||||
pub use hasher::Hasher;
|
pub use hasher::Hasher;
|
||||||
pub use iced_core::{
|
pub use iced_core::{
|
||||||
Align, Background, Color, Font, HorizontalAlignment, Length,
|
Align, Background, Color, Font, HorizontalAlignment, Length, Vector,
|
||||||
VerticalAlignment,
|
VerticalAlignment,
|
||||||
};
|
};
|
||||||
pub use iced_futures::{futures, Command};
|
pub use iced_futures::{executor, futures, Command};
|
||||||
pub use style::Style;
|
pub use style::Style;
|
||||||
pub use subscription::Subscription;
|
pub use subscription::Subscription;
|
||||||
|
|
||||||
|
#[doc(no_inline)]
|
||||||
pub use widget::*;
|
pub use widget::*;
|
||||||
|
|
||||||
|
#[doc(no_inline)]
|
||||||
|
pub use executor::Executor;
|
||||||
|
|
||||||
/// An interactive web application.
|
/// An interactive web application.
|
||||||
///
|
///
|
||||||
/// This trait is the main entrypoint of Iced. Once implemented, you can run
|
/// This trait is the main entrypoint of Iced. Once implemented, you can run
|
||||||
|
|
Loading…
Reference in New Issue