2011-09-28 13:16:26 +00:00
|
|
|
// -*- mode: js; js-indent-level: 4; indent-tabs-mode: nil -*-
|
2011-02-13 16:40:20 +00:00
|
|
|
|
2023-07-10 09:53:00 +00:00
|
|
|
import * as Signals from './signals.js';
|
|
|
|
import Clutter from 'gi://Clutter';
|
|
|
|
import * as Params from './params.js';
|
2011-02-13 16:40:20 +00:00
|
|
|
|
2023-07-10 09:53:00 +00:00
|
|
|
const DEFAULT_LIMIT = 512;
|
2011-02-13 16:40:20 +00:00
|
|
|
|
2023-07-10 09:53:00 +00:00
|
|
|
export class HistoryManager extends Signals.EventEmitter {
|
2017-10-31 01:19:44 +00:00
|
|
|
constructor(params) {
|
2022-07-04 22:30:44 +00:00
|
|
|
super();
|
|
|
|
|
2020-03-29 21:51:13 +00:00
|
|
|
params = Params.parse(params, {
|
|
|
|
gsettingsKey: null,
|
|
|
|
limit: DEFAULT_LIMIT,
|
|
|
|
entry: null,
|
|
|
|
});
|
2011-02-28 17:22:12 +00:00
|
|
|
|
|
|
|
this._key = params.gsettingsKey;
|
|
|
|
this._limit = params.limit;
|
|
|
|
|
|
|
|
this._historyIndex = 0;
|
|
|
|
if (this._key) {
|
|
|
|
this._history = global.settings.get_strv(this._key);
|
2019-01-30 00:18:24 +00:00
|
|
|
global.settings.connect(`changed::${this._key}`,
|
2017-12-02 00:27:35 +00:00
|
|
|
this._historyChanged.bind(this));
|
2011-02-28 17:22:12 +00:00
|
|
|
} else {
|
|
|
|
this._history = [];
|
|
|
|
}
|
|
|
|
|
|
|
|
this._entry = params.entry;
|
|
|
|
|
|
|
|
if (this._entry) {
|
2019-09-12 15:26:08 +00:00
|
|
|
this._entry.connect('key-press-event',
|
2017-12-02 00:27:35 +00:00
|
|
|
this._onEntryKeyPress.bind(this));
|
2011-02-28 17:22:12 +00:00
|
|
|
}
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2011-02-13 16:40:20 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_historyChanged() {
|
2011-02-13 16:40:20 +00:00
|
|
|
this._history = global.settings.get_strv(this._key);
|
|
|
|
this._historyIndex = this._history.length;
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2011-02-13 16:40:20 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_setPrevItem(text) {
|
2011-02-28 17:22:12 +00:00
|
|
|
if (this._historyIndex <= 0)
|
2012-09-17 18:50:27 +00:00
|
|
|
return false;
|
2011-02-28 17:22:12 +00:00
|
|
|
|
|
|
|
if (text)
|
|
|
|
this._history[this._historyIndex] = text;
|
|
|
|
this._historyIndex--;
|
2012-09-17 18:50:27 +00:00
|
|
|
this._indexChanged();
|
|
|
|
return true;
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2011-02-13 16:40:20 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_setNextItem(text) {
|
2011-02-28 17:22:12 +00:00
|
|
|
if (this._historyIndex >= this._history.length)
|
2012-09-17 18:50:27 +00:00
|
|
|
return false;
|
2011-02-28 17:22:12 +00:00
|
|
|
|
|
|
|
if (text)
|
|
|
|
this._history[this._historyIndex] = text;
|
|
|
|
this._historyIndex++;
|
2012-09-17 18:50:27 +00:00
|
|
|
this._indexChanged();
|
|
|
|
return true;
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2011-02-13 16:40:20 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
lastItem() {
|
2021-02-11 22:28:55 +00:00
|
|
|
if (this._historyIndex !== this._history.length) {
|
2011-02-28 17:22:12 +00:00
|
|
|
this._historyIndex = this._history.length;
|
|
|
|
this._indexChanged();
|
|
|
|
}
|
|
|
|
|
2019-01-29 01:27:05 +00:00
|
|
|
return this._historyIndex ? this._history[this._historyIndex - 1] : null;
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2011-02-13 16:40:20 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
addItem(input) {
|
2021-02-08 21:20:42 +00:00
|
|
|
input = input.trim();
|
|
|
|
if (input &&
|
|
|
|
(this._history.length === 0 ||
|
|
|
|
this._history[this._history.length - 1] !== input)) {
|
2021-02-11 22:28:55 +00:00
|
|
|
this._history = this._history.filter(entry => entry !== input);
|
2011-02-13 16:40:20 +00:00
|
|
|
this._history.push(input);
|
|
|
|
this._save();
|
2011-04-27 13:51:53 +00:00
|
|
|
}
|
|
|
|
this._historyIndex = this._history.length;
|
2021-02-08 21:20:42 +00:00
|
|
|
return input; // trimmed
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2011-02-13 16:40:20 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_onEntryKeyPress(entry, event) {
|
2011-02-28 17:22:12 +00:00
|
|
|
let symbol = event.get_key_symbol();
|
2021-02-11 22:28:55 +00:00
|
|
|
if (symbol === Clutter.KEY_Up)
|
2021-02-08 21:20:42 +00:00
|
|
|
return this._setPrevItem(entry.get_text().trim());
|
2021-02-11 22:28:55 +00:00
|
|
|
else if (symbol === Clutter.KEY_Down)
|
2021-02-08 21:20:42 +00:00
|
|
|
return this._setNextItem(entry.get_text().trim());
|
2019-08-20 00:51:42 +00:00
|
|
|
|
2013-11-29 18:17:34 +00:00
|
|
|
return Clutter.EVENT_PROPAGATE;
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2011-02-28 17:22:12 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_indexChanged() {
|
2011-02-13 16:40:20 +00:00
|
|
|
let current = this._history[this._historyIndex] || '';
|
|
|
|
this.emit('changed', current);
|
|
|
|
|
2011-02-28 17:22:12 +00:00
|
|
|
if (this._entry)
|
|
|
|
this._entry.set_text(current);
|
2017-10-31 01:19:44 +00:00
|
|
|
}
|
2011-02-13 16:40:20 +00:00
|
|
|
|
2017-10-31 00:03:21 +00:00
|
|
|
_save() {
|
2011-02-13 16:40:20 +00:00
|
|
|
if (this._history.length > this._limit)
|
2011-02-28 17:22:12 +00:00
|
|
|
this._history.splice(0, this._history.length - this._limit);
|
|
|
|
|
|
|
|
if (this._key)
|
|
|
|
global.settings.set_strv(this._key, this._history);
|
2011-02-13 16:40:20 +00:00
|
|
|
}
|
2023-07-10 09:53:00 +00:00
|
|
|
}
|