-
Notifications
You must be signed in to change notification settings - Fork 12
/
extension.js
95 lines (78 loc) · 2.21 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
import GLib from 'gi://GLib'
import UPower from 'gi://UPowerGlib'
import { Extension } from 'resource:///org/gnome/shell/extensions/extension.js'
import { panel } from 'resource:///org/gnome/shell/ui/main.js'
export default class AutohideBatteryExtension extends Extension {
batteryWatching = null
initTimeout = null
settings = null
settingsWatching = null
disable() {
if (this.settings) {
this.settings.disconnect(this.settingsWatching)
this.settings = null
}
if (this.initTimeout) {
GLib.Source.remove(this.initTimeout)
this.initTimeout = null
}
this.getBattery(proxy => {
proxy.disconnect(this.batteryWatching)
})
this.show()
}
enable() {
this.settings = this.getSettings()
this.settingsWatching = this.settings.connect('changed::hide-on', () => {
this.update()
})
this.getBattery(proxy => {
this.batteryWatching = proxy.connect('g-properties-changed', () => {
this.update()
})
})
this.update()
this.initTimeout = GLib.timeout_add_seconds(
GLib.PRIORITY_DEFAULT,
1,
() => {
this.update()
return GLib.SOURCE_CONTINUE
}
)
}
getBattery(callback) {
let system = panel.statusArea.quickSettings._system
if (system && system._systemItem._powerToggle) {
callback(system._systemItem._powerToggle._proxy, system)
}
}
hide() {
this.getBattery((proxy, icon) => {
icon.hide()
})
}
show() {
this.getBattery((proxy, icon) => {
icon.show()
})
}
update() {
let hideOn = this.settings.get_int('hide-on')
let hideAlways = this.settings.get_boolean('hide-always')
this.getBattery(proxy => {
let isPendingCharge = proxy.State === UPower.DeviceState.PENDING_CHARGE
let isFullyCharged = proxy.State === UPower.DeviceState.FULLY_CHARGED
let isDischarging = proxy.State === UPower.DeviceState.DISCHARGING
if (proxy.Type !== UPower.DeviceKind.BATTERY) {
this.show()
} else if ((hideAlways || !isDischarging) && proxy.Percentage >= hideOn) {
this.hide()
} else if (isFullyCharged || isPendingCharge) {
this.hide()
} else {
this.show()
}
})
}
}