zty199 6b8b31ebf6 fix: 修复 UOS 编译兼容性问题
网页通知获取依赖 QWebEngineNotification 类,在 Qt 5.13 引入

Log: 添加宏定义,判断 Qt 版本 >= 5.13 启用网页通知功能
2022-12-19 09:40:48 +08:00

100 lines
3.5 KiB
C++

#include "webengineview.h"
//#include "webengineurlrequestinterceptor.h"
#include "application.h"
#include <DGuiApplicationHelper>
#include <DNotifySender>
#include <QWebEngineSettings>
#include <QWebEngineProfile>
#include <QLocale>
#include <QFileInfo>
DGUI_USE_NAMESPACE
DCORE_USE_NAMESPACE
WebEngineView::WebEngineView(QWidget *parent)
: QWebEngineView(parent)
// , interceptor(new WebEngineUrlRequestInterceptor(this))
{
connect(this, &WebEngineView::urlChanged, this, [=]() {
// page()->setUrlRequestInterceptor(interceptor);
// page()->settings()->setAttribute(QWebEngineSettings::WebAttribute::LocalContentCanAccessRemoteUrls, true);
page()->profile()->setHttpAcceptLanguage(QLocale::system().name());
#if (QT_VERSION >= QT_VERSION_CHECK(5, 13, 0))
page()->profile()->setNotificationPresenter([&](std::unique_ptr<QWebEngineNotification> notification) {
WebEngineView::present(notification);
});
#endif
});
}
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");
}
#if (QT_VERSION >= QT_VERSION_CHECK(5, 13, 0))
void WebEngineView::present(std::unique_ptr<QWebEngineNotification> &newNotification)
{
qDebug() << Q_FUNC_INFO << "New notification received:" << newNotification->title() << newNotification->message();
QImage image = newNotification->icon();
QString tmpDir = qobject_cast<Application *>(qApp)->mainWindow()->tmpDir();
QString appIcon = tmpDir + "/" + "icon.png";
if (QFileInfo::exists(appIcon)) {
QFile::remove(appIcon);
}
image.save(appIcon, "PNG");
QString summary = newNotification->title();
QString appName = qobject_cast<Application *>(qApp)->mainWindow()->title();
QString appBody = newNotification->message();
quint32 replaceId = 0;
int timeOut = 3000;
QStringList actions = QStringList() << "view" << QObject::tr("View");
QString launchCmd = qobject_cast<Application *>(qApp)->launchParams().join(",");
qDebug() << launchCmd;
QVariantMap hints;
hints.insert("x-deepin-action-view", launchCmd);
DUtil::DNotifySender(summary)
.appName(appName)
.appIcon(appIcon)
.appBody(appBody)
.replaceId(replaceId)
.timeOut(timeOut)
.actions(actions)
.hints(hints)
.call();
}
#endif