diff options
author | Tor Arne Vestbø <tor.arne.vestbo@qt.io> | 2023-03-08 10:27:48 +0100 |
---|---|---|
committer | Tor Arne Vestbø <tor.arne.vestbo@qt.io> | 2023-03-09 09:41:33 +0100 |
commit | a0313c85a9b8ab6ba0ec273f0c8638e2f7b8cc18 (patch) | |
tree | 514e55d3c4bbd60e766b78db22f84107d9b9bae9 /src/plugins/platforms/cocoa | |
parent | 9255820ad591645897d261b795b5649b8d9a313f (diff) |
Plumb QMessageBox::setCheckBox() through QPlatformMessageDialogHelper
Without this plumbing we have no way of knowing if the QMessageBox
has a checkbox set, and can't decide to skip the native dialog,
leaving the user without the expected checkbox.
As the suppression checkbox on macOS can be customized, we can use
this plumbing to actually provide native dialog support for generic
check boxes.
This mechanism can also be used by QErrorMessage, which now matches
behavior between native and non-native dialogs in terms of the label
of the checkbox and its initial state. We might want to tweak this
in the future, since user's might expect the suppression label and
state to match the system default, but that's something we can
expose from the platform theme if so, and should apply equally
to the non-native dialog.
Fixes: QTBUG-111803
Pick-to: 6.5.0 6.5
Change-Id: Ied9fc34383fe79fbd8437592ad1c1993b9396178
Reviewed-by: Volker Hilsheimer <volker.hilsheimer@qt.io>
Diffstat (limited to 'src/plugins/platforms/cocoa')
-rw-r--r-- | src/plugins/platforms/cocoa/qcocoamessagedialog.mm | 31 |
1 files changed, 29 insertions, 2 deletions
diff --git a/src/plugins/platforms/cocoa/qcocoamessagedialog.mm b/src/plugins/platforms/cocoa/qcocoamessagedialog.mm index 8dcc5fab53..f19146c60c 100644 --- a/src/plugins/platforms/cocoa/qcocoamessagedialog.mm +++ b/src/plugins/platforms/cocoa/qcocoamessagedialog.mm @@ -46,6 +46,16 @@ static QString toPlainText(const QString &text) return textDocument.toPlainText(); } +static NSControlStateValue controlStateFor(Qt::CheckState state) +{ + switch (state) { + case Qt::Checked: return NSControlStateValueOn; + case Qt::Unchecked: return NSControlStateValueOff; + case Qt::PartiallyChecked: return NSControlStateValueMixed; + } + Q_UNREACHABLE(); +} + /* Called from QDialogPrivate::setNativeDialogVisible() when the message box is ready to be shown. @@ -185,7 +195,14 @@ bool QCocoaMessageDialog::show(Qt::WindowFlags windowFlags, Qt::WindowModality w StandardButton::Ok, ButtonRole::AcceptRole); } - m_alert.showsSuppressionButton = options()->supressionCheckBoxEnabled(); + if (auto checkBoxLabel = options()->checkBoxLabel(); !checkBoxLabel.isNull()) { + checkBoxLabel = QPlatformTheme::removeMnemonics(checkBoxLabel); + m_alert.suppressionButton.title = checkBoxLabel.toNSString(); + auto state = options()->checkBoxState(); + m_alert.suppressionButton.allowsMixedState = state == Qt::PartiallyChecked; + m_alert.suppressionButton.state = controlStateFor(state); + m_alert.showsSuppressionButton = YES; + } qCDebug(lcQpaDialogs) << "Showing" << m_alert; @@ -233,6 +250,16 @@ void QCocoaMessageDialog::exec() // Custom modal response code to record that the dialog was hidden by us static const NSInteger kModalResponseDialogHidden = NSAlertThirdButtonReturn + 1; +static Qt::CheckState checkStateFor(NSControlStateValue state) +{ + switch (state) { + case NSControlStateValueOn: return Qt::Checked; + case NSControlStateValueOff: return Qt::Unchecked; + case NSControlStateValueMixed: return Qt::PartiallyChecked; + } + Q_UNREACHABLE(); +} + void QCocoaMessageDialog::processResponse(NSModalResponse response) { qCDebug(lcQpaDialogs) << "Processing response" << response << "for" << m_alert; @@ -244,7 +271,7 @@ void QCocoaMessageDialog::processResponse(NSModalResponse response) [alert autorelease]; if (alert.showsSuppressionButton) - emit supressionCheckBoxChanged(alert.suppressionButton.state == NSControlStateValueOn); + emit checkBoxStateChanged(checkStateFor(alert.suppressionButton.state)); if (response >= NSAlertFirstButtonReturn) { // Safe range for user-defined modal responses |