Improve EventLoop encapsulation

Hide the winit loop and extract it only in the GL renderer
This commit is contained in:
Simon Hausmann 2020-06-17 14:37:47 +02:00
parent 096fd7bbb4
commit 85bf8a195a
4 changed files with 16 additions and 10 deletions

View file

@ -20,11 +20,13 @@ thread_local! {
pub(crate) static ALL_WINDOWS: RefCell<std::collections::HashMap<winit::window::WindowId, Weak<dyn GenericWindow>>> = RefCell::new(std::collections::HashMap::new()); pub(crate) static ALL_WINDOWS: RefCell<std::collections::HashMap<winit::window::WindowId, Weak<dyn GenericWindow>>> = RefCell::new(std::collections::HashMap::new());
} }
pub struct EventLoop(pub(crate) winit::event_loop::EventLoop<()>); pub struct EventLoop {
winit_loop: winit::event_loop::EventLoop<()>,
}
impl EventLoop { impl EventLoop {
pub fn new() -> Self { pub fn new() -> Self {
Self(winit::event_loop::EventLoop::new()) Self { winit_loop: winit::event_loop::EventLoop::new() }
} }
#[allow(unused_mut)] // mut need changes for wasm #[allow(unused_mut)] // mut need changes for wasm
@ -82,7 +84,7 @@ impl EventLoop {
}; };
#[cfg(not(target_arch = "wasm32"))] #[cfg(not(target_arch = "wasm32"))]
self.0.run_return(run_fn); self.winit_loop.run_return(run_fn);
#[cfg(target_arch = "wasm32")] #[cfg(target_arch = "wasm32")]
{ {
// Since wasm does not have a run_return function that takes a non-static closure, // Since wasm does not have a run_return function that takes a non-static closure,
@ -93,8 +95,12 @@ impl EventLoop {
&mut ControlFlow, &mut ControlFlow,
)); ));
RUN_FN_TLS.set(&mut run_fn, move || { RUN_FN_TLS.set(&mut run_fn, move || {
self.0.run(|e, t, cf| RUN_FN_TLS.with(|mut run_fn| run_fn(e, t, cf))) self.winit_loop.run(|e, t, cf| RUN_FN_TLS.with(|mut run_fn| run_fn(e, t, cf)))
}); });
} }
} }
pub fn get_winit_event_loop(&self) -> &winit::event_loop::EventLoop<()> {
&self.winit_loop
}
} }

View file

@ -108,14 +108,14 @@ impl<Backend: GraphicsBackend> RenderingCache<Backend> {
pub struct GraphicsWindow<Backend: GraphicsBackend + 'static> { pub struct GraphicsWindow<Backend: GraphicsBackend + 'static> {
graphics_backend_factory: graphics_backend_factory:
Box<dyn Fn(&winit::event_loop::EventLoop<()>, winit::window::WindowBuilder) -> Backend>, Box<dyn Fn(&crate::eventloop::EventLoop, winit::window::WindowBuilder) -> Backend>,
graphics_backend: Option<Backend>, graphics_backend: Option<Backend>,
rendering_cache: RenderingCache<Backend>, rendering_cache: RenderingCache<Backend>,
} }
impl<Backend: GraphicsBackend + 'static> GraphicsWindow<Backend> { impl<Backend: GraphicsBackend + 'static> GraphicsWindow<Backend> {
pub fn new( pub fn new(
graphics_backend_factory: impl Fn(&winit::event_loop::EventLoop<()>, winit::window::WindowBuilder) -> Backend graphics_backend_factory: impl Fn(&crate::eventloop::EventLoop, winit::window::WindowBuilder) -> Backend
+ 'static, + 'static,
) -> Rc<RefCell<Self>> { ) -> Rc<RefCell<Self>> {
let this = Rc::new(RefCell::new(Self { let this = Rc::new(RefCell::new(Self {
@ -222,7 +222,7 @@ impl<Backend: GraphicsBackend> crate::eventloop::GenericWindow
let mut this = self.borrow_mut(); let mut this = self.borrow_mut();
let factory = this.graphics_backend_factory.as_mut(); let factory = this.graphics_backend_factory.as_mut();
let backend = factory(&event_loop.0, window_builder); let backend = factory(&event_loop, window_builder);
let window_id = backend.window().id(); let window_id = backend.window().id();

View file

@ -45,12 +45,12 @@ pub use abi::properties::{EvaluationContext, Property};
#[doc(inline)] #[doc(inline)]
pub use abi::signals::Signal; pub use abi::signals::Signal;
mod eventloop; pub mod eventloop;
mod item_rendering; mod item_rendering;
pub fn run_component<GraphicsBackend: graphics::GraphicsBackend + 'static>( pub fn run_component<GraphicsBackend: graphics::GraphicsBackend + 'static>(
component: vtable::VRef<crate::abi::datastructures::ComponentVTable>, component: vtable::VRef<crate::abi::datastructures::ComponentVTable>,
graphics_backend_factory: impl Fn(&winit::event_loop::EventLoop<()>, winit::window::WindowBuilder) -> GraphicsBackend graphics_backend_factory: impl Fn(&eventloop::EventLoop, winit::window::WindowBuilder) -> GraphicsBackend
+ 'static, + 'static,
) { ) {
use eventloop::GenericWindow; use eventloop::GenericWindow;

View file

@ -635,6 +635,6 @@ pub extern "C" fn sixtyfps_runtime_run_component_with_gl_renderer(
component: vtable::VRef<ComponentVTable>, component: vtable::VRef<ComponentVTable>,
) { ) {
sixtyfps_corelib::run_component(component, |event_loop, window_builder| { sixtyfps_corelib::run_component(component, |event_loop, window_builder| {
GLRenderer::new(&event_loop, window_builder) GLRenderer::new(&event_loop.get_winit_event_loop(), window_builder)
}); });
} }