-
Notifications
You must be signed in to change notification settings - Fork 39
/
Copy pathextension.js
77 lines (59 loc) · 2.56 KB
/
extension.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
import GLib from 'gi://GLib';
import {Extension, gettext as _} from 'resource:///org/gnome/shell/extensions/extension.js';
import * as Main from 'resource:///org/gnome/shell/ui/main.js';
import * as PopupMenu from 'resource:///org/gnome/shell/ui/popupMenu.js';
import {UPowerController} from './UPowerController.js';
import {IndicatorController} from './indicator.js';
import {SettingsController} from './settings.js';
export default class BluetoothBatteryIndicatorExtension extends Extension {
enable() {
this._controller = new UPowerController(this.dir);
this._settings = new SettingsController(this.getSettings());
this._indicator = new IndicatorController();
Main.panel.addToStatusArea(this.uuid, this._indicator);
this._getRefreshButton();
this._loop = GLib.idle_add(GLib.PRIORITY_DEFAULT_IDLE, this._runLoop.bind(this));
}
_runLoop() {
this._refresh();
const interval = this._settings.getInterval();
this._loop = GLib.timeout_add_seconds(GLib.PRIORITY_DEFAULT, interval * 60, this._runLoop.bind(this));
}
_getRefreshButton() {
const refreshItem = new PopupMenu.PopupMenuItem(_('Refresh'));
refreshItem.connect('activate', () => {
this._refresh();
});
this._indicator._addMenuItem(refreshItem);
}
async _refresh() {
const settingsDevices = this._settings.getDevices();
const settingsHideIndicator = this._settings.getHideIndicator();
const uPowerDevices = await this._controller.getDevices();
const devices = this._mergeDevices(settingsDevices, uPowerDevices);
const devicesToShow = devices.filter((device) => device.isConnected && device.isActive);
this._indicator.refresh(devicesToShow);
this._settings.setDevices(devices);
if (settingsHideIndicator) {
Main.panel.statusArea[this.uuid].visible = !!devices.length;
}
}
_mergeDevices(settingsDevices, uPowerDevices) {
const filterByMac = (mac) => (device) => device.mac === mac;
const newDevices = uPowerDevices.filter((device) => !settingsDevices.some(filterByMac(device.mac)));
return [
...newDevices,
...settingsDevices.map((device) => ({
...device,
...uPowerDevices.find(filterByMac(device.mac)),
})),
];
}
disable() {
GLib.Source.remove(this._loop);
this._controller.destroy();
this._controller = null;
this._indicator.destroy();
this._indicator = null;
}
}