summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorVolker Hilsheimer <volker.hilsheimer@qt.io>2021-05-27 12:35:34 +0200
committerQt Cherry-pick Bot <cherrypick_bot@qt-project.org>2021-06-03 07:03:31 +0000
commitd3550bce2c2c6e9753e2d9ec6d66b4fa802597ad (patch)
treef2eeda48b4688a75666090f3ea81c3fa98635dc8
parentecbf45a732a93a533e80d37681595fb95be95493 (diff)
macOS: render shortcuts in context menus correctly aligned
On macOS, shortcuts should be rendered along the imaginary line between the modifiers, and the key. The modifiers are right-aligned on the left side of that line, the key left aligned on the right side. Make an exception for multi-chord sequences, render those always left aligned. Fixes: QTBUG-73990 Change-Id: Ie03f3f40278700bdfafbfca7aa52075825e20234 Reviewed-by: Tor Arne Vestbø <tor.arne.vestbo@qt.io> Reviewed-by: Timur Pocheptsov <timur.pocheptsov@qt.io> (cherry picked from commit 64a64fd485bd78f44b93beb3d712db67b6d33c05) Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
-rw-r--r--src/plugins/styles/mac/qmacstyle_mac.mm20
1 files changed, 18 insertions, 2 deletions
diff --git a/src/plugins/styles/mac/qmacstyle_mac.mm b/src/plugins/styles/mac/qmacstyle_mac.mm
index 71b9aeb909..54e40945af 100644
--- a/src/plugins/styles/mac/qmacstyle_mac.mm
+++ b/src/plugins/styles/mac/qmacstyle_mac.mm
@@ -4332,9 +4332,25 @@ void QMacStyle::drawControl(ControlElement ce, const QStyleOption *opt, QPainter
if (!rightMarginText.isEmpty()) {
p->setFont(qt_app_fonts_hash()->value("QMenuItem", p->font()));
int xp = mi->rect.right() - tabwidth - macRightBorder + 2;
- if (!isSubMenu)
+ if (isSubMenu) {
+ p->drawText(xp, yPos, tabwidth, mi->rect.height(), text_flags | Qt::AlignRight, rightMarginText);
+ } else {
xp -= macItemHMargin + macItemFrame + 3; // Adjust for shortcut
- p->drawText(xp, yPos, tabwidth, mi->rect.height(), text_flags | Qt::AlignRight, rightMarginText);
+ // try to render modifier part of shortcut string right aligned, key part left aligned
+ const QKeySequence seq = QKeySequence::fromString(rightMarginText, QKeySequence::NativeText);
+ if (seq.count() == 1) { // one-combo sequence, the right most character is the key
+ // we don't know which key of all menu items is the widest, so use the widest possible
+ const int maxKeyWidth = p->fontMetrics().maxWidth();
+ const QChar key = rightMarginText.at(rightMarginText.length() - 1);
+ const QString modifiers = rightMarginText.left(rightMarginText.size() - 1);
+ p->drawText(xp + tabwidth - maxKeyWidth, yPos, maxKeyWidth, mi->rect.height(), text_flags, key);
+ // don't clip the shortcuts; maxKeyWidth might be more than what we have been alotted by the menu
+ p->drawText(xp, yPos, tabwidth - maxKeyWidth, mi->rect.height(),
+ text_flags | Qt::AlignRight | Qt::TextDontClip, modifiers);
+ } else { // draw the whole thing left-aligned for complex or unparsable cases
+ p->drawText(xp, yPos, tabwidth, mi->rect.height(), text_flags, rightMarginText);
+ }
+ }
}
if (!s.isEmpty()) {