[gnome-shell] history: Fix HistoryManager



commit 75ae20965368d2dd423150cac71032e71a9b8865
Author: Jasper St. Pierre <jstpierre mecheye net>
Date:   Mon Feb 28 12:22:12 2011 -0500

    history: Fix HistoryManager
    
    Make GSettings support optional, refactor text entry handling,
    fix some off-by-one bugs in the management itself, use Params
    for parsing, fix other typos and bugs.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=642793

 js/misc/history.js    |   89 ++++++++++++++++++++++++++++++++++++-------------
 js/ui/lookingGlass.js |   19 +---------
 js/ui/runDialog.js    |   15 +-------
 3 files changed, 70 insertions(+), 53 deletions(-)
---
diff --git a/js/misc/history.js b/js/misc/history.js
index 69f27f9..24d1b51 100644
--- a/js/misc/history.js
+++ b/js/misc/history.js
@@ -2,22 +2,40 @@
 
 const Lang = imports.lang;
 const Signals = imports.signals;
+const Clutter = imports.gi.Clutter;
+const Params = imports.misc.params;
 
 const DEFAULT_LIMIT = 512;
 
-function HistoryManager(settings_key) {
-    this._init(settings_key);
+function HistoryManager(params) {
+    this._init(params);
 }
 
 HistoryManager.prototype = {
-    _init: function(settings_key, limit) {
-        this._limit = limit || DEFAULT_LIMIT;
-        this._key = settings_key;
-        this._history = global.settings.get_strv(settings_key);
-        this._historyIndex = -1;
-
-        global.settings.connect('changed::' + settings_key,
-                                Lang.bind(this, this._historyChanged));
+    _init: function(params) {
+        params = Params.parse(params, { gsettingsKey: null,
+                                        limit: DEFAULT_LIMIT,
+                                        entry: null });
+
+        this._key = params.gsettingsKey;
+        this._limit = params.limit;
+
+        this._historyIndex = 0;
+        if (this._key) {
+            this._history = global.settings.get_strv(this._key);
+            global.settings.connect('changed::' + this._key,
+                                    Lang.bind(this, this._historyChanged));
+
+        } else {
+            this._history = [];
+        }
+
+        this._entry = params.entry;
+
+        if (this._entry) {
+            this._entry.connect('key-press-event', 
+                                Lang.bind(this, this._onEntryKeyPress));
+        }
     },
 
     _historyChanged: function() {
@@ -26,18 +44,32 @@ HistoryManager.prototype = {
     },
 
     prevItem: function(text) {
-        this._setHistory(this._historyIndex--, text);
+        if (this._historyIndex <= 0)
+            return text;
+
+        if (text)
+            this._history[this._historyIndex] = text;
+        this._historyIndex--;
         return this._indexChanged();
     },
 
     nextItem: function(text) {
-        this._setHistory(this._historyIndex++, text);
+        if (this._historyIndex >= this._history.length)
+            return text;
+
+        if (text)
+            this._history[this._historyIndex] = text;
+        this._historyIndex++;
         return this._indexChanged();
     },
 
     lastItem: function() {
-        this._historyIndex = this._history.length;
-        return this._indexChanged();
+        if (this._historyIndex != this._history.length) {
+            this._historyIndex = this._history.length;
+            this._indexChanged();
+        }
+
+        return this._historyIndex[this._history.length];
     },
 
     addItem: function(input) {
@@ -45,28 +77,39 @@ HistoryManager.prototype = {
             this._history[this._history.length - 1] != input) {
 
             this._history.push(input);
+            this._historyIndex = this._history.length;
             this._save();
         }   
     },
 
+    _onEntryKeyPress: function(entry, event) {
+        let symbol = event.get_key_symbol();
+        if (symbol == Clutter.KEY_Up) {
+            this.prevItem(entry.get_text());
+            return true;
+        } else if (symbol == Clutter.KEY_Down) {
+            this.nextItem(entry.get_text());
+            return true;
+        }
+        return false;
+    },
+
     _indexChanged: function() {
         let current = this._history[this._historyIndex] || '';
         this.emit('changed', current);
-        return current;
-    },
 
-    _setHistory: function(index, text) {
-        this._historyIndex = Math.max(this._historyIndex, 0);
-        this._historyIndex = Math.min(this._historyIndex, this._history.length);
+        if (this._entry)
+            this._entry.set_text(current);
 
-        if (text)
-            this._history[index] = text;
+        return current;
     },
 
     _save: function() {
         if (this._history.length > this._limit)
-            this._history.splice(0, this._history.length - this._key);
-        global.settings.set_strv(this._key, this._history);
+            this._history.splice(0, this._history.length - this._limit);
+
+        if (this._key)
+            global.settings.set_strv(this._key, this._history);
     }
 };
 Signals.addSignalMethods(HistoryManager.prototype);
diff --git a/js/ui/lookingGlass.js b/js/ui/lookingGlass.js
index 8caf657..ede95a1 100644
--- a/js/ui/lookingGlass.js
+++ b/js/ui/lookingGlass.js
@@ -778,23 +778,8 @@ LookingGlass.prototype = {
             return true;
         }));
 
-        this._history = new History.HistoryManager(HISTORY_KEY);
-        this._history.connect('changed', Lang.bind(this, function(history, text) {
-            this._entry.text = text;
-        }));
-
-        this._entry.clutter_text.connect('key-press-event', Lang.bind(this, function(o, e) {
-            let symbol = e.get_key_symbol();
-            if (symbol == Clutter.Up) {
-                this._history.prevItem(o.get_text());
-                return true;
-            } else if (symbol == Clutter.Down) {
-                this._history.nextItem(o.get_text());
-                return true;
-            } else {
-                return false;
-            }
-        }));
+        this._history = new History.HistoryManager({ gsettingsKey: HISTORY_KEY, 
+                                                     entry: this._entry.clutter_text });
     },
 
     _updateFont: function() {
diff --git a/js/ui/runDialog.js b/js/ui/runDialog.js
index 219191d..15340d3 100644
--- a/js/ui/runDialog.js
+++ b/js/ui/runDialog.js
@@ -234,21 +234,10 @@ __proto__: ModalDialog.ModalDialog.prototype,
         this._commandCompleter = new CommandCompleter();
         this._group.connect('notify::visible', Lang.bind(this._commandCompleter, this._commandCompleter.update));
 
-        this._history = new History.HistoryManager(HISTORY_KEY);
-        this._history.connect('changed', Lang.bind(this, function(history, text) {
-            this._entryText.set_text(text);
-        }));
-
+        this._history = new History.HistoryManager({ gsettingsKey: HISTORY_KEY,
+                                                     entry: this._entryText });
         this._entryText.connect('key-press-event', Lang.bind(this, function(o, e) {
             let symbol = e.get_key_symbol();
-            if (symbol == Clutter.Down) {
-                this._history.nextItem(o.get_text());
-                return true;
-            }
-            if (symbol == Clutter.Up) {
-                this._history.prevItem(o.get_text());
-                return true;
-            }
             if (symbol == Clutter.Return || symbol == Clutter.KP_Enter) {
                 if (Shell.get_event_state(e) & Clutter.ModifierType.CONTROL_MASK)
                     this._run(o.get_text(), true);



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]