This repository has been archived by the owner on Mar 14, 2022. It is now read-only.
forked from MichalW/gnome-bluetooth-battery-indicator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindicator.js
84 lines (66 loc) · 2.44 KB
/
indicator.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
const PanelMenu = imports.ui.panelMenu;
const PopupMenu = imports.ui.popupMenu;
const Util = imports.misc.util;
const { GObject, St, Clutter } = imports.gi;
const UUID = "[email protected]";
var IndicatorController = GObject.registerClass(
class Indicator extends PanelMenu.Button {
_init() {
super._init(0.0, _('Bluetooth battery Indicator'));
this._container = new St.BoxLayout();
this._labels = [];
this._icons = [];
this._prevDevicesSettings = [];
this._addSettingsButton();
}
refresh(devices) {
const devicesSettings = devices.map(({ mac, icon }) => ({ mac, icon }));
if (JSON.stringify(devicesSettings) !== JSON.stringify(this._prevDevicesSettings)) {
this._container.remove_all_children();
this._addBoxes(devices);
}
this._prevDevicesSettings = devicesSettings;
}
_addMenuItem(item) {
this.menu.addMenuItem(item);
}
_addSettingsButton() {
const settings = new PopupMenu.PopupMenuItem(_('Settings'));
settings.connect('activate', () => {
Util.spawn(['gnome-extensions', 'prefs', UUID]);
});
this._addMenuItem(settings);
}
_addBoxes(devices) {
devices.forEach((device, index) => {
const box = this._getBox(device, index);
this._container.add_child(box);
});
this.add_child(this._container);
}
_getBox(device, index) {
const box = new St.BoxLayout({ style_class: 'panel-status-menu-box' });
this._icons[index] = this._getBoxIcon(device);
this._labels[index] = this._getBoxLabel();
box.add_child(this._icons[index]);
box.add_child(this._labels[index]);
return box;
}
_getBoxLabel() {
return new St.Label({
y_align: Clutter.ActorAlign.CENTER
});
}
_getBoxIcon(device) {
return new St.Icon({
icon_name: device.icon || 'battery-full-symbolic',
style_class: 'system-status-icon',
});
}
setPercentLabel(percent, index) {
if (this._labels[index]) {
this._labels[index].text = (percent || '').trim();
}
}
}
);