Skip to content

Commit

Permalink
[QT] fix OSX dock icon window reopening
Browse files Browse the repository at this point in the history
fixes #5878
  • Loading branch information
jonasschnelli committed Mar 11, 2015
1 parent e564e63 commit 89e70e9
Showing 1 changed file with 24 additions and 31 deletions.
55 changes: 24 additions & 31 deletions src/qt/macdockiconhandler.mm
Original file line number Diff line number Diff line change
Expand Up @@ -11,52 +11,46 @@

#undef slots
#include <Cocoa/Cocoa.h>
#include <objc/objc.h>
#include <objc/message.h>

#if QT_VERSION < 0x050000
extern void qt_mac_set_dock_menu(QMenu *);
#endif

@interface DockIconClickEventHandler : NSObject
{
MacDockIconHandler* dockIconHandler;
}

@end

@implementation DockIconClickEventHandler
static MacDockIconHandler *s_instance = NULL;

- (id)initWithDockIconHandler:(MacDockIconHandler *)aDockIconHandler
{
self = [super init];
if (self) {
dockIconHandler = aDockIconHandler;

[[NSAppleEventManager sharedAppleEventManager]
setEventHandler:self
andSelector:@selector(handleDockClickEvent:withReplyEvent:)
forEventClass:kCoreEventClass
andEventID:kAEReopenApplication];
}
return self;
bool dockClickHandler(id self,SEL _cmd,...) {
Q_UNUSED(self)
Q_UNUSED(_cmd)

s_instance->handleDockIconClickEvent();

// Return NO (false) to suppress the default OS X actions
return false;
}

- (void)handleDockClickEvent:(NSAppleEventDescriptor*)event withReplyEvent:(NSAppleEventDescriptor*)replyEvent
{
Q_UNUSED(event)
Q_UNUSED(replyEvent)

if (dockIconHandler) {
dockIconHandler->handleDockIconClickEvent();
void setupDockClickHandler() {
Class cls = objc_getClass("NSApplication");
id appInst = objc_msgSend((id)cls, sel_registerName("sharedApplication"));

if (appInst != NULL) {
id delegate = objc_msgSend(appInst, sel_registerName("delegate"));
Class delClass = (Class)objc_msgSend(delegate, sel_registerName("class"));
SEL shouldHandle = sel_registerName("applicationShouldHandleReopen:hasVisibleWindows:");
if (class_getInstanceMethod(delClass, shouldHandle))
class_replaceMethod(delClass, shouldHandle, (IMP)dockClickHandler, "B@:");
else
class_addMethod(delClass, shouldHandle, (IMP)dockClickHandler,"B@:");
}
}

@end

MacDockIconHandler::MacDockIconHandler() : QObject()
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

this->m_dockIconClickEventHandler = [[DockIconClickEventHandler alloc] initWithDockIconHandler:this];
setupDockClickHandler();
this->m_dummyWidget = new QWidget();
this->m_dockMenu = new QMenu(this->m_dummyWidget);
this->setMainWindow(NULL);
Expand Down Expand Up @@ -119,7 +113,6 @@ - (void)handleDockClickEvent:(NSAppleEventDescriptor*)event withReplyEvent:(NSAp

MacDockIconHandler *MacDockIconHandler::instance()
{
static MacDockIconHandler *s_instance = NULL;
if (!s_instance)
s_instance = new MacDockIconHandler();
return s_instance;
Expand Down

0 comments on commit 89e70e9

Please sign in to comment.