Skip to content

Commit f01fcfc

Browse files
authored
Merge pull request #13 from gistart/gsettings
Gsettings
2 parents c89f863 + f3b1ba6 commit f01fcfc

File tree

8 files changed

+126
-31
lines changed

8 files changed

+126
-31
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,5 @@ Simple extension to show current power consumption in Gnome battery widget.
99
Differs from original wattmeter -- it reads directly from `/sys/class/power_supply/BAT0/power_now`.
1010

1111
Tested with ThinkPad Carbon X1 Gen4
12+
13+
Download from: https://extensions.gnome.org/extension/2308/tp_wattmeter/

extension.js

Lines changed: 29 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,6 @@ const Config = imports.misc.config;
1212
/** Settings
1313
*/
1414

15-
const HISTORY_DEPTH = 5;
16-
const MEASURE_PERIOD = 1000;
17-
const FORCE_SYNC_PERIOD = 5000;
18-
1915
const BAT_STATUS = "/sys/class/power_supply/BAT0/status";
2016
const POWER_NOW = "/sys/class/power_supply/BAT0/power_now";
2117

@@ -31,14 +27,18 @@ var TPIndicator = GObject.registerClass(
3127
_init() {
3228
super._init();
3329

30+
this.settings = ExtensionUtils.getSettings('org.gnome.shell.extensions.tp_wattmeter');
31+
32+
// to detect changes FIXME: a better way?
33+
this.last_period_val = this.settings.get_double('period-sec');
34+
3435
this.readings = [];
3536
this.last_value = 0.0;
3637
this.tm_measure = null;
37-
this.tm_force_sync = null;
3838
}
3939

4040
_getBatteryStatus() {
41-
const pct = this._proxy.Percentage;
41+
const pct = this._proxy.Percentage.toFixed(0);
4242
const power = this.last_value.toFixed(1);
4343
const status = this._read_file(BAT_STATUS, '???');
4444

@@ -71,39 +71,40 @@ var TPIndicator = GObject.registerClass(
7171
const power = parseFloat(this._read_file(POWER_NOW), 0) / 1000000;
7272
this.readings.push(power)
7373

74-
if (this.readings.length >= HISTORY_DEPTH) {
75-
let avg = this.readings.reduce((acc, elem) => acc + elem, 0.0) / this.readings.length;
76-
this.last_value = avg;
77-
78-
while (this.readings.length) { this.readings.pop(); };
79-
this.readings.push(avg);
80-
}
81-
return true;
82-
}
83-
84-
_force_sync() {
85-
this._sync();
74+
const period_now = this.settings.get_double('period-sec');
75+
if (period_now.toFixed(1) != this.last_period_val.toFixed(1)) {
76+
// period changed, re-spawn
77+
this._spawn();
78+
this.last_period_val = period_now;
79+
};
80+
81+
const avg_of = this.settings.get_int('avg-of');
82+
if (this.readings.length >= avg_of) {
83+
this.last_value = this.readings.reduce((acc, elem) => acc + elem, 0.0) / this.readings.length; // simple mean
84+
this._sync(); // update battery widget now!
85+
this.readings.length = 0; // fastest way to clear array?
86+
}
8687
return true;
8788
}
8889

8990
_spawn() {
91+
if (this.tm_measure !== null) {
92+
GLib.source_remove(this.tm_measure);
93+
}
9094
this.tm_measure = GLib.timeout_add(
9195
GLib.PRIORITY_DEFAULT,
92-
MEASURE_PERIOD,
93-
Lang.bind(this, this._measure));
94-
this.tm_force_sync = GLib.timeout_add(
95-
GLib.PRIORITY_DEFAULT,
96-
FORCE_SYNC_PERIOD,
97-
Lang.bind(this, this._force_sync));
96+
this.settings.get_double('period-sec') * 1000,
97+
Lang.bind(this, this._measure)
98+
);
9899
}
99100

100101
_stop() {
101102
GLib.source_remove(this.tm_measure);
102-
GLib.source_remove(this.tm_force_sync);
103103
}
104104
}
105105
);
106106

107+
107108
/** Extension
108109
*/
109110

@@ -114,12 +115,12 @@ class TPWattMeter {
114115

115116
this.aggregateMenu = Panel.statusArea['aggregateMenu'];
116117
this.originalIndicator = this.aggregateMenu._power;
117-
this.aggregateMenu._indicators.replace_child(this.originalIndicator.indicators, this.customIndicator.indicators);
118+
this.aggregateMenu._indicators.replace_child(this.originalIndicator, this.customIndicator);
118119
}
119120

120121
destroy(arg) {
121122
this.customIndicator._stop();
122-
this.aggregateMenu._indicators.replace_child(this.customIndicator.indicators, this.originalIndicator.indicators);
123+
this.aggregateMenu._indicators.replace_child(this.customIndicator, this.originalIndicator);
123124
this.customIndicator = null;
124125
}
125126
}
@@ -132,7 +133,7 @@ let tp_wattmeter;
132133

133134

134135
function enable() {
135-
tp_wattmeter = new TPWattMeter(); //tp_reader, tp_indicator);
136+
tp_wattmeter = new TPWattMeter();
136137
}
137138

138139
function disable() {

metadata.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@
22
"name": "tp_wattmeter",
33
"uuid": "tp_wattmeter@gistart",
44
"url": "https://github.com/gistart/tp_wattmeter",
5-
"description": "Shows battery power consumption of ThinkPad laptops",
5+
"description": "Shows battery power consumption of ThinkPad laptops. Now configurable!",
66
"shell-version": [
7-
"3.36.0",
87
"40",
98
"41",
109
"42"

pack-extension.sh

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
11
#!/bin/bash
22

3-
zip -j tp_wattmeter.zip extension.js metadata.json
3+
# compile schema
4+
glib-compile-schemas schemas/
5+
6+
# pack extension
7+
zip -r tp_wattmeter.zip \
8+
extension.js \
9+
prefs.js \
10+
metadata.json \
11+
schemas/gschemas.compiled \
12+
schemas/org.gnome.shell.extensions.tp_wattmeter.gschema.xml
13+
14+
# install extension
15+
gnome-extensions install ./tp_wattmeter.zip --force
16+
17+
# test settings
18+
# gnome-extensions prefs tp_wattmeter@gistart

prefs.js

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
'use strict';
2+
3+
const { Adw, Gio, Gtk } = imports.gi;
4+
5+
const ExtensionUtils = imports.misc.extensionUtils;
6+
const Me = ExtensionUtils.getCurrentExtension();
7+
8+
9+
function init() {
10+
}
11+
12+
function fillPreferencesWindow(window) {
13+
const settings = ExtensionUtils.getSettings('org.gnome.shell.extensions.tp_wattmeter');
14+
15+
const page = new Adw.PreferencesPage();
16+
const group = new Adw.PreferencesGroup();
17+
page.add(group);
18+
19+
// history depth
20+
const avg_row = new Adw.ActionRow({ title: 'Show average of this many measurements' });
21+
group.add(avg_row);
22+
23+
const avg_spin = new Gtk.SpinButton({
24+
climb_rate: 1,
25+
digits: 0,
26+
adjustment: new Gtk.Adjustment({
27+
value: settings.get_uint('avg-of'),
28+
lower: 1,
29+
upper: 25,
30+
step_increment: 1,
31+
}),
32+
});
33+
settings.bind(
34+
'avg-of',
35+
avg_spin,
36+
'value',
37+
Gio.SettingsBindFlags.DEFAULT
38+
);
39+
avg_row.add_suffix(avg_spin);
40+
avg_row.activatable_widget = avg_spin;
41+
42+
// measure period
43+
const period_row = new Adw.ActionRow({ title: 'Period between measurements in seconds' });
44+
group.add(period_row);
45+
46+
const period_spin = new Gtk.SpinButton({
47+
climb_rate: 1,
48+
digits: 1,
49+
adjustment: new Gtk.Adjustment({
50+
value: settings.get_double('period-sec'),
51+
lower: 0.1,
52+
upper: 10.0,
53+
step_increment: 0.5,
54+
}),
55+
});
56+
settings.bind(
57+
'period-sec',
58+
period_spin,
59+
'value',
60+
Gio.SettingsBindFlags.DEFAULT
61+
);
62+
period_row.add_suffix(period_spin);
63+
period_row.activatable_widget = period_spin;
64+
65+
// done
66+
window.add(page);
67+
}

schemas/gschemas.compiled

364 Bytes
Binary file not shown.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<schemalist>
3+
<schema id="org.gnome.shell.extensions.tp_wattmeter" path="/org/gnome/shell/extensions/tp_wattmeter/">
4+
<key name="avg-of" type="i">
5+
<default>5</default>
6+
</key>
7+
<key name="period-sec" type="d">
8+
<default>1.0</default>
9+
</key>
10+
</schema>
11+
</schemalist>

screenshot.png

76.2 KB
Loading

0 commit comments

Comments
 (0)