diff options
Diffstat (limited to 'src/plugins/styles')
-rw-r--r-- | src/plugins/styles/mac/qmacstyle_mac.mm | 6 | ||||
-rw-r--r-- | src/plugins/styles/modernwindows/qwindows11style.cpp | 183 | ||||
-rw-r--r-- | src/plugins/styles/modernwindows/qwindowsvistastyle.cpp | 2 |
3 files changed, 119 insertions, 72 deletions
diff --git a/src/plugins/styles/mac/qmacstyle_mac.mm b/src/plugins/styles/mac/qmacstyle_mac.mm index 3f57f284e6..226da495f4 100644 --- a/src/plugins/styles/mac/qmacstyle_mac.mm +++ b/src/plugins/styles/mac/qmacstyle_mac.mm @@ -21,6 +21,7 @@ #include <QtCore/private/qcore_mac_p.h> #include <QtGui/qpainterpath.h> +#include <QtGui/qstylehints.h> #include <QtGui/private/qcoregraphics_p.h> #include <QtGui/qpa/qplatformfontdatabase.h> #include <QtGui/qpa/qplatformtheme.h> @@ -160,7 +161,10 @@ const int pushButtonBevelRectOffsets[3] = { QVector<QPointer<QObject> > QMacStylePrivate::scrollBars; -bool isDarkMode() { return QGuiApplicationPrivate::platformTheme()->colorScheme() == Qt::ColorScheme::Dark; } +static inline bool isDarkMode() +{ + return QGuiApplication::styleHints()->colorScheme() == Qt::ColorScheme::Dark; +} #if QT_CONFIG(tabwidget) /* diff --git a/src/plugins/styles/modernwindows/qwindows11style.cpp b/src/plugins/styles/modernwindows/qwindows11style.cpp index c44629360f..63663c74aa 100644 --- a/src/plugins/styles/modernwindows/qwindows11style.cpp +++ b/src/plugins/styles/modernwindows/qwindows11style.cpp @@ -9,12 +9,15 @@ #include <qstyleoption.h> #include <qpainter.h> #include <QGraphicsDropShadowEffect> +#include <QLatin1StringView> #include <QtWidgets/qcombobox.h> #include <QtWidgets/qcommandlinkbutton.h> #include <QtWidgets/qgraphicsview.h> #include <QtWidgets/qlistview.h> #include <QtWidgets/qmenu.h> +#if QT_CONFIG(mdiarea) #include <QtWidgets/qmdiarea.h> +#endif #include <QtWidgets/qtextedit.h> #include <QtWidgets/qtreeview.h> @@ -25,6 +28,7 @@ QT_BEGIN_NAMESPACE const static 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 +constexpr QLatin1StringView originalWidthProperty("_q_windows11_style_original_width"); enum WINUI3Color { subtleHighlightColor, //Subtle highlight based on alpha used for hovered elements @@ -134,6 +138,8 @@ static void drawArrow(const QStyle *style, const QStyleOptionToolButton *toolbut */ QWindows11Style::QWindows11Style() : QWindowsVistaStyle(*new QWindows11StylePrivate) { + highContrastTheme = QGuiApplicationPrivate::styleHints->colorScheme() == Qt::ColorScheme::Unknown; + colorSchemeIndex = QGuiApplicationPrivate::styleHints->colorScheme() == Qt::ColorScheme::Light ? 0 : 1; } /*! @@ -142,6 +148,8 @@ QWindows11Style::QWindows11Style() : QWindowsVistaStyle(*new QWindows11StylePriv */ QWindows11Style::QWindows11Style(QWindows11StylePrivate &dd) : QWindowsVistaStyle(dd) { + highContrastTheme = QGuiApplicationPrivate::styleHints->colorScheme() == Qt::ColorScheme::Unknown; + colorSchemeIndex = QGuiApplicationPrivate::styleHints->colorScheme() == Qt::ColorScheme::Light ? 0 : 1; } /*! @@ -434,12 +442,21 @@ void QWindows11Style::drawComplexControl(ComplexControl control, const QStyleOpt #if QT_CONFIG(combobox) case CC_ComboBox: if (const QStyleOptionComboBox *combobox = qstyleoption_cast<const QStyleOptionComboBox *>(option)) { - QBrush fillColor = state & State_MouseOver && !(state & State_HasFocus) ? QBrush(WINUI3Colors[colorSchemeIndex][subtleHighlightColor]) : option->palette.brush(QPalette::Base); + QBrush fillColor = combobox->palette.brush(QPalette::Base); QRectF rect = option->rect.adjusted(2,2,-2,-2); painter->setBrush(fillColor); painter->setPen(Qt::NoPen); painter->drawRoundedRect(rect, secondLevelRoundingRadius, secondLevelRoundingRadius); + // In case the QComboBox is hovered overdraw the background with a alpha mask to + // highlight the QComboBox. + if (state & State_MouseOver) { + fillColor = QBrush(WINUI3Colors[colorSchemeIndex][subtleHighlightColor]); + painter->setBrush(fillColor); + painter->setPen(Qt::NoPen); + painter->drawRoundedRect(rect, secondLevelRoundingRadius, secondLevelRoundingRadius); + } + rect.adjust(0.5,0.5,-0.5,-0.5); painter->setBrush(Qt::NoBrush); painter->setPen(highContrastTheme == true ? combobox->palette.buttonText().color() : WINUI3Colors[colorSchemeIndex][frameColorLight]); @@ -905,12 +922,20 @@ void QWindows11Style::drawPrimitive(PrimitiveElement element, const QStyleOption if (widget && widget->objectName() == "qt_spinbox_lineedit") break; if (const auto *panel = qstyleoption_cast<const QStyleOptionFrame *>(option)) { - QBrush fillColor = state & State_MouseOver && !(state & State_HasFocus) ? QBrush(WINUI3Colors[colorSchemeIndex][subtleHighlightColor]) : option->palette.brush(QPalette::Base); - painter->setBrush(fillColor); - painter->setPen(Qt::NoPen); QRectF frameRect = option->rect; frameRect.adjust(0.5,0.5,-0.5,-0.5); + QBrush fillColor = option->palette.brush(QPalette::Base); + painter->setBrush(fillColor); + painter->setPen(Qt::NoPen); painter->drawRoundedRect(frameRect, secondLevelRoundingRadius, secondLevelRoundingRadius); + // In case the QLineEdit is hovered overdraw the background with a alpha mask to + // highlight the QLineEdit. + if (state & State_MouseOver && !(state & State_HasFocus)) { + fillColor = QBrush(WINUI3Colors[colorSchemeIndex][subtleHighlightColor]); + painter->setBrush(fillColor); + painter->setPen(Qt::NoPen); + painter->drawRoundedRect(frameRect, secondLevelRoundingRadius, secondLevelRoundingRadius); + } if (panel->lineWidth > 0) proxy()->drawPrimitive(PE_FrameLineEdit, panel, painter, widget); } @@ -1104,7 +1129,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) + if (toolbutton->state & State_Raised || toolbutton->palette.isBrushSet(QPalette::Current, QPalette::ButtonText)) painter->setPen(QPen(toolbutton->palette.buttonText().color())); else painter->setPen(QPen(WINUI3Colors[colorSchemeIndex][controlTextSecondary])); @@ -1158,7 +1183,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) + if (toolbutton->state & State_Raised || toolbutton->palette.isBrushSet(QPalette::Current, QPalette::ButtonText)) painter->setPen(QPen(toolbutton->palette.buttonText().color())); else painter->setPen(QPen(WINUI3Colors[colorSchemeIndex][controlTextSecondary])); @@ -1361,13 +1386,15 @@ void QWindows11Style::drawControl(ControlElement element, const QStyleOption *op } else { QRectF rect = btn->rect.marginsRemoved(QMargins(2,2,2,2)); painter->setPen(Qt::NoPen); - if (flags & (State_Sunken)) - painter->setBrush(flags & State_On ? option->palette.accent().color().lighter(120) : WINUI3Colors[colorSchemeIndex][controlFillTertiary]); - else if (flags & State_MouseOver) - painter->setBrush(flags & State_On ? option->palette.accent().color().lighter(110) : WINUI3Colors[colorSchemeIndex][controlFillSecondary]); - else - painter->setBrush(flags & State_On ? option->palette.accent() : option->palette.button()); + painter->setBrush(flags & State_On ? option->palette.accent() : option->palette.button()); painter->drawRoundedRect(rect, secondLevelRoundingRadius, secondLevelRoundingRadius); + if (flags.testFlags(State_Sunken | State_MouseOver)) { + if (flags & (State_Sunken)) + painter->setBrush(flags & State_On ? option->palette.accent().color().lighter(120) : WINUI3Colors[colorSchemeIndex][controlFillTertiary]); + else if (flags & State_MouseOver) + painter->setBrush(flags & State_On ? option->palette.accent().color().lighter(110) : WINUI3Colors[colorSchemeIndex][controlFillSecondary]); + painter->drawRoundedRect(rect, secondLevelRoundingRadius, secondLevelRoundingRadius); + } rect.adjust(0.5,0.5,-0.5,-0.5); painter->setBrush(Qt::NoBrush); @@ -1575,7 +1602,13 @@ void QWindows11Style::drawControl(ControlElement element, const QStyleOption *op break; case CE_HeaderSection: { if (const QStyleOptionHeader *header = qstyleoption_cast<const QStyleOptionHeader *>(option)) { + painter->setPen(Qt::NoPen); + painter->setBrush(header->palette.button()); + painter->drawRect(header->rect); + painter->setPen(highContrastTheme == true ? header->palette.buttonText().color() : WINUI3Colors[colorSchemeIndex][frameColorLight]); + painter->setBrush(Qt::NoBrush); + if (header->position == QStyleOptionHeader::OnlyOneSection) { break; } @@ -1713,6 +1746,10 @@ int QWindows11Style::styleHint(StyleHint hint, const QStyleOption *opt, return 0; case QStyle::SH_ItemView_ShowDecorationSelected: return 1; + case QStyle::SH_Slider_AbsoluteSetButtons: + return Qt::LeftButton; + case QStyle::SH_Slider_PageSetButtons: + return 0; default: return QWindowsVistaStyle::styleHint(hint, opt, widget, returnData); } @@ -2011,12 +2048,25 @@ void QWindows11Style::polish(QWidget* widget) QLineEdit *le = cb->lineEdit(); le->setFrame(false); } + } else if (widget->inherits("QAbstractSpinBox")) { + const int minWidth = 2 * 24 + 40; + const int originalWidth = widget->size().width(); + if (originalWidth < minWidth) { + widget->resize(minWidth, widget->size().height()); + widget->setProperty(originalWidthProperty.constData(), originalWidth); + } + } else if (widget->inherits("QAbstractButton") || widget->inherits("QToolButton")) { + widget->setAutoFillBackground(false); } else if (qobject_cast<QGraphicsView *>(widget) && !qobject_cast<QTextEdit *>(widget)) { QPalette pal = widget->palette(); pal.setColor(QPalette::Base, pal.window().color()); widget->setPalette(pal); } else if (const auto *scrollarea = qobject_cast<QAbstractScrollArea *>(widget); - scrollarea && !qobject_cast<QMdiArea *>(widget)) { + scrollarea +#if QT_CONFIG(mdiarea) + && !qobject_cast<QMdiArea *>(widget) +#endif + ) { QPalette pal = scrollarea->viewport()->palette(); const QPalette originalPalette = pal; pal.setColor(scrollarea->viewport()->backgroundRole(), Qt::transparent); @@ -2035,59 +2085,57 @@ void QWindows11Style::unpolish(QWidget *widget) { QWindowsVistaStyle::unpolish(widget); if (const auto *scrollarea = qobject_cast<QAbstractScrollArea *>(widget); - scrollarea && !qobject_cast<QMdiArea *>(widget)) { + scrollarea +#if QT_CONFIG(mdiarea) + && !qobject_cast<QMdiArea *>(widget) +#endif + ) { const QPalette pal = scrollarea->viewport()->property("_q_original_background_palette").value<QPalette>(); scrollarea->viewport()->setPalette(pal); scrollarea->viewport()->setProperty("_q_original_background_palette", QVariant()); } + if (widget->inherits("QAbstractSpinBox")) { + const QVariant originalWidth = widget->property(originalWidthProperty.constData()); + if (originalWidth.isValid()) { + widget->resize(originalWidth.toInt(), widget->size().height()); + widget->setProperty(originalWidthProperty.constData(), QVariant()); + } + } } /* The colors for Windows 11 are taken from the official WinUI3 Figma style at https://www.figma.com/community/file/1159947337437047524 */ +#define SET_IF_UNRESOLVED(GROUP, ROLE, VALUE) \ + if (!result.isBrushSet(QPalette::Inactive, ROLE) || styleSheetChanged) \ + result.setColor(GROUP, ROLE, VALUE) + static void populateLightSystemBasePalette(QPalette &result) { static QString oldStyleSheet; const bool styleSheetChanged = oldStyleSheet != qApp->styleSheet(); - QPalette standardPalette = QApplication::palette(); const QColor textColor = QColor(0x00,0x00,0x00,0xE4); - const QColor btnFace = QColor(0xFF,0xFF,0xFF,0xB3); const QColor btnHighlight = result.accent().color(); const QColor btnColor = result.button().color(); - if (standardPalette.color(QPalette::Highlight) == result.color(QPalette::Highlight) || styleSheetChanged) - result.setColor(QPalette::Highlight, btnHighlight); - if (standardPalette.color(QPalette::WindowText) == result.color(QPalette::WindowText) || styleSheetChanged) - result.setColor(QPalette::WindowText, textColor); - if (standardPalette.color(QPalette::Button) == result.color(QPalette::Button) || styleSheetChanged) - result.setColor(QPalette::Button, btnFace); - if (standardPalette.color(QPalette::Light) == result.color(QPalette::Light) || styleSheetChanged) - result.setColor(QPalette::Light, btnColor.lighter(150)); - if (standardPalette.color(QPalette::Dark) == result.color(QPalette::Dark) || styleSheetChanged) - result.setColor(QPalette::Dark, btnColor.darker(200)); - if (standardPalette.color(QPalette::Mid) == result.color(QPalette::Mid) || styleSheetChanged) - result.setColor(QPalette::Mid, btnColor.darker(150)); - if (standardPalette.color(QPalette::Text) == result.color(QPalette::Text) || styleSheetChanged) - result.setColor(QPalette::Text, textColor); - if (standardPalette.color(QPalette::BrightText) != result.color(QPalette::BrightText) || styleSheetChanged) - result.setColor(QPalette::BrightText, btnHighlight); - if (standardPalette.color(QPalette::Base) == result.color(QPalette::Base) || styleSheetChanged) - result.setColor(QPalette::Base, btnFace); - if (standardPalette.color(QPalette::Window) == result.color(QPalette::Window) || styleSheetChanged) - result.setColor(QPalette::Window, QColor(0xF3,0xF3,0xF3,0xFF)); - if (standardPalette.color(QPalette::ButtonText) == result.color(QPalette::ButtonText) || styleSheetChanged) - result.setColor(QPalette::ButtonText, textColor); - if (standardPalette.color(QPalette::Midlight) == result.color(QPalette::Midlight) || styleSheetChanged) - result.setColor(QPalette::Midlight, btnColor.lighter(125)); - if (standardPalette.color(QPalette::Shadow) == result.color(QPalette::Shadow) || styleSheetChanged) - result.setColor(QPalette::Shadow, Qt::black); - if (standardPalette.color(QPalette::ToolTipBase) == result.color(QPalette::ToolTipBase) || styleSheetChanged) - result.setColor(QPalette::ToolTipBase, result.window().color()); - if (standardPalette.color(QPalette::ToolTipText) == result.color(QPalette::ToolTipText) || styleSheetChanged) - result.setColor(QPalette::ToolTipText, result.windowText().color()); + SET_IF_UNRESOLVED(QPalette::All, QPalette::Highlight, btnHighlight); + SET_IF_UNRESOLVED(QPalette::All, QPalette::WindowText, textColor); + SET_IF_UNRESOLVED(QPalette::All, QPalette::Button, btnFace); + SET_IF_UNRESOLVED(QPalette::All, QPalette::Light, btnColor.lighter(150)); + SET_IF_UNRESOLVED(QPalette::All, QPalette::Dark, btnColor.darker(200)); + SET_IF_UNRESOLVED(QPalette::All, QPalette::Mid, btnColor.darker(150)); + SET_IF_UNRESOLVED(QPalette::All, QPalette::Text, textColor); + SET_IF_UNRESOLVED(QPalette::All, QPalette::BrightText, btnHighlight); + SET_IF_UNRESOLVED(QPalette::All, QPalette::Base, btnFace); + SET_IF_UNRESOLVED(QPalette::All, QPalette::Window, QColor(0xF3,0xF3,0xF3,0xFF)); + SET_IF_UNRESOLVED(QPalette::All, QPalette::ButtonText, textColor); + SET_IF_UNRESOLVED(QPalette::All, QPalette::Midlight, btnColor.lighter(125)); + SET_IF_UNRESOLVED(QPalette::All, QPalette::Shadow, Qt::black); + SET_IF_UNRESOLVED(QPalette::All, QPalette::ToolTipBase, result.window().color()); + SET_IF_UNRESOLVED(QPalette::All, QPalette::ToolTipText, result.windowText().color()); if (result.midlight() == result.button()) result.setColor(QPalette::Midlight, btnColor.lighter(110)); @@ -2097,35 +2145,30 @@ static void populateLightSystemBasePalette(QPalette &result) /*! \internal */ -void QWindows11Style::polish(QPalette& pal) +void QWindows11Style::polish(QPalette& result) { - highContrastTheme = QGuiApplicationPrivate::colorScheme() == Qt::ColorScheme::Unknown; - colorSchemeIndex = QGuiApplicationPrivate::colorScheme() == Qt::ColorScheme::Light ? 0 : 1; + highContrastTheme = QGuiApplication::styleHints()->colorScheme() == Qt::ColorScheme::Unknown; + colorSchemeIndex = QGuiApplication::styleHints()->colorScheme() == Qt::ColorScheme::Light ? 0 : 1; if (!highContrastTheme && colorSchemeIndex == 0) - populateLightSystemBasePalette(pal); - - if (standardPalette().color(QPalette::Inactive, QPalette::Button) == pal.color(QPalette::Inactive, QPalette::Button)) - pal.setColor(QPalette::Inactive, QPalette::Button, pal.button().color()); - if (standardPalette().color(QPalette::Inactive, QPalette::Window) == pal.color(QPalette::Inactive, QPalette::Window)) - pal.setColor(QPalette::Inactive, QPalette::Window, pal.window().color()); - if (standardPalette().color(QPalette::Inactive, QPalette::Light) == pal.color(QPalette::Inactive, QPalette::Light)) - pal.setColor(QPalette::Inactive, QPalette::Light, pal.light().color()); - if (standardPalette().color(QPalette::Inactive, QPalette::Dark) == pal.color(QPalette::Inactive, QPalette::Dark)) - pal.setColor(QPalette::Inactive, QPalette::Dark, pal.dark().color()); - if (standardPalette().color(QPalette::Inactive, QPalette::Accent) == pal.color(QPalette::Inactive, QPalette::Accent)) - pal.setColor(QPalette::Inactive, QPalette::Accent, pal.accent().color()); - if (standardPalette().color(QPalette::Inactive, QPalette::Highlight) == pal.color(QPalette::Inactive, QPalette::Highlight)) - pal.setColor(QPalette::Inactive, QPalette::Highlight, pal.highlight().color()); - if (standardPalette().color(QPalette::Inactive, QPalette::HighlightedText) == pal.color(QPalette::Inactive, QPalette::HighlightedText)) - pal.setColor(QPalette::Inactive, QPalette::HighlightedText, pal.highlightedText().color()); - if (standardPalette().color(QPalette::Inactive, QPalette::Text) == pal.color(QPalette::Inactive, QPalette::Text)) - pal.setColor(QPalette::Inactive, QPalette::Text, pal.text().color()); - if (standardPalette().color(QPalette::Inactive, QPalette::WindowText) == pal.color(QPalette::Inactive, QPalette::WindowText)) - pal.setColor(QPalette::Inactive, QPalette::WindowText, pal.windowText().color()); + populateLightSystemBasePalette(result); + + const bool styleSheetChanged = false; // so the macro works + + SET_IF_UNRESOLVED(QPalette::Inactive, QPalette::Button, result.button().color()); + SET_IF_UNRESOLVED(QPalette::Inactive, QPalette::Window, result.window().color()); + SET_IF_UNRESOLVED(QPalette::Inactive, QPalette::Light, result.light().color()); + SET_IF_UNRESOLVED(QPalette::Inactive, QPalette::Dark, result.dark().color()); + SET_IF_UNRESOLVED(QPalette::Inactive, QPalette::Accent, result.accent().color()); + SET_IF_UNRESOLVED(QPalette::Inactive, QPalette::Highlight, result.highlight().color()); + SET_IF_UNRESOLVED(QPalette::Inactive, QPalette::HighlightedText, result.highlightedText().color()); + SET_IF_UNRESOLVED(QPalette::Inactive, QPalette::Text, result.text().color()); + SET_IF_UNRESOLVED(QPalette::Inactive, QPalette::WindowText, result.windowText().color()); if (highContrastTheme) - pal.setColor(QPalette::Active, QPalette::HighlightedText, pal.windowText().color()); + result.setColor(QPalette::Active, QPalette::HighlightedText, result.windowText().color()); } +#undef SET_IF_UNRESOLVED + QT_END_NAMESPACE diff --git a/src/plugins/styles/modernwindows/qwindowsvistastyle.cpp b/src/plugins/styles/modernwindows/qwindowsvistastyle.cpp index 1df134b629..beee1d6f31 100644 --- a/src/plugins/styles/modernwindows/qwindowsvistastyle.cpp +++ b/src/plugins/styles/modernwindows/qwindowsvistastyle.cpp @@ -4755,7 +4755,7 @@ void QWindowsVistaStyle::polish(QPalette &pal) { Q_D(QWindowsVistaStyle); - if (QGuiApplicationPrivate::colorScheme() == Qt::ColorScheme::Dark) { + if (QGuiApplication::styleHints()->colorScheme() == Qt::ColorScheme::Dark) { // System runs in dark mode, but the Vista style cannot use a dark palette. // Overwrite with the light system palette. using QWindowsApplication = QNativeInterface::Private::QWindowsApplication; |