Skip to content

Commit

Permalink
UI/Qt: Use Qt-reported dark theme
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
shlyakpavel authored and tcl3 committed Nov 23, 2024
1 parent ecdb53c commit 6ad93d2
Showing 1 changed file with 15 additions and 6 deletions.
21 changes: 15 additions & 6 deletions UI/Qt/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}

}
Expand Down

0 comments on commit 6ad93d2

Please sign in to comment.