UI/Qt: Use Qt-reported dark theme

For Qt >= 6.5, the system theme can be determined reliably, so no
guesswork is needed. A fallback remains for Qt < 6.5, but it is
hacky and less reliable.
This commit is contained in:
Pavel Shliak 2024-11-23 21:29:18 +04:00 committed by Tim Ledbetter
parent ecdb53cca6
commit 6ad93d2b21
Notes: github-actions[bot] 2024-11-23 19:55:29 +00:00

View file

@ -22,6 +22,10 @@
#include <UI/Qt/Settings.h>
#include <UI/Qt/WebContentView.h>
#if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)
# include <QStyleHints>
#endif
#if defined(AK_OS_MACOS)
# include <LibWebView/MachPortServer.h>
#endif
@ -32,15 +36,20 @@ namespace Ladybird {
bool is_using_dark_system_theme(QWidget&);
bool is_using_dark_system_theme(QWidget& widget)
{
// FIXME: Qt does not provide any method to query if the system is using a dark theme. We will have to implement
// platform-specific methods if we wish to have better detection. For now, this inspects if Qt is using a
// dark color for widget backgrounds using Rec. 709 luma coefficients.
// https://en.wikipedia.org/wiki/Rec._709#Luma_coefficients
#if QT_VERSION >= QT_VERSION_CHECK(6, 5, 0)
// We use that only for fallback mode
Q_UNUSED(widget);
// Use the new Qt API available from version 6.5.0
auto color_scheme = QGuiApplication::styleHints()->colorScheme();
return color_scheme == Qt::ColorScheme::Dark;
#else
// Fallback for older Qt versions
// Calculate luma based on Rec. 709 coefficients
// https://en.wikipedia.org/wiki/Rec._709#Luma_coefficients
auto color = widget.palette().color(widget.backgroundRole());
auto luma = 0.2126f * color.redF() + 0.7152f * color.greenF() + 0.0722f * color.blueF();
return luma <= 0.5f;
#endif
}
}