From b9d87ba86f08019f3a802708a84c273c87f1be9b Mon Sep 17 00:00:00 2001 From: Juraj Oravec Date: Thu, 20 Jun 2019 21:58:52 +0200 Subject: [PATCH] Save reader`s settings closes #4 closes #7 Signed-off-by: Juraj Oravec --- readability/JsObject.py | 59 +++++++++++++++++++++++++++++++++++++ readability/__init__.py | 9 ++++++ readability/data/Toolbar.js | 31 ++++++++++++++----- 3 files changed, 92 insertions(+), 7 deletions(-) create mode 100644 readability/JsObject.py diff --git a/readability/JsObject.py b/readability/JsObject.py new file mode 100644 index 0000000..29d5c99 --- /dev/null +++ b/readability/JsObject.py @@ -0,0 +1,59 @@ +# Readability - plugin for Falkon +# Copyright (C) 2019 Juraj Oravec +# +# This program 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. +# +# This program 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 this program. If not, see . + +import Falkon +from PySide2 import QtCore, QtGui + + +class JsObject(QtCore.QObject): + config = None + + configChanged = QtCore.Signal() + + def __init__(self, config, parent=None): + super().__init__(parent) + + self.config = config + + @QtCore.Slot(str) + def setColorTheme(self, colorTheme): + if colorTheme not in ("light", "sepia", "dark"): + return + + if self.config["colorTheme"] != colorTheme: + self.config["colorTheme"] = colorTheme + + self.configChanged.emit() + + @QtCore.Slot(str) + def setFont(self, font): + if font not in ("sans-serif", "serif"): + return + + if self.config["font"] != font: + self.config["font"] = font + + self.configChanged.emit() + + @QtCore.Slot(int) + def setFontSize(self, fontSize): + if fontSize < 1 and fontSize > 9: + return + + if self.config["fontSize"] != fontSize: + self.config["fontSize"] = fontSize + + self.configChanged.emit() diff --git a/readability/__init__.py b/readability/__init__.py index a95af43..7fb8d5b 100644 --- a/readability/__init__.py +++ b/readability/__init__.py @@ -20,12 +20,14 @@ from PySide2 import QtCore, QtGui from readability.settingsDialog import SettingsDialog from readability.button import Button from readability.str2bool import str2bool +from readability.JsObject import JsObject class Readability(Falkon.PluginInterface, QtCore.QObject): config = None view = None buttons = None + jsObject = None style = "" scriptReadability = "" @@ -45,6 +47,11 @@ class Readability(Falkon.PluginInterface, QtCore.QObject): self.loadSettings() self.onConfigUpdate() + self.jsObject = JsObject(self.config, self) + self.jsObject.configChanged.connect(self.saveSettings) + + Falkon.ExternalJsObject.registerExtraObject("readability", self.jsObject) + plugins = Falkon.MainApplication.instance().plugins() plugins.mainWindowCreated.connect(self.onMainWindowCreated) @@ -58,6 +65,8 @@ class Readability(Falkon.PluginInterface, QtCore.QObject): for window in Falkon.MainApplication.instance().windows(): self.onMainWindowDeleted(window) + Falkon.ExternalJsObject.unregisterExtraObject(self.jsObject) + def testPlugin(self): return True diff --git a/readability/data/Toolbar.js b/readability/data/Toolbar.js index cfd396b..8451062 100644 --- a/readability/data/Toolbar.js +++ b/readability/data/Toolbar.js @@ -34,16 +34,31 @@ function addListeners(){ document.getElementById('style-button').addEventListener("click", toolbarService); var lightButton = document.getElementById('light-button'); - lightButton.addEventListener("click", function(){switchToLight();}); + lightButton.addEventListener("click", function(){ + switchToLight(); + external.extra.readability.setColorTheme('light'); + }); var sepiaButton = document.getElementById('sepia-button'); - sepiaButton.addEventListener("click", function(){switchToSepia();}); + sepiaButton.addEventListener("click", function(){ + switchToSepia(); + external.extra.readability.setColorTheme('sepia'); + }); var darkButton = document.getElementById('dark-button'); - darkButton.addEventListener("click", function(){switchToDark();}); + darkButton.addEventListener("click", function(){ + switchToDark(); + external.extra.readability.setColorTheme('dark'); + }); var serifButton = document.getElementById('serif-button'); - serifButton.addEventListener("click", function(){switchToSerif();}); + serifButton.addEventListener("click", function(){ + switchToSerif(); + external.extra.readability.setFont('serif'); + }); var sansSerifButton = document.getElementById('sans-serif-button'); - sansSerifButton.addEventListener("click", function(){switchToSansSerif();}); + sansSerifButton.addEventListener("click", function(){ + switchToSansSerif(); + external.extra.readability.setFont('sans-serif'); + }); var fontSizePlusButton = document.getElementById('font-size-plus'); fontSizePlusButton.addEventListener("click", fontSizePlus); @@ -120,6 +135,7 @@ function fontSizePlus(){ var size = container.classList[i].substr(-1, 1); if(parseInt(size) < 9){ incrementedSize = parseInt(size) + 1; + external.extra.readability.setFontSize(incrementedSize); } else return; container.classList.remove(container.classList[i]); container.classList.add('font-size' + incrementedSize); @@ -135,10 +151,11 @@ function fontSizeMinus(){ if(container.classList[i].indexOf('font-size') > -1){ var size = container.classList[i].substr(-1, 1); if(parseInt(size) > 1){ - incrementedSize = parseInt(size) - 1; + decrementedSize = parseInt(size) - 1; + external.extra.readability.setFontSize(decrementedSize); } else return; container.classList.remove(container.classList[i]); - container.classList.add('font-size' + incrementedSize); + container.classList.add('font-size' + decrementedSize); return; } }