Windows11Style: Use pixmap cache for drawing CC_SpinBox

Use a pixmap cache for CC_SpinBox to avoid redundant paintings. The idea
is taken from the fusion style.

Change-Id: Ic8a669ef68b60475b67b836b5bb0d22e64bf0f90
Reviewed-by: Axel Spoerl <axel.spoerl@qt.io>
Reviewed-by: Oliver Wolff <oliver.wolff@qt.io>
This commit is contained in:
Christian Ehrlicher 2024-09-18 20:31:09 +02:00
parent 57f461e21a
commit 09f0edb781

View File

@ -246,29 +246,35 @@ void QWindows11Style::drawComplexControl(ComplexControl control, const QStyleOpt
#if QT_CONFIG(spinbox) #if QT_CONFIG(spinbox)
case CC_SpinBox: case CC_SpinBox:
if (const QStyleOptionSpinBox *sb = qstyleoption_cast<const QStyleOptionSpinBox *>(option)) { if (const QStyleOptionSpinBox *sb = qstyleoption_cast<const QStyleOptionSpinBox *>(option)) {
QCachedPainter cp(painter, QLatin1StringView("win11_spinbox") % HexString<uint8_t>(colorSchemeIndex),
sb, sb->rect.size());
if (cp.needsPainting()) {
if (sb->frame && (sub & SC_SpinBoxFrame)) { if (sb->frame && (sub & SC_SpinBoxFrame)) {
painter->save(); cp->save();
QRegion clipRegion = option->rect; QRegion clipRegion = option->rect;
clipRegion -= option->rect.adjusted(2, 2, -2, -2); clipRegion -= option->rect.adjusted(2, 2, -2, -2);
painter->setClipRegion(clipRegion); cp->setClipRegion(clipRegion);
QColor lineColor = state & State_HasFocus ? option->palette.accent().color() : QColor(0,0,0); QColor lineColor = state & State_HasFocus ? option->palette.accent().color() : QColor(0,0,0);
painter->setPen(QPen(lineColor)); cp->setPen(QPen(lineColor));
painter->drawLine(option->rect.bottomLeft() + QPointF(7,-0.5), option->rect.bottomRight() + QPointF(-7,-0.5)); cp->drawLine(option->rect.bottomLeft() + QPointF(7, -0.5),
painter->restore(); option->rect.bottomRight() + QPointF(-7, -0.5));
cp->restore();
} }
const QRectF frameRect = QRectF(option->rect).adjusted(2.5, 2.5, -2.5, -2.5); const QRectF frameRect = QRectF(option->rect).adjusted(2.5, 2.5, -2.5, -2.5);
const QBrush fillBrush = option->palette.brush(QPalette::Base); const QBrush fillBrush = option->palette.brush(QPalette::Base);
painter->setBrush(fillBrush); cp->setBrush(fillBrush);
painter->setPen(QPen(highContrastTheme == true ? sb->palette.buttonText().color() : WINUI3Colors[colorSchemeIndex][frameColorLight])); cp->setPen(QPen(highContrastTheme == true ? sb->palette.buttonText().color()
painter->drawRoundedRect(frameRect, secondLevelRoundingRadius, secondLevelRoundingRadius); : WINUI3Colors[colorSchemeIndex][frameColorLight]));
cp->drawRoundedRect(frameRect, secondLevelRoundingRadius, secondLevelRoundingRadius);
const QPoint mousePos = widget ? widget->mapFromGlobal(QCursor::pos()) : QPoint(); const QPoint mousePos = widget ? widget->mapFromGlobal(QCursor::pos()) : QPoint();
if (sub & SC_SpinBoxEditField) { if (sub & SC_SpinBoxEditField) {
const QRect rect = proxy()->subControlRect(CC_SpinBox, option, SC_SpinBoxEditField, widget).adjusted(0, 0, 0, 1); const QRect rect = proxy()->subControlRect(CC_SpinBox, option, SC_SpinBoxEditField,
widget).adjusted(0, 0, 0, 1);
if (!(state & State_HasFocus) && rect.contains(mousePos)) { if (!(state & State_HasFocus) && rect.contains(mousePos)) {
const QColor fillColor = WINUI3Colors[colorSchemeIndex][subtleHighlightColor]; const QColor fillColor = WINUI3Colors[colorSchemeIndex][subtleHighlightColor];
painter->setPen(Qt::NoPen); cp->setPen(Qt::NoPen);
painter->setBrush(QBrush(fillColor)); cp->setBrush(QBrush(fillColor));
painter->drawRoundedRect(option->rect.adjusted(2, 2, -2, -2), secondLevelRoundingRadius, cp->drawRoundedRect(option->rect.adjusted(2, 2, -2, -2), secondLevelRoundingRadius,
secondLevelRoundingRadius); secondLevelRoundingRadius);
} }
} }
@ -279,20 +285,21 @@ void QWindows11Style::drawComplexControl(ComplexControl control, const QStyleOpt
rect.adjust(0, 0, 0, 1); rect.adjust(0, 0, 0, 1);
if (rect.contains(mousePos)) { if (rect.contains(mousePos)) {
const QColor hoverColor = WINUI3Colors[colorSchemeIndex][subtleHighlightColor]; const QColor hoverColor = WINUI3Colors[colorSchemeIndex][subtleHighlightColor];
painter->setPen(Qt::NoPen); cp->setPen(Qt::NoPen);
painter->setBrush(QBrush(hoverColor)); cp->setBrush(QBrush(hoverColor));
painter->drawRoundedRect(rect.adjusted(1, 1, -1, -1), secondLevelRoundingRadius, cp->drawRoundedRect(rect.adjusted(1, 1, -1, -1), secondLevelRoundingRadius,
secondLevelRoundingRadius); secondLevelRoundingRadius);
} }
painter->setFont(assetFont); cp->setFont(assetFont);
painter->setPen(sb->palette.buttonText().color()); cp->setPen(sb->palette.buttonText().color());
painter->setBrush(Qt::NoBrush); cp->setBrush(Qt::NoBrush);
const auto str = isUp ? QStringLiteral(u"\uE70E") : QStringLiteral(u"\uE70D"); const auto str = isUp ? QStringLiteral(u"\uE70E") : QStringLiteral(u"\uE70D");
painter->drawText(rect, str, Qt::AlignVCenter | Qt::AlignHCenter); cp->drawText(rect, str, Qt::AlignVCenter | Qt::AlignHCenter);
}; };
if (sub & SC_SpinBoxUp) drawUpDown(SC_SpinBoxUp); if (sub & SC_SpinBoxUp) drawUpDown(SC_SpinBoxUp);
if (sub & SC_SpinBoxDown) drawUpDown(SC_SpinBoxDown); if (sub & SC_SpinBoxDown) drawUpDown(SC_SpinBoxDown);
} }
}
break; break;
#endif // QT_CONFIG(spinbox) #endif // QT_CONFIG(spinbox)
#if QT_CONFIG(slider) #if QT_CONFIG(slider)