From e9d7097f197a72bae2f7efb95aea101c5e5aed3f Mon Sep 17 00:00:00 2001 From: Wolfgang Rumpler Date: Fri, 3 Aug 2018 17:28:31 +0200 Subject: [PATCH] enhance history cache --- randomwallpaper@iflow.space/elements.js | 82 +++++++++++++++++++++--- randomwallpaper@iflow.space/extension.js | 50 +-------------- 2 files changed, 76 insertions(+), 56 deletions(-) diff --git a/randomwallpaper@iflow.space/elements.js b/randomwallpaper@iflow.space/elements.js index cb22c37..fa22e7c 100644 --- a/randomwallpaper@iflow.space/elements.js +++ b/randomwallpaper@iflow.space/elements.js @@ -246,7 +246,11 @@ var StatusElement = new Lang.Class({ time: 1, transition: 'easeInOutSine', onComplete: function () { - Tweener.addTween(_this, _this.loadingTweenOut); + try { + Tweener.addTween(_this, _this.loadingTweenOut); + } catch (e) { + // swollow (not really important) + } } }; @@ -256,7 +260,11 @@ var StatusElement = new Lang.Class({ transition: 'easeInOutSine', onComplete: function () { if (_this.isLoading) { - Tweener.addTween(_this, _this.loadingTweenIn); + try { + Tweener.addTween(_this, _this.loadingTweenIn); + } catch (e) { + // swollow (not really important) + } } else { return false; } @@ -283,6 +291,13 @@ var HistorySection = new Lang.Class({ Name: 'HistorySection', Extends: PopupMenu.PopupMenuSection, + /** + * Cache HistoryElements for performance of long histories. + */ + _historySectionCache: {}, + + _historyCache: [], + _init: function () { this.parent(); @@ -294,12 +309,63 @@ 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(); - } + updateList: function(history, onEnter, onLeave, onSelect) { + if (this._historyCache.length <= 1) { + this.removeAll(); // remove empty history element + } + + let existingHistoryElements = []; + + for (let i = 1; i < history.length; i++) { + let historyID = history[i].id; + let tmp; + + if (!(historyID in this._historySectionCache)) { + tmp = new HistoryElement(history[i], i); + + tmp.actor.connect('key-focus-in', onEnter); + tmp.actor.connect('key-focus-out', onLeave); + tmp.actor.connect('enter-event', onEnter); + + tmp.connect('activate', onSelect); + this._historySectionCache[historyID] = tmp; + + this.addMenuItem(tmp, i-1); + } else { + tmp = this._historySectionCache[historyID]; + tmp.setIndex(i); + } + + existingHistoryElements.push(historyID); + } + + this._cleanupHistoryCache(existingHistoryElements); + this._historyCache = history; + }, + + _cleanupHistoryCache: function(existingIDs) { + let destroyIDs = Object.keys(this._historySectionCache).filter((i) => existingIDs.indexOf(i) === -1); + + destroyIDs.map(id => { + this._historySectionCache[id].destroy(); + delete this._historySectionCache[id]; + }); + }, + + clear() { + this._cleanupHistoryCache([]); + this.removeAll(); + this.addMenuItem( + new PopupMenu.PopupMenuItem('No recent wallpaper ...', { + activate: false, + hover: false, + style_class: 'rwg-recent-lable', + can_focus: false + }) + ); + + this._historyCache = []; + }, }); diff --git a/randomwallpaper@iflow.space/extension.js b/randomwallpaper@iflow.space/extension.js index d6a9607..5525928 100644 --- a/randomwallpaper@iflow.space/extension.js +++ b/randomwallpaper@iflow.space/extension.js @@ -77,11 +77,6 @@ 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'); @@ -191,37 +186,12 @@ var RandomWallpaperEntry = new Lang.Class({ 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; - - if (!(historyID in this._historySectionCache)) { - tmp = new CustomElements.HistoryElement(history[i], i); - - tmp.actor.connect('key-focus-in', onEnter); - tmp.actor.connect('key-focus-out', onLeave); - tmp.actor.connect('enter-event', onEnter); - - 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(); } @@ -234,27 +204,11 @@ var RandomWallpaperEntry = new Lang.Class({ wallpaperController.setWallpaper(actor.historyEntry.id); } - }, - - _cleanupHistoryCache: function(existingIDs) { - let destroyIDs = Object.keys(this._historySectionCache).filter((i) => existingIDs.indexOf(i) === -1); - - destroyIDs.map(id => { - delete this._historySectionCache[id]; - }); + this.historySection.updateList(history, onEnter, onLeave, onSelect); }, clearHistoryList: function () { - this._cleanupHistoryCache([]); - this.historySection.removeAll(); - - let empty = new PopupMenu.PopupMenuItem('No recent wallpaper ...', { - activate: false, - hover: false, - style_class: 'rwg-recent-lable', - can_focus: false - }); - this.historySection.addMenuItem(empty); + this.historySection.clear(); }, });