From 3852e8394342275116aff26da1b107f87bf6b480 Mon Sep 17 00:00:00 2001 From: Wolfgang Rumpler Date: Mon, 30 Jul 2018 00:29:02 +0200 Subject: [PATCH] first HistoryElement cache implementation --- randomwallpaper@iflow.space/elements.js | 14 +++++++- randomwallpaper@iflow.space/extension.js | 42 +++++++++++++++++++----- randomwallpaper@iflow.space/prefs.js | 1 + 3 files changed, 47 insertions(+), 10 deletions(-) diff --git a/randomwallpaper@iflow.space/elements.js b/randomwallpaper@iflow.space/elements.js index ca539ff..cb22c37 100644 --- a/randomwallpaper@iflow.space/elements.js +++ b/randomwallpaper@iflow.space/elements.js @@ -149,7 +149,12 @@ var HistoryElement = new Lang.Class({ } } }) - } + }, + + setIndex: function(index) { + this.prefixLabel.set_text(String(index)); + }, + }); var CurrentImageElement = new Lang.Class({ @@ -289,5 +294,12 @@ var HistorySection = new Lang.Class({ this.actor.add_actor(this.box); }, + /** + * Clears the history section without destroying the child actors. + */ + clearSection: function() { + this.box.remove_all_children(); + } + }); diff --git a/randomwallpaper@iflow.space/extension.js b/randomwallpaper@iflow.space/extension.js index ff0947c..5efb29d 100644 --- a/randomwallpaper@iflow.space/extension.js +++ b/randomwallpaper@iflow.space/extension.js @@ -77,6 +77,11 @@ var RandomWallpaperEntry = new Lang.Class({ Name: "RandomWallpaperEntry", logger: null, + /** + * Cache HistoryElements for performance of long histories. + */ + _historySectionCache: {}, + _init: function (menuAlignment, nameText) { this.parent(menuAlignment, nameText); this.logger = new LoggerModule.Logger('RWG3', 'RandomWallpaperEntry'); @@ -184,28 +189,38 @@ var RandomWallpaperEntry = new Lang.Class({ setHistoryList: function () { this.setCurrentBackgroundElement(); - this.historySection.removeAll(); - let historyController = wallpaperController.getHistoryController(); let history = historyController.history; + this.historySection.clearSection(); if (history.length <= 1) { this.clearHistoryList(); return; } + let existingHistoryElements = []; for (let i = 1; i < history.length; i++) { - let historyid = history[i].id; - let tmp = new CustomElements.HistoryElement(history[i], i); + let historyID = history[i].id; + let tmp; - tmp.actor.connect('key-focus-in', onEnter); - tmp.actor.connect('key-focus-out', onLeave); - tmp.actor.connect('enter-event', onEnter); + if (!(historyID in this._historySectionCache)) { + tmp = new CustomElements.HistoryElement(history[i], i); - tmp.connect('activate', onSelect); + tmp.actor.connect('key-focus-in', onEnter); + tmp.actor.connect('key-focus-out', onLeave); + tmp.actor.connect('enter-event', onEnter); - this.historySection.addMenuItem(tmp); + tmp.connect('activate', onSelect); + this._historySectionCache[historyID] = tmp; + } else { + tmp = this._historySectionCache[historyID]; + tmp.setIndex(i); + } + + existingHistoryElements.push(historyID) + this.historySection.addMenuItem(tmp, i-1); } + this._cleanupHistoryCache(existingHistoryElements); function onLeave(actor) { wallpaperController.resetWallpaper(); @@ -221,7 +236,16 @@ var RandomWallpaperEntry = new Lang.Class({ }, + _cleanupHistoryCache: function(existingIDs) { + let destroyIDs = Object.keys(this._historySectionCache).filter((i) => existingIDs.indexOf(i) === -1); + + destroyIDs.map(id => { + delete this._historySectionCache[id]; + }); + }, + clearHistoryList: function () { + this._cleanupHistoryCache([]); this.historySection.removeAll(); let empty = new PopupMenu.PopupMenuItem('No recent wallpaper ...', { diff --git a/randomwallpaper@iflow.space/prefs.js b/randomwallpaper@iflow.space/prefs.js index 54d577a..7466d5a 100644 --- a/randomwallpaper@iflow.space/prefs.js +++ b/randomwallpaper@iflow.space/prefs.js @@ -249,6 +249,7 @@ var RandomWallpaperSettings = new Lang.Class({ let text = newWallpaperButton.get_label(); newWallpaperButton.set_label("Loading ..."); + this._wallpaperController.update(); this._wallpaperController.fetchNewWallpaper(()=>{ this._wallpaperController.update(); newWallpaperButton.set_label(text);