-
Notifications
You must be signed in to change notification settings - Fork 39
/
status_bar.js
199 lines (161 loc) · 5.19 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
const St = imports.gi.St;
const Gio = imports.gi.Gio;
const Lang = imports.lang;
const Animation = imports.ui.animation;
const Tweener = imports.ui.tweener;
const Mainloop = imports.mainloop;
const Params = imports.misc.params;
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 = 60;
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: 'translator-statusbar-box',
visible: false
});
this._message_label = new St.Label();
this._message_label.get_clutter_text().use_markup = true;
let spinner_icon = Gio.File.new_for_uri('resource:///org/gnome/shell/theme/process-working.svg');
this._spinner = new Animation.AnimatedIcon(
spinner_icon,
16
);
this.actor.add(this._spinner.actor);
this.actor.add(this._message_label);
this._messages = {};
},
_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._message_label.get_clutter_text().set_markup(message.markup);
this.actor.opacity = 0;
this.actor.show();
if(message.has_spinner) {
this._spinner.actor.show();
this._spinner.play();
}
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(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) {
this.hide_message(id);
delete this._messages[id];
this.show_last();
},
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 = {};
},
destroy: function() {
this.clear();
this.actor.destroy();
}
});