From d3550bce2c2c6e9753e2d9ec6d66b4fa802597ad Mon Sep 17 00:00:00 2001 From: Volker Hilsheimer Date: Thu, 27 May 2021 12:35:34 +0200 Subject: [PATCH] macOS: render shortcuts in context menus correctly aligned MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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ø Reviewed-by: Timur Pocheptsov (cherry picked from commit 64a64fd485bd78f44b93beb3d712db67b6d33c05) Reviewed-by: Qt Cherry-pick Bot --- src/plugins/styles/mac/qmacstyle_mac.mm | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/src/plugins/styles/mac/qmacstyle_mac.mm b/src/plugins/styles/mac/qmacstyle_mac.mm index 71b9aeb909e..54e40945af4 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()) {