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>
This commit is contained in:
Volker Hilsheimer 2021-05-27 12:35:34 +02:00 committed by Qt Cherry-pick Bot
parent ecbf45a732
commit d3550bce2c

View File

@ -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()) {