From 27bd796c2adae75e8088cde08ab26c3638e3e26e Mon Sep 17 00:00:00 2001 From: mitchmindtree Date: Sun, 19 Mar 2017 19:20:38 +1100 Subject: [PATCH] [macos] Remove Window from EventsLoop's Window list on close and drop Previously, a Window was only removed from the list when dropped. --- src/platform/macos/events_loop.rs | 12 ++++++++++++ src/platform/macos/window.rs | 12 +++++++----- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/src/platform/macos/events_loop.rs b/src/platform/macos/events_loop.rs index e4666a30..e7e4a12b 100644 --- a/src/platform/macos/events_loop.rs +++ b/src/platform/macos/events_loop.rs @@ -217,6 +217,18 @@ impl EventsLoop { } } + // Removes the window with the given `Id` from the `windows` list. + // + // This is called when a window is either `Closed` or `Drop`ped. + pub fn find_and_remove_window(&self, id: super::window::Id) { + if let Ok(mut windows) = self.windows.lock() { + windows.retain(|w| match w.upgrade() { + Some(w) => w.id() != id, + None => true, + }); + } + } + fn call_user_callback_with_pending_events(&self) { loop { let event = match self.pending_events.lock().unwrap().pop_front() { diff --git a/src/platform/macos/window.rs b/src/platform/macos/window.rs index 9ff25043..c3cafa13 100644 --- a/src/platform/macos/window.rs +++ b/src/platform/macos/window.rs @@ -66,6 +66,12 @@ impl WindowDelegate { let state: *mut c_void = *this.get_ivar("winitState"); let state = &mut *(state as *mut DelegateState); emit_event(state, WindowEvent::Closed); + + // Remove the window from the events_loop. + if let Some(events_loop) = state.events_loop.upgrade() { + let window_id = get_window_id(*state.window); + events_loop.find_and_remove_window(window_id); + } } YES } @@ -173,11 +179,7 @@ impl Drop for Window { // Remove this window from the `EventLoop`s list of windows. let id = self.id(); if let Some(ev) = self.delegate.state.events_loop.upgrade() { - let mut windows = ev.windows.lock().unwrap(); - windows.retain(|w| match w.upgrade() { - Some(w) => w.id() != id, - None => true, - }) + ev.find_and_remove_window(id); } } }