Skip to content

Commit

Permalink
Windows11Style: add two helper functions to avoid code duplication
Browse files Browse the repository at this point in the history
Add and use the two helper functions
 - buttonFillBrush() returning the correct fill brush for a button
 - buttonLabelPen() returning the correct pen for a button

Pick-to: 6.8
Change-Id: I6ee0dbe2490ad5c875b75d13b48371b5e798c290
Reviewed-by: Wladimir Leuschner <wladimir.leuschner@qt.io>
Reviewed-by: Oliver Wolff <oliver.wolff@qt.io>
  • Loading branch information
chehrlic committed Oct 18, 2024
1 parent d5d67a7 commit 436abff
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 26 deletions.
55 changes: 29 additions & 26 deletions src/plugins/styles/modernwindows/qwindows11style.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -849,14 +849,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]));
Expand Down Expand Up @@ -1182,12 +1176,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 {
Expand Down Expand Up @@ -1238,12 +1227,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 {
Expand Down Expand Up @@ -1429,13 +1413,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;
Expand Down Expand Up @@ -2300,6 +2278,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
5 changes: 5 additions & 0 deletions src/plugins/styles/modernwindows/qwindows11style_p.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ class QWindows11Style : public QWindowsVistaStyle
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)
Expand Down

0 comments on commit 436abff

Please sign in to comment.