Skip to content

Commit

Permalink
feat(browser): introduce centralized web settings management
Browse files Browse the repository at this point in the history
Additionally fixes a number of regressions from Qt WebEngine migration.
  • Loading branch information
trollixx committed May 10, 2020
1 parent 1256fd8 commit e477c4a
Show file tree
Hide file tree
Showing 7 changed files with 166 additions and 54 deletions.
1 change: 1 addition & 0 deletions src/libs/browser/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
add_library(Browser STATIC
searchtoolbar.cpp
settings.cpp
urlrequestinterceptor.cpp
webbridge.cpp
webcontrol.cpp
Expand Down
99 changes: 99 additions & 0 deletions src/libs/browser/settings.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
/****************************************************************************
**
** Copyright (C) 2020 Oleg Shparber
** Contact: https://go.zealdocs.org/l/contact
**
** This file is part of Zeal.
**
** Zeal is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** Zeal is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with Zeal. If not, see <http://www.gnu.org/licenses/>.
**
****************************************************************************/

#include "settings.h"

#include "urlrequestinterceptor.h"

#include <core/settings.h>

#include <QFileInfo>
#include <QWebEngineProfile>
#include <QWebEngineSettings>
#include <QWebEngineScript>
#include <QWebEngineScriptCollection>

namespace {
constexpr char DarkModeCssUrl[] = "qrc:///browser/assets/css/darkmode.css";
constexpr char HighlightOnNavigateCssUrl[] = "qrc:///browser/assets/css/highlight.css";
}

using namespace Zeal::Browser;

Settings::Settings(Core::Settings *appSettings, QObject *parent)
: QObject(parent)
, m_appSettings(appSettings)
, m_webProfile(QWebEngineProfile::defaultProfile())
{
// Setup URL interceptor.
m_webProfile->setUrlRequestInterceptor(new UrlRequestInterceptor(this));

// Disable on-disk cache.
m_webProfile->setHttpCacheType(QWebEngineProfile::MemoryHttpCache);

// Listen to settings changes.
connect(m_appSettings, &Core::Settings::updated, this, &Settings::applySettings);
applySettings();
}

void Settings::applySettings()
{
m_webProfile->settings()->setAttribute(QWebEngineSettings::ScrollAnimatorEnabled,
m_appSettings->isSmoothScrollingEnabled);

// Apply custom CSS.
// TODO: Apply to all open pages.
m_webProfile->scripts()->clear(); // Remove all scripts first.

if (m_appSettings->darkModeEnabled) {
setCustomStyleSheet(QStringLiteral("_zeal_darkstylesheet"), DarkModeCssUrl);
}

if (m_appSettings->highlightOnNavigateEnabled) {
setCustomStyleSheet(QStringLiteral("_zeal_highlightstylesheet"), HighlightOnNavigateCssUrl);
}

if (QFileInfo::exists(m_appSettings->customCssFile)) {
setCustomStyleSheet(QStringLiteral("_zeal_userstylesheet"), m_appSettings->customCssFile);
}
}

void Settings::setCustomStyleSheet(const QString &name, const QString &cssUrl)
{
QString cssInjectCode = QLatin1String("(function() {"
"let head = document.getElementsByTagName('head')[0];"
"if (!head) { console.error('Cannot set custom stylesheet.'); return; }"
"let link = document.createElement('link');"
"link.rel = 'stylesheet';"
"link.type = 'text/css';"
"link.href = '%1';"
"link.media = 'all';"
"head.appendChild(link);"
"})()");

QWebEngineScript script;
script.setName(name);
script.setSourceCode(cssInjectCode.arg(cssUrl));
script.setInjectionPoint(QWebEngineScript::DocumentReady);
script.setRunsOnSubFrames(true);
m_webProfile->scripts()->insert(script);
}
58 changes: 58 additions & 0 deletions src/libs/browser/settings.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/****************************************************************************
**
** Copyright (C) 2020 Oleg Shparber
** Contact: https://go.zealdocs.org/l/contact
**
** This file is part of Zeal.
**
** Zeal is free software: you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation, either version 3 of the License, or
** (at your option) any later version.
**
** Zeal is distributed in the hope that it will be useful,
** but WITHOUT ANY WARRANTY; without even the implied warranty of
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
** GNU General Public License for more details.
**
** You should have received a copy of the GNU General Public License
** along with Zeal. If not, see <http://www.gnu.org/licenses/>.
**
****************************************************************************/

#ifndef ZEAL_BROWSER_SETTINGS_H
#define ZEAL_BROWSER_SETTINGS_H

#include <QObject>

class QWebEngineProfile;

namespace Zeal {

namespace Core {
class Settings;
}

namespace Browser {

class Settings final : public QObject
{
Q_OBJECT
Q_DISABLE_COPY(Settings)
public:
explicit Settings(Core::Settings *appSettings, QObject *parent = nullptr);

private slots:
void applySettings();

private:
void setCustomStyleSheet(const QString &name, const QString &cssUrl);

Core::Settings *m_appSettings = nullptr;
QWebEngineProfile *m_webProfile = nullptr;
};

} // namespace Browser
} // namespace Zeal

#endif // ZEAL_BROWSER_SETTINGS_H
42 changes: 2 additions & 40 deletions src/libs/browser/webpage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,33 +32,12 @@
#include <QFileInfo>
#include <QMessageBox>
#include <QPushButton>
#include <QWebEngineScript>
#include <QWebEngineScriptCollection>

namespace {
constexpr char DarkModeCssUrl[] = "qrc:///browser/assets/css/darkmode.css";
constexpr char HighlightOnNavigateCssUrl[] = "qrc:///browser/assets/css/highlight.css";
}

using namespace Zeal::Browser;

WebPage::WebPage(QWebEngineProfile *profile, QObject *parent)
: QWebEnginePage (profile, parent)
WebPage::WebPage(QObject *parent)
: QWebEnginePage(parent)
{
auto settings = Core::Application::instance()->settings();

if (settings->darkModeEnabled) {
insertCssWithJS(DarkModeCssUrl);
setBackgroundColor(QColor::fromRgb(26, 26, 26));
}

if (settings->highlightOnNavigateEnabled) {
insertCssWithJS(HighlightOnNavigateCssUrl);
}

if (QFileInfo::exists(settings->customCssFile)) {
insertCssWithJS(settings->customCssFile);
}
}

bool WebPage::acceptNavigationRequest(const QUrl &url, QWebEnginePage::NavigationType type, bool isMainFrame)
Expand Down Expand Up @@ -123,20 +102,3 @@ bool WebPage::acceptNavigationRequest(const QUrl &url, QWebEnginePage::Navigatio

return false;
}

void WebPage::insertCssWithJS(const QString &cssUrl)
{
QString cssInjectCode = QLatin1String(
"var head = document.getElementsByTagName('head')[0];"
"var link = document.createElement('link');"
"link.rel = 'stylesheet';"
"link.type = 'text/css';"
"link.href = '%1';"
"link.media = 'all';"
"head.appendChild(link);");

QWebEngineScript injectCssScript;
injectCssScript.setSourceCode(cssInjectCode.arg(cssUrl));
injectCssScript.setInjectionPoint(QWebEngineScript::DocumentReady);
scripts().insert(injectCssScript);
}
5 changes: 1 addition & 4 deletions src/libs/browser/webpage.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,13 +34,10 @@ class WebPage final : public QWebEnginePage
Q_OBJECT
Q_DISABLE_COPY(WebPage)
public:
WebPage(QWebEngineProfile *profile, QObject *parent = nullptr);
explicit WebPage(QObject *parent = nullptr);

protected:
bool acceptNavigationRequest(const QUrl &url, NavigationType type, bool isMainFrame) override;

private:
void insertCssWithJS(const QString &cssUrl);
};

} // namespace Browser
Expand Down
7 changes: 1 addition & 6 deletions src/libs/browser/webview.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@

#include "webview.h"

#include "urlrequestinterceptor.h"
#include "webcontrol.h"
#include "webpage.h"

Expand All @@ -49,11 +48,7 @@ using namespace Zeal::Browser;
WebView::WebView(QWidget *parent)
: QWebEngineView(parent)
{
UrlRequestInterceptor *requestInterceptor = new UrlRequestInterceptor(this);
QWebEngineProfile *profile = new QWebEngineProfile(this);
profile->setUrlRequestInterceptor(requestInterceptor);
QWebEnginePage *p = new WebPage(profile, this);
setPage(p);
setPage(new WebPage(this));
setZoomLevel(defaultZoomLevel());

QApplication::instance()->installEventFilter(this);
Expand Down
8 changes: 4 additions & 4 deletions src/libs/ui/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
#include "sidebarviewprovider.h"
#include <qxtglobalshortcut/qxtglobalshortcut.h>

#include <browser/settings.h>
#include <browser/webbridge.h>
#include <browser/webcontrol.h>
#include <core/application.h>
Expand All @@ -47,7 +48,6 @@
#include <QShortcut>
#include <QSystemTrayIcon>
#include <QTabBar>
#include <QWebEngineSettings>

using namespace Zeal;
using namespace Zeal::WidgetUi;
Expand Down Expand Up @@ -193,6 +193,9 @@ MainWindow::MainWindow(Core::Application *app, QWidget *parent)
ui->splitter->insertWidget(0, sb);
ui->splitter->restoreState(m_settings->verticalSplitterGeometry);

// Setup web settings.
auto webSettings = new Browser::Settings(m_settings, this);

// Setup web bridge.
m_webBridge = new Browser::WebBridge(this);
connect(m_webBridge, &Browser::WebBridge::actionTriggered, this, [this](const QString &action) {
Expand Down Expand Up @@ -524,9 +527,6 @@ void MainWindow::applySettings()
createTrayIcon();
else
removeTrayIcon();

QWebEngineSettings::globalSettings()->setAttribute(QWebEngineSettings::ScrollAnimatorEnabled,
m_settings->isSmoothScrollingEnabled);
}

void MainWindow::toggleWindow()
Expand Down

0 comments on commit e477c4a

Please sign in to comment.