Initialize runtime values in application::run

This commit is contained in:
Héctor Ramón Jiménez 2020-11-04 21:33:18 +01:00
parent ee38b04d8b
commit ed2b9a91b4

View File

@ -121,35 +121,54 @@ where
C: window::Compositor<Renderer = A::Renderer> + 'static, C: window::Compositor<Renderer = A::Renderer> + 'static,
{ {
use futures::task::Poll; use futures::task::Poll;
use futures::{Future, FutureExt}; use futures::Future;
use winit::event_loop::EventLoop; use winit::event_loop::EventLoop;
let mut debug = Debug::new(); let mut debug = Debug::new();
debug.startup_started(); debug.startup_started();
let event_loop: EventLoop<A::Message> = EventLoop::with_user_event(); let (compositor, renderer) = C::new(compositor_settings)?;
let event_loop = EventLoop::with_user_event();
let mut runtime = {
let proxy = Proxy::new(event_loop.create_proxy());
let executor = E::new().map_err(Error::ExecutorCreationFailed)?;
Runtime::new(executor, proxy)
};
let (application, init_command) = {
let flags = settings.flags;
runtime.enter(|| A::new(flags))
};
let subscription = application.subscription();
runtime.spawn(init_command);
runtime.track(subscription);
let window = settings let window = settings
.window .window
.into_builder("", Mode::Windowed, event_loop.primary_monitor()) .into_builder(
&application.title(),
application.mode(),
event_loop.primary_monitor(),
)
.build(&event_loop) .build(&event_loop)
.map_err(Error::WindowCreationFailed)?; .map_err(Error::WindowCreationFailed)?;
let (mut sender, receiver) = mpsc::unbounded(); let (mut sender, receiver) = mpsc::unbounded();
let proxy = Proxy::new(event_loop.create_proxy());
let flags = settings.flags;
let mut event_logic = Box::pin( let mut event_logic = Box::pin(process_events::<A, E, C>(
process_events::<A, E, C>( application,
window, compositor,
proxy, renderer,
debug, window,
flags, runtime,
compositor_settings, debug,
receiver, receiver,
) ));
.map(|_| ()),
);
let mut context = let mut context =
futures::task::Context::from_waker(futures::task::noop_waker_ref()); futures::task::Context::from_waker(futures::task::noop_waker_ref());
@ -181,14 +200,14 @@ where
/// ///
/// [`Application`]: trait.Application.html /// [`Application`]: trait.Application.html
async fn process_events<A, E, C>( async fn process_events<A, E, C>(
mut application: A,
mut compositor: C,
mut renderer: A::Renderer,
window: winit::window::Window, window: winit::window::Window,
proxy: Proxy<A::Message>, mut runtime: Runtime<E, Proxy<A::Message>, A::Message>,
mut debug: Debug, mut debug: Debug,
flags: A::Flags,
compositor_settings: C::Settings,
mut receiver: mpsc::UnboundedReceiver<winit::event::Event<'_, A::Message>>, mut receiver: mpsc::UnboundedReceiver<winit::event::Event<'_, A::Message>>,
) -> Result<(), Error> ) where
where
A: Application + 'static, A: Application + 'static,
E: Executor + 'static, E: Executor + 'static,
C: window::Compositor<Renderer = A::Renderer> + 'static, C: window::Compositor<Renderer = A::Renderer> + 'static,
@ -196,29 +215,11 @@ where
use iced_futures::futures::stream::StreamExt; use iced_futures::futures::stream::StreamExt;
use winit::event; use winit::event;
let mut runtime = {
let executor = E::new().map_err(Error::ExecutorCreationFailed)?;
Runtime::new(executor, proxy)
};
let (mut application, init_command) = runtime.enter(|| A::new(flags));
runtime.spawn(init_command);
let subscription = application.subscription();
runtime.track(subscription);
let mut title = application.title(); let mut title = application.title();
let mut mode = application.mode(); let mut mode = application.mode();
let mut background_color = application.background_color(); let mut background_color = application.background_color();
let mut scale_factor = application.scale_factor(); let mut scale_factor = application.scale_factor();
let clipboard = Clipboard::new(&window);
// TODO: Encode cursor availability in the type-system
let mut cursor_position = winit::dpi::PhysicalPosition::new(-1.0, -1.0);
let mut mouse_interaction = mouse::Interaction::default();
let mut modifiers = winit::event::ModifiersState::default();
let physical_size = window.inner_size(); let physical_size = window.inner_size();
let mut viewport = Viewport::with_physical_size( let mut viewport = Viewport::with_physical_size(
Size::new(physical_size.width, physical_size.height), Size::new(physical_size.width, physical_size.height),
@ -226,16 +227,19 @@ where
); );
let mut resized = false; let mut resized = false;
let (mut compositor, mut renderer) = C::new(compositor_settings)?;
let surface = compositor.create_surface(&window); let surface = compositor.create_surface(&window);
let mut swap_chain = compositor.create_swap_chain( let mut swap_chain = compositor.create_swap_chain(
&surface, &surface,
physical_size.width, physical_size.width,
physical_size.height, physical_size.height,
); );
let clipboard = Clipboard::new(&window);
// TODO: Encode cursor availability in the type-system
let mut cursor_position = winit::dpi::PhysicalPosition::new(-1.0, -1.0);
let mut mouse_interaction = mouse::Interaction::default();
let mut modifiers = winit::event::ModifiersState::default();
let mut user_interface = std::mem::ManuallyDrop::new(build_user_interface( let mut user_interface = std::mem::ManuallyDrop::new(build_user_interface(
&mut application, &mut application,
Cache::default(), Cache::default(),
@ -432,8 +436,6 @@ where
_ => {} _ => {}
} }
} }
Ok(())
} }
/// Handles a `WindowEvent` and mutates the provided control flow to exit /// Handles a `WindowEvent` and mutates the provided control flow to exit