diff --git a/ui/qt/capture_file_dialog.cpp b/ui/qt/capture_file_dialog.cpp index 56e157a3e7..957aca23ca 100644 --- a/ui/qt/capture_file_dialog.cpp +++ b/ui/qt/capture_file_dialog.cpp @@ -155,7 +155,31 @@ check_savability_t CaptureFileDialog::checkSaveAsWithComments(QWidget * } #if defined(Q_OS_MAC) - discard_button->setAutoDefault(false); + /* + * In macOS, the "default button" is not necessarily the button that + * has the input focus; Enter/Return activates the default button, and + * the spacebar activates the button that has the input focus, and + * they might be different buttons. + * + * In a "do you want to save" dialog, for example, the "save" button + * is the default button, and the "don't save" button has the input + * focus, so you can press Enter/Return to save or space not to save + * (or Escape to dismiss the dialog). + * + * In Qt terms, this means "no auto-default", as auto-default makes the + * button with the input focus the default button, so that Enter/Return + * will activate it. + */ + QList buttons = msg_dialog.buttons(); + for (int i = 0; i < buttons.size(); ++i) { + QPushButton *button = static_cast(buttons.at(i));; + button->setAutoDefault(false); + } + + /* + * It also means that the "don't save" button should be the one + * initially given the focus. + */ discard_button->setFocus(); #endif diff --git a/ui/qt/main_window.cpp b/ui/qt/main_window.cpp index 875d768595..89231ce39f 100644 --- a/ui/qt/main_window.cpp +++ b/ui/qt/main_window.cpp @@ -78,6 +78,7 @@ DIAG_ON(frame-larger-than=) #include #include #include +#include #include #include #include @@ -1804,7 +1805,33 @@ bool MainWindow::testCaptureFileClose(QString before_what, FileCloseContext cont discard_button = msg_dialog.addButton(discard_button_text, QMessageBox::DestructiveRole); #if defined(Q_OS_MAC) - discard_button->setAutoDefault(false); + /* + * In macOS, the "default button" is not necessarily the + * button that has the input focus; Enter/Return activates + * the default button, and the spacebar activates the button + * that has the input focus, and they might be different + * buttons. + * + * In a "do you want to save" dialog, for example, the + * "save" button is the default button, and the "don't + * save" button has the input focus, so you can press + * Enter/Return to save or space not to save (or Escape + * to dismiss the dialog). + * + * In Qt terms, this means "no auto-default", as auto-default + * makes the button with the input focus the default button, + * so that Enter/Return will activate it. + */ + QList buttons = msg_dialog.buttons(); + for (int i = 0; i < buttons.size(); ++i) { + QPushButton *button = static_cast(buttons.at(i));; + button->setAutoDefault(false); + } + + /* + * It also means that the "don't save" button should be the one + * initially given the focus. + */ discard_button->setFocus(); #endif