mirror of
https://gitee.com/spark-store-project/spark-web-app-runtime.git
synced 2025-12-15 03:22:05 +08:00
feat: 支持启动时识别 DTK 主题颜色,判断网页是否需要显示为深色模式(切换 DTK 主题后重启生效,不支持实时生效)
https://github.com/qutebrowser/qutebrowser/issues/4840 启动时判断 DTK 主题颜色,QTWEBENGINE_CHROMIUM_FLAGS 环境变量添加 --blink-settings=preferredColorScheme=0 启用深色模式(或 --force-dark-mode)(Qt >= 5.14,当前仅 Deepin 支持) Log: 支持启动时识别 DTK 主题颜色,判断网页是否需要显示为深色模式
This commit is contained in:
@@ -1,20 +1,53 @@
|
||||
#include "webengineview.h"
|
||||
//#include "webengineurlrequestinterceptor.h"
|
||||
|
||||
#include <DGuiApplicationHelper>
|
||||
|
||||
#include <QWebEngineSettings>
|
||||
#include <QWebEngineProfile>
|
||||
#include <QLocale>
|
||||
|
||||
DGUI_USE_NAMESPACE
|
||||
|
||||
WebEngineView::WebEngineView(QWidget *parent)
|
||||
: QWebEngineView(parent)
|
||||
// , interceptor(new WebEngineUrlRequestInterceptor(this))
|
||||
{
|
||||
// page()->profile()->setHttpUserAgent(page()->profile()->httpUserAgent() + " MediaFeature/prefers-color-scheme:dark");
|
||||
|
||||
connect(this, &WebEngineView::urlChanged, this, [=]() {
|
||||
// page()->setUrlRequestInterceptor(interceptor);
|
||||
// page()->settings()->setAttribute(QWebEngineSettings::WebAttribute::LocalContentCanAccessRemoteUrls, true);
|
||||
page()->profile()->setHttpAcceptLanguage(QLocale::system().name());
|
||||
// qInfo() << "User Agent:" << page()->profile()->httpUserAgent();
|
||||
});
|
||||
}
|
||||
|
||||
void WebEngineView::handleChromiumFlags()
|
||||
{
|
||||
DGuiApplicationHelper::ColorType themeType = DGuiApplicationHelper::instance()->themeType();
|
||||
|
||||
QString env = qgetenv("QTWEBENGINE_CHROMIUM_FLAGS");
|
||||
QStringList flags = env.split(" ", QString::SkipEmptyParts);
|
||||
|
||||
/**
|
||||
* --blink-settings=preferredColorScheme=0 强制 prefers-color-scheme=dark (>= 5.14)
|
||||
* --blink-settings=darkModeEnabled=true,darkModeInversionAlgorithm=4 强制反转网页颜色 (>= 5.14 && < 5.15)
|
||||
* --blink-settings=forceDarkModeEnabled=true,darkModeInversionAlgorithm=4 强制反转网页颜色 (>= 5.15)
|
||||
* --force-dark-mode 强制 prefers-color-scheme=dark (>= 5.14)
|
||||
*/
|
||||
foreach (const QString &flag, flags) {
|
||||
if (flag == "--force-dark-mode") {
|
||||
flags.removeAll(flag);
|
||||
}
|
||||
if (flag.startsWith("--blink-settings")) {
|
||||
flags.removeAll(flag);
|
||||
}
|
||||
}
|
||||
|
||||
if (themeType == DGuiApplicationHelper::DarkType) {
|
||||
flags.append("--blink-settings=preferredColorScheme=0");
|
||||
} else {
|
||||
flags.append("--blink-settings=preferredColorScheme=1");
|
||||
}
|
||||
qputenv("QTWEBENGINE_CHROMIUM_FLAGS", flags.join(" ").toUtf8());
|
||||
|
||||
qDebug() << Q_FUNC_INFO << "QTWEBENGINE_CHROMIUM_FLAGS=" + qgetenv("QTWEBENGINE_CHROMIUM_FLAGS");
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user