7#include "webenginepage.h"
8#include "webenginemanagescript.h"
11#include <KLocalizedString>
13#include <KColorScheme>
16#include <QGuiApplication>
20#include <QWebEngineDownloadRequest>
21#include <QWebEngineProfile>
22#include <QWebEngineScriptCollection>
23#include <QWebEngineView>
25using namespace WebEngineViewer;
27class WebEnginePage::Private
30 std::optional<QPalette> lastPalette;
31 std::optional<KColorScheme> lastColorScheme;
32 QWebEngineScript refreshScript;
36 : QWebEnginePage( parent)
60void WebEnginePage::init()
62 connect(profile(), &QWebEngineProfile::downloadRequested,
this, &WebEnginePage::saveHtml);
63 qGuiApp->installEventFilter(
this);
66 d->refreshScript.setName(QStringLiteral(
"injectColorScheme"));
67 d->refreshScript.setSourceCode(refreshCssVariablesScript());
68 d->refreshScript.setInjectionPoint(QWebEngineScript::DocumentReady);
69 d->refreshScript.setRunsOnSubFrames(
true);
70 d->refreshScript.setWorldId(QWebEngineScript::ApplicationWorld);
71 scripts().insert(d->refreshScript);
82 auto cssVariablesScript = refreshCssVariablesScript();
83 runJavaScript(cssVariablesScript, WebEngineViewer::WebEngineManageScript::scriptWordId());
85 d->refreshScript.setSourceCode(cssVariablesScript);
86 scripts().remove(d->refreshScript);
87 scripts().insert(d->refreshScript);
92QString WebEnginePage::refreshCssVariablesScript()
95 auto palette = qGuiApp->palette();
96 if (!d->lastColorScheme || !d->lastPalette || palette != d->lastPalette) {
97 d->lastPalette = palette;
101 const auto script = QStringLiteral(R
"RAW(
103 const root = document.querySelector(':root');
104 root.style.setProperty('--kcolorscheme-background', '%1');
105 root.style.setProperty('--kcolorscheme-text', '%2');
106 root.style.setProperty('--kcolorscheme-highlight', '%3');
107 root.style.setProperty('--kcolorscheme-highlight-text', '%4');
108 root.style.setProperty('--kcolorscheme-accent', '%5');
109 root.style.setProperty('--kcolorscheme-positive-text', '%6');
110 root.style.setProperty('--kcolorscheme-positive-background', '%7');
111 root.style.setProperty('--kcolorscheme-neutral-text', '%8');
112 root.style.setProperty('--kcolorscheme-neutral-background', '%9');
113 root.style.setProperty('--kcolorscheme-negative-text', '%10');
114 root.style.setProperty('--kcolorscheme-negative-background', '%11');
115 root.style.setProperty('--kcolorscheme-active-text', '%12');
116 root.style.setProperty('--kcolorscheme-active-background', '%13');
117 root.style.setProperty('--kcolorscheme-text-disabled', '%14');
118 root.style.setProperty('--kcolorscheme-text-link', '%15');
123 palette.highlight().color().name(),
124 palette.highlightedText().color().name(),
125 palette.accent().color().name(),
142 return new WebHitTest(
this, mapToViewport(pos), pos);
145QPoint WebEnginePage::mapToViewport(
const QPoint &pos)
const
147 return QPoint(pos.
x() / zoomFactor(), pos.
y() / zoomFactor());
150void WebEnginePage::saveHtml(QWebEngineDownloadRequest *download)
154 download->setSavePageFormat(QWebEngineDownloadRequest::SingleHtmlSaveFormat);
156 download->setDownloadFileName(
QFileInfo(fileName).fileName());
161bool WebEnginePage::acceptNavigationRequest(
const QUrl &url, NavigationType type,
bool isMainFrame)
163 if (isMainFrame && type == NavigationTypeLinkClicked) {
164 Q_EMIT urlClicked(url);
170void WebEnginePage::javaScriptConsoleMessage(QWebEnginePage::JavaScriptConsoleMessageLevel level,
178 qDebug() <<
"WebEnginePage::javaScriptConsoleMessage lineNumber: " << lineNumber <<
" message: " << message;
179 Q_EMIT showConsoleMessage(message);
182#include "moc_webenginepage.cpp"
~WebEnginePage() override
Destructor.
WebEnginePage(QObject *parent=nullptr)
Constructor.
QString i18nc(const char *context, const char *text, const TYPE &arg...)
AKONADI_CALENDAR_EXPORT KCalendarCore::Event::Ptr event(const Akonadi::Item &item)
QString path(const QString &relativePath)
QString getSaveFileName(QWidget *parent, const QString &caption, const QString &dir, const QString &filter, QString *selectedFilter, Options options)
virtual bool eventFilter(QObject *watched, QEvent *event)
QString arg(Args &&... args) const const
bool isEmpty() const const