From 85bf8a195abb1a1303f1ed8f939b7da260ab8f48 Mon Sep 17 00:00:00 2001 From: Simon Hausmann Date: Wed, 17 Jun 2020 14:37:47 +0200 Subject: [PATCH] Improve EventLoop encapsulation Hide the winit loop and extract it only in the GL renderer --- sixtyfps_runtime/corelib/eventloop.rs | 14 ++++++++++---- sixtyfps_runtime/corelib/graphics.rs | 6 +++--- sixtyfps_runtime/corelib/lib.rs | 4 ++-- sixtyfps_runtime/rendering_backends/gl/lib.rs | 2 +- 4 files changed, 16 insertions(+), 10 deletions(-) diff --git a/sixtyfps_runtime/corelib/eventloop.rs b/sixtyfps_runtime/corelib/eventloop.rs index 12b88a321..e8c5f1962 100644 --- a/sixtyfps_runtime/corelib/eventloop.rs +++ b/sixtyfps_runtime/corelib/eventloop.rs @@ -20,11 +20,13 @@ thread_local! { pub(crate) static ALL_WINDOWS: RefCell>> = 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 { 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 @@ -82,7 +84,7 @@ impl EventLoop { }; #[cfg(not(target_arch = "wasm32"))] - self.0.run_return(run_fn); + self.winit_loop.run_return(run_fn); #[cfg(target_arch = "wasm32")] { // Since wasm does not have a run_return function that takes a non-static closure, @@ -93,8 +95,12 @@ impl EventLoop { &mut ControlFlow, )); 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 + } } diff --git a/sixtyfps_runtime/corelib/graphics.rs b/sixtyfps_runtime/corelib/graphics.rs index 4116dd1ef..30311d956 100644 --- a/sixtyfps_runtime/corelib/graphics.rs +++ b/sixtyfps_runtime/corelib/graphics.rs @@ -108,14 +108,14 @@ impl RenderingCache { pub struct GraphicsWindow { graphics_backend_factory: - Box, winit::window::WindowBuilder) -> Backend>, + Box Backend>, graphics_backend: Option, rendering_cache: RenderingCache, } impl GraphicsWindow { 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, ) -> Rc> { let this = Rc::new(RefCell::new(Self { @@ -222,7 +222,7 @@ impl crate::eventloop::GenericWindow let mut this = self.borrow_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(); diff --git a/sixtyfps_runtime/corelib/lib.rs b/sixtyfps_runtime/corelib/lib.rs index 59213484d..26d33ac5e 100644 --- a/sixtyfps_runtime/corelib/lib.rs +++ b/sixtyfps_runtime/corelib/lib.rs @@ -45,12 +45,12 @@ pub use abi::properties::{EvaluationContext, Property}; #[doc(inline)] pub use abi::signals::Signal; -mod eventloop; +pub mod eventloop; mod item_rendering; pub fn run_component( component: vtable::VRef, - 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, ) { use eventloop::GenericWindow; diff --git a/sixtyfps_runtime/rendering_backends/gl/lib.rs b/sixtyfps_runtime/rendering_backends/gl/lib.rs index d92b6684d..2ed726c35 100644 --- a/sixtyfps_runtime/rendering_backends/gl/lib.rs +++ b/sixtyfps_runtime/rendering_backends/gl/lib.rs @@ -635,6 +635,6 @@ pub extern "C" fn sixtyfps_runtime_run_component_with_gl_renderer( component: vtable::VRef, ) { 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) }); }