From b577bff1ae1187471e0df2e4477b75ff548f1510 Mon Sep 17 00:00:00 2001 From: Guy Harris Date: Sat, 24 Mar 2018 19:15:14 -0700 Subject: In some dialogs, have *no* auto-default buttons on macOS. In macOS dialogs, there's a default button, which is the button that Enter/Return activates, and that Enter/Return *always* activates, *regardless* of what button has the input focus. To activate the button that has the input focus, you use the space bar. To implement that, we need to disable auto-default on all buttons, including the Cancel button. Put in a comment explaining all this. We may want to do this in all alert boxes, and possibly all dialogs with buttons. Change-Id: I214dd2870a9720ea705d8db39adc5b6af2003fb1 Reviewed-on: https://code.wireshark.org/review/26629 Reviewed-by: Guy Harris --- ui/qt/capture_file_dialog.cpp | 26 +++++++++++++++++++++++++- ui/qt/main_window.cpp | 29 ++++++++++++++++++++++++++++- 2 files changed, 53 insertions(+), 2 deletions(-) (limited to 'ui') 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 -- cgit v1.2.3