Merge #16720: qt: Replace objc_msgSend() function calls with the native Objective-C syntax

0bb33b5348 qt: Replace objc_msgSend with native syntax (Hennadii Stepanov)

Pull request description:

  Changes in Xcode 11 Objective-C Runtime cause an error (#16387) during building on MacOS 10.15 Catalina.

  This PR fixes this issue by replacing `objc_msgSend()` function calls with the native Objective-C syntax.

  Refs:
  - [changes in `objc_msgSend` function](https://developer.apple.com/documentation/objectivec/1456712-objc_msgsend?changes=latest_minor&language=objc)
  - [`OBJC_OLD_DISPATCH_PROTOTYPES` macro](https://developer.apple.com/documentation/objectivec/objc_old_dispatch_prototypes?language=objc)

ACKs for top commit:
  l2a5b1:
    ACK 0bb33b5 - Diff looks good. Sending messages via native Objective-C code feels more robust and is more readable than casting all the `objc_msgSend` function calls to the appropriate function signature (which would also have fixed the issue).
  jonasschnelli:
    utACK 0bb33b5348 - Confirmed that the called macOS framework function is available on our build targets.
  fanquake:
    ACK 0bb33b5348 - Still works as expected.

Tree-SHA512: c09cb684d06bd1da053a17c182b7bb1642e45bb347d26c76e1c5d835c320567caee366d85e34bb7f2be38e63ed041e0d06a56c2a9d89f7e5bece9b19cc5c6772
This commit is contained in:
fanquake 2019-09-01 16:07:39 +08:00
commit 7d6f63cc2c
No known key found for this signature in database
GPG key ID: 2EEB9F5CC09526C1
3 changed files with 18 additions and 13 deletions

View file

@ -585,7 +585,7 @@ case $host in
fi fi
AX_CHECK_LINK_FLAG([[-Wl,-headerpad_max_install_names]], [LDFLAGS="$LDFLAGS -Wl,-headerpad_max_install_names"]) AX_CHECK_LINK_FLAG([[-Wl,-headerpad_max_install_names]], [LDFLAGS="$LDFLAGS -Wl,-headerpad_max_install_names"])
CPPFLAGS="$CPPFLAGS -DMAC_OSX" CPPFLAGS="$CPPFLAGS -DMAC_OSX -DOBJC_OLD_DISPATCH_PROTOTYPES=0"
OBJCXXFLAGS="$CXXFLAGS" OBJCXXFLAGS="$CXXFLAGS"
;; ;;
*android*) *android*)

View file

@ -57,9 +57,10 @@
#pragma GCC diagnostic push #pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations" #pragma GCC diagnostic ignored "-Wdeprecated-declarations"
#include <objc/objc-runtime.h>
#include <CoreServices/CoreServices.h> #include <CoreServices/CoreServices.h>
#include <QProcess> #include <QProcess>
void ForceActivation();
#endif #endif
namespace GUIUtil { namespace GUIUtil {
@ -359,10 +360,7 @@ bool isObscured(QWidget *w)
void bringToFront(QWidget* w) void bringToFront(QWidget* w)
{ {
#ifdef Q_OS_MAC #ifdef Q_OS_MAC
// Force application activation on macOS. With Qt 5.4 this is required when ForceActivation();
// an action in the dock menu is triggered.
id app = objc_msgSend((id) objc_getClass("NSApplication"), sel_registerName("sharedApplication"));
objc_msgSend(app, sel_registerName("activateIgnoringOtherApps:"), YES);
#endif #endif
if (w) { if (w) {

View file

@ -1,12 +1,11 @@
// Copyright (c) 2011-2018 The Bitcoin Core developers // Copyright (c) 2011-2019 The Bitcoin Core developers
// Distributed under the MIT software license, see the accompanying // Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include "macdockiconhandler.h" #include "macdockiconhandler.h"
#undef slots #include <AppKit/AppKit.h>
#include <objc/objc.h> #include <objc/runtime.h>
#include <objc/message.h>
static MacDockIconHandler *s_instance = nullptr; static MacDockIconHandler *s_instance = nullptr;
@ -21,9 +20,7 @@ bool dockClickHandler(id self, SEL _cmd, ...) {
} }
void setupDockClickHandler() { void setupDockClickHandler() {
id app = objc_msgSend((id)objc_getClass("NSApplication"), sel_registerName("sharedApplication")); Class delClass = (Class)[[[NSApplication sharedApplication] delegate] class];
id delegate = objc_msgSend(app, sel_registerName("delegate"));
Class delClass = (Class)objc_msgSend(delegate, sel_registerName("class"));
SEL shouldHandle = sel_registerName("applicationShouldHandleReopen:hasVisibleWindows:"); SEL shouldHandle = sel_registerName("applicationShouldHandleReopen:hasVisibleWindows:");
class_replaceMethod(delClass, shouldHandle, (IMP)dockClickHandler, "B@:"); class_replaceMethod(delClass, shouldHandle, (IMP)dockClickHandler, "B@:");
} }
@ -44,3 +41,13 @@ void MacDockIconHandler::cleanup()
{ {
delete s_instance; delete s_instance;
} }
/**
* Force application activation on macOS. With Qt 5.5.1 this is required when
* an action in the Dock menu is triggered.
* TODO: Define a Qt version where it's no-longer necessary.
*/
void ForceActivation()
{
[[NSApplication sharedApplication] activateIgnoringOtherApps:YES];
}