diff --git a/qutebrowser/app.py b/qutebrowser/app.py index eae915e34..4733771ac 100644 --- a/qutebrowser/app.py +++ b/qutebrowser/app.py @@ -394,7 +394,7 @@ class Application(QApplication): status.prompt.prompter.ask_question, Qt.DirectConnection) # config - self.config.style_changed.connect(style.invalidate_caches) + self.config.style_changed.connect(style.get_stylesheet.cache_clear) for obj in (tabs, completion, self.mainwindow, self.cmd_history, websettings, kp[utypes.KeyMode.normal], self.modeman, status, status.txt): diff --git a/qutebrowser/config/style.py b/qutebrowser/config/style.py index 29c52546a..62d84ef03 100644 --- a/qutebrowser/config/style.py +++ b/qutebrowser/config/style.py @@ -17,12 +17,7 @@ # You should have received a copy of the GNU General Public License # along with qutebrowser. If not, see . -"""Utilities related to the look&feel of qutebrowser. - -Module attributes: - _colordict: The global cached ColorDict. - _fontdict: The global cached FontDict. -""" +"""Utilities related to the look&feel of qutebrowser.""" import functools @@ -32,10 +27,7 @@ from qutebrowser.config import config from qutebrowser.utils import log, utils -_colordict = None -_fontdict = None - - +@functools.lru_cache(maxsize=16) def get_stylesheet(template): """Format a stylesheet based on a template. @@ -45,12 +37,9 @@ def get_stylesheet(template): Return: The formatted template as string. """ - global _colordict, _fontdict - if _colordict is None: - _colordict = ColorDict(config.section('colors')) - if _fontdict is None: - _fontdict = FontDict(config.section('fonts')) - return template.strip().format(color=_colordict, font=_fontdict, + colordict = ColorDict(config.section('colors')) + fontdict = FontDict(config.section('fonts')) + return template.strip().format(color=colordict, font=fontdict, config=config.instance()) @@ -78,15 +67,6 @@ def _update_stylesheet(obj, _section, _option): obj.setStyleSheet(get_stylesheet(obj.STYLESHEET)) -def invalidate_caches(section, _option): - """Invalidate cached dicts.""" - global _colordict, _fontdict - if section == 'colors': - _colordict = None - elif section == 'fonts': - _fontdict = None - - class ColorDict(dict): """A dict aimed at Qt stylesheet colors.""" diff --git a/qutebrowser/utils/debug.py b/qutebrowser/utils/debug.py index 272a03575..bae274532 100644 --- a/qutebrowser/utils/debug.py +++ b/qutebrowser/utils/debug.py @@ -29,7 +29,7 @@ from PyQt5.QtCore import pyqtRemoveInputHook, QEvent, QCoreApplication from qutebrowser.utils import log, utils from qutebrowser.commands import cmdutils -from qutebrowser.config import config +from qutebrowser.config import config, style @cmdutils.register(debug=True, name='debug-set-trace') @@ -87,9 +87,11 @@ def debug_all_objects(): @cmdutils.register(debug=True) def debug_cache_stats(): - """Print config LRU cache stats.""" - info = config.instance().get.cache_info() - log.misc.debug(info) + """Print LRU cache stats.""" + config_info = config.instance().get.cache_info() + style_info = style.get_stylesheet.cache_info() + log.misc.debug('config: {}'.format(config_info)) + log.misc.debug('style: {}'.format(style_info)) def log_events(klass):