This repository has been archived by the owner on Sep 15, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extension.js
180 lines (153 loc) · 6.05 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
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
/*
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
Copyright (C) 2017 Yannick Tanner
Copyright (C) 2022 Raphaël Rochet
**/
const St = imports.gi.St;
const GObject = imports.gi.GObject;
const Clutter = imports.gi.Clutter;
const GLib = imports.gi.GLib;
const Gio = imports.gi.Gio;
const UPower = imports.gi.UPowerGlib;
const Main = imports.ui.main;
const Panel = imports.ui.panel;
const CircularBatteryIndicator = GObject.registerClass(
{
_pT: null, // the PowerToggle instance
_old_qs_icon: null, // ref to icon in quickSettings we replace (to put it back later)
_indic_box: null, // ref to indicator container (contains battery icon and percentage text)
_old_indic_icon: null, // ref to indicator icon we replace (to put it back later)
_qs_drawing: null, // drawing replacement for QuickSettings panel
_indic_drawing: null, // drawing replacement for indicator panel
_percentage: null, // % full
_charging: null, // is battery charging
_idle: null, // is plugged but not charging
// Various event Id we shall track to disable later
_TimeoutId: null,
_qs_repaintId: null,
_indic_repaintId: null,
_powerProxyId: null,
},
class CircularBatteryIndicator extends GObject.Object {
_init() {
// Prepare drawing areas
this._indic_drawing = new St.DrawingArea({ y_align: Clutter.ActorAlign.CENTER });
this._indic_drawing.set_width(Panel.PANEL_ICON_SIZE);
this._indic_drawing.set_height(Panel.PANEL_ICON_SIZE);
this._indic_drawing.add_style_class_name('circular-battery-indicator');
this._qs_drawing = new St.DrawingArea({ y_align: Clutter.ActorAlign.CENTER });
this._qs_drawing.set_width(Panel.PANEL_ICON_SIZE);
this._qs_drawing.set_height(Panel.PANEL_ICON_SIZE);
this.patch();
}
patch() {
if (this._TimeoutId) GLib.source_remove(this._TimeoutId);
if (this._pT) {
return;
}
var PowerToggle = imports.ui.status.system.PowerToggle;
if (!PowerToggle) {
this._TimeoutId = GLib.timeout_add_seconds(GLib.PRIORITY_DEFAULT, 1, ()=>{this.patch(); return true;});
return;
}
// Replace indicator icon with our drawing
// We don't want to mess with battery percentage text but focus on the icon
this._old_indic_icon = Main.panel.statusArea.quickSettings._system._indicator;
this._indic_box = Main.panel.statusArea.quickSettings._system._indicator.get_parent();
this._indic_box.replace_child(this._old_indic_icon, this._indic_drawing);
this._indic_repaintId = this._indic_drawing.connect("repaint", this.draw.bind(this));
// Replace quicksettings battery icon by our drawing and capture repaint event
this._pT = Main.panel.statusArea.quickSettings._system._systemItem._powerToggle;
this._old_qs_icon = this._pT._icon;
this._pT._box.replace_child(this._pT._icon, this._qs_drawing);
this._qs_repaintId = this._qs_drawing.connect("repaint", this.draw.bind(this));
// React to power changes
let proxy = this._pT._proxy;
this._powerProxyId = proxy.connect('g-properties-changed', this._onPowerChanged.bind(this));
// React to setting
this._desktopSettings = new Gio.Settings({ schema_id: 'org.gnome.desktop.interface' });
this._desktopSettings.connect('changed::show-battery-percentage', this._onPowerChanged.bind(this));
// Update now
this._onPowerChanged();
}
destroy() {
if (this._TimeoutId) GLib.source_remove(this._TimeoutId);
if (this._pT) {
// Put icons back in place
this._pT._box.replace_child(this._qs_drawing, this._old_qs_icon);
this._indic_box.replace_child(this._indic_drawing, this._old_indic_icon);
// Disconnect from events
this._qs_drawing.disconnect(this._qs_repaintId);
this._indic_drawing.disconnect(this._indic_repaintId);
this._pT._proxy.disconnect(this._powerProxyId);
// Force sync
Main.panel.statusArea.quickSettings._system._systemItem._powerToggle._sync();
}
}
_onPowerChanged() {
let proxy = this._pT._proxy;
if (proxy.IsPresent) {
this._percentage = proxy.Percentage;
this._charging = proxy.State == UPower.DeviceState.CHARGING ;
this._idle = proxy.State == UPower.DeviceState.FULLY_CHARGED
|| proxy.State == UPower.DeviceState.PENDING_CHARGE ;
} else {
this._percentage = null;
this._idle = false;
this._charging = false;
}
this.update();
}
update() {
this._indic_drawing.queue_repaint();
this._qs_drawing.queue_repaint();
}
draw(area) {
let ctx = area.get_context();
let themeNode = this._indic_drawing.get_theme_node();
let color = themeNode.get_foreground_color();
let areaWidth = area.get_width();
let areaHeight = area.get_height();
let outer = Math.min(areaHeight, areaWidth ) / 2;
let width = outer * 0.285;
let inner = outer - (width / 2);
Clutter.cairo_set_source_color(ctx, color.darken().darken());
ctx.save();
ctx.translate(areaHeight / 2.0, areaHeight / 2.0);
ctx.rotate(3 / 2 * Math.PI);
ctx.setLineWidth(width);
ctx.arc(0, 0, inner, 0, 2 * Math.PI);
ctx.stroke();
Clutter.cairo_set_source_color(ctx, color);
ctx.setLineWidth(width);
ctx.arc(0, 0, inner, 0, (this._percentage / 100) * 2 * Math.PI);
ctx.stroke();
if (this._charging) {
ctx.arc(0, 0, inner - width * 1.4, 0, 2 * Math.PI);
ctx.fill();
}
if (this._idle) {
Clutter.cairo_set_source_color(ctx, color.darken().darken());
ctx.arc(0, 0, inner - width * 1.4, 0, 2 * Math.PI);
ctx.fill();
}
ctx.restore();
}
});
let circularbatteryindicator;
function enable() {
circularbatteryindicator = new CircularBatteryIndicator();
}
function disable() {
circularbatteryindicator.destroy();
circularbatteryindicator = null;
}