Skip to content

Commit bb06d6a

Browse files
authored
Merge pull request #14 from gistart/code-review
2 parents abf4532 + 6a13af6 commit bb06d6a

File tree

2 files changed

+76
-27
lines changed

2 files changed

+76
-27
lines changed

extension.js

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
const Lang = imports.lang;
2-
const UPower = imports.gi.UPowerGlib;
31
const BaseIndicator = imports.ui.status.power.Indicator;
42
const ExtensionUtils = imports.misc.extensionUtils;
53
const Panel = imports.ui.main.panel;
@@ -28,9 +26,7 @@ var TPIndicator = GObject.registerClass(
2826
super._init();
2927

3028
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');
29+
this.settings.connect('changed::period-sec', () => { this._spawn(); }); // restart timers on setting change
3430

3531
this.readings = [];
3632
this.last_value = 0.0;
@@ -71,13 +67,6 @@ var TPIndicator = GObject.registerClass(
7167
const power = parseFloat(this._read_file(POWER_NOW), 0) / 1000000;
7268
this.readings.push(power)
7369

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-
8170
const avg_of = this.settings.get_int('avg-of');
8271
if (this.readings.length >= avg_of) {
8372
this.last_value = this.readings.reduce((acc, elem) => acc + elem, 0.0) / this.readings.length; // simple mean
@@ -94,12 +83,13 @@ var TPIndicator = GObject.registerClass(
9483
this.tm_measure = GLib.timeout_add(
9584
GLib.PRIORITY_DEFAULT,
9685
this.settings.get_double('period-sec') * 1000,
97-
Lang.bind(this, this._measure)
86+
this._measure.bind(this),
9887
);
9988
}
10089

10190
_stop() {
10291
GLib.source_remove(this.tm_measure);
92+
this.tm_measure = null;
10393
}
10494
}
10595
);

prefs.js

Lines changed: 73 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
'use strict';
22

3-
const { Adw, Gio, Gtk } = imports.gi;
3+
const { Gio, Gtk } = imports.gi;
44

55
const ExtensionUtils = imports.misc.extensionUtils;
66
const Me = ExtensionUtils.getCurrentExtension();
@@ -10,21 +10,86 @@ function init() {
1010
}
1111

1212
function fillPreferencesWindow(window) {
13-
const settings = ExtensionUtils.getSettings('org.gnome.shell.extensions.tp_wattmeter');
13+
// Gnome 42+: GTK5 & libadwaita
14+
const Adw = imports.gi.Adw;
15+
16+
const settings = ExtensionUtils.getSettings(SETTINGS_ID);
1417

1518
const page = new Adw.PreferencesPage();
1619
const group = new Adw.PreferencesGroup();
1720
page.add(group);
1821

1922
// history depth
20-
const avg_row = new Adw.ActionRow({ title: 'Show average of this many measurements' });
23+
const avg_row = new Adw.ActionRow({ title: LBL_AVG });
2124
group.add(avg_row);
2225

26+
const avg_spin = makeAvgOfSpin(settings);
27+
avg_row.add_suffix(avg_spin);
28+
avg_row.activatable_widget = avg_spin;
29+
30+
// measure period
31+
const period_row = new Adw.ActionRow({ title: LBL_PERIOD });
32+
group.add(period_row);
33+
34+
const period_spin = makePeriodSpin(settings);
35+
period_row.add_suffix(period_spin);
36+
period_row.activatable_widget = period_spin;
37+
38+
// done
39+
window.add(page);
40+
}
41+
42+
43+
function buildPrefsWidget() {
44+
// Gnome 41-: GTK4 bare
45+
const settings = ExtensionUtils.getSettings(SETTINGS_ID);
46+
47+
// history
48+
const avg_lbl = new Gtk.Label({label: LBL_AVG});
49+
const avg_spin = makeAvgOfSpin(settings);
50+
51+
const avg_box = new Gtk.Box();
52+
avg_box.set_spacing(10);
53+
avg_box.set_orientation(Gtk.Orientation.HORIZONTAL);
54+
avg_box.prepend(avg_lbl);
55+
avg_box.append(avg_spin);
56+
57+
// period
58+
const period_lbl = new Gtk.Label({label: LBL_PERIOD});
59+
const period_spin = makePeriodSpin(settings);
60+
61+
const period_box = new Gtk.Box();
62+
period_box.set_spacing(10);
63+
period_box.set_orientation(Gtk.Orientation.HORIZONTAL);
64+
period_box.prepend(period_lbl);
65+
period_box.append(period_spin);
66+
67+
// main
68+
const main_box = new Gtk.Box();
69+
main_box.set_spacing(25);
70+
main_box.set_orientation(Gtk.Orientation.VERTICAL);
71+
main_box.append(avg_box);
72+
main_box.append(period_box);
73+
74+
// done
75+
return main_box;
76+
}
77+
78+
79+
/** Common
80+
*/
81+
82+
const SETTINGS_ID = 'org.gnome.shell.extensions.tp_wattmeter';
83+
const LBL_AVG = 'Show average of this many measurements';
84+
const LBL_PERIOD = 'Period between measurements in seconds';
85+
86+
87+
function makeAvgOfSpin(settings) {
2388
const avg_spin = new Gtk.SpinButton({
2489
climb_rate: 1,
2590
digits: 0,
2691
adjustment: new Gtk.Adjustment({
27-
value: settings.get_uint('avg-of'),
92+
value: settings.get_int('avg-of'),
2893
lower: 1,
2994
upper: 25,
3095
step_increment: 1,
@@ -36,13 +101,11 @@ function fillPreferencesWindow(window) {
36101
'value',
37102
Gio.SettingsBindFlags.DEFAULT
38103
);
39-
avg_row.add_suffix(avg_spin);
40-
avg_row.activatable_widget = avg_spin;
104+
return avg_spin;
105+
}
41106

42-
// measure period
43-
const period_row = new Adw.ActionRow({ title: 'Period between measurements in seconds' });
44-
group.add(period_row);
45107

108+
function makePeriodSpin(settings) {
46109
const period_spin = new Gtk.SpinButton({
47110
climb_rate: 1,
48111
digits: 1,
@@ -59,9 +122,5 @@ function fillPreferencesWindow(window) {
59122
'value',
60123
Gio.SettingsBindFlags.DEFAULT
61124
);
62-
period_row.add_suffix(period_spin);
63-
period_row.activatable_widget = period_spin;
64-
65-
// done
66-
window.add(page);
125+
return period_spin;
67126
}

0 commit comments

Comments
 (0)