QMacStyle: Set NSControlStateValueOn for selected tabs

We draw tab buttons as NSButtons of type PushOnPushOff. To draw a
selected tab, we used state ON (NSControlStateValueOn), which gives it
the accent color, and state OFF for non-selected tabs. To draw a
selected and pressed tab, we use state OFF and set highlight:
depending on isPressed.
This worked fine up until macOS 11 because when setting highlight to
true the push button would draw the accent color no matter the state.

In macOS 12, things are different. A highlighted NSButton doesn't draw
the accent color anymore, but rather a gray background.
So when we draw a selected tab using NSControlStateValueOn (blue/accent
color) and then press it (state changes to NSControlStateValueOff), the
tab will change color from accent/blue to gray. The text remains white,
so it's not clearly visible.

To fix, set the NSControlStateValueOn for selected, pressed tabs on
macOS 12, so the background color doesn't change when pressing on a
selected tab.

Fixes: QTBUG-101000
Change-Id: Iaf48a7e2ae536c7c591578bb3c1065bd0e29b2e1
Reviewed-by: Tor Arne Vestbø <tor.arne.vestbo@qt.io>
(cherry picked from commit 0709af1c02a653f3121c76ae7879cc64b2e246b7)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Doris Verria 2022-10-17 20:48:58 +02:00 committed by Qt Cherry-pick Bot
parent 4866cac6f1
commit a68acbd0cf

View File

@ -3935,8 +3935,13 @@ void QMacStyle::drawControl(ControlElement ce, const QStyleOption *opt, QPainter
pb.enabled = isEnabled;
[pb highlight:isPressed];
// Set off state when inactive. See needsInactiveHack for when it's selected
pb.state = (isActive && isSelected && !isPressed) ? NSControlStateValueOn : NSControlStateValueOff;
// On macOS 12, don't set the Off state for selected tabs as it draws a gray backgorund even when highlighted
if (QOperatingSystemVersion::current() > QOperatingSystemVersion::MacOSBigSur)
pb.state = (isActive && isSelected) ? NSControlStateValueOn : NSControlStateValueOff;
else
pb.state = (isActive && isSelected && !isPressed) ? NSControlStateValueOn : NSControlStateValueOff;
const auto drawBezelBlock = ^(CGContextRef ctx, const CGRect &r) {
CGContextClipToRect(ctx, opt->rect.toCGRect());