Skip to content

Commit fb98636

Browse files
committed
Windows11Style: make glyph constants enumerations
Use an enum class instead simple defines. Change-Id: Ieb0e14134400d8263afe6e97e636478c9acb9e4e Reviewed-by: Volker Hilsheimer <[email protected]> (cherry picked from commit e3263e8)
1 parent 70f4ccc commit fb98636

File tree

1 file changed

+66
-61
lines changed

1 file changed

+66
-61
lines changed

src/plugins/styles/modernwindows/qwindows11style.cpp

Lines changed: 66 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -87,32 +87,35 @@ inline ControlState calcControlState(const QStyleOption *option)
8787

8888
} // namespace StyleOptionHelper
8989

90-
#define AcceptMedium u"\uF78C"_s
91-
// QStringLiteral(u"\uE73C")
92-
#define Dash12 u"\uE629"_s
93-
#define CheckMark u"\uE73E"_s
94-
95-
#define CaretLeftSolid8 u"\uEDD9"_s
96-
#define CaretRightSolid8 u"\uEDDA"_s
97-
#define CaretUpSolid8 u"\uEDDB"_s
98-
#define CaretDownSolid8 u"\uEDDC"_s
99-
100-
#define ChevronDown u"\uE70D"_s
101-
#define ChevronUp u"\uE70E"_s
102-
103-
#define ChevronDownMed u"\uE972"_s
104-
#define ChevronLeftMed u"\uE973"_s
105-
#define ChevronRightMed u"\uE974"_s
106-
107-
#define ChevronUpSmall u"\uE96D"_s
108-
#define ChevronDownSmall u"\uE96E"_s
109-
110-
#define ChromeMinimize u"\uE921"_s
111-
#define ChromeMaximize u"\uE922"_s
112-
#define ChromeRestore u"\uE923"_s
113-
#define ChromeClose u"\uE8BB"_s
90+
enum class Icon : ushort
91+
{
92+
AcceptMedium = 0xF78C,
93+
Dash12 = 0xE629,
94+
CheckMark = 0xE73E,
95+
CaretLeftSolid8 = 0xEDD9,
96+
CaretRightSolid8 = 0xEDDA,
97+
CaretUpSolid8 = 0xEDDB,
98+
CaretDownSolid8 = 0xEDDC,
99+
ChevronDown = 0xE70D,
100+
ChevronUp = 0xE70E,
101+
ChevronDownMed = 0xE972,
102+
ChevronLeftMed = 0xE973,
103+
ChevronRightMed = 0xE974,
104+
ChevronUpSmall = 0xE96D,
105+
ChevronDownSmall = 0xE96E,
106+
ChromeMinimize = 0xE921,
107+
ChromeMaximize = 0xE922,
108+
ChromeRestore = 0xE923,
109+
ChromeClose = 0xE8BB,
110+
More = 0xE712,
111+
Help = 0xE897,
112+
Clear = 0xE894,
113+
};
114114

115-
#define Help u"\uE897"_s
115+
static inline QString fluentIcon(Icon i)
116+
{
117+
return QChar(ushort(i));
118+
}
116119

117120
template <typename R, typename P, typename B>
118121
static inline void drawRoundedRect(QPainter *p, R &&rect, P &&pen, B &&brush)
@@ -314,18 +317,18 @@ void QWindows11Style::drawComplexControl(ComplexControl control, const QStyleOpt
314317
{
315318
QWindows11StylePrivate *d = const_cast<QWindows11StylePrivate*>(d_func());
316319

317-
const auto drawTitleBarButton = [&](ComplexControl control, SubControl sc, const QString &str) {
320+
const auto drawTitleBarButton = [&](ComplexControl control, SubControl sc, Icon ico) {
318321
using namespace StyleOptionHelper;
319322
const QRect buttonRect = proxy()->subControlRect(control, option, sc, widget);
320323
if (buttonRect.isValid()) {
321324
const bool hover = option->activeSubControls == sc && isHover(option);
322325
if (hover)
323326
painter->fillRect(buttonRect, winUI3Color(subtleHighlightColor));
324327
painter->setPen(option->palette.color(QPalette::WindowText));
325-
painter->drawText(buttonRect, Qt::AlignCenter, str);
328+
painter->drawText(buttonRect, Qt::AlignCenter, fluentIcon(ico));
326329
}
327330
};
328-
const auto drawTitleBarCloseButton = [&](ComplexControl control, SubControl sc, const QString &str) {
331+
const auto drawTitleBarCloseButton = [&](ComplexControl control, SubControl sc, Icon ico) {
329332
using namespace StyleOptionHelper;
330333
const QRect buttonRect = proxy()->subControlRect(control, option, sc, widget);
331334
if (buttonRect.isValid()) {
@@ -347,7 +350,7 @@ void QWindows11Style::drawComplexControl(ComplexControl control, const QStyleOpt
347350
break;
348351
}
349352
painter->setPen(pen);
350-
painter->drawText(buttonRect, Qt::AlignCenter, str);
353+
painter->drawText(buttonRect, Qt::AlignCenter, fluentIcon(ico));
351354
}
352355
};
353356

@@ -430,7 +433,7 @@ void QWindows11Style::drawComplexControl(ComplexControl control, const QStyleOpt
430433
cp->setFont(d->assetFont);
431434
cp->setPen(sb->palette.buttonText().color());
432435
cp->setBrush(Qt::NoBrush);
433-
cp->drawText(rect, Qt::AlignCenter, isUp ? ChevronUp : ChevronDown);
436+
cp->drawText(rect, Qt::AlignCenter, fluentIcon(isUp ? Icon::ChevronUp : Icon::ChevronDown));
434437
};
435438
if (sub & SC_SpinBoxUp) drawUpDown(SC_SpinBoxUp);
436439
if (sub & SC_SpinBoxDown) drawUpDown(SC_SpinBoxDown);
@@ -594,7 +597,7 @@ void QWindows11Style::drawComplexControl(ComplexControl control, const QStyleOpt
594597
QRectF rect = proxy()->subControlRect(CC_ComboBox, option, SC_ComboBoxArrow, widget);
595598
painter->setFont(d->assetFont);
596599
painter->setPen(controlTextColor(option));
597-
painter->drawText(rect, Qt::AlignCenter, ChevronDownMed);
600+
painter->drawText(rect, Qt::AlignCenter, fluentIcon(Icon::ChevronDownMed));
598601
}
599602
if (state & State_KeyboardFocusChange && hasFocus) {
600603
QStyleOptionFocusRect fropt;
@@ -656,9 +659,9 @@ void QWindows11Style::drawComplexControl(ComplexControl control, const QStyleOpt
656659
f.setPointSize(6);
657660
cp->setFont(f);
658661
cp->setPen(Qt::gray);
659-
const auto str = vertical ? CaretDownSolid8
660-
: (isRtl ? CaretLeftSolid8 : CaretRightSolid8);
661-
cp->drawText(rect, Qt::AlignCenter, str);
662+
const auto ico = vertical ? Icon::CaretDownSolid8
663+
: (isRtl ? Icon::CaretLeftSolid8 : Icon::CaretRightSolid8);
664+
cp->drawText(rect, Qt::AlignCenter, fluentIcon(ico));
662665
}
663666
}
664667
if (sub & SC_ScrollBarSubLine) {
@@ -668,9 +671,9 @@ void QWindows11Style::drawComplexControl(ComplexControl control, const QStyleOpt
668671
f.setPointSize(6);
669672
cp->setFont(f);
670673
cp->setPen(Qt::gray);
671-
const auto str = vertical ? CaretUpSolid8
672-
: (isRtl ? CaretRightSolid8 : CaretLeftSolid8);
673-
cp->drawText(rect, Qt::AlignCenter, str);
674+
const auto ico = vertical ? Icon::CaretUpSolid8
675+
: (isRtl ? Icon::CaretRightSolid8 : Icon::CaretLeftSolid8);
676+
cp->drawText(rect, Qt::AlignCenter, fluentIcon(ico));
674677
}
675678
}
676679
}
@@ -680,9 +683,9 @@ void QWindows11Style::drawComplexControl(ComplexControl control, const QStyleOpt
680683
QFont buttonFont = QFont(d->assetFont);
681684
buttonFont.setPointSize(8);
682685
painter->setFont(buttonFont);
683-
drawTitleBarCloseButton(CC_MdiControls, SC_MdiCloseButton, ChromeClose);
684-
drawTitleBarButton(CC_MdiControls, SC_MdiNormalButton, ChromeRestore);
685-
drawTitleBarButton(CC_MdiControls, SC_MdiMinButton, ChromeMinimize);
686+
drawTitleBarCloseButton(CC_MdiControls, SC_MdiCloseButton, Icon::ChromeClose);
687+
drawTitleBarButton(CC_MdiControls, SC_MdiNormalButton, Icon::ChromeRestore);
688+
drawTitleBarButton(CC_MdiControls, SC_MdiMinButton, Icon::ChromeMinimize);
686689
}
687690
break;
688691
case CC_TitleBar:
@@ -710,39 +713,39 @@ void QWindows11Style::drawComplexControl(ComplexControl control, const QStyleOpt
710713
// min button
711714
if (shouldDrawButton(SC_TitleBarMinButton, Qt::WindowMinimizeButtonHint) &&
712715
!(titlebar->titleBarState & Qt::WindowMinimized)) {
713-
drawTitleBarButton(CC_TitleBar, SC_TitleBarMinButton, ChromeMinimize);
716+
drawTitleBarButton(CC_TitleBar, SC_TitleBarMinButton, Icon::ChromeMinimize);
714717
}
715718

716719
// max button
717720
if (shouldDrawButton(SC_TitleBarMaxButton, Qt::WindowMaximizeButtonHint) &&
718721
!(titlebar->titleBarState & Qt::WindowMaximized)) {
719-
drawTitleBarButton(CC_TitleBar, SC_TitleBarMaxButton, ChromeMaximize);
722+
drawTitleBarButton(CC_TitleBar, SC_TitleBarMaxButton, Icon::ChromeMaximize);
720723
}
721724

722725
// close button
723726
if (shouldDrawButton(SC_TitleBarCloseButton, Qt::WindowSystemMenuHint))
724-
drawTitleBarCloseButton(CC_TitleBar, SC_TitleBarCloseButton, ChromeClose);
727+
drawTitleBarCloseButton(CC_TitleBar, SC_TitleBarCloseButton, Icon::ChromeClose);
725728

726729
// normalize button
727730
if ((titlebar->subControls & SC_TitleBarNormalButton) &&
728731
(((titlebar->titleBarFlags & Qt::WindowMinimizeButtonHint) &&
729732
(titlebar->titleBarState & Qt::WindowMinimized)) ||
730733
((titlebar->titleBarFlags & Qt::WindowMaximizeButtonHint) &&
731734
(titlebar->titleBarState & Qt::WindowMaximized)))) {
732-
drawTitleBarButton(CC_TitleBar, SC_TitleBarNormalButton, ChromeRestore);
735+
drawTitleBarButton(CC_TitleBar, SC_TitleBarNormalButton, Icon::ChromeRestore);
733736
}
734737

735738
// context help button
736739
if (shouldDrawButton(SC_TitleBarContextHelpButton, Qt::WindowContextHelpButtonHint))
737-
drawTitleBarButton(CC_TitleBar, SC_TitleBarContextHelpButton, Help);
740+
drawTitleBarButton(CC_TitleBar, SC_TitleBarContextHelpButton, Icon::Help);
738741

739742
// shade button
740743
if (shouldDrawButton(SC_TitleBarShadeButton, Qt::WindowShadeButtonHint))
741-
drawTitleBarButton(CC_TitleBar, SC_TitleBarShadeButton, ChevronUpSmall);
744+
drawTitleBarButton(CC_TitleBar, SC_TitleBarShadeButton, Icon::ChevronUpSmall);
742745

743746
// unshade button
744747
if (shouldDrawButton(SC_TitleBarUnshadeButton, Qt::WindowShadeButtonHint))
745-
drawTitleBarButton(CC_TitleBar, SC_TitleBarUnshadeButton, ChevronDownSmall);
748+
drawTitleBarButton(CC_TitleBar, SC_TitleBarUnshadeButton, Icon::ChevronDownSmall);
746749

747750
// window icon for system menu
748751
if (shouldDrawButton(SC_TitleBarSysMenu, Qt::WindowSystemMenuHint)) {
@@ -866,9 +869,9 @@ void QWindows11Style::drawPrimitive(PrimitiveElement element, const QStyleOption
866869
f.setPointSize(6);
867870
painter->setFont(f);
868871
painter->setPen(header->palette.text().color());
869-
painter->drawText(option->rect, Qt::AlignCenter,
870-
indicator == QStyleOptionHeader::SortUp ? ChevronDown
871-
: ChevronUp);
872+
const auto ico = indicator == QStyleOptionHeader::SortUp ? Icon::ChevronDown
873+
: Icon::ChevronUp;
874+
painter->drawText(option->rect, Qt::AlignCenter, fluentIcon(ico));
872875
}
873876
}
874877
break;
@@ -886,8 +889,9 @@ void QWindows11Style::drawPrimitive(PrimitiveElement element, const QStyleOption
886889
painter->setFont(d->assetFont);
887890
painter->setPen(controlTextColor(option, QPalette::Window));
888891
qreal clipWidth = 1.0;
892+
const QString str = fluentIcon(Icon::AcceptMedium);
889893
QFontMetrics fm(d->assetFont);
890-
QRectF clipRect = fm.boundingRect(AcceptMedium);
894+
QRectF clipRect = fm.boundingRect(str);
891895
if (d->transitionsEnabled() && option->styleObject) {
892896
QNumberStyleAnimation *animation = qobject_cast<QNumberStyleAnimation *>(
893897
d->animation(option->styleObject));
@@ -898,11 +902,11 @@ void QWindows11Style::drawPrimitive(PrimitiveElement element, const QStyleOption
898902
clipRect.moveCenter(center);
899903
clipRect.setLeft(rect.x() + (rect.width() - clipRect.width()) / 2.0 + 0.5);
900904
clipRect.setWidth(clipWidth * clipRect.width());
901-
painter->drawText(clipRect, Qt::AlignVCenter | Qt::AlignLeft, AcceptMedium);
905+
painter->drawText(clipRect, Qt::AlignVCenter | Qt::AlignLeft, str);
902906
} else if (isPartial) {
903907
painter->setFont(d->assetFont);
904908
painter->setPen(controlTextColor(option, QPalette::Window));
905-
painter->drawText(rect, Qt::AlignCenter, Dash12);
909+
painter->drawText(rect, Qt::AlignCenter, fluentIcon(Icon::Dash12));
906910
}
907911
}
908912
break;
@@ -915,8 +919,10 @@ void QWindows11Style::drawPrimitive(PrimitiveElement element, const QStyleOption
915919
painter->setFont(f);
916920
painter->setPen(option->palette.color(isOpen ? QPalette::Active : QPalette::Disabled,
917921
QPalette::WindowText));
918-
const auto str = isOpen ? ChevronDownMed : (isReverse ? ChevronLeftMed : ChevronRightMed);
919-
painter->drawText(option->rect, Qt::AlignCenter, str);
922+
const auto ico = isOpen ? Icon::ChevronDownMed
923+
: (isReverse ? Icon::ChevronLeftMed
924+
: Icon::ChevronRightMed);
925+
painter->drawText(option->rect, Qt::AlignCenter, fluentIcon(ico));
920926
}
921927
}
922928
break;
@@ -1510,7 +1516,7 @@ void QWindows11Style::drawControl(ControlElement element, const QStyleOption *op
15101516
if (isEnabled)
15111517
penColor.setAlpha(percentToAlpha(60.63)); // fillColorTextSecondary
15121518
painter->setPen(penColor);
1513-
painter->drawText(vindRect, Qt::AlignCenter, ChevronDownMed);
1519+
painter->drawText(vindRect, Qt::AlignCenter, fluentIcon(Icon::ChevronDownMed));
15141520
}
15151521
}
15161522
break;
@@ -1583,8 +1589,7 @@ void QWindows11Style::drawControl(ControlElement element, const QStyleOption *op
15831589
QPainterStateGuard psg(painter);
15841590
painter->setFont(d->assetFont);
15851591
painter->setPen(option->palette.text().color());
1586-
const auto textToDraw = QStringLiteral(u"\uE73E");
1587-
painter->drawText(vRect, Qt::AlignCenter, textToDraw);
1592+
painter->drawText(vRect, Qt::AlignCenter, fluentIcon(Icon::CheckMark));
15881593
}
15891594
if (menuitem->menuHasCheckableItems)
15901595
xOffset += checkMarkWidth + contentItemHMargin;
@@ -1665,8 +1670,8 @@ void QWindows11Style::drawControl(ControlElement element, const QStyleOption *op
16651670
QRect vSubMenuRect = visualMenuRect(submenuRect);
16661671
painter->setPen(option->palette.text().color());
16671672
const bool isReverse = option->direction == Qt::RightToLeft;
1668-
const auto str = isReverse ? ChevronLeftMed : ChevronRightMed;
1669-
painter->drawText(vSubMenuRect, Qt::AlignCenter, str);
1673+
const auto ico = isReverse ? Icon::ChevronLeftMed : Icon::ChevronRightMed;
1674+
painter->drawText(vSubMenuRect, Qt::AlignCenter, fluentIcon(ico));
16701675
}
16711676
}
16721677
break;
@@ -2348,7 +2353,7 @@ int QWindows11Style::pixelMetric(PixelMetric metric, const QStyleOption *option,
23482353
QFont f(d->assetFont);
23492354
f.setPointSize(qRound(fontSize * 0.9f)); // a little bit smaller
23502355
QFontMetrics fm(f);
2351-
res += fm.horizontalAdvance(ChevronDownMed);
2356+
res += fm.horizontalAdvance(fluentIcon(Icon::ChevronDownMed));
23522357
} else {
23532358
res += 12;
23542359
}

0 commit comments

Comments
 (0)