summaryrefslogtreecommitdiffstats
path: root/src/core/web_contents_adapter.cpp
diff options
context:
space:
mode:
authorKaloyan Chehlarski <kaloyan.chehlarski@qt.io>2024-02-12 15:18:34 +0100
committerKaloyan Chehlarski <kaloyan.chehlarski@qt.io>2024-02-20 10:34:46 +0000
commitd192c2503d22c71742461a21392e5c110408ca88 (patch)
tree9f127b2f43bd652490cd8a310c66dda7837eefac /src/core/web_contents_adapter.cpp
parent6ff3d0144cc9eb9950ec184614631e6e02e0c5d1 (diff)
Propagate system light/dark mode to webpages
This change makes sure that Chromium is aware of any switches between light/dark mode in the system theme. This allows webpages that use the 'prefers-color-scheme' media feature query to see the system color scheme, and adapt accordingly. An example is the following page (set theme to 'OS default' in the top right): https://developer.mozilla.org/en-US/docs/Web Change-Id: Ifee20d15bbc1681c6ea2ee016f0283a6fa09fc77 Reviewed-by: Allan Sandfeld Jensen <allan.jensen@qt.io>
Diffstat (limited to 'src/core/web_contents_adapter.cpp')
-rw-r--r--src/core/web_contents_adapter.cpp11
1 files changed, 11 insertions, 0 deletions
diff --git a/src/core/web_contents_adapter.cpp b/src/core/web_contents_adapter.cpp
index f549bfac0..a33355e61 100644
--- a/src/core/web_contents_adapter.cpp
+++ b/src/core/web_contents_adapter.cpp
@@ -60,6 +60,7 @@
#include "ui/base/clipboard/clipboard_constants.h"
#include "ui/base/clipboard/custom_data_helper.h"
#include "ui/gfx/font_render_params.h"
+#include "ui/native_theme/native_theme.h"
#include "qtwebengine/browser/qtwebenginepage.mojom.h"
#include <QtCore/QVariant>
@@ -453,6 +454,10 @@ bool WebContentsAdapter::isInitialized() const
return (bool)m_webContentsDelegate;
}
+ui::NativeTheme::PreferredColorScheme toWeb(Qt::ColorScheme colorScheme) {
+ return colorScheme == Qt::ColorScheme::Dark ? ui::NativeTheme::PreferredColorScheme::kDark : ui::NativeTheme::PreferredColorScheme::kLight;
+}
+
void WebContentsAdapter::initialize(content::SiteInstance *site)
{
Q_ASSERT(m_adapterClient);
@@ -506,6 +511,12 @@ void WebContentsAdapter::initialize(content::SiteInstance *site)
m_webContentsDelegate->RenderViewHostChanged(nullptr, rvh);
+ // Make sure the system theme's light/dark mode is propagated to webpages
+ QObject::connect(QGuiApplication::styleHints(), &QStyleHints::colorSchemeChanged, [](Qt::ColorScheme colorScheme){
+ ui::NativeTheme::GetInstanceForWeb()->set_preferred_color_scheme(toWeb(colorScheme));
+ });
+ ui::NativeTheme::GetInstanceForWeb()->set_preferred_color_scheme(toWeb(QGuiApplication::styleHints()->colorScheme()));
+
m_adapterClient->initializationFinished();
}