Allow passing external state to Application::new

This commit is contained in:
Héctor Ramón Jiménez 2020-03-30 18:00:15 +02:00
parent 6e9ab1cd6f
commit c4c5216e3b
13 changed files with 103 additions and 60 deletions

View file

@ -23,8 +23,9 @@ enum Message {
impl Application for Clock { impl Application for Clock {
type Executor = executor::Default; type Executor = executor::Default;
type Message = Message; type Message = Message;
type Flags = ();
fn new() -> (Self, Command<Message>) { fn new(_flags: ()) -> (Self, Command<Message>) {
( (
Clock { Clock {
now: chrono::Local::now().into(), now: chrono::Local::now().into(),

View file

@ -26,8 +26,9 @@ pub enum Message {
impl Application for Example { impl Application for Example {
type Executor = executor::Default; type Executor = executor::Default;
type Message = Message; type Message = Message;
type Flags = ();
fn new() -> (Example, Command<Message>) { fn new(_flags: ()) -> (Example, Command<Message>) {
( (
Example::Idle { Example::Idle {
button: button::State::new(), button: button::State::new(),

View file

@ -22,8 +22,9 @@ enum Message {
impl Application for Events { impl Application for Events {
type Executor = executor::Default; type Executor = executor::Default;
type Message = Message; type Message = Message;
type Flags = ();
fn new() -> (Events, Command<Message>) { fn new(_flags: ()) -> (Events, Command<Message>) {
(Events::default(), Command::none()) (Events::default(), Command::none())
} }

View file

@ -29,8 +29,9 @@ enum Message {
impl Application for Pokedex { impl Application for Pokedex {
type Executor = iced::executor::Default; type Executor = iced::executor::Default;
type Message = Message; type Message = Message;
type Flags = ();
fn new() -> (Pokedex, Command<Message>) { fn new(_flags: ()) -> (Pokedex, Command<Message>) {
( (
Pokedex::Loading, Pokedex::Loading,
Command::perform(Pokemon::search(), Message::PokemonFound), Command::perform(Pokemon::search(), Message::PokemonFound),

View file

@ -7,8 +7,8 @@
//! //!
//! [1]: https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API/Tutorial/Basic_animations#An_animated_solar_system //! [1]: https://developer.mozilla.org/en-US/docs/Web/API/Canvas_API/Tutorial/Basic_animations#An_animated_solar_system
use iced::{ use iced::{
canvas, executor, Application, Canvas, Color, Command, Container, Element, canvas, executor, window, Application, Canvas, Color, Command, Container,
Length, Point, Settings, Size, Subscription, Vector, Element, Length, Point, Settings, Size, Subscription, Vector,
}; };
use std::time::Instant; use std::time::Instant;
@ -33,8 +33,9 @@ enum Message {
impl Application for SolarSystem { impl Application for SolarSystem {
type Executor = executor::Default; type Executor = executor::Default;
type Message = Message; type Message = Message;
type Flags = ();
fn new() -> (Self, Command<Message>) { fn new(_flags: ()) -> (Self, Command<Message>) {
( (
SolarSystem { SolarSystem {
state: State::new(), state: State::new(),
@ -95,7 +96,7 @@ impl State {
pub fn new() -> State { pub fn new() -> State {
let now = Instant::now(); let now = Instant::now();
let (width, height) = Settings::default().window.size; let (width, height) = window::Settings::default().size;
State { State {
start: now, start: now,

View file

@ -30,8 +30,9 @@ enum Message {
impl Application for Stopwatch { impl Application for Stopwatch {
type Executor = iced_futures::executor::AsyncStd; type Executor = iced_futures::executor::AsyncStd;
type Message = Message; type Message = Message;
type Flags = ();
fn new() -> (Stopwatch, Command<Message>) { fn new(_flags: ()) -> (Stopwatch, Command<Message>) {
( (
Stopwatch { Stopwatch {
duration: Duration::default(), duration: Duration::default(),

View file

@ -40,8 +40,9 @@ enum Message {
impl Application for Todos { impl Application for Todos {
type Executor = iced::executor::Default; type Executor = iced::executor::Default;
type Message = Message; type Message = Message;
type Flags = ();
fn new() -> (Todos, Command<Message>) { fn new(_flags: ()) -> (Todos, Command<Message>) {
( (
Todos::Loading, Todos::Loading,
Command::perform(SavedState::load(), Message::Loaded), Command::perform(SavedState::load(), Message::Loaded),

View file

@ -41,8 +41,9 @@ use crate::{window, Command, Element, Executor, Settings, Subscription};
/// impl Application for Counter { /// impl Application for Counter {
/// type Executor = executor::Null; /// type Executor = executor::Null;
/// type Message = Message; /// type Message = Message;
/// type Flags = ();
/// ///
/// fn new() -> (Self, Command<Message>) { /// fn new(_flags: ()) -> (Self, Command<Message>) {
/// (Self::default(), Command::none()) /// (Self::default(), Command::none())
/// } /// }
/// ///
@ -94,7 +95,13 @@ pub trait Application: Sized {
/// [`Application`]: trait.Application.html /// [`Application`]: trait.Application.html
type Message: std::fmt::Debug + Send; 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. /// Here is where you should return the initial state of your app.
/// ///
@ -104,7 +111,9 @@ pub trait Application: Sized {
/// request, etc. /// request, etc.
/// ///
/// [`Application`]: trait.Application.html /// [`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`]. /// 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. /// It should probably be that last thing you call in your `main` function.
/// ///
/// [`Application`]: trait.Application.html /// [`Application`]: trait.Application.html
fn run(_settings: Settings) fn run(settings: Settings<Self::Flags>)
where where
Self: 'static, Self: 'static,
{ {
#[cfg(not(target_arch = "wasm32"))] #[cfg(not(target_arch = "wasm32"))]
<Instance<Self> as iced_winit::Application>::run( {
_settings.into(), let wgpu_settings = iced_wgpu::Settings {
iced_wgpu::Settings { default_font: settings.default_font,
default_font: _settings.default_font, antialiasing: if settings.antialiasing {
antialiasing: if _settings.antialiasing {
Some(iced_wgpu::settings::Antialiasing::MSAAx4) Some(iced_wgpu::settings::Antialiasing::MSAAx4)
} else { } else {
None None
}, },
..iced_wgpu::Settings::default() ..iced_wgpu::Settings::default()
}, };
);
<Instance<Self> as iced_winit::Application>::run(
settings.into(),
wgpu_settings,
);
}
#[cfg(target_arch = "wasm32")] #[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 Backend = iced_wgpu::window::Backend;
type Executor = A::Executor; type Executor = A::Executor;
type Flags = A::Flags;
type Message = A::Message; type Message = A::Message;
fn new() -> (Self, Command<A::Message>) { fn new(flags: Self::Flags) -> (Self, Command<A::Message>) {
let (app, command) = A::new(); let (app, command) = A::new(flags);
(Instance(app), command) (Instance(app), command)
} }
@ -238,11 +252,12 @@ impl<A> iced_web::Application for Instance<A>
where where
A: Application, A: Application,
{ {
type Message = A::Message;
type Executor = A::Executor; type Executor = A::Executor;
type Message = A::Message;
type Flags = A::Flags;
fn new() -> (Self, Command<A::Message>) { fn new(flags: Self::Flags) -> (Self, Command<A::Message>) {
let (app, command) = A::new(); let (app, command) = A::new(flags);
(Instance(app), command) (Instance(app), command)
} }

View file

@ -121,7 +121,7 @@ pub trait Sandbox {
/// It should probably be that last thing you call in your `main` function. /// It should probably be that last thing you call in your `main` function.
/// ///
/// [`Sandbox`]: trait.Sandbox.html /// [`Sandbox`]: trait.Sandbox.html
fn run(settings: Settings) fn run(settings: Settings<()>)
where where
Self: 'static + Sized, Self: 'static + Sized,
{ {
@ -134,9 +134,10 @@ where
T: Sandbox, T: Sandbox,
{ {
type Executor = executor::Null; type Executor = executor::Null;
type Flags = ();
type Message = T::Message; type Message = T::Message;
fn new() -> (Self, Command<T::Message>) { fn new(_flags: ()) -> (Self, Command<T::Message>) {
(T::new(), Command::none()) (T::new(), Command::none())
} }

View file

@ -3,7 +3,7 @@ use crate::window;
/// The settings of an application. /// The settings of an application.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)] #[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub struct Settings { pub struct Settings<Flags> {
/// The window settings. /// The window settings.
/// ///
/// They will be ignored on the Web. /// They will be ignored on the Web.
@ -11,6 +11,11 @@ pub struct Settings {
/// [`Window`]: struct.Window.html /// [`Window`]: struct.Window.html
pub window: window::Settings, 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. /// The bytes of the font that will be used by default.
/// ///
/// If `None` is provided, a default system font will be chosen. /// If `None` is provided, a default system font will be chosen.
@ -28,8 +33,8 @@ pub struct Settings {
} }
#[cfg(not(target_arch = "wasm32"))] #[cfg(not(target_arch = "wasm32"))]
impl From<Settings> for iced_winit::Settings { impl<Flags> From<Settings<Flags>> for iced_winit::Settings<Flags> {
fn from(settings: Settings) -> iced_winit::Settings { fn from(settings: Settings<Flags>) -> iced_winit::Settings<Flags> {
iced_winit::Settings { iced_winit::Settings {
window: iced_winit::settings::Window { window: iced_winit::settings::Window {
size: settings.window.size, size: settings.window.size,
@ -37,6 +42,7 @@ impl From<Settings> for iced_winit::Settings {
decorations: settings.window.decorations, decorations: settings.window.decorations,
platform_specific: Default::default(), platform_specific: Default::default(),
}, },
flags: settings.flags,
} }
} }
} }

View file

@ -94,11 +94,6 @@ pub use executor::Executor;
/// An [`Application`](trait.Application.html) can execute asynchronous actions /// An [`Application`](trait.Application.html) can execute asynchronous actions
/// by returning a [`Command`](struct.Command.html) in some of its methods. /// by returning a [`Command`](struct.Command.html) in some of its methods.
pub trait Application { pub trait Application {
/// The type of __messages__ your [`Application`] will produce.
///
/// [`Application`]: trait.Application.html
type Message: Send;
/// The [`Executor`] that will run commands and subscriptions. /// The [`Executor`] that will run commands and subscriptions.
/// ///
/// The [`executor::WasmBindgen`] can be a good choice for the Web. /// The [`executor::WasmBindgen`] can be a good choice for the Web.
@ -107,6 +102,16 @@ pub trait Application {
/// [`executor::Default`]: executor/struct.Default.html /// [`executor::Default`]: executor/struct.Default.html
type Executor: Executor; type Executor: Executor;
/// The type of __messages__ your [`Application`] will produce.
///
/// [`Application`]: trait.Application.html
type Message: Send;
/// The data needed to initialize your [`Application`].
///
/// [`Application`]: trait.Application.html
type Flags;
/// Initializes the [`Application`]. /// Initializes the [`Application`].
/// ///
/// Here is where you should return the initial state of your app. /// Here is where you should return the initial state of your app.
@ -117,7 +122,7 @@ pub trait Application {
/// request, etc. /// request, etc.
/// ///
/// [`Application`]: trait.Application.html /// [`Application`]: trait.Application.html
fn new() -> (Self, Command<Self::Message>) fn new(flags: Self::Flags) -> (Self, Command<Self::Message>)
where where
Self: Sized; Self: Sized;
@ -165,21 +170,16 @@ pub trait Application {
/// Runs the [`Application`]. /// Runs the [`Application`].
/// ///
/// [`Application`]: trait.Application.html /// [`Application`]: trait.Application.html
fn run() fn run(flags: Self::Flags)
where where
Self: 'static + Sized, Self: 'static + Sized,
{ {
use futures::stream::StreamExt; use futures::stream::StreamExt;
let (app, command) = Self::new();
let window = web_sys::window().unwrap(); let window = web_sys::window().unwrap();
let document = window.document().unwrap(); let document = window.document().unwrap();
let body = document.body().unwrap(); let body = document.body().unwrap();
let mut title = app.title();
document.set_title(&title);
let (sender, receiver) = let (sender, receiver) =
iced_futures::futures::channel::mpsc::unbounded(); iced_futures::futures::channel::mpsc::unbounded();
@ -187,6 +187,12 @@ pub trait Application {
Self::Executor::new().expect("Create executor"), Self::Executor::new().expect("Create executor"),
sender.clone(), sender.clone(),
); );
let (app, command) = Self::new(flags);
let mut title = app.title();
document.set_title(&title);
runtime.spawn(command); runtime.spawn(command);
let application = Rc::new(RefCell::new(app)); let application = Rc::new(RefCell::new(app));

View file

@ -28,7 +28,13 @@ pub trait Application: Sized {
/// [`Application`]: trait.Application.html /// [`Application`]: trait.Application.html
type Message: std::fmt::Debug + Send; 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. /// Here is where you should return the initial state of your app.
/// ///
@ -38,7 +44,9 @@ pub trait Application: Sized {
/// request, etc. /// request, etc.
/// ///
/// [`Application`]: trait.Application.html /// [`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`]. /// Returns the current title of the [`Application`].
/// ///
@ -90,7 +98,7 @@ pub trait Application: Sized {
Mode::Windowed Mode::Windowed
} }
/// Runs the [`Application`]. /// Runs the [`Application`] with the provided [`Settings`].
/// ///
/// This method will take control of the current thread and __will NOT /// This method will take control of the current thread and __will NOT
/// return__. /// return__.
@ -98,8 +106,9 @@ pub trait Application: Sized {
/// It should probably be that last thing you call in your `main` function. /// It should probably be that last thing you call in your `main` function.
/// ///
/// [`Application`]: trait.Application.html /// [`Application`]: trait.Application.html
/// [`Settings`]: struct.Settings.html
fn run( fn run(
settings: Settings, settings: Settings<Self::Flags>,
backend_settings: <Self::Backend as window::Backend>::Settings, backend_settings: <Self::Backend as window::Backend>::Settings,
) where ) where
Self: 'static, Self: 'static,
@ -123,7 +132,9 @@ pub trait Application: Sized {
Runtime::new(executor, Proxy::new(event_loop.create_proxy())) Runtime::new(executor, Proxy::new(event_loop.create_proxy()))
}; };
let (mut application, init_command) = runtime.enter(Self::new); let flags = settings.flags;
let (mut application, init_command) =
runtime.enter(|| Self::new(flags));
runtime.spawn(init_command); runtime.spawn(init_command);
let subscription = application.subscription(); let subscription = application.subscription();

View file

@ -1,28 +1,25 @@
//! Configure your application. //! Configure your application.
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
#[path = "windows.rs"] #[path = "settings/windows.rs"]
mod platform; mod platform;
#[cfg(not(target_os = "windows"))] #[cfg(not(target_os = "windows"))]
#[path = "not_windows.rs"] #[path = "settings/not_windows.rs"]
mod platform; mod platform;
pub use platform::PlatformSpecific; pub use platform::PlatformSpecific;
/// The settings of an application. /// The settings of an application.
#[derive(Debug, Clone, Copy, PartialEq)] #[derive(Debug, Clone, Copy, PartialEq, Default)]
pub struct Settings { pub struct Settings<Flags> {
/// The [`Window`] settings /// The [`Window`] settings
/// ///
/// [`Window`]: struct.Window.html /// [`Window`]: struct.Window.html
pub window: Window, pub window: Window,
}
impl Default for Settings { /// The data needed to initialize an [`Application`].
fn default() -> Settings { ///
Settings { /// [`Application`]: trait.Application.html
window: Window::default(), pub flags: Flags,
}
}
} }
/// The window settings of an application. /// The window settings of an application.