Skip to content

Commit

Permalink
Merge #15065: 0.17: GUI Backports #14123 #14133 #14383 #14597
Browse files Browse the repository at this point in the history
27beb83 qt: All tray menu actions call showNormalIfMinimized (João Barbosa)
c470bbd qt: Use GUIUtil::bringToFront where possible (João Barbosa)
ac73c7d qt: Add GUIUtil::bringToFront (João Barbosa)
0c2fb87 Remove obj_c for macOS Dock icon menu (Hennadii Stepanov)
9034714 Use Qt signal for macOS Dock icon click event (Hennadii Stepanov)
4d4bc37 Remove obj_c for macOS Dock icon setting (Hennadii Stepanov)
d2ed162 Clean systray icon menu for -disablewallet mode (Hennadii Stepanov)
298dc15 gui: Favor macOS show / hide action in dock menu (João Barbosa)

Pull request description:

  Backport #14123 #14133 #14383 and #14597 to 0.17 branch to fix #13606 (comment).

Tree-SHA512: 543c80e7e2130870e801e0c9a69b06b9eea27c288478fc5dddeb662f7f3ec5b56b30916e5a9a629fced3fffcb8be77e2cd155e75cfd0a4392299add9730840f4
  • Loading branch information
laanwj committed Jan 3, 2019
2 parents 16521ce + 27beb83 commit 5ff7b37
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 159 deletions.
71 changes: 33 additions & 38 deletions src/qt/bitcoingui.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,12 +89,8 @@ BitcoinGUI::BitcoinGUI(interfaces::Node& node, const PlatformStyle *_platformSty
windowTitle += tr("Node");
}
windowTitle += " " + networkStyle->getTitleAddText();
#ifndef Q_OS_MAC
QApplication::setWindowIcon(networkStyle->getTrayAndWindowIcon());
setWindowIcon(networkStyle->getTrayAndWindowIcon());
#else
MacDockIconHandler::instance()->setIcon(networkStyle->getAppIcon());
#endif
setWindowTitle(windowTitle);

rpcConsole = new RPCConsole(node, _platformStyle, 0);
Expand Down Expand Up @@ -342,7 +338,9 @@ void BitcoinGUI::createActions()
connect(encryptWalletAction, SIGNAL(triggered(bool)), walletFrame, SLOT(encryptWallet(bool)));
connect(backupWalletAction, SIGNAL(triggered()), walletFrame, SLOT(backupWallet()));
connect(changePassphraseAction, SIGNAL(triggered()), walletFrame, SLOT(changePassphrase()));
connect(signMessageAction, SIGNAL(triggered()), this, SLOT(showNormalIfMinimized()));
connect(signMessageAction, SIGNAL(triggered()), this, SLOT(gotoSignMessageTab()));
connect(verifyMessageAction, SIGNAL(triggered()), this, SLOT(showNormalIfMinimized()));
connect(verifyMessageAction, SIGNAL(triggered()), this, SLOT(gotoVerifyMessageTab()));
connect(usedSendingAddressesAction, SIGNAL(triggered()), walletFrame, SLOT(usedSendingAddresses()));
connect(usedReceivingAddressesAction, SIGNAL(triggered()), walletFrame, SLOT(usedReceivingAddresses()));
Expand Down Expand Up @@ -589,7 +587,7 @@ void BitcoinGUI::createTrayIcon(const NetworkStyle *networkStyle)
void BitcoinGUI::createTrayIconMenu()
{
#ifndef Q_OS_MAC
// return if trayIcon is unset (only on non-Mac OSes)
// return if trayIcon is unset (only on non-macOSes)
if (!trayIcon)
return;

Expand All @@ -599,24 +597,31 @@ void BitcoinGUI::createTrayIconMenu()
connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
this, SLOT(trayIconActivated(QSystemTrayIcon::ActivationReason)));
#else
// Note: On Mac, the dock icon is used to provide the tray's functionality.
// Note: On macOS, the Dock icon is used to provide the tray's functionality.
MacDockIconHandler *dockIconHandler = MacDockIconHandler::instance();
dockIconHandler->setMainWindow(static_cast<QMainWindow*>(this));
trayIconMenu = dockIconHandler->dockMenu();
connect(dockIconHandler, &MacDockIconHandler::dockIconClicked, this, &BitcoinGUI::macosDockIconActivated);

trayIconMenu = new QMenu(this);
trayIconMenu->setAsDockMenu();
#endif

// Configuration of the tray icon (or dock icon) icon menu
// Configuration of the tray icon (or Dock icon) menu
#ifndef Q_OS_MAC
// Note: On macOS, the Dock icon's menu already has Show / Hide action.
trayIconMenu->addAction(toggleHideAction);
trayIconMenu->addSeparator();
trayIconMenu->addAction(sendCoinsMenuAction);
trayIconMenu->addAction(receiveCoinsMenuAction);
trayIconMenu->addSeparator();
trayIconMenu->addAction(signMessageAction);
trayIconMenu->addAction(verifyMessageAction);
trayIconMenu->addSeparator();
#endif
if (enableWallet) {
trayIconMenu->addAction(sendCoinsMenuAction);
trayIconMenu->addAction(receiveCoinsMenuAction);
trayIconMenu->addSeparator();
trayIconMenu->addAction(signMessageAction);
trayIconMenu->addAction(verifyMessageAction);
trayIconMenu->addSeparator();
trayIconMenu->addAction(openRPCConsoleAction);
}
trayIconMenu->addAction(optionsAction);
trayIconMenu->addAction(openRPCConsoleAction);
#ifndef Q_OS_MAC // This is built-in on Mac
#ifndef Q_OS_MAC // This is built-in on macOS
trayIconMenu->addSeparator();
trayIconMenu->addAction(quitAction);
#endif
Expand All @@ -631,6 +636,12 @@ void BitcoinGUI::trayIconActivated(QSystemTrayIcon::ActivationReason reason)
toggleHidden();
}
}
#else
void BitcoinGUI::macosDockIconActivated()
{
show();
activateWindow();
}
#endif

void BitcoinGUI::optionsClicked()
Expand All @@ -654,10 +665,7 @@ void BitcoinGUI::aboutClicked()

void BitcoinGUI::showDebugWindow()
{
rpcConsole->showNormal();
rpcConsole->show();
rpcConsole->raise();
rpcConsole->activateWindow();
GUIUtil::bringToFront(rpcConsole);
}

void BitcoinGUI::showDebugWindowActivateConsole()
Expand Down Expand Up @@ -1128,24 +1136,11 @@ void BitcoinGUI::showNormalIfMinimized(bool fToggleHidden)
if(!clientModel)
return;

// activateWindow() (sometimes) helps with keyboard focus on Windows
if (isHidden())
{
show();
activateWindow();
}
else if (isMinimized())
{
showNormal();
activateWindow();
}
else if (GUIUtil::isObscured(this))
{
raise();
activateWindow();
}
else if(fToggleHidden)
if (!isHidden() && !isMinimized() && !GUIUtil::isObscured(this) && fToggleHidden) {
hide();
} else {
GUIUtil::bringToFront(this);
}
}

void BitcoinGUI::toggleHidden()
Expand Down
3 changes: 3 additions & 0 deletions src/qt/bitcoingui.h
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,9 @@ private Q_SLOTS:
#ifndef Q_OS_MAC
/** Handle tray icon clicked */
void trayIconActivated(QSystemTrayIcon::ActivationReason reason);
#else
/** Handle macOS Dock icon clicked */
void macosDockIconActivated();
#endif

/** Show window if hidden, unminimize when minimized, rise when obscured or show if hidden and fToggleHidden is true */
Expand Down
34 changes: 29 additions & 5 deletions src/qt/guiutil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,14 @@

static fs::detail::utf8_codecvt_facet utf8;

#if defined(Q_OS_MAC)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"

#include <objc/objc-runtime.h>
#include <CoreServices/CoreServices.h>
#endif

namespace GUIUtil {

QString dateTimeStr(const QDateTime &date)
Expand Down Expand Up @@ -357,6 +365,27 @@ bool isObscured(QWidget *w)
&& checkPoint(QPoint(w->width() / 2, w->height() / 2), w));
}

void bringToFront(QWidget* w)
{
#ifdef Q_OS_MAC
// Force application activation on macOS. With Qt 5.4 this is required when
// 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

if (w) {
// activateWindow() (sometimes) helps with keyboard focus on Windows
if (w->isMinimized()) {
w->showNormal();
} else {
w->show();
}
w->activateWindow();
w->raise();
}
}

void openDebugLogfile()
{
fs::path pathDebug = GetDataDir() / "debug.log";
Expand Down Expand Up @@ -682,13 +711,8 @@ bool SetStartOnSystemStartup(bool fAutoStart)


#elif defined(Q_OS_MAC)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdeprecated-declarations"
// based on: https://github.com/Mozketo/LaunchAtLoginController/blob/master/LaunchAtLoginController.m

#include <CoreFoundation/CoreFoundation.h>
#include <CoreServices/CoreServices.h>

LSSharedFileListItemRef findStartupItemInList(LSSharedFileListRef list, CFURLRef findUrl);
LSSharedFileListItemRef findStartupItemInList(LSSharedFileListRef list, CFURLRef findUrl)
{
Expand Down
3 changes: 3 additions & 0 deletions src/qt/guiutil.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,6 +115,9 @@ namespace GUIUtil
// Determine whether a widget is hidden behind other windows
bool isObscured(QWidget *w);

// Activate, show and raise the widget
void bringToFront(QWidget* w);

// Open debug.log
void openDebugLogfile();

Expand Down
21 changes: 2 additions & 19 deletions src/qt/macdockiconhandler.h
Original file line number Diff line number Diff line change
@@ -1,44 +1,27 @@
// Copyright (c) 2011-2015 The Bitcoin Core developers
// 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 BITCOIN_QT_MACDOCKICONHANDLER_H
#define BITCOIN_QT_MACDOCKICONHANDLER_H

#include <QMainWindow>
#include <QObject>

QT_BEGIN_NAMESPACE
class QIcon;
class QMenu;
class QWidget;
QT_END_NAMESPACE

/** Macintosh-specific dock icon handler.
/** macOS-specific Dock icon handler.
*/
class MacDockIconHandler : public QObject
{
Q_OBJECT

public:
~MacDockIconHandler();

QMenu *dockMenu();
void setIcon(const QIcon &icon);
void setMainWindow(QMainWindow *window);
static MacDockIconHandler *instance();
static void cleanup();
void handleDockIconClickEvent();

Q_SIGNALS:
void dockIconClicked();

private:
MacDockIconHandler();

QWidget *m_dummyWidget;
QMenu *m_dockMenu;
QMainWindow *mainWindow;
};

#endif // BITCOIN_QT_MACDOCKICONHANDLER_H
100 changes: 9 additions & 91 deletions src/qt/macdockiconhandler.mm
Original file line number Diff line number Diff line change
@@ -1,107 +1,36 @@
// Copyright (c) 2011-2013 The Bitcoin Core developers
// 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.

#include "macdockiconhandler.h"

#include <QImageWriter>
#include <QMenu>
#include <QBuffer>
#include <QWidget>

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

static MacDockIconHandler *s_instance = nullptr;

bool dockClickHandler(id self,SEL _cmd,...) {
bool dockClickHandler(id self, SEL _cmd, ...) {
Q_UNUSED(self)
Q_UNUSED(_cmd)

s_instance->handleDockIconClickEvent();
Q_EMIT s_instance->dockIconClicked();

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

void setupDockClickHandler() {
Class cls = objc_getClass("NSApplication");
id appInst = objc_msgSend((id)cls, sel_registerName("sharedApplication"));

if (appInst != nullptr) {
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@:");
}
id app = objc_msgSend((id)objc_getClass("NSApplication"), sel_registerName("sharedApplication"));
id delegate = objc_msgSend(app, sel_registerName("delegate"));
Class delClass = (Class)objc_msgSend(delegate, sel_registerName("class"));
SEL shouldHandle = sel_registerName("applicationShouldHandleReopen:hasVisibleWindows:");
class_replaceMethod(delClass, shouldHandle, (IMP)dockClickHandler, "B@:");
}


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

setupDockClickHandler();
this->m_dummyWidget = new QWidget();
this->m_dockMenu = new QMenu(this->m_dummyWidget);
this->setMainWindow(nullptr);
#if QT_VERSION >= 0x050200
this->m_dockMenu->setAsDockMenu();
#endif
[pool release];
}

void MacDockIconHandler::setMainWindow(QMainWindow *window) {
this->mainWindow = window;
}

MacDockIconHandler::~MacDockIconHandler()
{
delete this->m_dummyWidget;
this->setMainWindow(nullptr);
}

QMenu *MacDockIconHandler::dockMenu()
{
return this->m_dockMenu;
}

void MacDockIconHandler::setIcon(const QIcon &icon)
{
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSImage *image = nil;
if (icon.isNull())
image = [[NSImage imageNamed:@"NSApplicationIcon"] retain];
else {
// generate NSImage from QIcon and use this as dock icon.
QSize size = icon.actualSize(QSize(128, 128));
QPixmap pixmap = icon.pixmap(size);

// Write image into a R/W buffer from raw pixmap, then save the image.
QBuffer notificationBuffer;
if (!pixmap.isNull() && notificationBuffer.open(QIODevice::ReadWrite)) {
QImageWriter writer(&notificationBuffer, "PNG");
if (writer.write(pixmap.toImage())) {
NSData* macImgData = [NSData dataWithBytes:notificationBuffer.buffer().data()
length:notificationBuffer.buffer().size()];
image = [[NSImage alloc] initWithData:macImgData];
}
}

if(!image) {
// if testnet image could not be created, load std. app icon
image = [[NSImage imageNamed:@"NSApplicationIcon"] retain];
}
}

[NSApp setApplicationIconImage:image];
[image release];
[pool release];
}

MacDockIconHandler *MacDockIconHandler::instance()
Expand All @@ -115,14 +44,3 @@ void setupDockClickHandler() {
{
delete s_instance;
}

void MacDockIconHandler::handleDockIconClickEvent()
{
if (this->mainWindow)
{
this->mainWindow->activateWindow();
this->mainWindow->show();
}

Q_EMIT this->dockIconClicked();
}
Loading

0 comments on commit 5ff7b37

Please sign in to comment.