From 72195ade9d0722eab639017378f43f9e1dd884dc Mon Sep 17 00:00:00 2001 From: Andreas Kling Date: Sat, 29 Apr 2023 18:35:06 +0200 Subject: [PATCH] Ladybird: Let WebContent know if the current system theme is dark This means we now actually respect @media (prefers-color-scheme: dark) by default when in dark mode. :^) --- Ladybird/ConsoleWidget.cpp | 15 ++------------- Ladybird/WebContentView.cpp | 4 ++++ Ladybird/main.cpp | 13 +++++++++++++ 3 files changed, 19 insertions(+), 13 deletions(-) diff --git a/Ladybird/ConsoleWidget.cpp b/Ladybird/ConsoleWidget.cpp index e272ecd7eb3..ce8abdf17d7 100644 --- a/Ladybird/ConsoleWidget.cpp +++ b/Ladybird/ConsoleWidget.cpp @@ -19,21 +19,10 @@ #include #include +bool is_using_dark_system_theme(QWidget&); + namespace Ladybird { -static 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 - - 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; -} - ConsoleWidget::ConsoleWidget() { setLayout(new QVBoxLayout); diff --git a/Ladybird/WebContentView.cpp b/Ladybird/WebContentView.cpp index 84ca46ee622..f371dfa187e 100644 --- a/Ladybird/WebContentView.cpp +++ b/Ladybird/WebContentView.cpp @@ -53,6 +53,8 @@ #include #include +bool is_using_dark_system_theme(QWidget&); + WebContentView::WebContentView(StringView webdriver_content_ipc_path, WebView::EnableCallgrindProfiling enable_callgrind_profiling) : m_webdriver_content_ipc_path(webdriver_content_ipc_path) { @@ -590,6 +592,8 @@ static Core::AnonymousBuffer make_system_theme_from_qt_palette(QWidget& widget, translate(Gfx::ColorRole::Selection, QPalette::ColorRole::Highlight); translate(Gfx::ColorRole::SelectionText, QPalette::ColorRole::HighlightedText); + palette.set_flag(Gfx::FlagRole::IsDark, is_using_dark_system_theme(widget)); + return theme; } diff --git a/Ladybird/main.cpp b/Ladybird/main.cpp index 11665684729..78ce8ba6ff5 100644 --- a/Ladybird/main.cpp +++ b/Ladybird/main.cpp @@ -113,3 +113,16 @@ ErrorOr serenity_main(Main::Arguments arguments) return event_loop.exec(); } + +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 + + 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; +}