mirror of
https://github.com/slint-ui/slint.git
synced 2025-10-03 07:04:34 +00:00
Improve EventLoop encapsulation
Hide the winit loop and extract it only in the GL renderer
This commit is contained in:
parent
096fd7bbb4
commit
85bf8a195a
4 changed files with 16 additions and 10 deletions
|
@ -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 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
|
||||
}
|
||||
}
|
||||
|
|
|
@ -108,14 +108,14 @@ impl<Backend: GraphicsBackend> RenderingCache<Backend> {
|
|||
|
||||
pub struct GraphicsWindow<Backend: GraphicsBackend + 'static> {
|
||||
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>,
|
||||
rendering_cache: RenderingCache<Backend>,
|
||||
}
|
||||
|
||||
impl<Backend: GraphicsBackend + 'static> GraphicsWindow<Backend> {
|
||||
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<RefCell<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 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();
|
||||
|
||||
|
|
|
@ -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<GraphicsBackend: graphics::GraphicsBackend + 'static>(
|
||||
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,
|
||||
) {
|
||||
use eventloop::GenericWindow;
|
||||
|
|
|
@ -635,6 +635,6 @@ pub extern "C" fn sixtyfps_runtime_run_component_with_gl_renderer(
|
|||
component: vtable::VRef<ComponentVTable>,
|
||||
) {
|
||||
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)
|
||||
});
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue