QMacStyle: Make CocoaControl a full type

And one small code clean-up.

Change-Id: I57c71ce2e18c95529ee45ba4fdfc68e21209e384
Reviewed-by: Morten Johan Sørvig <morten.sorvig@qt.io>
This commit is contained in:
Gabriel de Dietrich 2018-03-13 18:04:30 -07:00
parent 14157a1a3e
commit 3691c41115
2 changed files with 80 additions and 50 deletions

View File

@ -1210,13 +1210,13 @@ void QMacStylePrivate::drawFocusRing(QPainter *p, const QRect &targetRect, int h
QPainterPath focusRingPath; QPainterPath focusRingPath;
qreal hOffset = 0.0; qreal hOffset = 0.0;
qreal vOffset = 0.0; qreal vOffset = 0.0;
switch (cw.first) { switch (cw.type) {
case Button_CheckBox: { case Button_CheckBox: {
const auto cbInnerRadius = (cw.second == QStyleHelper::SizeMini ? 2.0 : 3.0); const auto cbInnerRadius = (cw.size == QStyleHelper::SizeMini ? 2.0 : 3.0);
const auto cbSize = cw.second == QStyleHelper::SizeLarge ? 13 : const auto cbSize = cw.size == QStyleHelper::SizeLarge ? 13 :
cw.second == QStyleHelper::SizeSmall ? 11 : 9; // As measured cw.size == QStyleHelper::SizeSmall ? 11 : 9; // As measured
hOffset = hMargin + (cw.second == QStyleHelper::SizeLarge ? 2.5 : hOffset = hMargin + (cw.size == QStyleHelper::SizeLarge ? 2.5 :
cw.second == QStyleHelper::SizeSmall ? 2.0 : 1.0); // As measured cw.size == QStyleHelper::SizeSmall ? 2.0 : 1.0); // As measured
vOffset = 0.5 * qreal(targetRect.height() - cbSize); vOffset = 0.5 * qreal(targetRect.height() - cbSize);
const auto cbInnerRect = QRectF(0, 0, cbSize, cbSize); const auto cbInnerRect = QRectF(0, 0, cbSize, cbSize);
const auto cbOutterRadius = cbInnerRadius + focusRingWidth; const auto cbOutterRadius = cbInnerRadius + focusRingWidth;
@ -1227,10 +1227,10 @@ void QMacStylePrivate::drawFocusRing(QPainter *p, const QRect &targetRect, int h
break; break;
} }
case Button_RadioButton: { case Button_RadioButton: {
const auto rbSize = cw.second == QStyleHelper::SizeLarge ? 15 : const auto rbSize = cw.size == QStyleHelper::SizeLarge ? 15 :
cw.second == QStyleHelper::SizeSmall ? 13 : 9; // As measured cw.size == QStyleHelper::SizeSmall ? 13 : 9; // As measured
hOffset = hMargin + (cw.second == QStyleHelper::SizeLarge ? 1.5 : hOffset = hMargin + (cw.size == QStyleHelper::SizeLarge ? 1.5 :
cw.second == QStyleHelper::SizeSmall ? 1.0 : 1.0); // As measured cw.size == QStyleHelper::SizeSmall ? 1.0 : 1.0); // As measured
vOffset = 0.5 * qreal(targetRect.height() - rbSize); vOffset = 0.5 * qreal(targetRect.height() - rbSize);
const auto rbInnerRect = QRectF(0, 0, rbSize, rbSize); const auto rbInnerRect = QRectF(0, 0, rbSize, rbSize);
const auto rbOutterRect = rbInnerRect.adjusted(-focusRingWidth, -focusRingWidth, focusRingWidth, focusRingWidth); const auto rbOutterRect = rbInnerRect.adjusted(-focusRingWidth, -focusRingWidth, focusRingWidth, focusRingWidth);
@ -1501,6 +1501,26 @@ QSize QMacStylePrivate::pushButtonSizeFromContents(const QStyleOptionButton *btn
return csz; return csz;
} }
uint qHash(const QMacStylePrivate::CocoaControl &cw, uint seed = 0)
{
return ((cw.type << 2) | cw.size) ^ seed;
}
QMacStylePrivate::CocoaControl::CocoaControl()
: type(NoControl), size(QStyleHelper::SizeDefault)
{
}
QMacStylePrivate::CocoaControl::CocoaControl(CocoaControlType t, QStyleHelper::WidgetSizePolicy s)
: type(t), size(s)
{
}
bool QMacStylePrivate::CocoaControl::operator==(const CocoaControl &other) const
{
return other.type == type && other.size == size;
}
/** /**
Checks if the actual contents of btn fits inside the free content bounds of Checks if the actual contents of btn fits inside the free content bounds of
'buttonKindToCheck'. Meant as a helper function for 'initHIThemePushButton' 'buttonKindToCheck'. Meant as a helper function for 'initHIThemePushButton'
@ -1605,8 +1625,8 @@ void QMacStylePrivate::initComboboxBdi(const QStyleOptionComboBox *combo, HIThem
bdi->state = tds; bdi->state = tds;
QStyleHelper::WidgetSizePolicy aSize = aquaSizeConstrain(combo, widget); QStyleHelper::WidgetSizePolicy aSize = aquaSizeConstrain(combo, widget);
cw->first = combo->editable ? ComboBox : Button_PopupButton; cw->type = combo->editable ? ComboBox : Button_PopupButton;
cw->second = aSize; cw->size = aSize;
switch (aSize) { switch (aSize) {
case QStyleHelper::SizeMini: case QStyleHelper::SizeMini:
bdi->kind = combo->editable ? ThemeButtonKind(kThemeComboBoxMini) bdi->kind = combo->editable ? ThemeButtonKind(kThemeComboBoxMini)
@ -1638,26 +1658,26 @@ void QMacStylePrivate::initComboboxBdi(const QStyleOptionComboBox *combo, HIThem
// or return any subcontrol size of the poor thing. // or return any subcontrol size of the poor thing.
if (h < 9) { if (h < 9) {
bdi->kind = kThemeComboBoxMini; bdi->kind = kThemeComboBoxMini;
cw->second = QStyleHelper::SizeMini; cw->size = QStyleHelper::SizeMini;
} else if (h < 22) { } else if (h < 22) {
bdi->kind = kThemeComboBoxSmall; bdi->kind = kThemeComboBoxSmall;
cw->second = QStyleHelper::SizeSmall; cw->size = QStyleHelper::SizeSmall;
} else { } else {
bdi->kind = kThemeComboBox; bdi->kind = kThemeComboBox;
cw->second = QStyleHelper::SizeLarge; cw->size = QStyleHelper::SizeLarge;
} }
} else } else
#endif #endif
{ {
if (h < 21) { if (h < 21) {
bdi->kind = kThemeComboBoxMini; bdi->kind = kThemeComboBoxMini;
cw->second = QStyleHelper::SizeMini; cw->size = QStyleHelper::SizeMini;
} else if (h < 26) { } else if (h < 26) {
bdi->kind = kThemeComboBoxSmall; bdi->kind = kThemeComboBoxSmall;
cw->second = QStyleHelper::SizeSmall; cw->size = QStyleHelper::SizeSmall;
} else { } else {
bdi->kind = kThemeComboBox; bdi->kind = kThemeComboBox;
cw->second = QStyleHelper::SizeLarge; cw->size = QStyleHelper::SizeLarge;
} }
} }
} else { } else {
@ -1668,13 +1688,13 @@ void QMacStylePrivate::initComboboxBdi(const QStyleOptionComboBox *combo, HIThem
// the use of LargeButtons explicit. // the use of LargeButtons explicit.
if (h < 21) { if (h < 21) {
bdi->kind = kThemePopupButtonMini; bdi->kind = kThemePopupButtonMini;
cw->second = QStyleHelper::SizeMini; cw->size = QStyleHelper::SizeMini;
} else if (h < 26) { } else if (h < 26) {
bdi->kind = kThemePopupButtonSmall; bdi->kind = kThemePopupButtonSmall;
cw->second = QStyleHelper::SizeSmall; cw->size = QStyleHelper::SizeSmall;
} else { } else {
bdi->kind = kThemePopupButton; bdi->kind = kThemePopupButton;
cw->second = QStyleHelper::SizeLarge; cw->size = QStyleHelper::SizeLarge;
} }
} }
break; break;
@ -1691,8 +1711,8 @@ CGRect QMacStylePrivate::comboboxInnerBounds(const CGRect &outerBounds, const Co
// Carbon draw parts of the view outside the rect. // Carbon draw parts of the view outside the rect.
// So make the rect a bit smaller to compensate // So make the rect a bit smaller to compensate
// (I wish HIThemeGetButtonBackgroundBounds worked) // (I wish HIThemeGetButtonBackgroundBounds worked)
if (cocoaWidget.first == Button_PopupButton) { if (cocoaWidget.type == Button_PopupButton) {
switch (cocoaWidget.second) { switch (cocoaWidget.size) {
case QStyleHelper::SizeSmall: case QStyleHelper::SizeSmall:
innerBounds.origin.x += 3; innerBounds.origin.x += 3;
innerBounds.origin.y += 3; innerBounds.origin.y += 3;
@ -1712,8 +1732,8 @@ CGRect QMacStylePrivate::comboboxInnerBounds(const CGRect &outerBounds, const Co
innerBounds.size.width -= 5; innerBounds.size.width -= 5;
innerBounds.size.height -= 6; innerBounds.size.height -= 6;
} }
} else if (cocoaWidget.first == ComboBox) { } else if (cocoaWidget.type == ComboBox) {
switch (cocoaWidget.second) { switch (cocoaWidget.size) {
case QStyleHelper::SizeSmall: case QStyleHelper::SizeSmall:
innerBounds.origin.x += 3; innerBounds.origin.x += 3;
innerBounds.origin.y += 3; innerBounds.origin.y += 3;
@ -1866,28 +1886,28 @@ ThemeDrawState QMacStylePrivate::getDrawState(QStyle::State flags)
case kThemePopupButton: case kThemePopupButton:
case kThemePopupButtonSmall: case kThemePopupButtonSmall:
case kThemePopupButtonMini: case kThemePopupButtonMini:
w.first = Button_PopupButton; w.type = Button_PopupButton;
break; break;
case kThemeComboBox: case kThemeComboBox:
w.first = ComboBox; w.type = ComboBox;
break; break;
case kThemeArrowButton: case kThemeArrowButton:
w.first = Button_Disclosure; w.type = Button_Disclosure;
break; break;
case kThemeCheckBox: case kThemeCheckBox:
case kThemeCheckBoxSmall: case kThemeCheckBoxSmall:
case kThemeCheckBoxMini: case kThemeCheckBoxMini:
w.first = Button_CheckBox; w.type = Button_CheckBox;
break; break;
case kThemeRadioButton: case kThemeRadioButton:
case kThemeRadioButtonSmall: case kThemeRadioButtonSmall:
case kThemeRadioButtonMini: case kThemeRadioButtonMini:
w.first = Button_RadioButton; w.type = Button_RadioButton;
break; break;
case kThemePushButton: case kThemePushButton:
case kThemePushButtonSmall: case kThemePushButtonSmall:
case kThemePushButtonMini: case kThemePushButtonMini:
w.first = Button_PushButton; w.type = Button_PushButton;
break; break;
default: default:
break; break;
@ -1898,16 +1918,16 @@ ThemeDrawState QMacStylePrivate::getDrawState(QStyle::State flags)
case kThemePopupButtonSmall: case kThemePopupButtonSmall:
case kThemeCheckBoxSmall: case kThemeCheckBoxSmall:
case kThemeRadioButtonSmall: case kThemeRadioButtonSmall:
w.second = QStyleHelper::SizeSmall; w.size = QStyleHelper::SizeSmall;
break; break;
case kThemePushButtonMini: case kThemePushButtonMini:
case kThemePopupButtonMini: case kThemePopupButtonMini:
case kThemeCheckBoxMini: case kThemeCheckBoxMini:
case kThemeRadioButtonMini: case kThemeRadioButtonMini:
w.second = QStyleHelper::SizeMini; w.size = QStyleHelper::SizeMini;
break; break;
default: default:
w.second = QStyleHelper::SizeLarge; w.size = QStyleHelper::SizeLarge;
break; break;
} }
@ -1928,7 +1948,7 @@ NSView *QMacStylePrivate::cocoaControl(CocoaControl widget) const
NSView *bv = cocoaControls.value(widget, nil); NSView *bv = cocoaControls.value(widget, nil);
if (!bv) { if (!bv) {
switch (widget.first) { switch (widget.type) {
case Box: { case Box: {
NSBox *bc = [[NSBox alloc] init]; NSBox *bc = [[NSBox alloc] init];
bc.title = @""; bc.title = @"";
@ -1948,7 +1968,7 @@ NSView *QMacStylePrivate::cocoaControl(CocoaControl widget) const
case Button_PullDown: { case Button_PullDown: {
NSPopUpButton *bc = [[NSPopUpButton alloc] init]; NSPopUpButton *bc = [[NSPopUpButton alloc] init];
bc.title = @""; bc.title = @"";
if (widget.first == Button_PullDown) if (widget.type == Button_PullDown)
bc.pullsDown = YES; bc.pullsDown = YES;
bv = bc; bv = bc;
break; break;
@ -1963,7 +1983,7 @@ NSView *QMacStylePrivate::cocoaControl(CocoaControl widget) const
case Button_WindowMiniaturize: case Button_WindowMiniaturize:
case Button_WindowZoom: { case Button_WindowZoom: {
const NSWindowButton button = [=] { const NSWindowButton button = [=] {
switch (widget.first) { switch (widget.type) {
case Button_WindowClose: case Button_WindowClose:
return NSWindowCloseButton; return NSWindowCloseButton;
case Button_WindowMiniaturize: case Button_WindowMiniaturize:
@ -2030,7 +2050,7 @@ NSView *QMacStylePrivate::cocoaControl(CocoaControl widget) const
if ([bv isKindOfClass:[NSControl class]]) { if ([bv isKindOfClass:[NSControl class]]) {
auto *ctrl = static_cast<NSControl *>(bv); auto *ctrl = static_cast<NSControl *>(bv);
switch (widget.second) { switch (widget.size) {
case QStyleHelper::SizeSmall: case QStyleHelper::SizeSmall:
ctrl.controlSize = NSSmallControlSize; ctrl.controlSize = NSSmallControlSize;
break; break;
@ -2040,11 +2060,11 @@ NSView *QMacStylePrivate::cocoaControl(CocoaControl widget) const
default: default:
break; break;
} }
} else if (widget.first == ProgressIndicator_Determinate || } else if (widget.type == ProgressIndicator_Determinate ||
widget.first == ProgressIndicator_Indeterminate) { widget.type == ProgressIndicator_Indeterminate) {
auto *pi = static_cast<NSProgressIndicator *>(bv); auto *pi = static_cast<NSProgressIndicator *>(bv);
pi.indeterminate = (widget.first == ProgressIndicator_Indeterminate); pi.indeterminate = (widget.type == ProgressIndicator_Indeterminate);
switch (widget.second) { switch (widget.size) {
case QStyleHelper::SizeSmall: case QStyleHelper::SizeSmall:
pi.controlSize = NSSmallControlSize; pi.controlSize = NSSmallControlSize;
break; break;
@ -2066,7 +2086,7 @@ NSCell *QMacStylePrivate::cocoaCell(CocoaControl widget) const
{ {
NSCell *cell = cocoaCells[widget]; NSCell *cell = cocoaCells[widget];
if (!cell) { if (!cell) {
switch (widget.first) { switch (widget.type) {
case Stepper: case Stepper:
cell = [[NSStepperCell alloc] init]; cell = [[NSStepperCell alloc] init];
break; break;
@ -2081,7 +2101,7 @@ NSCell *QMacStylePrivate::cocoaCell(CocoaControl widget) const
break; break;
} }
switch (widget.second) { switch (widget.size) {
case QStyleHelper::SizeSmall: case QStyleHelper::SizeSmall:
cell.controlSize = NSSmallControlSize; cell.controlSize = NSSmallControlSize;
break; break;
@ -3839,13 +3859,13 @@ void QMacStyle::drawControl(ControlElement ce, const QStyleOption *opt, QPainter
QMacStylePrivate::CocoaControl cw = QMacStylePrivate::cocoaControlFromHIThemeButtonKind(bdi.kind); QMacStylePrivate::CocoaControl cw = QMacStylePrivate::cocoaControlFromHIThemeButtonKind(bdi.kind);
if (hasMenu) if (hasMenu)
cw.first = QMacStylePrivate::Button_PullDown; cw.type = QMacStylePrivate::Button_PullDown;
if (hasMenu && bdi.kind != kThemeBevelButton) { if (hasMenu && bdi.kind != kThemeBevelButton) {
NSPopUpButton *pdb = (NSPopUpButton *)d->cocoaControl(cw); NSPopUpButton *pdb = (NSPopUpButton *)d->cocoaControl(cw);
[pdb highlight:(bdi.state == kThemeStatePressed)]; [pdb highlight:(bdi.state == kThemeStatePressed)];
pdb.enabled = bdi.state != kThemeStateUnavailable && bdi.state != kThemeStateUnavailableInactive; pdb.enabled = bdi.state != kThemeStateUnavailable && bdi.state != kThemeStateUnavailableInactive;
QRect rect = opt->rect; QRect rect = opt->rect;
rect.adjust(0, 0, cw.second == QStyleHelper::SizeSmall ? -4 : cw.second == QStyleHelper::SizeMini ? -9 : -6, 0); rect.adjust(0, 0, cw.size == QStyleHelper::SizeSmall ? -4 : cw.size == QStyleHelper::SizeMini ? -9 : -6, 0);
d->drawNSViewInRect(cw, pdb, rect, p, w != 0); d->drawNSViewInRect(cw, pdb, rect, p, w != 0);
} else if (hasMenu && bdi.state == kThemeStatePressed) } else if (hasMenu && bdi.state == kThemeStatePressed)
d->drawColorlessButton(newRect, &bdi, cw, p, opt); d->drawColorlessButton(newRect, &bdi, cw, p, opt);
@ -4513,7 +4533,7 @@ void QMacStyle::drawControl(ControlElement ce, const QStyleOption *opt, QPainter
const auto cw = QMacStylePrivate::CocoaControl(ct, QStyleHelper::SizeLarge); const auto cw = QMacStylePrivate::CocoaControl(ct, QStyleHelper::SizeLarge);
auto *sv = static_cast<NSSplitView *>(d->cocoaControl(cw)); auto *sv = static_cast<NSSplitView *>(d->cocoaControl(cw));
sv.frame = opt->rect.toCGRect(); sv.frame = opt->rect.toCGRect();
d->drawNSViewInRect(cw, sv, opt->rect, p, w != nullptr, ^(CGContextRef ctx, const CGRect &rect) { d->drawNSViewInRect(cw, sv, opt->rect, p, w != nullptr, ^(CGContextRef __unused ctx, const CGRect &rect) {
[sv drawDividerInRect:rect]; [sv drawDividerInRect:rect];
}); });
} else { } else {
@ -6467,8 +6487,8 @@ QSize QMacStyle::sizeFromContents(ContentsType ct, const QStyleOption *opt,
if (const QStyleOptionComboBox *combo = qstyleoption_cast<const QStyleOptionComboBox *>(opt)){ if (const QStyleOptionComboBox *combo = qstyleoption_cast<const QStyleOptionComboBox *>(opt)){
const auto widgetSize = d->aquaSizeConstrain(opt, widget); const auto widgetSize = d->aquaSizeConstrain(opt, widget);
QMacStylePrivate::CocoaControl cw; QMacStylePrivate::CocoaControl cw;
cw.first = combo->editable ? QMacStylePrivate::ComboBox : QMacStylePrivate::Button_PopupButton; cw.type = combo->editable ? QMacStylePrivate::ComboBox : QMacStylePrivate::Button_PopupButton;
cw.second = widgetSize; cw.size = widgetSize;
const CGRect diffRect = QMacStylePrivate::comboboxInnerBounds(CGRectZero, cw); const CGRect diffRect = QMacStylePrivate::comboboxInnerBounds(CGRectZero, cw);
sz.rwidth() -= qRound(diffRect.size.width); sz.rwidth() -= qRound(diffRect.size.width);
sz.rheight() -= qRound(diffRect.size.height); sz.rheight() -= qRound(diffRect.size.height);

View File

@ -206,7 +206,17 @@ public:
TextField TextField
}; };
typedef QPair<CocoaControlType, QStyleHelper::WidgetSizePolicy> CocoaControl;
struct CocoaControl {
CocoaControl();
CocoaControl(CocoaControlType t, QStyleHelper::WidgetSizePolicy s);
CocoaControlType type;
QStyleHelper::WidgetSizePolicy size;
bool operator==(const CocoaControl &other) const;
};
typedef void (^DrawRectBlock)(CGContextRef, const CGRect &); typedef void (^DrawRectBlock)(CGContextRef, const CGRect &);