Minor improvements of windows style

1. Make some global constants constexpr.
2. Make some file-scope global constants static.
3. Use QStringLiteral instead of plain const char*
4. Add "u" to QStringLiteral's content.

Pick-to: 6.8
Change-Id: Icbc105366ba40e970b256fe3da41231a6fb5064b
Reviewed-by: Christian Ehrlicher <ch.ehrlicher@gmx.de>
(cherry picked from commit ad06099b93d79a954d133d6822517d4d0a10adbc)
Reviewed-by: Qt Cherry-pick Bot <cherrypick_bot@qt-project.org>
This commit is contained in:
Yuhang Zhao 2024-11-29 10:11:08 +08:00 committed by Qt Cherry-pick Bot
parent 1a737083f1
commit 7c3723b2a9
2 changed files with 39 additions and 39 deletions

View File

@ -32,9 +32,9 @@
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
const static int topLevelRoundingRadius = 8; //Radius for toplevel items like popups for round corners static constexpr int topLevelRoundingRadius = 8; //Radius for toplevel items like popups for round corners
const static int secondLevelRoundingRadius = 4; //Radius for second level items like hovered menu item round corners static constexpr int secondLevelRoundingRadius = 4; //Radius for second level items like hovered menu item round corners
constexpr QLatin1StringView originalWidthProperty("_q_windows11_style_original_width"); static constexpr QLatin1StringView originalWidthProperty("_q_windows11_style_original_width");
enum WINUI3Color { enum WINUI3Color {
subtleHighlightColor, //Subtle highlight based on alpha used for hovered elements subtleHighlightColor, //Subtle highlight based on alpha used for hovered elements
@ -57,7 +57,7 @@ enum WINUI3Color {
textAccentDisabled textAccentDisabled
}; };
const static QColor WINUI3ColorsLight [] { static const QColor WINUI3ColorsLight [] {
QColor(0x00,0x00,0x00,0x09), //subtleHighlightColor QColor(0x00,0x00,0x00,0x09), //subtleHighlightColor
QColor(0x00,0x00,0x00,0x06), //subtlePressedColor QColor(0x00,0x00,0x00,0x06), //subtlePressedColor
QColor(0x00,0x00,0x00,0x0F), //frameColorLight QColor(0x00,0x00,0x00,0x0F), //frameColorLight
@ -78,7 +78,7 @@ const static QColor WINUI3ColorsLight [] {
QColor(0xFF,0xFF,0xFF,0xFF), //textAccentDisabled QColor(0xFF,0xFF,0xFF,0xFF), //textAccentDisabled
}; };
const static QColor WINUI3ColorsDark[] { static const QColor WINUI3ColorsDark[] {
QColor(0xFF,0xFF,0xFF,0x0F), //subtleHighlightColor QColor(0xFF,0xFF,0xFF,0x0F), //subtleHighlightColor
QColor(0xFF,0xFF,0xFF,0x0A), //subtlePressedColor QColor(0xFF,0xFF,0xFF,0x0A), //subtlePressedColor
QColor(0xFF,0xFF,0xFF,0x12), //frameColorLight QColor(0xFF,0xFF,0xFF,0x12), //frameColorLight
@ -99,12 +99,12 @@ const static QColor WINUI3ColorsDark[] {
QColor(0xFF,0xFF,0xFF,0x87), //textAccentDisabled QColor(0xFF,0xFF,0xFF,0x87), //textAccentDisabled
}; };
const static QColor* WINUI3Colors[] { static const QColor* WINUI3Colors[] {
WINUI3ColorsLight, WINUI3ColorsLight,
WINUI3ColorsDark WINUI3ColorsDark
}; };
const QColor shellCloseButtonColor(0xC4,0x2B,0x1C,0xFF); //Color of close Button in Titlebar static const QColor shellCloseButtonColor(0xC4,0x2B,0x1C,0xFF); //Color of close Button in Titlebar
#if QT_CONFIG(toolbutton) #if QT_CONFIG(toolbutton)
static void drawArrow(const QStyle *style, const QStyleOptionToolButton *toolbutton, static void drawArrow(const QStyle *style, const QStyleOptionToolButton *toolbutton,
@ -467,7 +467,7 @@ void QWindows11Style::drawComplexControl(ComplexControl control, const QStyleOpt
QRectF rect = proxy()->subControlRect(CC_ComboBox, option, SC_ComboBoxArrow, widget).adjusted(4, 0, -4, 1); QRectF rect = proxy()->subControlRect(CC_ComboBox, option, SC_ComboBoxArrow, widget).adjusted(4, 0, -4, 1);
painter->setFont(assetFont); painter->setFont(assetFont);
painter->setPen(combobox->palette.text().color()); painter->setPen(combobox->palette.text().color());
painter->drawText(rect,"\uE70D", Qt::AlignVCenter | Qt::AlignHCenter); painter->drawText(rect, QStringLiteral(u"\uE70D"), Qt::AlignVCenter | Qt::AlignHCenter);
} }
if (combobox->editable) { if (combobox->editable) {
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);
@ -530,7 +530,7 @@ void QWindows11Style::drawComplexControl(ComplexControl control, const QStyleOpt
f.setPointSize(6); f.setPointSize(6);
cp->setFont(f); cp->setFont(f);
cp->setPen(Qt::gray); cp->setPen(Qt::gray);
const auto str = vertical ? QStringLiteral("\uEDDC") : QStringLiteral("\uEDDA"); const auto str = vertical ? QStringLiteral(u"\uEDDC") : QStringLiteral(u"\uEDDA");
cp->drawText(rect, str, Qt::AlignVCenter | Qt::AlignHCenter); cp->drawText(rect, str, Qt::AlignVCenter | Qt::AlignHCenter);
} }
} }
@ -541,7 +541,7 @@ void QWindows11Style::drawComplexControl(ComplexControl control, const QStyleOpt
f.setPointSize(6); f.setPointSize(6);
cp->setFont(f); cp->setFont(f);
cp->setPen(Qt::gray); cp->setPen(Qt::gray);
const auto str = vertical ? QStringLiteral("\uEDDB") : QStringLiteral("\uEDD9"); const auto str = vertical ? QStringLiteral(u"\uEDDB") : QStringLiteral(u"\uEDD9");
cp->drawText(rect, str, Qt::AlignVCenter | Qt::AlignHCenter); cp->drawText(rect, str, Qt::AlignVCenter | Qt::AlignHCenter);
} }
} }
@ -558,7 +558,7 @@ void QWindows11Style::drawComplexControl(ComplexControl control, const QStyleOpt
bool hover = closeButtonRect.contains(mousePos); bool hover = closeButtonRect.contains(mousePos);
if (hover) if (hover)
painter->fillRect(closeButtonRect,shellCloseButtonColor); painter->fillRect(closeButtonRect,shellCloseButtonColor);
const QString textToDraw("\uE8BB"); const QString textToDraw(QStringLiteral(u"\uE8BB"));
painter->setPen(QPen(hover ? option->palette.highlightedText().color() : option->palette.text().color())); painter->setPen(QPen(hover ? option->palette.highlightedText().color() : option->palette.text().color()));
painter->setFont(buttonFont); painter->setFont(buttonFont);
painter->drawText(closeButtonRect, Qt::AlignVCenter | Qt::AlignHCenter, textToDraw); painter->drawText(closeButtonRect, Qt::AlignVCenter | Qt::AlignHCenter, textToDraw);
@ -570,7 +570,7 @@ void QWindows11Style::drawComplexControl(ComplexControl control, const QStyleOpt
bool hover = normalButtonRect.contains(mousePos); bool hover = normalButtonRect.contains(mousePos);
if (hover) if (hover)
painter->fillRect(normalButtonRect,WINUI3Colors[colorSchemeIndex][subtleHighlightColor]); painter->fillRect(normalButtonRect,WINUI3Colors[colorSchemeIndex][subtleHighlightColor]);
const QString textToDraw("\uE923"); const QString textToDraw(QStringLiteral(u"\uE923"));
painter->setPen(QPen(option->palette.text().color())); painter->setPen(QPen(option->palette.text().color()));
painter->setFont(buttonFont); painter->setFont(buttonFont);
painter->drawText(normalButtonRect, Qt::AlignVCenter | Qt::AlignHCenter, textToDraw); painter->drawText(normalButtonRect, Qt::AlignVCenter | Qt::AlignHCenter, textToDraw);
@ -582,7 +582,7 @@ void QWindows11Style::drawComplexControl(ComplexControl control, const QStyleOpt
bool hover = minButtonRect.contains(mousePos); bool hover = minButtonRect.contains(mousePos);
if (hover) if (hover)
painter->fillRect(minButtonRect,WINUI3Colors[colorSchemeIndex][subtleHighlightColor]); painter->fillRect(minButtonRect,WINUI3Colors[colorSchemeIndex][subtleHighlightColor]);
const QString textToDraw("\uE921"); const QString textToDraw(QStringLiteral(u"\uE921"));
painter->setPen(QPen(option->palette.text().color())); painter->setPen(QPen(option->palette.text().color()));
painter->setFont(buttonFont); painter->setFont(buttonFont);
painter->drawText(minButtonRect, Qt::AlignVCenter | Qt::AlignHCenter, textToDraw); painter->drawText(minButtonRect, Qt::AlignVCenter | Qt::AlignHCenter, textToDraw);
@ -630,18 +630,18 @@ void QWindows11Style::drawComplexControl(ComplexControl control, const QStyleOpt
// min button // min button
if (shouldDrawButton(SC_TitleBarMinButton, Qt::WindowMinimizeButtonHint) && if (shouldDrawButton(SC_TitleBarMinButton, Qt::WindowMinimizeButtonHint) &&
!(titlebar->titleBarState & Qt::WindowMinimized)) { !(titlebar->titleBarState & Qt::WindowMinimized)) {
drawButton(SC_TitleBarMinButton, QStringLiteral("\uE921")); drawButton(SC_TitleBarMinButton, QStringLiteral(u"\uE921"));
} }
// max button // max button
if (shouldDrawButton(SC_TitleBarMaxButton, Qt::WindowMaximizeButtonHint) && if (shouldDrawButton(SC_TitleBarMaxButton, Qt::WindowMaximizeButtonHint) &&
!(titlebar->titleBarState & Qt::WindowMaximized)) { !(titlebar->titleBarState & Qt::WindowMaximized)) {
drawButton(SC_TitleBarMaxButton, QStringLiteral("\uE922")); drawButton(SC_TitleBarMaxButton, QStringLiteral(u"\uE922"));
} }
// close button // close button
if (shouldDrawButton(SC_TitleBarCloseButton, Qt::WindowSystemMenuHint)) if (shouldDrawButton(SC_TitleBarCloseButton, Qt::WindowSystemMenuHint))
drawButton(SC_TitleBarCloseButton, QStringLiteral("\uE8BB"), shellCloseButtonColor); drawButton(SC_TitleBarCloseButton, QStringLiteral(u"\uE8BB"), shellCloseButtonColor);
// normalize button // normalize button
if ((titlebar->subControls & SC_TitleBarNormalButton) && if ((titlebar->subControls & SC_TitleBarNormalButton) &&
@ -649,20 +649,20 @@ void QWindows11Style::drawComplexControl(ComplexControl control, const QStyleOpt
(titlebar->titleBarState & Qt::WindowMinimized)) || (titlebar->titleBarState & Qt::WindowMinimized)) ||
((titlebar->titleBarFlags & Qt::WindowMaximizeButtonHint) && ((titlebar->titleBarFlags & Qt::WindowMaximizeButtonHint) &&
(titlebar->titleBarState & Qt::WindowMaximized)))) { (titlebar->titleBarState & Qt::WindowMaximized)))) {
drawButton(SC_TitleBarNormalButton, QStringLiteral("\uE923")); drawButton(SC_TitleBarNormalButton, QStringLiteral(u"\uE923"));
} }
// context help button // context help button
if (shouldDrawButton(SC_TitleBarContextHelpButton, Qt::WindowContextHelpButtonHint)) if (shouldDrawButton(SC_TitleBarContextHelpButton, Qt::WindowContextHelpButtonHint))
drawButton(SC_TitleBarContextHelpButton, QStringLiteral("\uE897")); drawButton(SC_TitleBarContextHelpButton, QStringLiteral(u"\uE897"));
// shade button // shade button
if (shouldDrawButton(SC_TitleBarShadeButton, Qt::WindowShadeButtonHint)) if (shouldDrawButton(SC_TitleBarShadeButton, Qt::WindowShadeButtonHint))
drawButton(SC_TitleBarShadeButton, QStringLiteral("\uE96D")); drawButton(SC_TitleBarShadeButton, QStringLiteral(u"\uE96D"));
// unshade button // unshade button
if (shouldDrawButton(SC_TitleBarUnshadeButton, Qt::WindowShadeButtonHint)) if (shouldDrawButton(SC_TitleBarUnshadeButton, Qt::WindowShadeButtonHint))
drawButton(SC_TitleBarUnshadeButton, QStringLiteral("\uE96E")); drawButton(SC_TitleBarUnshadeButton, QStringLiteral(u"\uE96E"));
// window icon for system menu // window icon for system menu
if (shouldDrawButton(SC_TitleBarSysMenu, Qt::WindowSystemMenuHint)) { if (shouldDrawButton(SC_TitleBarSysMenu, Qt::WindowSystemMenuHint)) {
@ -799,9 +799,9 @@ void QWindows11Style::drawPrimitive(PrimitiveElement element, const QStyleOption
painter->setPen(header->palette.text().color()); painter->setPen(header->palette.text().color());
QRectF rect = option->rect; QRectF rect = option->rect;
if (header->sortIndicator & QStyleOptionHeader::SortUp) { if (header->sortIndicator & QStyleOptionHeader::SortUp) {
painter->drawText(rect,Qt::AlignCenter,"\uE96D"); painter->drawText(rect, Qt::AlignCenter, QStringLiteral(u"\uE96D"));
} else if (header->sortIndicator & QStyleOptionHeader::SortDown) { } else if (header->sortIndicator & QStyleOptionHeader::SortDown) {
painter->drawText(rect,Qt::AlignCenter,"\uE96E"); painter->drawText(rect, Qt::AlignCenter, QStringLiteral(u"\uE96E"));
} }
} }
break; break;
@ -817,7 +817,7 @@ void QWindows11Style::drawPrimitive(PrimitiveElement element, const QStyleOption
rect.moveCenter(center); rect.moveCenter(center);
float clipWidth = animation != nullptr ? animation->currentValue() : 1.0f; float clipWidth = animation != nullptr ? animation->currentValue() : 1.0f;
QRectF clipRect = fm.boundingRect("\uE73E"); QRectF clipRect = fm.boundingRect(QStringLiteral(u"\uE73E"));
clipRect.moveCenter(center); clipRect.moveCenter(center);
clipRect.setLeft(rect.x() + (rect.width() - clipRect.width()) / 2.0); clipRect.setLeft(rect.x() + (rect.width() - clipRect.width()) / 2.0);
clipRect.setWidth(clipWidth * clipRect.width()); clipRect.setWidth(clipWidth * clipRect.width());
@ -834,9 +834,9 @@ void QWindows11Style::drawPrimitive(PrimitiveElement element, const QStyleOption
painter->setPen(option->palette.highlightedText().color()); painter->setPen(option->palette.highlightedText().color());
painter->setBrush(option->palette.highlightedText()); painter->setBrush(option->palette.highlightedText());
if (option->state & State_On) if (option->state & State_On)
painter->drawText(clipRect, Qt::AlignVCenter | Qt::AlignLeft,"\uE73E"); painter->drawText(clipRect, Qt::AlignVCenter | Qt::AlignLeft, QStringLiteral(u"\uE73E"));
else if (option->state & State_NoChange) else if (option->state & State_NoChange)
painter->drawText(rect, Qt::AlignVCenter | Qt::AlignHCenter,"\uE73C"); painter->drawText(rect, Qt::AlignVCenter | Qt::AlignHCenter, QStringLiteral(u"\uE73C"));
} }
break; break;
case PE_IndicatorBranch: { case PE_IndicatorBranch: {
@ -944,7 +944,7 @@ void QWindows11Style::drawPrimitive(PrimitiveElement element, const QStyleOption
break; break;
} }
case PE_PanelLineEdit: case PE_PanelLineEdit:
if (widget && widget->objectName() == "qt_spinbox_lineedit") if (widget && widget->objectName() == QStringLiteral(u"qt_spinbox_lineedit"))
break; break;
if (const auto *panel = qstyleoption_cast<const QStyleOptionFrame *>(option)) { if (const auto *panel = qstyleoption_cast<const QStyleOptionFrame *>(option)) {
QRectF frameRect = option->rect; QRectF frameRect = option->rect;
@ -1373,7 +1373,7 @@ void QWindows11Style::drawControl(ControlElement element, const QStyleOption *op
menuSplitter = QLineF(indicatorRect.topRight(),indicatorRect.bottomRight()); menuSplitter = QLineF(indicatorRect.topRight(),indicatorRect.bottomRight());
textRect = textRect.adjusted(indicatorSize, 0, 0, 0); textRect = textRect.adjusted(indicatorSize, 0, 0, 0);
} }
painter->drawText(indicatorRect,"\uE70D",Qt::AlignVCenter|Qt::AlignHCenter); painter->drawText(indicatorRect, QStringLiteral(u"\uE70D"), Qt::AlignVCenter | Qt::AlignHCenter);
painter->setPen(QPen(WINUI3Colors[colorSchemeIndex][controlStrokePrimary])); painter->setPen(QPen(WINUI3Colors[colorSchemeIndex][controlStrokePrimary]));
painter->drawLine(menuSplitter); painter->drawLine(menuSplitter);
} }
@ -1548,7 +1548,7 @@ void QWindows11Style::drawControl(ControlElement element, const QStyleOption *op
painter->setPen(menuitem->palette.text().color()); painter->setPen(menuitem->palette.text().color());
painter->setFont(assetFont); painter->setFont(assetFont);
const int text_flags = Qt::AlignVCenter | Qt::AlignHCenter | Qt::TextDontClip | Qt::TextSingleLine; const int text_flags = Qt::AlignVCenter | Qt::AlignHCenter | Qt::TextDontClip | Qt::TextSingleLine;
const auto textToDraw = QStringLiteral("\uE73E"); const auto textToDraw = QStringLiteral(u"\uE73E");
painter->setPen(option->palette.text().color()); painter->setPen(option->palette.text().color());
painter->drawText(vCheckRect, text_flags, textToDraw); painter->drawText(vCheckRect, text_flags, textToDraw);
painter->restore(); painter->restore();

View File

@ -23,11 +23,11 @@ QT_BEGIN_NAMESPACE
using namespace Qt::StringLiterals; using namespace Qt::StringLiterals;
static const int windowsItemFrame = 2; // menu item frame width static constexpr int windowsItemFrame = 2; // menu item frame width
static const int windowsItemHMargin = 3; // menu item hor text margin static constexpr int windowsItemHMargin = 3; // menu item hor text margin
static const int windowsItemVMargin = 4; // menu item ver text margin static constexpr int windowsItemVMargin = 4; // menu item ver text margin
static const int windowsArrowHMargin = 6; // arrow horizontal margin static constexpr int windowsArrowHMargin = 6; // arrow horizontal margin
static const int windowsRightBorder = 15; // right border on windows static constexpr int windowsRightBorder = 15; // right border on windows
#ifndef TMT_CONTENTMARGINS #ifndef TMT_CONTENTMARGINS
# define TMT_CONTENTMARGINS 3602 # define TMT_CONTENTMARGINS 3602
@ -106,8 +106,8 @@ static inline HWND createTreeViewHelperWindow(const QScreen *screen)
HWND result = nullptr; HWND result = nullptr;
if (auto nativeWindowsApp = dynamic_cast<QWindowsApplication *>(QGuiApplicationPrivate::platformIntegration())) if (auto nativeWindowsApp = dynamic_cast<QWindowsApplication *>(QGuiApplicationPrivate::platformIntegration()))
result = nativeWindowsApp->createMessageWindow(QStringLiteral("QTreeViewThemeHelperWindowClass"), result = nativeWindowsApp->createMessageWindow(QStringLiteral(u"QTreeViewThemeHelperWindowClass"),
QStringLiteral("QTreeViewThemeHelperWindow")); QStringLiteral(u"QTreeViewThemeHelperWindow"));
const auto topLeft = screen->geometry().topLeft(); const auto topLeft = screen->geometry().topLeft();
// make it a top-level window and move it the the correct screen to paint with the correct dpr later on // make it a top-level window and move it the the correct screen to paint with the correct dpr later on
SetParent(result, NULL); SetParent(result, NULL);
@ -750,7 +750,7 @@ bool QWindowsVistaStylePrivate::drawBackgroundThruNativeBuffer(QWindowsThemeData
bool partIsTransparent; bool partIsTransparent;
bool potentialInvalidAlpha; bool potentialInvalidAlpha;
QString pixmapCacheKey = QStringLiteral("$qt_xp_"); QString pixmapCacheKey = QStringLiteral(u"$qt_xp_");
pixmapCacheKey.append(themeName(themeData.theme)); pixmapCacheKey.append(themeName(themeData.theme));
pixmapCacheKey.append(QLatin1Char('p')); pixmapCacheKey.append(QLatin1Char('p'));
pixmapCacheKey.append(QString::number(partId)); pixmapCacheKey.append(QString::number(partId));
@ -2112,7 +2112,7 @@ void QWindowsVistaStyle::drawPrimitive(PrimitiveElement element, const QStyleOpt
if (hover || selected) { if (hover || selected) {
if (sectionSize.width() > 0 && sectionSize.height() > 0) { if (sectionSize.width() > 0 && sectionSize.height() > 0) {
QString key = QString::fromLatin1("qvdelegate-%1-%2-%3-%4-%5").arg(sectionSize.width()) QString key = QStringLiteral(u"qvdelegate-%1-%2-%3-%4-%5").arg(sectionSize.width())
.arg(sectionSize.height()).arg(selected).arg(active).arg(hover); .arg(sectionSize.height()).arg(selected).arg(active).arg(hover);
if (!QPixmapCache::find(key, &pixmap)) { if (!QPixmapCache::find(key, &pixmap)) {
pixmap = QPixmap(sectionSize); pixmap = QPixmap(sectionSize);
@ -2697,7 +2697,7 @@ void QWindowsVistaStyle::drawControl(ControlElement element, const QStyleOption
option->rect, animRect); option->rect, animRect);
pixmapSize.setWidth(animRect.width()); pixmapSize.setWidth(animRect.width());
} }
QString name = QString::fromLatin1("qiprogress-%1-%2").arg(pixmapSize.width()).arg(pixmapSize.height()); QString name = QStringLiteral(u"qiprogress-%1-%2").arg(pixmapSize.width()).arg(pixmapSize.height());
QPixmap pixmap; QPixmap pixmap;
if (!QPixmapCache::find(name, &pixmap)) { if (!QPixmapCache::find(name, &pixmap)) {
QImage image(pixmapSize, QImage::Format_ARGB32); QImage image(pixmapSize, QImage::Format_ARGB32);
@ -4807,7 +4807,7 @@ void QWindowsVistaStyle::polish(QApplication *app)
{ {
// Override windows theme palettes to light // Override windows theme palettes to light
if (qApp->styleHints()->colorScheme() == Qt::ColorScheme::Dark) { if (qApp->styleHints()->colorScheme() == Qt::ColorScheme::Dark) {
static const char* themedWidgets[] = { static constexpr const char* themedWidgets[] = {
"QToolButton", "QToolButton",
"QAbstractButton", "QAbstractButton",
"QCheckBox", "QCheckBox",