diff options
Diffstat (limited to 'src/plugins/styles/mac/qmacstyle_mac.mm')
-rw-r--r-- | src/plugins/styles/mac/qmacstyle_mac.mm | 222 |
1 files changed, 138 insertions, 84 deletions
diff --git a/src/plugins/styles/mac/qmacstyle_mac.mm b/src/plugins/styles/mac/qmacstyle_mac.mm index 3405f01046..b84448d5e2 100644 --- a/src/plugins/styles/mac/qmacstyle_mac.mm +++ b/src/plugins/styles/mac/qmacstyle_mac.mm @@ -143,22 +143,12 @@ static QWindow *qt_getWindow(const QWidget *widget) return widget ? widget->window()->windowHandle() : 0; } -@interface QT_MANGLE_NAMESPACE(NotificationReceiver) : NSObject { -QMacStylePrivate *mPrivate; -} -- (id)initWithPrivate:(QMacStylePrivate *)priv; -- (void)scrollBarStyleDidChange:(NSNotification *)notification; +@interface QT_MANGLE_NAMESPACE(NotificationReceiver) : NSObject @end QT_NAMESPACE_ALIAS_OBJC_CLASS(NotificationReceiver); @implementation NotificationReceiver -- (id)initWithPrivate:(QMacStylePrivate *)priv -{ - self = [super init]; - mPrivate = priv; - return self; -} - (void)scrollBarStyleDidChange:(NSNotification *)notification { @@ -248,6 +238,33 @@ QT_NAMESPACE_ALIAS_OBJC_CLASS(QVerticalSplitView); } @end +// See render code in drawPrimitive(PE_FrameTabWidget) +@interface QT_MANGLE_NAMESPACE(QDarkNSBox) : NSBox +@end + +QT_NAMESPACE_ALIAS_OBJC_CLASS(QDarkNSBox); + +@implementation QDarkNSBox +- (instancetype)init +{ + if ((self = [super init])) { + self.title = @""; + self.titlePosition = NSNoTitle; + self.boxType = NSBoxCustom; + self.cornerRadius = 3; + self.borderColor = [NSColor.controlColor colorWithAlphaComponent:0.1]; + self.fillColor = [NSColor.darkGrayColor colorWithAlphaComponent:0.2]; + } + + return self; +} + +- (void)drawRect:(NSRect)rect +{ + [super drawRect:rect]; +} +@end + QT_BEGIN_NAMESPACE // The following constants are used for adjusting the size @@ -263,33 +280,50 @@ QVector<QPointer<QObject> > QMacStylePrivate::scrollBars; static QLinearGradient titlebarGradientActive() { - static QLinearGradient gradient; - if (gradient == QLinearGradient()) { + static QLinearGradient darkGradient = [](){ + QLinearGradient gradient; + // FIXME: colors are chosen somewhat arbitrarily and could be fine-tuned, + // or ideally determined by calling a native API. + gradient.setColorAt(0, QColor(47, 47, 47)); + return gradient; + }(); + static QLinearGradient lightGradient = [](){ + QLinearGradient gradient; gradient.setColorAt(0, QColor(235, 235, 235)); gradient.setColorAt(0.5, QColor(210, 210, 210)); gradient.setColorAt(0.75, QColor(195, 195, 195)); gradient.setColorAt(1, QColor(180, 180, 180)); - } - return gradient; + return gradient; + }(); + return qt_mac_applicationIsInDarkMode() ? darkGradient : lightGradient; } static QLinearGradient titlebarGradientInactive() { - static QLinearGradient gradient; - if (gradient == QLinearGradient()) { + static QLinearGradient darkGradient = [](){ + QLinearGradient gradient; + gradient.setColorAt(1, QColor(42, 42, 42)); + return gradient; + }(); + static QLinearGradient lightGradient = [](){ + QLinearGradient gradient; gradient.setColorAt(0, QColor(250, 250, 250)); gradient.setColorAt(1, QColor(225, 225, 225)); - } - return gradient; + return gradient; + }(); + return qt_mac_applicationIsInDarkMode() ? darkGradient : lightGradient; } static const QColor titlebarSeparatorLineActive(111, 111, 111); static const QColor titlebarSeparatorLineInactive(131, 131, 131); +static const QColor darkModeSeparatorLine(88, 88, 88); // Gradient colors used for the dock widget title bar and // non-unifed tool bar bacground. -static const QColor mainWindowGradientBegin(240, 240, 240); -static const QColor mainWindowGradientEnd(200, 200, 200); +static const QColor lightMainWindowGradientBegin(240, 240, 240); +static const QColor lightMainWindowGradientEnd(200, 200, 200); +static const QColor darkMainWindowGradientBegin(47, 47, 47); +static const QColor darkMainWindowGradientEnd(47, 47, 47); static const int DisclosureOffset = 4; @@ -404,9 +438,9 @@ static bool setupSlider(NSSlider *slider, const QStyleOptionSlider *sl) const bool ticksAbove = sl->tickPosition == QSlider::TicksAbove; if (sl->orientation == Qt::Horizontal) - slider.tickMarkPosition = ticksAbove ? NSTickMarkAbove : NSTickMarkBelow; + slider.tickMarkPosition = ticksAbove ? NSTickMarkPositionAbove : NSTickMarkPositionBelow; else - slider.tickMarkPosition = ticksAbove ? NSTickMarkLeft : NSTickMarkRight; + slider.tickMarkPosition = ticksAbove ? NSTickMarkPositionLeading : NSTickMarkPositionTrailing; } else { slider.numberOfTickMarks = 0; } @@ -1461,8 +1495,8 @@ QRectF QMacStylePrivate::CocoaControl::adjustedControlFrame(const QRectF &rect) QRectF frameRect; const auto frameSize = defaultFrameSize(); if (type == QMacStylePrivate::Button_SquareButton) { - frameRect = rect.adjusted(3, 1, -3, -5) - .adjusted(focusRingWidth, focusRingWidth, -focusRingWidth, -focusRingWidth); + frameRect = rect.adjusted(3, 1, -3, -1) + .adjusted(focusRingWidth, focusRingWidth, -focusRingWidth, -focusRingWidth); } else if (type == QMacStylePrivate::Button_PushButton) { // Start from the style option's top-left corner. frameRect = QRectF(rect.topLeft(), @@ -1478,7 +1512,7 @@ QRectF QMacStylePrivate::CocoaControl::adjustedControlFrame(const QRectF &rect) frameRect = frameRect.translated(rect.topLeft()); if (type == QMacStylePrivate::Button_PullDown || type == QMacStylePrivate::Button_PopupButton) { if (size == QStyleHelper::SizeLarge) - frameRect = frameRect.adjusted(0, 0, -6, 0).translated(3, -1); + frameRect = frameRect.adjusted(0, 0, -6, 0).translated(3, 0); else if (size == QStyleHelper::SizeSmall) frameRect = frameRect.adjusted(0, 0, -4, 0).translated(2, 1); else if (size == QStyleHelper::SizeMini) @@ -1697,18 +1731,28 @@ NSView *QMacStylePrivate::cocoaControl(CocoaControl widget) const || widget.size == QStyleHelper::SizeDefault) return nil; + if (widget.type == Box) { + if (__builtin_available(macOS 10.14, *)) { + if (qt_mac_applicationIsInDarkMode()) { + // See render code in drawPrimitive(PE_FrameTabWidget) + widget.type = Box_Dark; + } + } + } + NSView *bv = cocoaControls.value(widget, nil); if (!bv) { switch (widget.type) { case Box: { - NSBox *bc = [[NSBox alloc] init]; - bc.title = @""; - bc.titlePosition = NSNoTitle; - bc.boxType = NSBoxPrimary; - bc.borderType = NSBezelBorder; - bv = bc; + NSBox *box = [[NSBox alloc] init]; + bv = box; + box.title = @""; + box.titlePosition = NSNoTitle; break; } + case Box_Dark: + bv = [[QDarkNSBox alloc] init]; + break; case Button_CheckBox: case Button_Disclosure: case Button_PushButton: @@ -1745,17 +1789,10 @@ NSView *QMacStylePrivate::cocoaControl(CocoaControl widget) const } Q_UNREACHABLE(); } (); -#if QT_MACOS_PLATFORM_SDK_EQUAL_OR_ABOVE(__MAC_10_12) const auto styleMask = NSWindowStyleMaskTitled | NSWindowStyleMaskClosable | NSWindowStyleMaskMiniaturizable | NSWindowStyleMaskResizable; -#else - const auto styleMask = NSTitledWindowMask - | NSClosableWindowMask - | NSMiniaturizableWindowMask - | NSResizableWindowMask; -#endif bv = [NSWindow standardWindowButton:button forStyleMask:styleMask]; [bv retain]; break; @@ -1802,10 +1839,10 @@ NSView *QMacStylePrivate::cocoaControl(CocoaControl widget) const auto *ctrl = static_cast<NSControl *>(bv); switch (widget.size) { case QStyleHelper::SizeSmall: - ctrl.controlSize = NSSmallControlSize; + ctrl.controlSize = NSControlSizeSmall; break; case QStyleHelper::SizeMini: - ctrl.controlSize = NSMiniControlSize; + ctrl.controlSize = NSControlSizeMini; break; default: break; @@ -1816,10 +1853,10 @@ NSView *QMacStylePrivate::cocoaControl(CocoaControl widget) const pi.indeterminate = (widget.type == ProgressIndicator_Indeterminate); switch (widget.size) { case QStyleHelper::SizeSmall: - pi.controlSize = NSSmallControlSize; + pi.controlSize = NSControlSizeSmall; break; case QStyleHelper::SizeMini: - pi.controlSize = NSMiniControlSize; + pi.controlSize = NSControlSizeMini; break; default: break; @@ -1865,10 +1902,10 @@ NSCell *QMacStylePrivate::cocoaCell(CocoaControl widget) const switch (widget.size) { case QStyleHelper::SizeSmall: - cell.controlSize = NSSmallControlSize; + cell.controlSize = NSControlSizeSmall; break; case QStyleHelper::SizeMini: - cell.controlSize = NSMiniControlSize; + cell.controlSize = NSControlSizeMini; break; default: break; @@ -1936,7 +1973,7 @@ QMacStyle::QMacStyle() Q_D(QMacStyle); QMacAutoReleasePool pool; - d->receiver = [[NotificationReceiver alloc] initWithPrivate:d]; + d->receiver = [[NotificationReceiver alloc] init]; [[NSNotificationCenter defaultCenter] addObserver:d->receiver selector:@selector(scrollBarStyleDidChange:) name:NSPreferredScrollerStyleDidChangeNotification @@ -2208,9 +2245,9 @@ int QMacStyle::pixelMetric(PixelMetric metric, const QStyleOption *opt, const QW ret = 0; break; case PM_TitleBarHeight: { - NSUInteger style = NSTitledWindowMask; + NSUInteger style = NSWindowStyleMaskTitled; if (widget && ((widget->windowFlags() & Qt::Tool) == Qt::Tool)) - style |= NSUtilityWindowMask; + style |= NSWindowStyleMaskUtilityWindow; ret = int([NSWindow frameRectForContentRect:NSZeroRect styleMask:style].size.height); break; } @@ -2752,6 +2789,9 @@ int QMacStyle::styleHint(StyleHint sh, const QStyleOption *opt, const QWidget *w case SH_SpinBox_ButtonsInsideFrame: ret = false; break; + case SH_Table_GridLineColor: + ret = int(qt_mac_toQColor(NSColor.gridColor).rgb()); + break; default: ret = QCommonStyle::styleHint(sh, opt, w, hret); break; @@ -2820,8 +2860,7 @@ void QMacStyle::drawPrimitive(PrimitiveElement pe, const QStyleOption *opt, QPai { Q_D(const QMacStyle); QMacCGContext cg(p); - QWindow *window = w && w->window() ? w->window()->windowHandle() : - QStyleHelper::styleObjectWindow(opt->styleObject); + QWindow *window = w && w->window() ? w->window()->windowHandle() : nullptr; d->resolveCurrentNSView(window); switch (pe) { case PE_IndicatorArrowUp: @@ -2923,10 +2962,28 @@ void QMacStyle::drawPrimitive(PrimitiveElement pe, const QStyleOption *opt, QPai { const auto cw = QMacStylePrivate::CocoaControl(QMacStylePrivate::Box, QStyleHelper::SizeLarge); auto *box = static_cast<NSBox *>(d->cocoaControl(cw)); + // FIXME Since macOS 10.14, simply calling drawRect: won't display anything anymore. + // The AppKit team is aware of this and has proposed a couple of solutions. + // The first solution was to call displayRectIgnoringOpacity:inContext: instead. + // However, it doesn't seem to work on 10.13. More importantly, dark mode on 10.14 + // is extremely slow. Light mode works fine. + // The second solution is to subclass NSBox and reimplement a trivial drawRect: which + // would only call super. This works without any issue on 10.13, but a double border + // shows on 10.14 in both light and dark modes. + // The code below picks what works on each version and mode. On 10.13 and earlier, we + // simply call drawRect: on a regular NSBox. On 10.14, we call displayRectIgnoringOpacity: + // inContext:, but only in light mode. In dark mode, we use a custom NSBox subclass, + // QDarkNSBox, of type NSBoxCustom. Its appearance is close enough to the real thing so + // we can use this for now. d->drawNSViewInRect(box, opt->rect, p, ^(CGContextRef ctx, const CGRect &rect) { CGContextTranslateCTM(ctx, 0, rect.origin.y + rect.size.height); CGContextScaleCTM(ctx, 1, -1); - [box drawRect:rect]; + if (QOperatingSystemVersion::current() < QOperatingSystemVersion::MacOSMojave + || [box isMemberOfClass:QDarkNSBox.class]) { + [box drawRect:rect]; + } else { + [box displayRectIgnoringOpacity:box.bounds inContext:NSGraphicsContext.currentContext]; + } }); break; } @@ -3136,8 +3193,7 @@ void QMacStyle::drawPrimitive(PrimitiveElement pe, const QStyleOption *opt, QPai tf.bezeled = YES; static_cast<NSTextFieldCell *>(tf.cell).bezelStyle = isRounded ? NSTextFieldRoundedBezel : NSTextFieldSquareBezel; tf.frame = opt->rect.toCGRect(); - d->drawNSViewInRect(tf, opt->rect, p, ^(CGContextRef ctx, const CGRect &rect) { - Q_UNUSED(ctx); + d->drawNSViewInRect(tf, opt->rect, p, ^(CGContextRef, const CGRect &rect) { [tf.cell drawWithFrame:rect inView:tf]; }); } else { @@ -3173,7 +3229,13 @@ void QMacStyle::drawPrimitive(PrimitiveElement pe, const QStyleOption *opt, QPai break; case PE_IndicatorTabClose: { // Make close button visible only on the hovered tab. - if (QTabBar *tabBar = qobject_cast<QTabBar*>(w->parentWidget())) { + QTabBar *tabBar = qobject_cast<QTabBar*>(w->parentWidget()); + if (!tabBar) { + // QStyleSheetStyle instead of CloseButton (which has + // a QTabBar as a parent widget) uses the QTabBar itself: + tabBar = qobject_cast<QTabBar *>(const_cast<QWidget*>(w)); + } + if (tabBar) { const bool documentMode = tabBar->documentMode(); const QTabBarPrivate *tabBarPrivate = static_cast<QTabBarPrivate *>(QObjectPrivate::get(tabBar)); const int hoveredTabIndex = tabBarPrivate->hoveredTabIndex(); @@ -3266,8 +3328,7 @@ void QMacStyle::drawControl(ControlElement ce, const QStyleOption *opt, QPainter { Q_D(const QMacStyle); QMacCGContext cg(p); - QWindow *window = w && w->window() ? w->window()->windowHandle() : - QStyleHelper::styleObjectWindow(opt->styleObject); + QWindow *window = w && w->window() ? w->window()->windowHandle() : nullptr; d->resolveCurrentNSView(window); switch (ce) { case CE_HeaderSection: @@ -3468,7 +3529,7 @@ void QMacStyle::drawControl(ControlElement ce, const QStyleOption *opt, QPainter pb.enabled = isEnabled; [pb highlight:isPressed]; pb.state = isHighlighted && !isPressed ? NSOnState : NSOffState; - d->drawNSViewInRect(pb, frameRect, p, ^(CGContextRef __unused ctx, const CGRect &r) { + d->drawNSViewInRect(pb, frameRect, p, ^(CGContextRef, const CGRect &r) { [pb.cell drawBezelWithFrame:r inView:pb.superview]; }); [pb highlight:NO]; @@ -4210,7 +4271,7 @@ void QMacStyle::drawControl(ControlElement ce, const QStyleOption *opt, QPainter const auto cw = QMacStylePrivate::CocoaControl(ct, QStyleHelper::SizeLarge); auto *sv = static_cast<NSSplitView *>(d->cocoaControl(cw)); sv.frame = opt->rect.toCGRect(); - d->drawNSViewInRect(sv, opt->rect, p, ^(CGContextRef __unused ctx, const CGRect &rect) { + d->drawNSViewInRect(sv, opt->rect, p, ^(CGContextRef, const CGRect &rect) { [sv drawDividerInRect:rect]; }); } else { @@ -4253,12 +4314,13 @@ void QMacStyle::drawControl(ControlElement ce, const QStyleOption *opt, QPainter #ifndef QT_NO_TOOLBAR case CE_ToolBar: { const QStyleOptionToolBar *toolBar = qstyleoption_cast<const QStyleOptionToolBar *>(opt); + const bool isDarkMode = qt_mac_applicationIsInDarkMode(); // Unified title and toolbar drawing. In this mode the cocoa platform plugin will // fill the top toolbar area part with a background gradient that "unifies" with // the title bar. The following code fills the toolBar area with transparent pixels // to make that gradient visible. - if (w) { + if (w) { #if QT_CONFIG(mainwindow) if (QMainWindow * mainWindow = qobject_cast<QMainWindow *>(w->window())) { if (toolBar && toolBar->toolBarArea == Qt::TopToolBarArea && mainWindow->unifiedTitleAndToolBarOnMac()) { @@ -4268,7 +4330,7 @@ void QMacStyle::drawControl(ControlElement ce, const QStyleOption *opt, QPainter p->fillRect(opt->rect, Qt::transparent); p->restore(); - // Drow a horizontal separator line at the toolBar bottom if the "unified" area ends here. + // Draw a horizontal separator line at the toolBar bottom if the "unified" area ends here. // There might be additional toolbars or other widgets such as tab bars in document // mode below. Determine this by making a unified toolbar area test for the row below // this toolbar. @@ -4277,7 +4339,7 @@ void QMacStyle::drawControl(ControlElement ce, const QStyleOption *opt, QPainter if (isEndOfUnifiedArea) { const int margin = qt_mac_aqua_get_metric(SeparatorSize); const auto separatorRect = QRect(opt->rect.left(), opt->rect.bottom(), opt->rect.width(), margin); - p->fillRect(separatorRect, opt->palette.dark().color()); + p->fillRect(separatorRect, isDarkMode ? darkModeSeparatorLine : opt->palette.dark().color()); } break; } @@ -4292,21 +4354,23 @@ void QMacStyle::drawControl(ControlElement ce, const QStyleOption *opt, QPainter else linearGrad = QLinearGradient(opt->rect.left(), 0, opt->rect.right(), 0); + QColor mainWindowGradientBegin = isDarkMode ? darkMainWindowGradientBegin : lightMainWindowGradientBegin; + QColor mainWindowGradientEnd = isDarkMode ? darkMainWindowGradientEnd : lightMainWindowGradientEnd; + linearGrad.setColorAt(0, mainWindowGradientBegin); linearGrad.setColorAt(1, mainWindowGradientEnd); p->fillRect(opt->rect, linearGrad); p->save(); if (opt->state & State_Horizontal) { - p->setPen(mainWindowGradientBegin.lighter(114)); + p->setPen(isDarkMode ? darkModeSeparatorLine : mainWindowGradientBegin.lighter(114)); p->drawLine(opt->rect.topLeft(), opt->rect.topRight()); - p->setPen(mainWindowGradientEnd.darker(114)); + p->setPen(isDarkMode ? darkModeSeparatorLine :mainWindowGradientEnd.darker(114)); p->drawLine(opt->rect.bottomLeft(), opt->rect.bottomRight()); - } else { - p->setPen(mainWindowGradientBegin.lighter(114)); + p->setPen(isDarkMode ? darkModeSeparatorLine : mainWindowGradientBegin.lighter(114)); p->drawLine(opt->rect.topLeft(), opt->rect.bottomLeft()); - p->setPen(mainWindowGradientEnd.darker(114)); + p->setPen(isDarkMode ? darkModeSeparatorLine : mainWindowGradientEnd.darker(114)); p->drawLine(opt->rect.topRight(), opt->rect.bottomRight()); } p->restore(); @@ -4841,8 +4905,7 @@ void QMacStyle::drawComplexControl(ComplexControl cc, const QStyleOptionComplex { Q_D(const QMacStyle); QMacCGContext cg(p); - QWindow *window = widget && widget->window() ? widget->window()->windowHandle() : - QStyleHelper::styleObjectWindow(opt->styleObject); + QWindow *window = widget && widget->window() ? widget->window()->windowHandle() : nullptr; d->resolveCurrentNSView(window); switch (cc) { case CC_ScrollBar: @@ -5217,7 +5280,7 @@ void QMacStyle::drawComplexControl(ComplexControl cc, const QStyleOptionComplex } pb.frame = frameRect.toCGRect(); [pb highlight:isPressed]; - d->drawNSViewInRect(pb, frameRect, p, ^(CGContextRef __unused ctx, const CGRect &r) { + d->drawNSViewInRect(pb, frameRect, p, ^(CGContextRef, const CGRect &r) { [pb.cell drawBezelWithFrame:r inView:pb.superview]; }); } else if (cw.type == QMacStylePrivate::ComboBox) { @@ -5233,7 +5296,7 @@ void QMacStyle::drawComplexControl(ComplexControl cc, const QStyleOptionComplex // TODO Render to pixmap and darken the button manually } - d->drawNSViewInRect(cb, frameRect, p, ^(CGContextRef __unused ctx, const CGRect &r) { + d->drawNSViewInRect(cb, frameRect, p, ^(CGContextRef, const CGRect &r) { // FIXME This is usually drawn in the control's superview, but we wouldn't get inactive look in this case [cb.cell drawWithFrame:r inView:cb]; }); @@ -5305,13 +5368,10 @@ void QMacStyle::drawComplexControl(ComplexControl cc, const QStyleOptionComplex Q_UNUSED(isHovered); // FIXME No public API for this const auto buttonRect = proxy()->subControlRect(CC_TitleBar, titlebar, sc, widget); - const auto drawBlock = ^ (CGContextRef ctx, const CGRect &rect) { - Q_UNUSED(ctx); - Q_UNUSED(rect); + d->drawNSViewInRect(wb, buttonRect, p, ^(CGContextRef, const CGRect &rect) { auto *wbCell = static_cast<NSButtonCell *>(wb.cell); [wbCell drawWithFrame:rect inView:wb]; - }; - d->drawNSViewInRect(wb, buttonRect, p, drawBlock); + }); } } @@ -5376,12 +5436,6 @@ void QMacStyle::drawComplexControl(ComplexControl cc, const QStyleOptionComplex d->drawToolbarButtonArrow(tb, p); } if (tb->state & State_On) { - QWindow *window = 0; - if (widget && widget->window()) - window = widget->window()->windowHandle(); - else if (opt->styleObject) - window = opt->styleObject->property("_q_styleObjectWindow").value<QWindow *>(); - NSView *view = window ? (NSView *)window->winId() : nil; bool isKey = false; if (view) @@ -5420,8 +5474,8 @@ void QMacStyle::drawComplexControl(ComplexControl cc, const QStyleOptionComplex pb.enabled = isEnabled; [pb highlight:isPressed]; pb.state = isHighlighted && !isPressed ? NSOnState : NSOffState; - const auto buttonRect = proxy()->subControlRect(cc, tb, SC_ToolButton, widget); - d->drawNSViewInRect(pb, buttonRect, p, ^(CGContextRef __unused ctx, const CGRect &rect) { + const auto buttonRect = proxy()->subControlRect(cc, tb, SC_ToolButton, widget); + d->drawNSViewInRect(pb, buttonRect, p, ^(CGContextRef, const CGRect &rect) { [pb.cell drawBezelWithFrame:rect inView:pb]; }); } @@ -5904,7 +5958,7 @@ QSize QMacStyle::sizeFromContents(ContentsType ct, const QStyleOption *opt, switch (ct) { #if QT_CONFIG(spinbox) case CT_SpinBox: - if (const QStyleOptionSpinBox *vopt = qstyleoption_cast<const QStyleOptionSpinBox *>(opt)) { + if (qstyleoption_cast<const QStyleOptionSpinBox *>(opt)) { const int buttonWidth = 20; // FIXME Use subControlRect() sz += QSize(buttonWidth, 0); } |