CMD+W is the standard shortcut in macOS to close a window without exiting the program.
This adds support to use the shortcut in both main and debug windows.
CMD+W is the standard shortcut in macOS to close a window without exiting the program.
This adds support to use the shortcut in both main and debug windows.
Thanks! Tested ACK 20859aef8b571542772a42b4c413c4a71a6576c5
The macOS HIG about Command+W: https://developer.apple.com/design/human-interface-guidelines/macos/user-interaction/keyboard/
408 | @@ -409,6 +409,10 @@ void BitcoinGUI::createActions() 409 | 410 | connect(new QShortcut(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_C), this), &QShortcut::activated, this, &BitcoinGUI::showDebugWindowActivateConsole); 411 | connect(new QShortcut(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_D), this), &QShortcut::activated, this, &BitcoinGUI::showDebugWindow); 412 | + 413 | +#ifdef Q_OS_MAC
Do you mind moving this to something like GUIUtil::handleHideShortcut(QWdiget*)? There are dialogs, like coin control dialog, transaction details, that could use the same feature.
408 | @@ -409,6 +409,10 @@ void BitcoinGUI::createActions() 409 | 410 | connect(new QShortcut(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_C), this), &QShortcut::activated, this, &BitcoinGUI::showDebugWindowActivateConsole); 411 | connect(new QShortcut(QKeySequence(Qt::CTRL + Qt::SHIFT + Qt::Key_D), this), &QShortcut::activated, this, &BitcoinGUI::showDebugWindow); 412 | + 413 | +#ifdef Q_OS_MAC 414 | + connect(new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_W), this), &QShortcut::activated, this, [this]() { this->hide(); });
No need to lambda, could just be ... , &QShortcut::activated, this, &QWidget::hide)?
Pressing ESC also closes the dialogs (except the main window). Is this something to keep in macos? (not suggesting it though, just want to mention the redundancy)
FWIW: I've verified that a disassembly of the bitcoind binary built on a Ubuntu 18.04 machine with this patch applied is identical to a disassembly of the bitcoind binary built against master (as expected).
utACK, ideally this kind of stuff would be handled by the cross-platform windowing system, so it's a bit disappointing that it's not, but also, this is only a few lines and increases user friendliness.
BTW: CTRL-W is also quite a common shortcut for closing windows in applications outside of OS-X, for example in browsers it closes the tab.
I will do the suggested changes and add the functionality to all the windows.
Thanks for the review.
QShortcut has a Qt::ApplicationShortcut for application-global shortcuts (see https://doc.qt.io/Qt-5/qt.html#ShortcutContext-enum )—would this be useful here?
Any reason not to enable this on all platforms?
I have updated the PR with the given suggestions and added the shortcut to most windows.
The only one without this shortcut is the 'About Qt' as it is part of Qt and I have not been able to add the shortcut to it.
If I have missed any other window please let me know.
@laanwj Qt::ApplicationShortcut does not work for the intended purpose. It makes the shortcut works even if it is not focused.
@luke-jr CMD+W is usually only used on macOS, more information can be found in the macOS HID posted by @jonasschnelli and in Wikepedia's Table of keyboard shortcuts.
I use Ctrl+W on Linux all the time to close browser tabs. My IRC client seems to use it for closing tabs too.
Furthermore, if macOS is using it, we can't use it for anything else (eg, closing wallets?).
So overall, I'd say it makes sense to just support it on all platforms.
Note: When extending it to support other platforms, care needs to be taken that the main window is not actually hidden if there's no system tray icon. Probably should minimise in that case? Not sure...
159 | @@ -158,6 +160,8 @@ ShutdownWindow::ShutdownWindow(QWidget *parent, Qt::WindowFlags f): 160 | tr("%1 is shutting down...").arg(tr(PACKAGE_NAME)) + "<br /><br />" + 161 | tr("Do not shut down the computer until this window disappears."))); 162 | setLayout(layout); 163 | + 164 | + GUIUtil::handleHideShortcut(this);
Do we want this to be hide-able??
The criteria I followed to make windows hide-able has been what I understand is the common usage in macOS, meaning that every window that has a close button can also be closed using the CMD+W shortcut.
Probably a number of windows ought to be closed normally, not merely hidden?
370 | @@ -370,6 +371,13 @@ void bringToFront(QWidget* w) 371 | } 372 | } 373 | 374 | +void handleHideShortcut(QWidget* w) 375 | +{ 376 | +#ifdef Q_OS_MAC 377 | + w->connect(new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_W), w), &QShortcut::activated, w, &QWidget::hide);
I haven't looked closely at this,... but is there a proper deallocation of that QShortcut?
I have followed the official documentation in this regard[0]. If you consider that this is not a correct application please let me know a more appropriate way.
I use Ctrl+W on Linux all the time to close browser tabs. My IRC client seems to use it for closing tabs too.
Furthermore, if macOS is using it, we can't use it for anything else (eg, closing wallets?).
So overall, I'd say it makes sense to just support it on all platforms.
I wasn't aware that this shortcut was also commonly used in other OSs other than macOS. Considering this I will remove the ifdef to support all OSs.
Probably a number of windows ought to be closed normally, not merely hidden?
I agree and after reading the QT documentation I will replace the hide call with a close call.
Should I continue working on this PR considering that the name of the branch is add_cmd_w_support_in_macos or would it be preferable to create another PR?
Two jobs failed in Travis CI during the set up:
I have not found a way to force a re-run. Nevertheless the other jobs completed successfully.
209 | @@ -210,6 +210,8 @@ BitcoinGUI::BitcoinGUI(interfaces::Node& node, const PlatformStyle *_platformSty 210 | #ifdef Q_OS_MAC 211 | m_app_nap_inhibitor = new CAppNapInhibitor; 212 | #endif 213 | + 214 | + GUIUtil::handleCloseWindowShortcut(this);
Ctrl+W should close a subwindow within an app but not the main window.
I think it should also close the main window (at least on macOS it is the case for almost all apps).
The CTRL+W/CMD+W behaviour in macOS is usually to close the focused window.
7 | @@ -8,6 +8,7 @@ 8 | 9 | #include <qt/splashscreen.h> 10 | 11 | +#include <qt/guiutil.h>
Could splash screen be untouched?
The rationale for adding the shortcut here is that on every window with an enabled close button the shortcut should be applicable.
It could be worth it taking into consideration disabling the close button or removing the bar all together.
114 | @@ -114,6 +115,8 @@ HelpMessageDialog::HelpMessageDialog(interfaces::Node& node, QWidget *parent, bo 115 | ui->scrollArea->setVisible(false); 116 | ui->aboutLogo->setVisible(false); 117 | } 118 | + 119 | + GUIUtil::handleCloseWindowShortcut(this);
The "About Qt" dialog does not accept Ctrl+W shortcut. So the "About Bitcoin Core" dialog should do for consistency.
Which of the following cases is preferable?
Two windows with their close behaviour consistent among them but inconsistent with the behaviour of the rest of them or keeping the inconsistency only on the QT window?
155 | @@ -153,6 +156,8 @@ ShutdownWindow::ShutdownWindow(QWidget *parent, Qt::WindowFlags f): 156 | tr("%1 is shutting down...").arg(PACKAGE_NAME) + "<br /><br />" + 157 | tr("Do not shut down the computer until this window disappears."))); 158 | setLayout(layout); 159 | + 160 | + GUIUtil::handleCloseWindowShortcut(this);
Could shutdown window be untouched?
Same comment as for the splash screen.
378 | @@ -378,6 +379,11 @@ void bringToFront(QWidget* w) 379 | } 380 | } 381 | 382 | +void handleCloseWindowShortcut(QWidget* w) 383 | +{ 384 | + w->connect(new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_W), w), &QShortcut::activated, w, &QWidget::close);
QObject::connect() is a static function of QObject:
QObject::connect(new QShortcut(QKeySequence(Qt::CTRL + Qt::Key_W), w), &QShortcut::activated, w, &QWidget::close);
<!--e57a25ab6845829454e8d69fc972939a-->
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.
<!--174a7506f384e20aa4161008e828411d-->
Reviewers, this pull request conflicts with the following ones:
If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.
Needs rebase. I think this is almost done. Let's finish it @IPGlider.
IPglider hasn't been active since April I think - (when) is it okay if I try to pick it up and include hebasto's suggestions?
Sorry for the delay, I will rebase and continue working on this PR this weekend.
Excellent, good luck!
CMD+W/CTRL+W is the standard shortcut to close a window without
exiting the program.
ACK f5a3a5b9ab362c58fa424261f313aa9cf46d2a98, tested on Linux Mint 19.3 by manually opening available dialogs and sub-windows, and applying the Ctrl+W shortcut. Also tested with "Minimize on close" option enabled / disabled.
Tested ACK f5a3a5b9ab362c58fa424261f313aa9cf46d2a98