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
This commit is contained in:
Jasper St. Pierre 2011-02-28 12:22:12 -05:00
parent 8ce97961b4
commit 75ae209653
3 changed files with 70 additions and 53 deletions

View File

@ -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;
_init: function(params) {
params = Params.parse(params, { gsettingsKey: null,
limit: DEFAULT_LIMIT,
entry: null });
global.settings.connect('changed::' + settings_key,
Lang.bind(this, this._historyChanged));
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);
if (this._entry)
this._entry.set_text(current);
return current;
},
_setHistory: function(index, text) {
this._historyIndex = Math.max(this._historyIndex, 0);
this._historyIndex = Math.min(this._historyIndex, this._history.length);
if (text)
this._history[index] = text;
},
_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);

View File

@ -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() {

View File

@ -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);