From cc5c9ad742b894a20b8fac2c00d7b8f7dce831f0 Mon Sep 17 00:00:00 2001 From: hatoo Date: Tue, 17 Dec 2019 17:16:16 +0900 Subject: [PATCH 1/3] Fix run_return in MacOS --- CHANGELOG.md | 1 + src/platform_impl/macos/app_state.rs | 23 ++++++++++++++++++++--- 2 files changed, 21 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 801eb1162d..7801b3dfe0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ - On X11, generate synthetic key events for keys held when a window gains or loses focus. - On X11, issue a `CursorMoved` event when a `Touch` event occurs, as X11 implicitly moves the cursor for such events. +- On macOS, fix application not to terminate on `run_return` # 0.20.0 Alpha 4 (2019-10-18) diff --git a/src/platform_impl/macos/app_state.rs b/src/platform_impl/macos/app_state.rs index bc585a52a5..09f7aa8655 100644 --- a/src/platform_impl/macos/app_state.rs +++ b/src/platform_impl/macos/app_state.rs @@ -11,7 +11,7 @@ use std::{ time::Instant, }; -use cocoa::{appkit::NSApp, base::nil}; +use cocoa::{appkit::NSApp, base::nil, foundation::NSString}; use crate::{ event::{Event, StartCause, WindowEvent}, @@ -19,6 +19,7 @@ use crate::{ platform_impl::platform::{observer::EventLoopWaker, util::Never}, window::WindowId, }; +use objc::runtime::Object; lazy_static! { static ref HANDLER: Handler = Default::default(); @@ -275,8 +276,24 @@ impl AppState { HANDLER.set_in_callback(false); } if HANDLER.should_exit() { - let _: () = unsafe { msg_send![NSApp(), terminate: nil] }; - return; + unsafe { + let _: () = msg_send![NSApp(), stop: nil]; + + let main_window: *const Object = msg_send![NSApp(), mainWindow]; + assert_ne!(main_window, nil); + + let title: *const Object = msg_send![main_window, title]; + assert_ne!(title, nil); + let postfix = NSString::alloc(nil).init_str("*"); + let some_unique_title: *const Object = + msg_send![title, stringByAppendingString: postfix]; + assert_ne!(some_unique_title, nil); + + // To stop event loop immediately, we need to send some UI event here. + let _: () = msg_send![main_window, setTitle: some_unique_title]; + // And restore it. + let _: () = msg_send![main_window, setTitle: title]; + }; } HANDLER.update_start_time(); match HANDLER.get_old_and_new_control_flow() { From b77a52f665a59f2ca3accc4951f915ffdff10923 Mon Sep 17 00:00:00 2001 From: hatoo Date: Wed, 18 Dec 2019 12:40:33 +0900 Subject: [PATCH 2/3] MacOS: Fix the way of getting a window in run_return --- src/platform_impl/macos/app_state.rs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/src/platform_impl/macos/app_state.rs b/src/platform_impl/macos/app_state.rs index 09f7aa8655..7e1165677a 100644 --- a/src/platform_impl/macos/app_state.rs +++ b/src/platform_impl/macos/app_state.rs @@ -279,10 +279,11 @@ impl AppState { unsafe { let _: () = msg_send![NSApp(), stop: nil]; - let main_window: *const Object = msg_send![NSApp(), mainWindow]; - assert_ne!(main_window, nil); + let windows: *const Object = msg_send![NSApp(), windows]; + let window: *const Object = msg_send![windows, objectAtIndex:0]; + assert_ne!(window, nil); - let title: *const Object = msg_send![main_window, title]; + let title: *const Object = msg_send![window, title]; assert_ne!(title, nil); let postfix = NSString::alloc(nil).init_str("*"); let some_unique_title: *const Object = @@ -290,9 +291,9 @@ impl AppState { assert_ne!(some_unique_title, nil); // To stop event loop immediately, we need to send some UI event here. - let _: () = msg_send![main_window, setTitle: some_unique_title]; + let _: () = msg_send![window, setTitle: some_unique_title]; // And restore it. - let _: () = msg_send![main_window, setTitle: title]; + let _: () = msg_send![window, setTitle: title]; }; } HANDLER.update_start_time(); From 98a55425c3810af148a010d9e76d78a24aa7626d Mon Sep 17 00:00:00 2001 From: hatoo Date: Wed, 18 Dec 2019 15:21:51 +0900 Subject: [PATCH 3/3] Fix CHANGELOG.md --- CHANGELOG.md | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7801b3dfe0..369d733806 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,7 @@ # Unreleased +- On macOS, fix application not to terminate on `run_return`. + # # 0.20.0 Alpha 5 (2019-12-09) - On macOS, fix application termination on `ControlFlow::Exit` @@ -17,7 +19,6 @@ - On X11, generate synthetic key events for keys held when a window gains or loses focus. - On X11, issue a `CursorMoved` event when a `Touch` event occurs, as X11 implicitly moves the cursor for such events. -- On macOS, fix application not to terminate on `run_return` # 0.20.0 Alpha 4 (2019-10-18)