mirror of
https://gitee.com/spark-store-project/spark-web-app-runtime.git
synced 2025-07-04 04:36:00 +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:
parent
c6b6734c9e
commit
6b25b50696
spark-webapp-runtime
@ -4,10 +4,11 @@
|
|||||||
*/
|
*/
|
||||||
#include "application.h"
|
#include "application.h"
|
||||||
#include "mainwindow.h"
|
#include "mainwindow.h"
|
||||||
#include "globaldefine.h"
|
#include "webengineview.h"
|
||||||
#include "httpd.h"
|
#include "httpd.h"
|
||||||
|
|
||||||
#include <DSysInfo>
|
#include <DSysInfo>
|
||||||
|
#include <DApplicationSettings>
|
||||||
|
|
||||||
#include <QCommandLineParser>
|
#include <QCommandLineParser>
|
||||||
#include <QCommandLineOption>
|
#include <QCommandLineOption>
|
||||||
@ -50,6 +51,9 @@ int main(int argc, char *argv[])
|
|||||||
}
|
}
|
||||||
Application a(fakeArgc, fakeArgv.data());
|
Application a(fakeArgc, fakeArgv.data());
|
||||||
|
|
||||||
|
// 保存 DTK 主题
|
||||||
|
DApplicationSettings settings;
|
||||||
|
|
||||||
// 解析命令行启动参数
|
// 解析命令行启动参数
|
||||||
QCommandLineParser parser;
|
QCommandLineParser parser;
|
||||||
|
|
||||||
@ -255,12 +259,14 @@ int main(int argc, char *argv[])
|
|||||||
toUseGPU = parser.value(useGPU).toUInt();
|
toUseGPU = parser.value(useGPU).toUInt();
|
||||||
}
|
}
|
||||||
if (toUseGPU == true) {
|
if (toUseGPU == true) {
|
||||||
qputenv("QTWEBENGINE_CHROMIUM_FLAGS", "--ignore-gpu-blocklist --enable-gpu-rasterization --enable-native-gpu-memory-buffers --enable-accelerated-video-decode --blink-settings=darkMode=4,darkModeImagePolicy=2");
|
qputenv("QTWEBENGINE_CHROMIUM_FLAGS", "--ignore-gpu-blocklist --enable-gpu-rasterization --enable-native-gpu-memory-buffers --enable-accelerated-video-decode");
|
||||||
#ifdef __sw_64__
|
#ifdef __sw_64__
|
||||||
qputenv("QTWEBENGINE_CHROMIUM_FLAGS", "--ignore-gpu-blocklist --enable-gpu-rasterization --enable-native-gpu-memory-buffers --enable-accelerated-video-decode --blink-settings=darkMode=4,darkModeImagePolicy=2 --no-sandbox");
|
qputenv("QTWEBENGINE_CHROMIUM_FLAGS", "--ignore-gpu-blocklist --enable-gpu-rasterization --enable-native-gpu-memory-buffers --enable-accelerated-video-decode --no-sandbox");
|
||||||
#endif
|
#endif
|
||||||
qDebug() << "Setting GPU to True.";
|
qDebug() << "Setting GPU to True.";
|
||||||
}
|
}
|
||||||
|
// 初始化 QtWebEngine 深色模式环境变量
|
||||||
|
WebEngineView::handleChromiumFlags();
|
||||||
|
|
||||||
#if SSL_SERVER
|
#if SSL_SERVER
|
||||||
if (parser.isSet(optSSLPort)) {
|
if (parser.isSet(optSSLPort)) {
|
||||||
|
@ -1,20 +1,53 @@
|
|||||||
#include "webengineview.h"
|
#include "webengineview.h"
|
||||||
//#include "webengineurlrequestinterceptor.h"
|
//#include "webengineurlrequestinterceptor.h"
|
||||||
|
|
||||||
|
#include <DGuiApplicationHelper>
|
||||||
|
|
||||||
#include <QWebEngineSettings>
|
#include <QWebEngineSettings>
|
||||||
#include <QWebEngineProfile>
|
#include <QWebEngineProfile>
|
||||||
#include <QLocale>
|
#include <QLocale>
|
||||||
|
|
||||||
|
DGUI_USE_NAMESPACE
|
||||||
|
|
||||||
WebEngineView::WebEngineView(QWidget *parent)
|
WebEngineView::WebEngineView(QWidget *parent)
|
||||||
: QWebEngineView(parent)
|
: QWebEngineView(parent)
|
||||||
// , interceptor(new WebEngineUrlRequestInterceptor(this))
|
// , interceptor(new WebEngineUrlRequestInterceptor(this))
|
||||||
{
|
{
|
||||||
// page()->profile()->setHttpUserAgent(page()->profile()->httpUserAgent() + " MediaFeature/prefers-color-scheme:dark");
|
|
||||||
|
|
||||||
connect(this, &WebEngineView::urlChanged, this, [=]() {
|
connect(this, &WebEngineView::urlChanged, this, [=]() {
|
||||||
// page()->setUrlRequestInterceptor(interceptor);
|
// page()->setUrlRequestInterceptor(interceptor);
|
||||||
// page()->settings()->setAttribute(QWebEngineSettings::WebAttribute::LocalContentCanAccessRemoteUrls, true);
|
// page()->settings()->setAttribute(QWebEngineSettings::WebAttribute::LocalContentCanAccessRemoteUrls, true);
|
||||||
page()->profile()->setHttpAcceptLanguage(QLocale::system().name());
|
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");
|
||||||
|
}
|
||||||
|
@ -11,6 +11,8 @@ class WebEngineView : public QWebEngineView
|
|||||||
public:
|
public:
|
||||||
explicit WebEngineView(QWidget *parent = nullptr);
|
explicit WebEngineView(QWidget *parent = nullptr);
|
||||||
|
|
||||||
|
static void handleChromiumFlags();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
// WebEngineUrlRequestInterceptor *interceptor = nullptr;
|
// WebEngineUrlRequestInterceptor *interceptor = nullptr;
|
||||||
};
|
};
|
||||||
|
@ -13,37 +13,8 @@ Widget::Widget(QString szUrl, QWidget *parent)
|
|||||||
, mainLayout(new QStackedLayout(this))
|
, mainLayout(new QStackedLayout(this))
|
||||||
, m_szUrl(szUrl)
|
, m_szUrl(szUrl)
|
||||||
{
|
{
|
||||||
m_spinner->setFixedSize(96, 96);
|
initUI();
|
||||||
|
initConnections();
|
||||||
m_webEngineView->setObjectName(QStringLiteral("webEngineView"));
|
|
||||||
m_webEngineView->setEnabled(true);
|
|
||||||
m_webEngineView->setAutoFillBackground(false);
|
|
||||||
|
|
||||||
DApplication *dApp = qobject_cast<DApplication *>(qApp);
|
|
||||||
m_webEngineView->setZoomFactor(dApp->devicePixelRatio());
|
|
||||||
|
|
||||||
WebEnginePage *page = new WebEnginePage(m_webEngineView);
|
|
||||||
m_webEngineView->setPage(page);
|
|
||||||
|
|
||||||
page->setUrl(QUrl());
|
|
||||||
if (!m_szUrl.isEmpty()) {
|
|
||||||
page->setUrl(QUrl(m_szUrl));
|
|
||||||
}
|
|
||||||
|
|
||||||
QWidget *spinnerWidget = new QWidget(this);
|
|
||||||
QHBoxLayout *spinnerLayout = new QHBoxLayout(spinnerWidget);
|
|
||||||
spinnerLayout->setMargin(0);
|
|
||||||
spinnerLayout->setSpacing(0);
|
|
||||||
spinnerLayout->setAlignment(Qt::AlignCenter);
|
|
||||||
spinnerLayout->addStretch();
|
|
||||||
spinnerLayout->addWidget(m_spinner);
|
|
||||||
spinnerLayout->addStretch();
|
|
||||||
|
|
||||||
mainLayout->addWidget(spinnerWidget);
|
|
||||||
mainLayout->addWidget(m_webEngineView);
|
|
||||||
|
|
||||||
connect(m_webEngineView, &QWebEngineView::loadStarted, this, &Widget::on_loadStarted);
|
|
||||||
connect(m_webEngineView, &QWebEngineView::loadFinished, this, &Widget::on_loadFinished);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Widget::~Widget()
|
Widget::~Widget()
|
||||||
@ -70,6 +41,63 @@ void Widget::refresh()
|
|||||||
m_webEngineView->reload();
|
m_webEngineView->reload();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Widget::initUI()
|
||||||
|
{
|
||||||
|
m_spinner->setFixedSize(96, 96);
|
||||||
|
|
||||||
|
DApplication *dApp = qobject_cast<DApplication *>(qApp);
|
||||||
|
m_webEngineView->setZoomFactor(dApp->devicePixelRatio());
|
||||||
|
|
||||||
|
WebEnginePage *page = new WebEnginePage(m_webEngineView);
|
||||||
|
m_webEngineView->setPage(page);
|
||||||
|
|
||||||
|
page->setUrl(QUrl());
|
||||||
|
if (!m_szUrl.isEmpty()) {
|
||||||
|
page->setUrl(QUrl(m_szUrl));
|
||||||
|
}
|
||||||
|
|
||||||
|
QWidget *spinnerWidget = new QWidget(this);
|
||||||
|
QHBoxLayout *spinnerLayout = new QHBoxLayout(spinnerWidget);
|
||||||
|
spinnerLayout->setMargin(0);
|
||||||
|
spinnerLayout->setSpacing(0);
|
||||||
|
spinnerLayout->setAlignment(Qt::AlignCenter);
|
||||||
|
spinnerLayout->addStretch();
|
||||||
|
spinnerLayout->addWidget(m_spinner);
|
||||||
|
spinnerLayout->addStretch();
|
||||||
|
|
||||||
|
mainLayout->addWidget(spinnerWidget);
|
||||||
|
mainLayout->addWidget(m_webEngineView);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Widget::initConnections()
|
||||||
|
{
|
||||||
|
connect(m_webEngineView, &QWebEngineView::loadStarted, this, &Widget::on_loadStarted, Qt::UniqueConnection);
|
||||||
|
connect(m_webEngineView, &QWebEngineView::loadFinished, this, &Widget::on_loadFinished, Qt::UniqueConnection);
|
||||||
|
|
||||||
|
// FIXME: DTK 主题切换时,动态修改 QtWebEngine prefers-color-scheme
|
||||||
|
// connect(DGuiApplicationHelper::instance(), &DGuiApplicationHelper::paletteTypeChanged, this, &Widget::slotPaletteTypeChanged, Qt::UniqueConnection);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Widget::updateLayout()
|
||||||
|
{
|
||||||
|
on_loadStarted();
|
||||||
|
|
||||||
|
mainLayout->removeWidget(m_webEngineView);
|
||||||
|
QUrl url = m_webEngineView->url();
|
||||||
|
m_webEngineView->deleteLater();
|
||||||
|
|
||||||
|
m_webEngineView = new WebEngineView(this);
|
||||||
|
mainLayout->addWidget(m_webEngineView);
|
||||||
|
initConnections();
|
||||||
|
|
||||||
|
DApplication *dApp = qobject_cast<DApplication *>(qApp);
|
||||||
|
m_webEngineView->setZoomFactor(dApp->devicePixelRatio());
|
||||||
|
|
||||||
|
WebEnginePage *page = new WebEnginePage(m_webEngineView);
|
||||||
|
m_webEngineView->setPage(page);
|
||||||
|
page->setUrl(url);
|
||||||
|
}
|
||||||
|
|
||||||
void Widget::on_loadStarted()
|
void Widget::on_loadStarted()
|
||||||
{
|
{
|
||||||
mainLayout->setCurrentIndex(0);
|
mainLayout->setCurrentIndex(0);
|
||||||
@ -81,3 +109,13 @@ void Widget::on_loadFinished()
|
|||||||
m_spinner->stop();
|
m_spinner->stop();
|
||||||
mainLayout->setCurrentIndex(1);
|
mainLayout->setCurrentIndex(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void Widget::slotPaletteTypeChanged(DGuiApplicationHelper::ColorType paletteType)
|
||||||
|
{
|
||||||
|
Q_UNUSED(paletteType)
|
||||||
|
|
||||||
|
WebEngineView::handleChromiumFlags();
|
||||||
|
if (m_webEngineView) {
|
||||||
|
updateLayout();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -2,12 +2,14 @@
|
|||||||
#define WIDGET_H
|
#define WIDGET_H
|
||||||
|
|
||||||
#include <DSpinner>
|
#include <DSpinner>
|
||||||
|
#include <DGuiApplicationHelper>
|
||||||
|
|
||||||
#include <QWidget>
|
#include <QWidget>
|
||||||
#include <QWebEnginePage>
|
#include <QWebEnginePage>
|
||||||
#include <QStackedLayout>
|
#include <QStackedLayout>
|
||||||
|
|
||||||
DWIDGET_USE_NAMESPACE
|
DWIDGET_USE_NAMESPACE
|
||||||
|
DGUI_USE_NAMESPACE
|
||||||
|
|
||||||
class WebEngineView;
|
class WebEngineView;
|
||||||
class Widget : public QWidget
|
class Widget : public QWidget
|
||||||
@ -23,10 +25,17 @@ public:
|
|||||||
void goForward();
|
void goForward();
|
||||||
void refresh();
|
void refresh();
|
||||||
|
|
||||||
|
private:
|
||||||
|
void initUI();
|
||||||
|
void initConnections();
|
||||||
|
void updateLayout();
|
||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
void on_loadStarted();
|
void on_loadStarted();
|
||||||
void on_loadFinished();
|
void on_loadFinished();
|
||||||
|
|
||||||
|
void slotPaletteTypeChanged(DGuiApplicationHelper::ColorType paletteType);
|
||||||
|
|
||||||
private:
|
private:
|
||||||
WebEngineView *m_webEngineView = nullptr;
|
WebEngineView *m_webEngineView = nullptr;
|
||||||
DSpinner *m_spinner = nullptr;
|
DSpinner *m_spinner = nullptr;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user