-
Notifications
You must be signed in to change notification settings - Fork 795
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(browser): introduce centralized web settings management
Additionally fixes a number of regressions from Qt WebEngine migration.
- Loading branch information
Showing
7 changed files
with
166 additions
and
54 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters