From 993fa70777d5f9bdda61d04a043ca258c1a693e5 Mon Sep 17 00:00:00 2001 From: Christian Ehrlicher Date: Fri, 4 Oct 2024 20:20:52 +0200 Subject: [PATCH] Windows11Style: add two helper functions to avoid code duplication Add and use the two helper functions - buttonFillBrush() returning the correct fill brush for a button - buttonLabelPen() returning the correct pen for a button Change-Id: I6ee0dbe2490ad5c875b75d13b48371b5e798c290 Reviewed-by: Wladimir Leuschner Reviewed-by: Oliver Wolff (cherry picked from commit 436abff5cce198bb90c90e92104922373a3f99cd) Reviewed-by: Qt Cherry-pick Bot --- .../styles/modernwindows/qwindows11style.cpp | 55 ++++++++++--------- .../styles/modernwindows/qwindows11style_p.h | 5 ++ 2 files changed, 34 insertions(+), 26 deletions(-) diff --git a/src/plugins/styles/modernwindows/qwindows11style.cpp b/src/plugins/styles/modernwindows/qwindows11style.cpp index 9a791beffe7..e26d2341825 100644 --- a/src/plugins/styles/modernwindows/qwindows11style.cpp +++ b/src/plugins/styles/modernwindows/qwindows11style.cpp @@ -836,14 +836,8 @@ void QWindows11Style::drawPrimitive(PrimitiveElement element, const QStyleOption clipRect.setLeft(rect.x() + (rect.width() - clipRect.width()) / 2.0); clipRect.setWidth(clipWidth * clipRect.width()); - - QBrush fillBrush = (option->state & State_On || option->state & State_NoChange) ? option->palette.accent() : option->palette.window(); - if (state & State_MouseOver && (option->state & State_On || option->state & State_NoChange)) - fillBrush.setColor(fillBrush.color().lighter(107)); - else if (state & State_MouseOver && !(option->state & State_On || option->state & State_NoChange)) - fillBrush.setColor(fillBrush.color().darker(107)); painter->setPen(Qt::NoPen); - painter->setBrush(fillBrush); + painter->setBrush(buttonFillBrush(option)); painter->drawRoundedRect(rect, secondLevelRoundingRadius, secondLevelRoundingRadius, Qt::AbsoluteSize); painter->setPen(QPen(highContrastTheme == true ? option->palette.buttonText().color() : WINUI3Colors[colorSchemeIndex][frameColorStrong])); @@ -1169,12 +1163,7 @@ void QWindows11Style::drawControl(ControlElement element, const QStyleOption *op rect.translate(shiftX, shiftY); painter->setFont(toolbutton->font); const QString text = d->toolButtonElideText(toolbutton, rect, alignment); - if (toolbutton->state & State_Raised || toolbutton->palette.isBrushSet(QPalette::Current, QPalette::ButtonText)) - painter->setPen(QPen(toolbutton->palette.buttonText().color())); - else if (!(toolbutton->state & State_Enabled)) - painter->setPen(flags & State_On ? QPen(WINUI3Colors[colorSchemeIndex][textAccentDisabled]) : QPen(toolbutton->palette.buttonText().color())); - else - painter->setPen(QPen(WINUI3Colors[colorSchemeIndex][controlTextSecondary])); + painter->setPen(buttonLabelPen(option, colorSchemeIndex)); proxy()->drawItemText(painter, rect, alignment, toolbutton->palette, toolbutton->state & State_Enabled, text); } else { @@ -1225,12 +1214,7 @@ void QWindows11Style::drawControl(ControlElement element, const QStyleOption *op } tr.translate(shiftX, shiftY); const QString text = d->toolButtonElideText(toolbutton, tr, alignment); - if (toolbutton->state & State_Raised || toolbutton->palette.isBrushSet(QPalette::Current, QPalette::ButtonText)) - painter->setPen(QPen(toolbutton->palette.buttonText().color())); - else if (!(toolbutton->state & State_Enabled)) - painter->setPen(flags & State_On ? QPen(WINUI3Colors[colorSchemeIndex][textAccentDisabled]) : QPen(toolbutton->palette.buttonText().color())); - else - painter->setPen(QPen(WINUI3Colors[colorSchemeIndex][controlTextSecondary])); + painter->setPen(buttonLabelPen(option, colorSchemeIndex)); proxy()->drawItemText(painter, QStyle::visualRect(toolbutton->direction, rect, tr), alignment, toolbutton->palette, toolbutton->state & State_Enabled, text); } else { @@ -1416,13 +1400,7 @@ void QWindows11Style::drawControl(ControlElement element, const QStyleOption *op tf |= Qt::AlignHCenter; } - - if (btn->state & State_Sunken) - painter->setPen(flags & State_On ? QPen(WINUI3Colors[colorSchemeIndex][textOnAccentSecondary]) : QPen(WINUI3Colors[colorSchemeIndex][controlTextSecondary])); - else if (!(btn->state & State_Enabled)) - painter->setPen(flags & State_On ? QPen(WINUI3Colors[colorSchemeIndex][textAccentDisabled]) : QPen(btn->palette.buttonText().color())); - else - painter->setPen(flags & State_On ? QPen(WINUI3Colors[colorSchemeIndex][textOnAccentPrimary]) : QPen(btn->palette.buttonText().color())); + painter->setPen(buttonLabelPen(option, colorSchemeIndex)); proxy()->drawItemText(painter, textRect, tf, option->palette,btn->state & State_Enabled, btn->text); } break; @@ -2281,6 +2259,31 @@ void QWindows11Style::polish(QPalette& result) result.setColor(QPalette::Active, QPalette::HighlightedText, result.windowText().color()); } +QBrush QWindows11Style::buttonFillBrush(const QStyleOption *option) +{ + const bool isOn = (option->state & QStyle::State_On || option->state & QStyle::State_NoChange); + QBrush brush = isOn ? option->palette.accent() : option->palette.window(); + if (option->state & QStyle::State_MouseOver) + brush.setColor(isOn ? brush.color().lighter(107) : brush.color().darker(107)); + return brush; +} + +QPen QWindows11Style::buttonLabelPen(const QStyleOption *option, int colorSchemeIndex) +{ + if (option->palette.isBrushSet(QPalette::Current, QPalette::ButtonText)) + return QPen(option->palette.buttonText().color()); + + const bool isOn = option->state & QStyle::State_On; + if (option->state & QStyle::State_Sunken) + return QPen(isOn ? WINUI3Colors[colorSchemeIndex][textOnAccentSecondary] + : WINUI3Colors[colorSchemeIndex][controlTextSecondary]); + if (!(option->state & QStyle::State_Enabled)) + return QPen(isOn ? WINUI3Colors[colorSchemeIndex][textAccentDisabled] + : option->palette.buttonText().color()); + return QPen(isOn ? WINUI3Colors[colorSchemeIndex][textOnAccentPrimary] + : option->palette.buttonText().color()); +} + #undef SET_IF_UNRESOLVED QT_END_NAMESPACE diff --git a/src/plugins/styles/modernwindows/qwindows11style_p.h b/src/plugins/styles/modernwindows/qwindows11style_p.h index 9c54afd967a..05b483ca8cb 100644 --- a/src/plugins/styles/modernwindows/qwindows11style_p.h +++ b/src/plugins/styles/modernwindows/qwindows11style_p.h @@ -51,6 +51,11 @@ public: void unpolish(QWidget *widget) override; protected: QWindows11Style(QWindows11StylePrivate &dd); + +private: + static inline QBrush buttonFillBrush(const QStyleOption *option); + static inline QPen buttonLabelPen(const QStyleOption *option, int colorSchemeIndex); + private: Q_DISABLE_COPY_MOVE(QWindows11Style) Q_DECLARE_PRIVATE(QWindows11Style)