-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathstatus_bar.js
234 lines (190 loc) · 6.1 KB
/
status_bar.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
const St = imports.gi.St;
const Lang = imports.lang;
const Panel = imports.ui.panel;
const Tweener = imports.ui.tweener;
const Mainloop = imports.mainloop;
const Params = imports.misc.params;
const Signals = imports.signals;
const Clutter = imports.gi.Clutter;
const ExtensionUtils = imports.misc.extensionUtils;
const Me = ExtensionUtils.getCurrentExtension();
const Utils = Me.imports.utils;
const MESSAGE_TYPES = {
error: 0,
info: 1,
success: 2
};
const MAX_MESSAGE_LENGTH = 90;
const StatusBarMessage = new Lang.Class({
Name: 'StatusBarMessage',
_init: function(text, timeout, type, has_spinner) {
this._text = text;
this._markup = this._prepare_message(text, type);
this._type = type || MESSAGE_TYPES.info;
this._timeout = timeout || 0;
this._has_spinner = has_spinner || false;
},
_prepare_message: function(message, type) {
message = message.trim();
message = message.slice(0, MAX_MESSAGE_LENGTH);
message = Utils.escape_html(message);
let message_markup = '<span color="%s">%s</span>';
switch(type) {
case MESSAGE_TYPES.error:
message_markup = message_markup.format('red', message);
break;
case MESSAGE_TYPES.info:
message_markup = message_markup.format('grey', message);
break;
case MESSAGE_TYPES.success:
message_markup = message_markup.format('green', message);
break;
default:
message_markup = message_markup.format('grey', message);
}
return message_markup;
},
get text() {
return this._text;
},
get markup() {
return this._markup;
},
get type() {
return this._type;
},
get timeout() {
return this._timeout;
},
get has_spinner() {
return this._has_spinner;
}
});
const StatusBar = new Lang.Class({
Name: 'StatusBar',
_init: function(params) {
this.actor = new St.BoxLayout({
style_class: 'everpad-statusbar-box',
visible: false
});
this._message_label = new St.Label({
reactive: true
});
this._message_label.connect('button-release-event',
Lang.bind(this, function(o, e) {
let button = e.get_button();
if(button === Clutter.BUTTON_PRIMARY) {
this.emit('message-clicked', this._current_message);
}
else if(button === Clutter.BUTTON_SECONDARY) {
this.remove_message(this._current_message.id);
}
})
);
this._message_label.get_clutter_text().use_markup = true;
this._spinner = new Panel.AnimatedIcon(
'process-working.svg',
24
);
this.actor.add(this._spinner.actor);
this.actor.add(this._message_label);
this._is_bloked = false;
this._messages = {};
this._current_message = null;
},
_get_max_id: function() {
let max_id = Math.max.apply(Math, Object.keys(this._messages));
let result = max_id > 0 ? max_id : 0;
return result;
},
_generate_id: function() {
let max_id = this._get_max_id();
let result = max_id > 0 ? (max_id + 1) : 1;
return result;
},
show_message: function(id) {
let message = this._messages[id];
if(message === undefined || !message instanceof StatusBarMessage) return;
this._current_message = message;
this._current_message.id = id;
this._message_label.get_clutter_text().set_markup(message.markup);
this.actor.opacity = 0;
this.actor.show();
if(message.has_spinner) {
this._spinner.actor.show();
}
else {
this._spinner.actor.hide();
}
Tweener.addTween(this.actor, {
time: 0.3,
opacity: 255,
transition: 'easeOutQuad',
onComplete: Lang.bind(this, function() {
let timeout = parseInt(message.timeout, 10);
if(timeout > 0) {
Mainloop.timeout_add(message.timeout,
Lang.bind(this, function() {
this.remove_message(id);
})
);
}
})
});
},
hide_message: function(id) {
if(this._message_label.visible != true) return;
let message = this._messages[id];
if(message === undefined || !message instanceof StatusBarMessage) return;
Tweener.addTween(this.actor, {
time: 0.3,
opacity: 0,
transition: 'easeOutQuad',
onComplete: Lang.bind(this, function() {
this.actor.hide();
})
});
},
add_message: function(message, timeout, type, has_spinner) {
if(this._is_bloked) return false;
if(Utils.is_blank(message)) return false;
message = new StatusBarMessage(message, timeout, type, has_spinner);
let id = this._generate_id();
this._messages[id] = message;
this.show_message(id);
return id;
},
remove_message: function(id) {
if(this._is_bloked) return false;
this.hide_message(id);
delete this._messages[id];
this.show_last();
return true;
},
remove_last: function() {
let max_id = this._get_max_id();
if(max_id > 0) this.remove_message(max_id);
},
show_last: function() {
let max_id = this._get_max_id();
if(max_id > 0) this.show_message(max_id);
},
clear: function() {
this.actor.hide();
this._messages = {};
},
block: function() {
this._is_bloked = true;
},
unblock: function() {
this._is_bloked = false;
},
is_empty: function() {
return Object.keys(this._messages).length === 0;
},
destroy: function() {
this.clear();
this.actor.destroy();
}
});
Signals.addSignalMethods(StatusBar.prototype);