summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorLibravatar Héctor Ramón <hector0193@gmail.com>2020-03-31 17:49:56 +0200
committerLibravatar GitHub <noreply@github.com>2020-03-31 17:49:56 +0200
commite79e832092385346beec47f63e40f24800c535f8 (patch)
treec29df36b1987de66a58cfb981c5958d25712d51e /src
parent327347501c78a1c8e55a39fe1b5d54d7c2fcbcab (diff)
parentf0ebcc24742aba79cc779a4145a188f2534a5e35 (diff)
downloadiced-e79e832092385346beec47f63e40f24800c535f8.tar.gz
iced-e79e832092385346beec47f63e40f24800c535f8.tar.bz2
iced-e79e832092385346beec47f63e40f24800c535f8.zip
Merge pull request #246 from hecrj/feature/application-flags
Application flags
Diffstat (limited to '')
-rw-r--r--src/application.rs49
-rw-r--r--src/executor.rs10
-rw-r--r--src/sandbox.rs5
-rw-r--r--src/settings.rs12
4 files changed, 53 insertions, 23 deletions
diff --git a/src/application.rs b/src/application.rs
index 2ee3337f..551a9a79 100644
--- a/src/application.rs
+++ b/src/application.rs
@@ -41,8 +41,9 @@ use crate::{window, Command, Element, Executor, Settings, Subscription};
/// impl Application for Counter {
/// type Executor = executor::Null;
/// type Message = Message;
+/// type Flags = ();
///
-/// fn new() -> (Self, Command<Message>) {
+/// fn new(_flags: ()) -> (Self, Command<Message>) {
/// (Self::default(), Command::none())
/// }
///
@@ -94,7 +95,13 @@ pub trait Application: Sized {
/// [`Application`]: trait.Application.html
type Message: std::fmt::Debug + Send;
- /// Initializes the [`Application`].
+ /// The data needed to initialize your [`Application`].
+ ///
+ /// [`Application`]: trait.Application.html
+ type Flags;
+
+ /// Initializes the [`Application`] with the flags provided to
+ /// [`run`] as part of the [`Settings`]:
///
/// Here is where you should return the initial state of your app.
///
@@ -104,7 +111,9 @@ pub trait Application: Sized {
/// request, etc.
///
/// [`Application`]: trait.Application.html
- fn new() -> (Self, Command<Self::Message>);
+ /// [`run`]: #method.run.html
+ /// [`Settings`]: struct.Settings.html
+ fn new(flags: Self::Flags) -> (Self, Command<Self::Message>);
/// Returns the current title of the [`Application`].
///
@@ -169,26 +178,30 @@ pub trait Application: Sized {
/// It should probably be that last thing you call in your `main` function.
///
/// [`Application`]: trait.Application.html
- fn run(_settings: Settings)
+ fn run(settings: Settings<Self::Flags>)
where
Self: 'static,
{
#[cfg(not(target_arch = "wasm32"))]
- <Instance<Self> as iced_winit::Application>::run(
- _settings.into(),
- iced_wgpu::Settings {
- default_font: _settings.default_font,
- antialiasing: if _settings.antialiasing {
+ {
+ let wgpu_settings = iced_wgpu::Settings {
+ default_font: settings.default_font,
+ antialiasing: if settings.antialiasing {
Some(iced_wgpu::settings::Antialiasing::MSAAx4)
} else {
None
},
..iced_wgpu::Settings::default()
- },
- );
+ };
+
+ <Instance<Self> as iced_winit::Application>::run(
+ settings.into(),
+ wgpu_settings,
+ );
+ }
#[cfg(target_arch = "wasm32")]
- <Instance<Self> as iced_web::Application>::run();
+ <Instance<Self> as iced_web::Application>::run(settings.flags);
}
}
@@ -201,10 +214,11 @@ where
{
type Backend = iced_wgpu::window::Backend;
type Executor = A::Executor;
+ type Flags = A::Flags;
type Message = A::Message;
- fn new() -> (Self, Command<A::Message>) {
- let (app, command) = A::new();
+ fn new(flags: Self::Flags) -> (Self, Command<A::Message>) {
+ let (app, command) = A::new(flags);
(Instance(app), command)
}
@@ -238,11 +252,12 @@ impl<A> iced_web::Application for Instance<A>
where
A: Application,
{
- type Message = A::Message;
type Executor = A::Executor;
+ type Message = A::Message;
+ type Flags = A::Flags;
- fn new() -> (Self, Command<A::Message>) {
- let (app, command) = A::new();
+ fn new(flags: Self::Flags) -> (Self, Command<A::Message>) {
+ let (app, command) = A::new(flags);
(Instance(app), command)
}
diff --git a/src/executor.rs b/src/executor.rs
index b4be5264..6c5425d1 100644
--- a/src/executor.rs
+++ b/src/executor.rs
@@ -51,7 +51,11 @@ mod platform {
/// A default cross-platform executor.
///
- /// - On native platforms, it will use `iced_futures::executor::ThreadPool`.
+ /// - On native platforms, it will use:
+ /// - `iced_futures::executor::Tokio` when the `tokio` feature is enabled.
+ /// - `iced_futures::executor::AsyncStd` when the `async-std` feature is
+ /// enabled.
+ /// - `iced_futures::executor::ThreadPool` otherwise.
/// - On the Web, it will use `iced_futures::executor::WasmBindgen`.
#[derive(Debug)]
pub struct Default(WasmBindgen);
@@ -64,5 +68,9 @@ mod platform {
fn spawn(&self, future: impl futures::Future<Output = ()> + 'static) {
self.0.spawn(future);
}
+
+ fn enter<R>(&self, f: impl FnOnce() -> R) -> R {
+ self.0.enter(f)
+ }
}
}
diff --git a/src/sandbox.rs b/src/sandbox.rs
index 2c0332ff..4ea8de98 100644
--- a/src/sandbox.rs
+++ b/src/sandbox.rs
@@ -121,7 +121,7 @@ pub trait Sandbox {
/// It should probably be that last thing you call in your `main` function.
///
/// [`Sandbox`]: trait.Sandbox.html
- fn run(settings: Settings)
+ fn run(settings: Settings<()>)
where
Self: 'static + Sized,
{
@@ -134,9 +134,10 @@ where
T: Sandbox,
{
type Executor = executor::Null;
+ type Flags = ();
type Message = T::Message;
- fn new() -> (Self, Command<T::Message>) {
+ fn new(_flags: ()) -> (Self, Command<T::Message>) {
(T::new(), Command::none())
}
diff --git a/src/settings.rs b/src/settings.rs
index 32ec583c..f36ec85f 100644
--- a/src/settings.rs
+++ b/src/settings.rs
@@ -3,7 +3,7 @@ use crate::window;
/// The settings of an application.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
-pub struct Settings {
+pub struct Settings<Flags> {
/// The window settings.
///
/// They will be ignored on the Web.
@@ -11,6 +11,11 @@ pub struct Settings {
/// [`Window`]: struct.Window.html
pub window: window::Settings,
+ /// The data needed to initialize an [`Application`].
+ ///
+ /// [`Application`]: trait.Application.html
+ pub flags: Flags,
+
/// The bytes of the font that will be used by default.
///
/// If `None` is provided, a default system font will be chosen.
@@ -28,8 +33,8 @@ pub struct Settings {
}
#[cfg(not(target_arch = "wasm32"))]
-impl From<Settings> for iced_winit::Settings {
- fn from(settings: Settings) -> iced_winit::Settings {
+impl<Flags> From<Settings<Flags>> for iced_winit::Settings<Flags> {
+ fn from(settings: Settings<Flags>) -> iced_winit::Settings<Flags> {
iced_winit::Settings {
window: iced_winit::settings::Window {
size: settings.window.size,
@@ -37,6 +42,7 @@ impl From<Settings> for iced_winit::Settings {
decorations: settings.window.decorations,
platform_specific: Default::default(),
},
+ flags: settings.flags,
}
}
}