-
Notifications
You must be signed in to change notification settings - Fork 7
/
pinned_items_manager.js
85 lines (66 loc) · 1.99 KB
/
pinned_items_manager.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
const St = imports.gi.St;
const Lang = imports.lang;
const Signals = imports.signals;
const ExtensionUtils = imports.misc.extensionUtils;
const Me = ExtensionUtils.getCurrentExtension();
const Utils = Me.imports.utils;
const PrefsKeys = Me.imports.prefs_keys;
const CHANGE_TYPE = {
ADDED: 0,
REMOVED: 1
};
const PinnedItemsManager = new Lang.Class({
Name: 'PinnedItemsManager',
_init: function() {
this._items = Utils.SETTINGS.get_strv(PrefsKeys.PINNED_ITEMS_KEY);
},
is_index_exists: function(index) {
return this._items[index] !== undefined;
},
get_index_by_text: function(text) {
return this._items.indexOf(text);
},
add: function(text) {
if(!text) return false;
let index = this._items.push(text);
Utils.SETTINGS.set_strv(PrefsKeys.PINNED_ITEMS_KEY, this._items);
this.emit('changed', CHANGE_TYPE.ADDED);
return index;
},
remove: function(index) {
if(!this.is_index_exists(index)) return false;
this._items.splice(index, 1);
Utils.SETTINGS.set_strv(PrefsKeys.PINNED_ITEMS_KEY, this._items);
this.emit('changed', CHANGE_TYPE.REMOVED);
return true;
},
activate: function(index) {
if(!this.is_index_exists(index)) return false;
let clipboard = St.Clipboard.get_default();
clipboard.set_text(St.ClipboardType.CLIPBOARD, this._items[index]);
return true;
},
list_items: function() {
return this._items.slice();
},
destroy: function() {
delete this._items;
this.emit('destroy');
},
get length() {
return this._items.length;
}
});
Signals.addSignalMethods(PinnedItemsManager.prototype);
let MANAGER = null;
function get_manager() {
if(MANAGER === null) {
MANAGER = new PinnedItemsManager();
MANAGER.connect('destroy',
Lang.bind(this, function() {
MANAGER = null;
})
);
}
return MANAGER;
}