summaryrefslogtreecommitdiffstats
path: root/src/plugins/styles/mac/qmacstyle_mac.mm
diff options
context:
space:
mode:
Diffstat (limited to 'src/plugins/styles/mac/qmacstyle_mac.mm')
-rw-r--r--src/plugins/styles/mac/qmacstyle_mac.mm160
1 files changed, 99 insertions, 61 deletions
diff --git a/src/plugins/styles/mac/qmacstyle_mac.mm b/src/plugins/styles/mac/qmacstyle_mac.mm
index 67c045e8bd..948dd55304 100644
--- a/src/plugins/styles/mac/qmacstyle_mac.mm
+++ b/src/plugins/styles/mac/qmacstyle_mac.mm
@@ -1163,6 +1163,8 @@ static QStyleHelper::WidgetSizePolicy qt_aqua_guess_size(const QWidget *widg, QS
void QMacStylePrivate::drawFocusRing(QPainter *p, const QRectF &targetRect, int hMargin, int vMargin, const CocoaControl &cw) const
{
+ const bool isBigSurOrAbove = QOperatingSystemVersion::current() >= QOperatingSystemVersion::MacOSBigSur;
+
QPainterPath focusRingPath;
focusRingPath.setFillRule(Qt::OddEvenFill);
@@ -1174,6 +1176,8 @@ void QMacStylePrivate::drawFocusRing(QPainter *p, const QRectF &targetRect, int
case SegmentedControl_Middle:
case TextField: {
auto innerRect = targetRect;
+ if (cw.type == Button_SquareButton)
+ innerRect = cw.adjustedControlFrame(targetRect.adjusted(hMargin, vMargin, -hMargin, -vMargin));
if (cw.type == TextField)
innerRect = innerRect.adjusted(hMargin, vMargin, -hMargin, -vMargin).adjusted(0.5, 0.5, -0.5, -0.5);
const auto outerRect = innerRect.adjusted(-focusRingWidth, -focusRingWidth, focusRingWidth, focusRingWidth);
@@ -1208,15 +1212,48 @@ void QMacStylePrivate::drawFocusRing(QPainter *p, const QRectF &targetRect, int
focusRingPath.addEllipse(rbOuterRect);
break;
}
- case Button_PopupButton:
case Button_PullDown:
- case Button_PushButton:
- case SegmentedControl_Single: {
+ case Button_PushButton: {
+ QRectF focusRect;
+ auto *pb = static_cast<NSButton *>(cocoaControl(cw));
+ const QRectF frameRect = cw.adjustedControlFrame(targetRect.adjusted(hMargin, vMargin, -hMargin, -vMargin));
+ pb.frame = frameRect.toCGRect();
+ focusRect = QRectF::fromCGRect([pb alignmentRectForFrame:pb.frame]);
+ if (cw.type == QMacStylePrivate::Button_PushButton) {
+ focusRect -= pushButtonShadowMargins[cw.size];
+ }
+ else if (cw.type == QMacStylePrivate::Button_PullDown) {
+ focusRect -= pullDownButtonShadowMargins[cw.size];
+ //fix focus ring drawn slightly off for pull downs
+ if (cw.size == QStyleHelper::SizeLarge)
+ focusRect = focusRect.adjusted(0, 0, 0, -1);
+ else if (cw.size == QStyleHelper::SizeMini)
+ focusRect = focusRect.adjusted(0, -1, 0, 0);
+ }
+ if (isBigSurOrAbove)
+ focusRect = focusRect.translated(0, 1);
const qreal innerRadius = cw.type == Button_PushButton ? 3 : 4;
const qreal outerRadius = innerRadius + focusRingWidth;
- hOffset = targetRect.left();
- vOffset = targetRect.top();
- const auto innerRect = targetRect.translated(-targetRect.topLeft());
+ hOffset = focusRect.left();
+ vOffset = focusRect.top();
+ const auto innerRect = focusRect.translated(-focusRect.topLeft());
+ const auto outerRect = innerRect.adjusted(-hMargin, -vMargin, hMargin, vMargin);
+ focusRingPath.addRoundedRect(innerRect, innerRadius, innerRadius);
+ focusRingPath.addRoundedRect(outerRect, outerRadius, outerRadius);
+ break;
+ }
+ case Button_PopupButton:
+ case SegmentedControl_Single: {
+ QRectF focusRect = targetRect;
+ if (isBigSurOrAbove)
+ focusRect.translate(0, -1.5);
+ else if (QOperatingSystemVersion::current() >= QOperatingSystemVersion::MacOSCatalina)
+ focusRect.adjust(0, 0, 0, -1);
+ const qreal innerRadius = 4;
+ const qreal outerRadius = innerRadius + focusRingWidth;
+ hOffset = focusRect.left();
+ vOffset = focusRect.top();
+ const auto innerRect = focusRect.translated(-focusRect.topLeft());
const auto outerRect = innerRect.adjusted(-hMargin, -vMargin, hMargin, vMargin);
focusRingPath.addRoundedRect(innerRect, innerRadius, innerRadius);
focusRingPath.addRoundedRect(outerRect, outerRadius, outerRadius);
@@ -1643,8 +1680,8 @@ bool QMacStylePrivate::CocoaControl::getCocoaButtonTypeAndBezelStyle(NSButtonTyp
*bezelStyle = NSShadowlessSquareBezelStyle;
break;
case Button_PushButton:
- *buttonType = NSPushOnPushOffButton;
- *bezelStyle = NSRoundedBezelStyle;
+ *buttonType = NSButtonTypeMomentaryPushIn;
+ *bezelStyle = NSBezelStyleRounded;
break;
default:
return false;
@@ -3688,8 +3725,20 @@ void QMacStyle::drawControl(ControlElement ce, const QStyleOption *opt, QPainter
pb.frame = frameRect.toCGRect();
pb.enabled = isEnabled;
+
+ // With the 'momentary push in' type this gives an impression of a
+ // button in a 'pressed' state (the 'momentary push in' does
+ // not show its state otherwise):
[pb highlight:isPressed];
- pb.state = isHighlighted && !isPressed ? NSOnState : NSOffState;
+
+ if (cw.type == QMacStylePrivate::Button_SquareButton) {
+ pb.state = isHighlighted && !isPressed ? NSControlStateValueOn : NSControlStateValueOff;
+ } else {
+ // For default/checked button this will give the required
+ // button accent color:
+ pb.keyEquivalent = isHighlighted ? @"\r" : @"";
+ }
+
d->drawNSViewInRect(pb, frameRect, p, ^(CGContextRef, const CGRect &r) {
[pb.cell drawBezelWithFrame:r inView:pb.superview];
});
@@ -3716,24 +3765,6 @@ void QMacStyle::drawControl(ControlElement ce, const QStyleOption *opt, QPainter
arrowOpt.rect = ar;
proxy()->drawPrimitive(PE_IndicatorArrowDown, &arrowOpt, p, w);
}
-
-
- if (btn->state & State_HasFocus) {
- // TODO Remove and use QFocusFrame instead.
- const int hMargin = proxy()->pixelMetric(QStyle::PM_FocusFrameHMargin, btn, w);
- const int vMargin = proxy()->pixelMetric(QStyle::PM_FocusFrameVMargin, btn, w);
- QRectF focusRect;
- if (cw.type == QMacStylePrivate::Button_SquareButton) {
- focusRect = frameRect;
- } else {
- focusRect = QRectF::fromCGRect([pb alignmentRectForFrame:pb.frame]);
- if (cw.type == QMacStylePrivate::Button_PushButton)
- focusRect -= pushButtonShadowMargins[cw.size];
- else if (cw.type == QMacStylePrivate::Button_PullDown)
- focusRect -= pullDownButtonShadowMargins[cw.size];
- }
- d->drawFocusRing(p, focusRect, hMargin, vMargin, cw);
- }
}
break;
case CE_PushButtonLabel:
@@ -3749,16 +3780,27 @@ void QMacStyle::drawControl(ControlElement ce, const QStyleOption *opt, QPainter
const bool hasText = !btn.text.isEmpty();
const bool isActive = btn.state & State_Active;
const bool isPressed = btn.state & State_Sunken;
+ const bool isDefault = (btn.features & QStyleOptionButton::DefaultButton && !d->autoDefaultButton)
+ || d->autoDefaultButton == btn.styleObject;
const auto ct = cocoaControlType(&btn, w);
if (!hasMenu && ct != QMacStylePrivate::Button_SquareButton) {
- if (isPressed
- || (isActive && isEnabled
- && ((btn.state & State_On)
- || ((btn.features & QStyleOptionButton::DefaultButton) && !d->autoDefaultButton)
- || d->autoDefaultButton == btn.styleObject)))
- btn.palette.setColor(QPalette::ButtonText, Qt::white);
+ if (isPressed || (isActive && isEnabled && ((btn.state & State_On) || isDefault))) {
+ if (!isPressed && NSApplication.sharedApplication.active) {
+ // !isPressed means isActive, but it can differ from
+ // the actual application state, so don't set white.
+ btn.palette.setColor(QPalette::ButtonText, Qt::white);
+ }
+ }
+ }
+
+ if (isEnabled && !isDarkMode() && QOperatingSystemVersion::current() > QOperatingSystemVersion::MacOSBigSur) {
+ if (!isDefault && !(btn.state & State_On)) {
+ // On macOS 12 it's a gray button, white text color (if set in the
+ // previous statement) would be almost invisible.
+ btn.palette.setColor(QPalette::ButtonText, Qt::black);
+ }
}
if ((!hasIcon && !hasMenu) || (hasIcon && !hasText)) {
@@ -3936,6 +3978,13 @@ void QMacStyle::drawControl(ControlElement ce, const QStyleOption *opt, QPainter
}
pb.frame = frameRect.toCGRect();
+ if (!isPopupButton) {
+ // Note: these days we use 'momentary push in' for Button_PushButton,
+ // but tabs are also rendered using NSButton/ButtonPushButton, and
+ // here we need 'push on/off' to make it work (tab highlight colors).
+ pb.buttonType = NSButtonTypePushOnPushOff;
+ }
+
pb.enabled = isEnabled;
[pb highlight:isPressed];
// Set off state when inactive. See needsInactiveHack for when it's selected
@@ -4054,27 +4103,6 @@ void QMacStyle::drawControl(ControlElement ce, const QStyleOption *opt, QPainter
}
p->restore();
}
-
- // TODO Needs size adjustment to fit the focus ring
- if (tabOpt->state & State_HasFocus) {
- QMacStylePrivate::CocoaControlType focusRingType;
- switch (tp) {
- case QStyleOptionTab::Beginning:
- focusRingType = verticalTabs ? QMacStylePrivate::SegmentedControl_Last
- : QMacStylePrivate::SegmentedControl_First;
- break;
- case QStyleOptionTab::Middle:
- focusRingType = QMacStylePrivate::SegmentedControl_Middle;
- break;
- case QStyleOptionTab::End:
- focusRingType = verticalTabs ? QMacStylePrivate::SegmentedControl_First
- : QMacStylePrivate::SegmentedControl_Last;
- break;
- case QStyleOptionTab::OnlyOneTab:
- focusRingType = QMacStylePrivate::SegmentedControl_Single;
- break;
- }
- }
}
break;
case CE_TabBarTabLabel:
@@ -4164,6 +4192,15 @@ void QMacStyle::drawControl(ControlElement ce, const QStyleOption *opt, QPainter
return QMacStylePrivate::Button_RadioButton;
if (ffw->inherits("QLineEdit") || ffw->inherits("QTextEdit"))
return QMacStylePrivate::TextField;
+ if (const auto *pb = qobject_cast<const QPushButton *>(ffw)) {
+ if (pb->isFlat() || (pb->rect().height()
+ > pushButtonDefaultHeight[QStyleHelper::SizeLarge]
+ && !(pb->testAttribute(Qt::WA_MacNormalSize))))
+ return QMacStylePrivate::Button_SquareButton;
+ if (pb->menu() != nullptr)
+ return QMacStylePrivate::Button_PullDown;
+ return QMacStylePrivate::Button_PushButton;
+ }
}
return QMacStylePrivate::Box; // Not really, just make it the default
@@ -4358,8 +4395,10 @@ void QMacStyle::drawControl(ControlElement ce, const QStyleOption *opt, QPainter
// line-break the string if it doesn't fit the given rect. It's better to draw outside
// the rect and possibly overlap something than to have part of the text disappear.
[s.toNSString() drawAtPoint:CGPointMake(xpos, yPos)
- withAttributes:@{ NSFontAttributeName:f, NSForegroundColorAttributeName:c,
- NSObliquenessAttributeName: [NSNumber numberWithDouble: myFont.italic() ? 0.3 : 0.0]}];
+ withAttributes:@{ NSFontAttributeName:f, NSForegroundColorAttributeName:c,
+ NSObliquenessAttributeName: [NSNumber numberWithDouble: myFont.italic() ? 0.3 : 0.0],
+ NSUnderlineStyleAttributeName: [NSNumber numberWithInt: myFont.underline() ? NSUnderlineStyleSingle
+ : NSUnderlineStyleNone]}];
d->restoreNSGraphicsContext(cgCtx);
} else {
@@ -5153,7 +5192,7 @@ void QMacStyle::drawComplexControl(ComplexControl cc, const QStyleOptionComplex
if (const QStyleOptionSlider *sb = qstyleoption_cast<const QStyleOptionSlider *>(opt)) {
const bool drawTrack = sb->subControls & SC_ScrollBarGroove;
- const bool drawKnob = sb->subControls & SC_ScrollBarSlider;
+ const bool drawKnob = sb->subControls & SC_ScrollBarSlider && sb->minimum != sb->maximum;
if (!drawTrack && !drawKnob)
break;
@@ -6343,14 +6382,13 @@ QSize QMacStyle::sizeFromContents(ContentsType ct, const QStyleOption *opt,
break;
#endif
case QStyle::CT_PushButton: {
- if (const QStyleOptionButton *btn = qstyleoption_cast<const QStyleOptionButton *>(opt))
+if (const QStyleOptionButton *btn = qstyleoption_cast<const QStyleOptionButton *>(opt))
if (btn->features & QStyleOptionButton::CommandLinkButton)
return QCommonStyle::sizeFromContents(ct, opt, sz, widget);
// By default, we fit the contents inside a normal rounded push button.
// Do this by add enough space around the contents so that rounded
// borders (including highlighting when active) will show.
- // TODO Use QFocusFrame and get rid of these horrors.
QSize macsz;
const auto controlSize = d->effectiveAquaSizeConstrain(opt, widget, CT_PushButton, sz, &macsz);
// FIXME See comment in CT_PushButton case in qt_aqua_get_known_size().
@@ -6437,9 +6475,9 @@ QSize QMacStyle::sizeFromContents(ContentsType ct, const QStyleOption *opt,
if (const auto *cb = qstyleoption_cast<const QStyleOptionComboBox *>(opt)) {
const auto controlSize = d->effectiveAquaSizeConstrain(opt, widget);
if (!cb->editable) {
- // Same as CT_PushButton, because we have to fit the focus
+ // See CT_PushButton; we have to fit the focus
// ring and a non-editable combo box is a NSPopUpButton.
- sz.rwidth() += QMacStylePrivate::PushButtonLeftOffset + QMacStylePrivate::PushButtonRightOffset + 12;
+ sz.rwidth() += QMacStylePrivate::PushButtonLeftOffset + QMacStylePrivate::PushButtonRightOffset + 24;
// All values as measured from HIThemeGetButtonBackgroundBounds()
if (controlSize != QStyleHelper::SizeMini)
sz.rwidth() += 12; // We like 12 over here.