Blob Blame History Raw
From 3d8874aaf5384818a51f158cd2880b7976c9a93a Mon Sep 17 00:00:00 2001
From: Jan Grulich <jgrulich@redhat.com>
Date: Wed, 26 Jul 2023 12:41:53 +0200
Subject: [PATCH 05/22] Account for dark system themes in qt_fusionPalette

On Ubuntu (Gnome), the Fusion style didn't account for when a dark
theme was set in the system's settings. Neither QGtk3Theme, nor
QGnomeTheme (which it derives from) provide a palette() implementation,
so they'd fall back to QPlatformTheme's. This default implementation
calls QPlatformThemePrivate::initializeSystemPalette(), which uses
qt_fusionPalette().

This patch accounts for QPlatformTheme::appearance() in
qt_fusionPalette(), adjusting the palette roles accordingly
to look correct when run under a dark theme.

This also fixes the same issue for the Fusion style in Qt Quick
Controls.
---
 src/gui/kernel/qpalette.cpp       | 36 -------------------------
 src/gui/kernel/qplatformtheme.cpp | 44 ++++++++++++++++++++++++++++++-
 2 files changed, 43 insertions(+), 37 deletions(-)

diff --git a/src/gui/kernel/qpalette.cpp b/src/gui/kernel/qpalette.cpp
index a192b7aef4..0549f19699 100644
--- a/src/gui/kernel/qpalette.cpp
+++ b/src/gui/kernel/qpalette.cpp
@@ -1164,42 +1164,6 @@ void QPalette::setColorGroup(ColorGroup cg, const QBrush &foreground, const QBru
     setBrush(cg, ToolTipText, toolTipText);
 }
 
-Q_GUI_EXPORT QPalette qt_fusionPalette()
-{
-    QColor backGround(239, 239, 239);
-    QColor light = backGround.lighter(150);
-    QColor mid(backGround.darker(130));
-    QColor midLight = mid.lighter(110);
-    QColor base = Qt::white;
-    QColor disabledBase(backGround);
-    QColor dark = backGround.darker(150);
-    QColor darkDisabled = QColor(209, 209, 209).darker(110);
-    QColor text = Qt::black;
-    QColor hightlightedText = Qt::white;
-    QColor disabledText = QColor(190, 190, 190);
-    QColor button = backGround;
-    QColor shadow = dark.darker(135);
-    QColor disabledShadow = shadow.lighter(150);
-
-    QPalette fusionPalette(Qt::black,backGround,light,dark,mid,text,base);
-    fusionPalette.setBrush(QPalette::Midlight, midLight);
-    fusionPalette.setBrush(QPalette::Button, button);
-    fusionPalette.setBrush(QPalette::Shadow, shadow);
-    fusionPalette.setBrush(QPalette::HighlightedText, hightlightedText);
-
-    fusionPalette.setBrush(QPalette::Disabled, QPalette::Text, disabledText);
-    fusionPalette.setBrush(QPalette::Disabled, QPalette::WindowText, disabledText);
-    fusionPalette.setBrush(QPalette::Disabled, QPalette::ButtonText, disabledText);
-    fusionPalette.setBrush(QPalette::Disabled, QPalette::Base, disabledBase);
-    fusionPalette.setBrush(QPalette::Disabled, QPalette::Dark, darkDisabled);
-    fusionPalette.setBrush(QPalette::Disabled, QPalette::Shadow, disabledShadow);
-
-    fusionPalette.setBrush(QPalette::Active, QPalette::Highlight, QColor(48, 140, 198));
-    fusionPalette.setBrush(QPalette::Inactive, QPalette::Highlight, QColor(48, 140, 198));
-    fusionPalette.setBrush(QPalette::Disabled, QPalette::Highlight, QColor(145, 145, 145));
-    return fusionPalette;
-}
-
 #ifndef QT_NO_DEBUG_STREAM
 QDebug operator<<(QDebug dbg, const QPalette &p)
 {
diff --git a/src/gui/kernel/qplatformtheme.cpp b/src/gui/kernel/qplatformtheme.cpp
index 662fef1426..62f569bbef 100644
--- a/src/gui/kernel/qplatformtheme.cpp
+++ b/src/gui/kernel/qplatformtheme.cpp
@@ -364,7 +364,49 @@ QPlatformThemePrivate::~QPlatformThemePrivate()
     delete systemPalette;
 }
 
-Q_GUI_EXPORT QPalette qt_fusionPalette();
+Q_GUI_EXPORT QPalette qt_fusionPalette()
+{
+    const bool darkAppearance = QGuiApplicationPrivate::platformTheme()->appearance()
+        == QPlatformTheme::Appearance::Dark;
+    const QColor windowText = darkAppearance ? QColor(240, 240, 240) : Qt::black;
+    const QColor backGround = darkAppearance ? QColor(50, 50, 50) : QColor(239, 239, 239);
+    const QColor light = backGround.lighter(150);
+    const QColor mid = (backGround.darker(130));
+    const QColor midLight = mid.lighter(110);
+    const QColor base = darkAppearance ? backGround.darker(140) : Qt::white;
+    const QColor disabledBase(backGround);
+    const QColor dark = backGround.darker(150);
+    const QColor darkDisabled = QColor(209, 209, 209).darker(110);
+    const QColor text = darkAppearance ? windowText : Qt::black;
+    const QColor hightlightedText = darkAppearance ? windowText : Qt::white;
+    const QColor disabledText = darkAppearance ? QColor(130, 130, 130) : QColor(190, 190, 190);
+    const QColor button = backGround;
+    const QColor shadow = dark.darker(135);
+    const QColor disabledShadow = shadow.lighter(150);
+    QColor placeholder = text;
+    placeholder.setAlpha(128);
+
+    QPalette fusionPalette(windowText, backGround, light, dark, mid, text, base);
+    fusionPalette.setBrush(QPalette::Midlight, midLight);
+    fusionPalette.setBrush(QPalette::Button, button);
+    fusionPalette.setBrush(QPalette::Shadow, shadow);
+    fusionPalette.setBrush(QPalette::HighlightedText, hightlightedText);
+
+    fusionPalette.setBrush(QPalette::Disabled, QPalette::Text, disabledText);
+    fusionPalette.setBrush(QPalette::Disabled, QPalette::WindowText, disabledText);
+    fusionPalette.setBrush(QPalette::Disabled, QPalette::ButtonText, disabledText);
+    fusionPalette.setBrush(QPalette::Disabled, QPalette::Base, disabledBase);
+    fusionPalette.setBrush(QPalette::Disabled, QPalette::Dark, darkDisabled);
+    fusionPalette.setBrush(QPalette::Disabled, QPalette::Shadow, disabledShadow);
+
+    fusionPalette.setBrush(QPalette::Active, QPalette::Highlight, QColor(48, 140, 198));
+    fusionPalette.setBrush(QPalette::Inactive, QPalette::Highlight, QColor(48, 140, 198));
+    fusionPalette.setBrush(QPalette::Disabled, QPalette::Highlight, QColor(145, 145, 145));
+
+    fusionPalette.setBrush(QPalette::PlaceholderText, placeholder);
+
+    return fusionPalette;
+}
 
 void QPlatformThemePrivate::initializeSystemPalette()
 {
-- 
2.41.0