From aeb0b83b49c6da92114062b9ff436b8685d36a34 Mon Sep 17 00:00:00 2001 From: HampusM Date: Thu, 26 Nov 2026 15:44:46 +0100 Subject: refactor(engine): make windowing app thread store strong window refs --- engine/src/windowing.rs | 45 +++++++++++++++++++++------------------------ 1 file changed, 21 insertions(+), 24 deletions(-) diff --git a/engine/src/windowing.rs b/engine/src/windowing.rs index 8214515..49c5f02 100644 --- a/engine/src/windowing.rs +++ b/engine/src/windowing.rs @@ -2,7 +2,7 @@ use std::collections::VecDeque; use std::hint::cold_path; use std::panic::{catch_unwind, AssertUnwindSafe}; use std::sync::atomic::{AtomicBool, Ordering}; -use std::sync::{Arc, Condvar, Mutex, Weak}; +use std::sync::{Arc, Condvar, Mutex}; use std::thread::Builder as ThreadBuilder; use std::time::Duration; @@ -498,9 +498,11 @@ fn handle_window_removed( context.windows.remove(window.wid()); actions.add_components(evt_match.entity_id(), (WindowClosed,)); + + context.try_send_message_to_app(MessageToApp::DestroyWindow(window.wid())); } - if window_query.iter().count() == 1 { + if window_query.iter().nth(1).is_none() { actions.stop(); } } @@ -814,6 +816,7 @@ enum WindowMessage enum MessageToApp { CreateWindow(Uid, WindowCreationAttributes), + DestroyWindow(WindowId), SetWindowSettings(WindowId, WindowSettings), } @@ -914,7 +917,7 @@ struct InitData struct App { shared_state: Arc, - windows: IntMap, WindowSettings)>, + windows: IntMap, WindowSettings)>, } impl App @@ -952,7 +955,7 @@ impl App self.windows.insert( WindowId::from_inner(winit_window.id()), - (Arc::downgrade(&winit_window), window_settings.clone()), + (winit_window.clone(), window_settings.clone()), ); self.send_message(MessageFromApp::Window(WindowMessage::Created( @@ -961,6 +964,9 @@ impl App window_settings, ))); } + MessageToApp::DestroyWindow(window_id) => { + self.windows.remove(window_id); + } MessageToApp::SetWindowSettings(window_id, new_window_settings) => { let Some((winit_window, window_settings)) = self.windows.get_mut(window_id) @@ -973,10 +979,6 @@ impl App continue; }; - let Some(winit_window) = winit_window.upgrade() else { - continue; - }; - new_window_settings .apply_to_winit_window(&winit_window, window_settings); @@ -1064,7 +1066,7 @@ impl App input.absolute_mouse_pos = position; } - let Some((window, window_settings)) = self.windows.get(window_id) else { + let Some((winit_window, window_settings)) = self.windows.get(window_id) else { cold_path(); return; }; @@ -1073,21 +1075,20 @@ impl App return; } - let Some(window) = window.upgrade() else { - cold_path(); - return; - }; - - if !window.has_focus() { + if !winit_window.has_focus() { return; } - let window_size = window.inner_size().to_logical::(window.scale_factor()); + let window_size = winit_window + .inner_size() + .to_logical::(winit_window.scale_factor()); - if let Err(err) = window.set_cursor_position(Position::Logical(LogicalPosition { - x: window_size.width / 2.0, - y: window_size.height / 2.0, - })) { + if let Err(err) = + winit_window.set_cursor_position(Position::Logical(LogicalPosition { + x: window_size.width / 2.0, + y: window_size.height / 2.0, + })) + { cold_path(); tracing::error!( window_id=?window_id, @@ -1195,10 +1196,6 @@ impl ApplicationHandler for App fn about_to_wait(&mut self, _event_loop: &ActiveEventLoop) { for (window, _) in self.windows.values() { - let Some(window) = window.upgrade() else { - continue; - }; - window.request_redraw(); } } -- cgit v1.2.3-18-g5258