Skip to content

Commit 75a41e2

Browse files
committed
Use Qt signal for macOS Dock icon click event
This moves the Dock icon click reaction code to the common place and allows some cleanup in obj_c code. According to the Apple's docs `class_replaceMethod` behaves as `class_addMethod`, if the method identified by name does not yet exist; or as `method_setImplementation`, if it does exist. Coming from btc@2464925e7be832d4926b6204169bbbc1646c6368
1 parent ff319ad commit 75a41e2

File tree

4 files changed

+22
-40
lines changed

4 files changed

+22
-40
lines changed

src/qt/macdockiconhandler.h

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// Copyright (c) 2011-2013 The Bitcoin developers
1+
// Copyright (c) 2011-2020 The Bitcoin developers
22
// Distributed under the MIT/X11 software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

@@ -23,10 +23,8 @@ class MacDockIconHandler : public QObject
2323
~MacDockIconHandler();
2424

2525
QMenu* dockMenu();
26-
void setMainWindow(QMainWindow* window);
2726
static MacDockIconHandler* instance();
2827
static void cleanup();
29-
void handleDockIconClickEvent();
3028

3129
Q_SIGNALS:
3230
void dockIconClicked();
@@ -36,7 +34,6 @@ class MacDockIconHandler : public QObject
3634

3735
QWidget* m_dummyWidget;
3836
QMenu* m_dockMenu;
39-
QMainWindow* mainWindow;
4037
};
4138

4239
#endif // BITCOIN_QT_MACDOCKICONHANDLER_H

src/qt/macdockiconhandler.mm

Lines changed: 7 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -18,25 +18,18 @@ bool dockClickHandler(id self,SEL _cmd,...) {
1818
Q_UNUSED(self)
1919
Q_UNUSED(_cmd)
2020

21-
s_instance->handleDockIconClickEvent();
21+
Q_EMIT s_instance->dockIconClicked();
2222

23-
// Return NO (false) to suppress the default OS X actions
23+
// Return NO (false) to suppress the default macOS actions
2424
return false;
2525
}
2626

2727
void setupDockClickHandler() {
28-
Class cls = objc_getClass("NSApplication");
29-
id appInst = objc_msgSend((id)cls, sel_registerName("sharedApplication"));
30-
31-
if (appInst != NULL) {
32-
id delegate = objc_msgSend(appInst, sel_registerName("delegate"));
33-
Class delClass = (Class)objc_msgSend(delegate, sel_registerName("class"));
34-
SEL shouldHandle = sel_registerName("applicationShouldHandleReopen:hasVisibleWindows:");
35-
if (class_getInstanceMethod(delClass, shouldHandle))
36-
class_replaceMethod(delClass, shouldHandle, (IMP)dockClickHandler, "B@:");
37-
else
38-
class_addMethod(delClass, shouldHandle, (IMP)dockClickHandler,"B@:");
39-
}
28+
id app = objc_msgSend((id)objc_getClass("NSApplication"), sel_registerName("sharedApplication"));
29+
id delegate = objc_msgSend(app, sel_registerName("delegate"));
30+
Class delClass = (Class)objc_msgSend(delegate, sel_registerName("class"));
31+
SEL shouldHandle = sel_registerName("applicationShouldHandleReopen:hasVisibleWindows:");
32+
class_replaceMethod(delClass, shouldHandle, (IMP)dockClickHandler, "B@:");
4033
}
4134

4235

@@ -47,19 +40,13 @@ void setupDockClickHandler() {
4740
setupDockClickHandler();
4841
this->m_dummyWidget = new QWidget();
4942
this->m_dockMenu = new QMenu(this->m_dummyWidget);
50-
this->setMainWindow(nullptr);
5143
this->m_dockMenu->setAsDockMenu();
5244
[pool release];
5345
}
5446

55-
void MacDockIconHandler::setMainWindow(QMainWindow *window) {
56-
this->mainWindow = window;
57-
}
58-
5947
MacDockIconHandler::~MacDockIconHandler()
6048
{
6149
delete this->m_dummyWidget;
62-
this->setMainWindow(NULL);
6350
}
6451

6552
QMenu *MacDockIconHandler::dockMenu()
@@ -78,14 +65,3 @@ void setupDockClickHandler() {
7865
{
7966
delete s_instance;
8067
}
81-
82-
void MacDockIconHandler::handleDockIconClickEvent()
83-
{
84-
if (this->mainWindow)
85-
{
86-
this->mainWindow->activateWindow();
87-
this->mainWindow->show();
88-
}
89-
90-
Q_EMIT this->dockIconClicked();
91-
}

src/qt/pivx/pivxgui.cpp

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -276,7 +276,7 @@ void PIVXGUI::setClientModel(ClientModel* clientModel) {
276276

277277
void PIVXGUI::createTrayIconMenu() {
278278
#ifndef Q_OS_MAC
279-
// return if trayIcon is unset (only on non-Mac OSes)
279+
// return if trayIcon is unset (only on non-macOSes)
280280
if (!trayIcon)
281281
return;
282282

@@ -285,17 +285,17 @@ void PIVXGUI::createTrayIconMenu() {
285285

286286
connect(trayIcon, &QSystemTrayIcon::activated, this, &PIVXGUI::trayIconActivated);
287287
#else
288-
// Note: On Mac, the dock icon is used to provide the tray's functionality.
288+
// Note: On macOS, the Dock icon is used to provide the tray's functionality.
289289
MacDockIconHandler* dockIconHandler = MacDockIconHandler::instance();
290-
dockIconHandler->setMainWindow((QMainWindow*)this);
290+
connect(dockIconHandler, &MacDockIconHandler::dockIconClicked, this, &PIVXGUI::macosDockIconActivated);
291291
trayIconMenu = dockIconHandler->dockMenu();
292292
#endif
293293

294-
// Configuration of the tray icon (or dock icon) icon menu
294+
// Configuration of the tray icon (or Dock icon) icon menu
295295
trayIconMenu->addAction(toggleHideAction);
296296
trayIconMenu->addSeparator();
297297

298-
#ifndef Q_OS_MAC // This is built-in on Mac
298+
#ifndef Q_OS_MAC // This is built-in on macOS
299299
trayIconMenu->addSeparator();
300300
trayIconMenu->addAction(quitAction);
301301
#endif
@@ -309,6 +309,12 @@ void PIVXGUI::trayIconActivated(QSystemTrayIcon::ActivationReason reason)
309309
toggleHidden();
310310
}
311311
}
312+
#else
313+
void PIVXGUI::macosDockIconActivated()
314+
{
315+
show();
316+
activateWindow();
317+
}
312318
#endif
313319

314320
void PIVXGUI::changeEvent(QEvent* e)

src/qt/pivx/pivxgui.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,9 @@ private Q_SLOTS:
176176
#ifndef Q_OS_MAC
177177
/** Handle tray icon clicked */
178178
void trayIconActivated(QSystemTrayIcon::ActivationReason reason);
179+
#else
180+
/** Handle macOS Dock icon clicked */
181+
void macosDockIconActivated();
179182
#endif
180183

181184
Q_SIGNALS:

0 commit comments

Comments
 (0)