mirror of
https://github.com/rust-windowing/winit.git
synced 2026-06-28 07:33:14 -04:00
macOS: Close windows automatically when exiting
This disallows carrying over open windows between calls of `run_app_on_demand` (which wasn't intended to be supported anyhow).
This commit is contained in:
committed by
Kirill Chibisov
parent
7672fd5657
commit
6556cde246
@@ -11,7 +11,7 @@ use objc2_app_kit::{
|
||||
use objc2_foundation::{MainThreadMarker, NSNotification, NSObject, NSObjectProtocol};
|
||||
|
||||
use super::event_handler::EventHandler;
|
||||
use super::event_loop::{stop_app_immediately, ActiveEventLoop, PanicInfo};
|
||||
use super::event_loop::{notify_windows_of_exit, stop_app_immediately, ActiveEventLoop, PanicInfo};
|
||||
use super::observer::{EventLoopWaker, RunLoop};
|
||||
use super::{menu, WindowId, DEVICE_ID};
|
||||
use crate::event::{DeviceEvent, Event, StartCause, WindowEvent};
|
||||
@@ -165,7 +165,9 @@ impl ApplicationDelegate {
|
||||
|
||||
fn will_terminate(&self, _notification: &NSNotification) {
|
||||
trace_scope!("applicationWillTerminate:");
|
||||
// TODO: Notify every window that it will be destroyed, like done in iOS?
|
||||
let mtm = MainThreadMarker::from(self);
|
||||
let app = NSApplication::sharedApplication(mtm);
|
||||
notify_windows_of_exit(&app);
|
||||
self.internal_exit();
|
||||
}
|
||||
|
||||
@@ -392,6 +394,7 @@ impl ApplicationDelegate {
|
||||
if self.exiting() {
|
||||
let app = NSApplication::sharedApplication(mtm);
|
||||
stop_app_immediately(&app);
|
||||
notify_windows_of_exit(&app);
|
||||
}
|
||||
|
||||
if self.ivars().stop_before_wait.get() {
|
||||
|
||||
@@ -421,6 +421,22 @@ pub(super) fn stop_app_immediately(app: &NSApplication) {
|
||||
});
|
||||
}
|
||||
|
||||
/// Tell all windows to close.
|
||||
///
|
||||
/// This will synchronously trigger `WindowEvent::Destroyed` within
|
||||
/// `windowWillClose:`, giving the application one last chance to handle
|
||||
/// those events. It doesn't matter if the user also ends up closing the
|
||||
/// windows in `Window`'s `Drop` impl, once a window has been closed once, it
|
||||
/// stays closed.
|
||||
///
|
||||
/// This ensures that no windows linger on after the event loop has exited,
|
||||
/// see <https://github.com/rust-windowing/winit/issues/4135>.
|
||||
pub(super) fn notify_windows_of_exit(app: &NSApplication) {
|
||||
for window in app.windows() {
|
||||
window.close();
|
||||
}
|
||||
}
|
||||
|
||||
/// Catches panics that happen inside `f` and when a panic
|
||||
/// happens, stops the `sharedApplication`
|
||||
#[inline]
|
||||
|
||||
Reference in New Issue
Block a user