Merge #15065: 0.17: GUI Backports #14123 #14133 #14383 #14597

27beb83222 qt: All tray menu actions call showNormalIfMinimized (João Barbosa)
c470bbd19d qt: Use GUIUtil::bringToFront where possible (João Barbosa)
ac73c7d433 qt: Add GUIUtil::bringToFront (João Barbosa)
0c2fb87dc1 Remove obj_c for macOS Dock icon menu (Hennadii Stepanov)
90347141bd Use Qt signal for macOS Dock icon click event (Hennadii Stepanov)
4d4bc37df9 Remove obj_c for macOS Dock icon setting (Hennadii Stepanov)
d2ed162ce0 Clean systray icon menu for -disablewallet mode (Hennadii Stepanov)
298dc15686 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 https://github.com/bitcoin/bitcoin/issues/13606#issuecomment-449846983.

Tree-SHA512: 543c80e7e2130870e801e0c9a69b06b9eea27c288478fc5dddeb662f7f3ec5b56b30916e5a9a629fced3fffcb8be77e2cd155e75cfd0a4392299add9730840f4
This commit is contained in:
Wladimir J. van der Laan 2019-01-03 09:53:13 +01:00
commit 5ff7b372cd
No known key found for this signature in database
GPG key ID: 1E4AED62986CD25D
7 changed files with 81 additions and 159 deletions

View file

@ -89,12 +89,8 @@ BitcoinGUI::BitcoinGUI(interfaces::Node& node, const PlatformStyle *_platformSty
windowTitle += tr("Node"); windowTitle += tr("Node");
} }
windowTitle += " " + networkStyle->getTitleAddText(); windowTitle += " " + networkStyle->getTitleAddText();
#ifndef Q_OS_MAC
QApplication::setWindowIcon(networkStyle->getTrayAndWindowIcon()); QApplication::setWindowIcon(networkStyle->getTrayAndWindowIcon());
setWindowIcon(networkStyle->getTrayAndWindowIcon()); setWindowIcon(networkStyle->getTrayAndWindowIcon());
#else
MacDockIconHandler::instance()->setIcon(networkStyle->getAppIcon());
#endif
setWindowTitle(windowTitle); setWindowTitle(windowTitle);
rpcConsole = new RPCConsole(node, _platformStyle, 0); rpcConsole = new RPCConsole(node, _platformStyle, 0);
@ -342,7 +338,9 @@ void BitcoinGUI::createActions()
connect(encryptWalletAction, SIGNAL(triggered(bool)), walletFrame, SLOT(encryptWallet(bool))); connect(encryptWalletAction, SIGNAL(triggered(bool)), walletFrame, SLOT(encryptWallet(bool)));
connect(backupWalletAction, SIGNAL(triggered()), walletFrame, SLOT(backupWallet())); connect(backupWalletAction, SIGNAL(triggered()), walletFrame, SLOT(backupWallet()));
connect(changePassphraseAction, SIGNAL(triggered()), walletFrame, SLOT(changePassphrase())); connect(changePassphraseAction, SIGNAL(triggered()), walletFrame, SLOT(changePassphrase()));
connect(signMessageAction, SIGNAL(triggered()), this, SLOT(showNormalIfMinimized()));
connect(signMessageAction, SIGNAL(triggered()), this, SLOT(gotoSignMessageTab())); connect(signMessageAction, SIGNAL(triggered()), this, SLOT(gotoSignMessageTab()));
connect(verifyMessageAction, SIGNAL(triggered()), this, SLOT(showNormalIfMinimized()));
connect(verifyMessageAction, SIGNAL(triggered()), this, SLOT(gotoVerifyMessageTab())); connect(verifyMessageAction, SIGNAL(triggered()), this, SLOT(gotoVerifyMessageTab()));
connect(usedSendingAddressesAction, SIGNAL(triggered()), walletFrame, SLOT(usedSendingAddresses())); connect(usedSendingAddressesAction, SIGNAL(triggered()), walletFrame, SLOT(usedSendingAddresses()));
connect(usedReceivingAddressesAction, SIGNAL(triggered()), walletFrame, SLOT(usedReceivingAddresses())); connect(usedReceivingAddressesAction, SIGNAL(triggered()), walletFrame, SLOT(usedReceivingAddresses()));
@ -589,7 +587,7 @@ void BitcoinGUI::createTrayIcon(const NetworkStyle *networkStyle)
void BitcoinGUI::createTrayIconMenu() void BitcoinGUI::createTrayIconMenu()
{ {
#ifndef Q_OS_MAC #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) if (!trayIcon)
return; return;
@ -599,24 +597,31 @@ void BitcoinGUI::createTrayIconMenu()
connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)), connect(trayIcon, SIGNAL(activated(QSystemTrayIcon::ActivationReason)),
this, SLOT(trayIconActivated(QSystemTrayIcon::ActivationReason))); this, SLOT(trayIconActivated(QSystemTrayIcon::ActivationReason)));
#else #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(); MacDockIconHandler *dockIconHandler = MacDockIconHandler::instance();
dockIconHandler->setMainWindow(static_cast<QMainWindow*>(this)); connect(dockIconHandler, &MacDockIconHandler::dockIconClicked, this, &BitcoinGUI::macosDockIconActivated);
trayIconMenu = dockIconHandler->dockMenu();
trayIconMenu = new QMenu(this);
trayIconMenu->setAsDockMenu();
#endif #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->addAction(toggleHideAction);
trayIconMenu->addSeparator(); trayIconMenu->addSeparator();
#endif
if (enableWallet) {
trayIconMenu->addAction(sendCoinsMenuAction); trayIconMenu->addAction(sendCoinsMenuAction);
trayIconMenu->addAction(receiveCoinsMenuAction); trayIconMenu->addAction(receiveCoinsMenuAction);
trayIconMenu->addSeparator(); trayIconMenu->addSeparator();
trayIconMenu->addAction(signMessageAction); trayIconMenu->addAction(signMessageAction);
trayIconMenu->addAction(verifyMessageAction); trayIconMenu->addAction(verifyMessageAction);
trayIconMenu->addSeparator(); trayIconMenu->addSeparator();
trayIconMenu->addAction(optionsAction);
trayIconMenu->addAction(openRPCConsoleAction); trayIconMenu->addAction(openRPCConsoleAction);
#ifndef Q_OS_MAC // This is built-in on Mac }
trayIconMenu->addAction(optionsAction);
#ifndef Q_OS_MAC // This is built-in on macOS
trayIconMenu->addSeparator(); trayIconMenu->addSeparator();
trayIconMenu->addAction(quitAction); trayIconMenu->addAction(quitAction);
#endif #endif
@ -631,6 +636,12 @@ void BitcoinGUI::trayIconActivated(QSystemTrayIcon::ActivationReason reason)
toggleHidden(); toggleHidden();
} }
} }
#else
void BitcoinGUI::macosDockIconActivated()
{
show();
activateWindow();
}
#endif #endif
void BitcoinGUI::optionsClicked() void BitcoinGUI::optionsClicked()
@ -654,10 +665,7 @@ void BitcoinGUI::aboutClicked()
void BitcoinGUI::showDebugWindow() void BitcoinGUI::showDebugWindow()
{ {
rpcConsole->showNormal(); GUIUtil::bringToFront(rpcConsole);
rpcConsole->show();
rpcConsole->raise();
rpcConsole->activateWindow();
} }
void BitcoinGUI::showDebugWindowActivateConsole() void BitcoinGUI::showDebugWindowActivateConsole()
@ -1128,24 +1136,11 @@ void BitcoinGUI::showNormalIfMinimized(bool fToggleHidden)
if(!clientModel) if(!clientModel)
return; return;
// activateWindow() (sometimes) helps with keyboard focus on Windows if (!isHidden() && !isMinimized() && !GUIUtil::isObscured(this) && fToggleHidden) {
if (isHidden())
{
show();
activateWindow();
}
else if (isMinimized())
{
showNormal();
activateWindow();
}
else if (GUIUtil::isObscured(this))
{
raise();
activateWindow();
}
else if(fToggleHidden)
hide(); hide();
} else {
GUIUtil::bringToFront(this);
}
} }
void BitcoinGUI::toggleHidden() void BitcoinGUI::toggleHidden()

View file

@ -250,6 +250,9 @@ private Q_SLOTS:
#ifndef Q_OS_MAC #ifndef Q_OS_MAC
/** Handle tray icon clicked */ /** Handle tray icon clicked */
void trayIconActivated(QSystemTrayIcon::ActivationReason reason); void trayIconActivated(QSystemTrayIcon::ActivationReason reason);
#else
/** Handle macOS Dock icon clicked */
void macosDockIconActivated();
#endif #endif
/** Show window if hidden, unminimize when minimized, rise when obscured or show if hidden and fToggleHidden is true */ /** Show window if hidden, unminimize when minimized, rise when obscured or show if hidden and fToggleHidden is true */

View file

@ -64,6 +64,14 @@
static fs::detail::utf8_codecvt_facet utf8; 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 { namespace GUIUtil {
QString dateTimeStr(const QDateTime &date) QString dateTimeStr(const QDateTime &date)
@ -357,6 +365,27 @@ bool isObscured(QWidget *w)
&& checkPoint(QPoint(w->width() / 2, w->height() / 2), 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() void openDebugLogfile()
{ {
fs::path pathDebug = GetDataDir() / "debug.log"; fs::path pathDebug = GetDataDir() / "debug.log";
@ -682,13 +711,8 @@ bool SetStartOnSystemStartup(bool fAutoStart)
#elif defined(Q_OS_MAC) #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 // 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);
LSSharedFileListItemRef findStartupItemInList(LSSharedFileListRef list, CFURLRef findUrl) LSSharedFileListItemRef findStartupItemInList(LSSharedFileListRef list, CFURLRef findUrl)
{ {

View file

@ -115,6 +115,9 @@ namespace GUIUtil
// Determine whether a widget is hidden behind other windows // Determine whether a widget is hidden behind other windows
bool isObscured(QWidget *w); bool isObscured(QWidget *w);
// Activate, show and raise the widget
void bringToFront(QWidget* w);
// Open debug.log // Open debug.log
void openDebugLogfile(); void openDebugLogfile();

View file

@ -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 // 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.
#ifndef BITCOIN_QT_MACDOCKICONHANDLER_H #ifndef BITCOIN_QT_MACDOCKICONHANDLER_H
#define BITCOIN_QT_MACDOCKICONHANDLER_H #define BITCOIN_QT_MACDOCKICONHANDLER_H
#include <QMainWindow>
#include <QObject> #include <QObject>
QT_BEGIN_NAMESPACE /** macOS-specific Dock icon handler.
class QIcon;
class QMenu;
class QWidget;
QT_END_NAMESPACE
/** Macintosh-specific dock icon handler.
*/ */
class MacDockIconHandler : public QObject class MacDockIconHandler : public QObject
{ {
Q_OBJECT Q_OBJECT
public: public:
~MacDockIconHandler();
QMenu *dockMenu();
void setIcon(const QIcon &icon);
void setMainWindow(QMainWindow *window);
static MacDockIconHandler *instance(); static MacDockIconHandler *instance();
static void cleanup(); static void cleanup();
void handleDockIconClickEvent();
Q_SIGNALS: Q_SIGNALS:
void dockIconClicked(); void dockIconClicked();
private: private:
MacDockIconHandler(); MacDockIconHandler();
QWidget *m_dummyWidget;
QMenu *m_dockMenu;
QMainWindow *mainWindow;
}; };
#endif // BITCOIN_QT_MACDOCKICONHANDLER_H #endif // BITCOIN_QT_MACDOCKICONHANDLER_H

View file

@ -1,16 +1,10 @@
// 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 // 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"
#include <QImageWriter>
#include <QMenu>
#include <QBuffer>
#include <QWidget>
#undef slots #undef slots
#include <Cocoa/Cocoa.h>
#include <objc/objc.h> #include <objc/objc.h>
#include <objc/message.h> #include <objc/message.h>
@ -20,88 +14,23 @@ bool dockClickHandler(id self,SEL _cmd,...) {
Q_UNUSED(self) Q_UNUSED(self)
Q_UNUSED(_cmd) 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; return false;
} }
void setupDockClickHandler() { void setupDockClickHandler() {
Class cls = objc_getClass("NSApplication"); id app = objc_msgSend((id)objc_getClass("NSApplication"), sel_registerName("sharedApplication"));
id appInst = objc_msgSend((id)cls, sel_registerName("sharedApplication")); id delegate = objc_msgSend(app, sel_registerName("delegate"));
if (appInst != nullptr) {
id delegate = objc_msgSend(appInst, sel_registerName("delegate"));
Class delClass = (Class)objc_msgSend(delegate, sel_registerName("class")); Class delClass = (Class)objc_msgSend(delegate, sel_registerName("class"));
SEL shouldHandle = sel_registerName("applicationShouldHandleReopen:hasVisibleWindows:"); SEL shouldHandle = sel_registerName("applicationShouldHandleReopen:hasVisibleWindows:");
if (class_getInstanceMethod(delClass, shouldHandle))
class_replaceMethod(delClass, shouldHandle, (IMP)dockClickHandler, "B@:"); class_replaceMethod(delClass, shouldHandle, (IMP)dockClickHandler, "B@:");
else
class_addMethod(delClass, shouldHandle, (IMP)dockClickHandler,"B@:");
} }
}
MacDockIconHandler::MacDockIconHandler() : QObject() MacDockIconHandler::MacDockIconHandler() : QObject()
{ {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
setupDockClickHandler(); 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() MacDockIconHandler *MacDockIconHandler::instance()
@ -115,14 +44,3 @@ void MacDockIconHandler::cleanup()
{ {
delete s_instance; delete s_instance;
} }
void MacDockIconHandler::handleDockIconClickEvent()
{
if (this->mainWindow)
{
this->mainWindow->activateWindow();
this->mainWindow->show();
}
Q_EMIT this->dockIconClicked();
}

View file

@ -293,9 +293,7 @@ void WalletView::usedSendingAddresses()
if(!walletModel) if(!walletModel)
return; return;
usedSendingAddressesPage->show(); GUIUtil::bringToFront(usedSendingAddressesPage);
usedSendingAddressesPage->raise();
usedSendingAddressesPage->activateWindow();
} }
void WalletView::usedReceivingAddresses() void WalletView::usedReceivingAddresses()
@ -303,9 +301,7 @@ void WalletView::usedReceivingAddresses()
if(!walletModel) if(!walletModel)
return; return;
usedReceivingAddressesPage->show(); GUIUtil::bringToFront(usedReceivingAddressesPage);
usedReceivingAddressesPage->raise();
usedReceivingAddressesPage->activateWindow();
} }
void WalletView::showProgress(const QString &title, int nProgress) void WalletView::showProgress(const QString &title, int nProgress)