diff --git a/retroshare-gui/src/gui/MainWindow.cpp b/retroshare-gui/src/gui/MainWindow.cpp index be53e41c9..d69e2bb42 100644 --- a/retroshare-gui/src/gui/MainWindow.cpp +++ b/retroshare-gui/src/gui/MainWindow.cpp @@ -33,6 +33,10 @@ #include #include +#if defined(Q_OS_DARWIN) +#include "gui/common/MacDockIconHandler.h" +#endif + #ifdef MESSENGER_WINDOW #include "MessengerWindow.h" #endif @@ -381,6 +385,7 @@ MainWindow::~MainWindow() #if defined(Q_OS_DARWIN) delete menuBar; delete dockMenu; + MacDockIconHandler::cleanup(); #endif // delete notifyMenu; // already deleted by the deletion of trayMenu StatisticsWindow::releaseInstance(); @@ -656,6 +661,15 @@ void MainWindow::createTrayIcon() trayIcon->setContextMenu(trayMenu); trayIcon->setIcon(QIcon(IMAGE_NOONLINE)); +#if defined(Q_OS_DARWIN) + // Note: On macOS, the Dock icon is used to provide the tray's functionality. + MacDockIconHandler* dockIconHandler = MacDockIconHandler::instance(); + connect(dockIconHandler, &MacDockIconHandler::dockIconClicked, [this] { + show(); + activateWindow(); + }); +#endif + #if defined(Q_OS_DARWIN) createMenuBar(); #endif @@ -694,10 +708,6 @@ void MainWindow::createMenuBar() dockMenu->addAction(tr("Options"), this, SLOT(showSettings())); dockMenu->addAction(tr("Help"), this, SLOT(showHelpDialog())); - dockMenu->addSeparator(); - QObject::connect(dockMenu, SIGNAL(aboutToShow()), this, SLOT(updateMenu())); - toggleVisibilityAction = dockMenu->addAction(tr("Show/Hide"), this, SLOT(toggleVisibilitycontextmenu())); - dockMenu->addSeparator(); QMenu *statusMenu = dockMenu->addMenu(tr("Status")); initializeStatusObject(statusMenu, true); diff --git a/retroshare-gui/src/gui/common/MacDockIconHandler.h b/retroshare-gui/src/gui/common/MacDockIconHandler.h new file mode 100644 index 000000000..d9d4d41f5 --- /dev/null +++ b/retroshare-gui/src/gui/common/MacDockIconHandler.h @@ -0,0 +1,27 @@ +// Copyright (c) 2011-2018 The Bitcoin Core developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#ifndef MACDOCKICONHANDLER_H +#define MACDOCKICONHANDLER_H + +#include + +/** macOS-specific Dock icon handler. + */ +class MacDockIconHandler : public QObject +{ + Q_OBJECT + +public: + static MacDockIconHandler *instance(); + static void cleanup(); + +Q_SIGNALS: + void dockIconClicked(); + +private: + MacDockIconHandler(); +}; + +#endif // MACDOCKICONHANDLER_H diff --git a/retroshare-gui/src/gui/common/MacDockIconHandler.mm b/retroshare-gui/src/gui/common/MacDockIconHandler.mm new file mode 100644 index 000000000..51a26135d --- /dev/null +++ b/retroshare-gui/src/gui/common/MacDockIconHandler.mm @@ -0,0 +1,53 @@ +// Copyright (c) 2011-2019 The Bitcoin Core developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#include "MacDockIconHandler.h" + +#include +#include + +static MacDockIconHandler *s_instance = nullptr; + +bool dockClickHandler(id self, SEL _cmd, ...) { + Q_UNUSED(self) + Q_UNUSED(_cmd) + + Q_EMIT s_instance->dockIconClicked(); + + // Return NO (false) to suppress the default macOS actions + return false; +} + +void setupDockClickHandler() { + Class delClass = (Class)[[[NSApplication sharedApplication] delegate] class]; + SEL shouldHandle = sel_registerName("applicationShouldHandleReopen:hasVisibleWindows:"); + class_replaceMethod(delClass, shouldHandle, (IMP)dockClickHandler, "B@:"); +} + +MacDockIconHandler::MacDockIconHandler() : QObject() +{ + setupDockClickHandler(); +} + +MacDockIconHandler *MacDockIconHandler::instance() +{ + if (!s_instance) + s_instance = new MacDockIconHandler(); + return s_instance; +} + +void MacDockIconHandler::cleanup() +{ + 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]; +} diff --git a/retroshare-gui/src/retroshare-gui.pro b/retroshare-gui/src/retroshare-gui.pro index 93734b454..b27102d42 100644 --- a/retroshare-gui/src/retroshare-gui.pro +++ b/retroshare-gui/src/retroshare-gui.pro @@ -304,6 +304,9 @@ macx { #DEFINES *= MAC_IDLE # for idle feature CONFIG -= uitools + + OBJECTIVE_SOURCES += gui/common/MacDockIconHandler.mm + OBJECTIVE_HEADERS += gui/common/MacDockIconHandler.h } ##################################### FreeBSD ######################################