2011-09-28 09:16:26 -04:00
|
|
|
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
|
2011-02-13 11:40:20 -05:00
|
|
|
|
|
|
|
const Signals = imports.signals;
|
2011-02-28 12:22:12 -05:00
|
|
|
const Clutter = imports.gi.Clutter;
|
|
|
|
const Params = imports.misc.params;
|
2011-02-13 11:40:20 -05:00
|
|
|
|
2017-07-18 13:47:27 -04:00
|
|
|
var DEFAULT_LIMIT = 512;
|
2011-02-13 11:40:20 -05:00
|
|
|
|
2017-10-30 21:19:44 -04:00
|
|
|
var HistoryManager = class {
|
|
|
|
constructor(params) {
|
2011-02-28 12:22:12 -05:00
|
|
|
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);
|
2019-01-29 19:18:24 -05:00
|
|
|
global.settings.connect(`changed::${this._key}`,
|
2017-12-01 19:27:35 -05:00
|
|
|
this._historyChanged.bind(this));
|
2011-02-28 12:22:12 -05:00
|
|
|
|
|
|
|
} else {
|
|
|
|
this._history = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
this._entry = params.entry;
|
|
|
|
|
|
|
|
if (this._entry) {
|
2019-09-12 11:26:08 -04:00
|
|
|
this._entry.connect('key-press-event',
|
2017-12-01 19:27:35 -05:00
|
|
|
this._onEntryKeyPress.bind(this));
|
2011-02-28 12:22:12 -05:00
|
|
|
}
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2011-02-13 11:40:20 -05:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_historyChanged() {
|
2011-02-13 11:40:20 -05:00
|
|
|
this._history = global.settings.get_strv(this._key);
|
|
|
|
this._historyIndex = this._history.length;
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2011-02-13 11:40:20 -05:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_setPrevItem(text) {
|
2011-02-28 12:22:12 -05:00
|
|
|
if (this._historyIndex <= 0)
|
2012-09-17 14:50:27 -04:00
|
|
|
return false;
|
2011-02-28 12:22:12 -05:00
|
|
|
|
|
|
|
if (text)
|
|
|
|
this._history[this._historyIndex] = text;
|
|
|
|
this._historyIndex--;
|
2012-09-17 14:50:27 -04:00
|
|
|
this._indexChanged();
|
|
|
|
return true;
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2011-02-13 11:40:20 -05:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_setNextItem(text) {
|
2011-02-28 12:22:12 -05:00
|
|
|
if (this._historyIndex >= this._history.length)
|
2012-09-17 14:50:27 -04:00
|
|
|
return false;
|
2011-02-28 12:22:12 -05:00
|
|
|
|
|
|
|
if (text)
|
|
|
|
this._history[this._historyIndex] = text;
|
|
|
|
this._historyIndex++;
|
2012-09-17 14:50:27 -04:00
|
|
|
this._indexChanged();
|
|
|
|
return true;
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2011-02-13 11:40:20 -05:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
lastItem() {
|
2011-02-28 12:22:12 -05:00
|
|
|
if (this._historyIndex != this._history.length) {
|
|
|
|
this._historyIndex = this._history.length;
|
|
|
|
this._indexChanged();
|
|
|
|
}
|
|
|
|
|
2019-01-28 20:27:05 -05:00
|
|
|
return this._historyIndex ? this._history[this._historyIndex - 1] : null;
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2011-02-13 11:40:20 -05:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
addItem(input) {
|
2011-02-13 11:40:20 -05:00
|
|
|
if (this._history.length == 0 ||
|
|
|
|
this._history[this._history.length - 1] != input) {
|
|
|
|
|
history: Stop saving non-consecutive duplicate entries
Whenever a command runs in the run dialog, it
will be added to the history unless it is
already the last entry. This does not apply
for entries that are not consecutive, which can
result in long chains of commands which
alternate, e.g. lg, r, lg, r, lg, r. Not only is
this wasteful in terms of space, but also
inconsistent with how history works elsewhere,
e.g. in the shell.
Therefore, remove entries in the history that are
equal to the one that will be added to the end of
of the history when the entry already exists.
https://gitlab.gnome.org/GNOME/gnome-shell/issues/524
2018-09-22 11:43:22 -04:00
|
|
|
this._history = this._history.filter(entry => entry != input);
|
2011-02-13 11:40:20 -05:00
|
|
|
this._history.push(input);
|
|
|
|
this._save();
|
2011-04-27 09:51:53 -04:00
|
|
|
}
|
|
|
|
this._historyIndex = this._history.length;
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2011-02-13 11:40:20 -05:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_onEntryKeyPress(entry, event) {
|
2011-02-28 12:22:12 -05:00
|
|
|
let symbol = event.get_key_symbol();
|
|
|
|
if (symbol == Clutter.KEY_Up) {
|
2012-09-17 14:50:27 -04:00
|
|
|
return this._setPrevItem(entry.get_text());
|
2011-02-28 12:22:12 -05:00
|
|
|
} else if (symbol == Clutter.KEY_Down) {
|
2012-09-17 14:50:27 -04:00
|
|
|
return this._setNextItem(entry.get_text());
|
2011-02-28 12:22:12 -05:00
|
|
|
}
|
2013-11-29 13:17:34 -05:00
|
|
|
return Clutter.EVENT_PROPAGATE;
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2011-02-28 12:22:12 -05:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_indexChanged() {
|
2011-02-13 11:40:20 -05:00
|
|
|
let current = this._history[this._historyIndex] || '';
|
|
|
|
this.emit('changed', current);
|
|
|
|
|
2011-02-28 12:22:12 -05:00
|
|
|
if (this._entry)
|
|
|
|
this._entry.set_text(current);
|
2017-10-30 21:19:44 -04:00
|
|
|
}
|
2011-02-13 11:40:20 -05:00
|
|
|
|
2017-10-30 20:03:21 -04:00
|
|
|
_save() {
|
2011-02-13 11:40:20 -05:00
|
|
|
if (this._history.length > this._limit)
|
2011-02-28 12:22:12 -05:00
|
|
|
this._history.splice(0, this._history.length - this._limit);
|
|
|
|
|
|
|
|
if (this._key)
|
|
|
|
global.settings.set_strv(this._key, this._history);
|
2011-02-13 11:40:20 -05:00
|
|
|
}
|
2017-10-30 21:19:44 -04:00
|
|
|
};
|
2011-02-13 11:40:20 -05:00
|
|
|
Signals.addSignalMethods(HistoryManager.prototype);
|