Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

UI/Qt: Set dark theme as reported by Qt #2529

Merged
merged 1 commit into from
Nov 23, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading