diff options
author | Volker Hilsheimer <volker.hilsheimer@qt.io> | 2021-05-27 12:35:34 +0200 |
---|---|---|
committer | Volker Hilsheimer <volker.hilsheimer@qt.io> | 2021-06-02 23:40:54 +0200 |
commit | 64a64fd485bd78f44b93beb3d712db67b6d33c05 (patch) | |
tree | 5df089f81a4a6f172f0d8f3d833485a65bab5e2c /src/plugins | |
parent | 01639b7cc25aa9235eb86b072cd4aa1cfd767069 (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.
Pick-to: 6.1
Fixes: QTBUG-73990
Change-Id: Ie03f3f40278700bdfafbfca7aa52075825e20234
Reviewed-by: Tor Arne Vestbø <tor.arne.vestbo@qt.io>
Reviewed-by: Timur Pocheptsov <timur.pocheptsov@qt.io>
Diffstat (limited to 'src/plugins')
-rw-r--r-- | src/plugins/styles/mac/qmacstyle_mac.mm | 20 |
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()) { |