Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 3 additions & 13 deletions extension.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
const Lang = imports.lang;
const UPower = imports.gi.UPowerGlib;
const BaseIndicator = imports.ui.status.power.Indicator;
const ExtensionUtils = imports.misc.extensionUtils;
const Panel = imports.ui.main.panel;
Expand Down Expand Up @@ -28,9 +26,7 @@ var TPIndicator = GObject.registerClass(
super._init();

this.settings = ExtensionUtils.getSettings('org.gnome.shell.extensions.tp_wattmeter');

// to detect changes FIXME: a better way?
this.last_period_val = this.settings.get_double('period-sec');
this.settings.connect('changed::period-sec', () => { this._spawn(); }); // restart timers on setting change

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

const period_now = this.settings.get_double('period-sec');
if (period_now.toFixed(1) != this.last_period_val.toFixed(1)) {
// period changed, re-spawn
this._spawn();
this.last_period_val = period_now;
};

const avg_of = this.settings.get_int('avg-of');
if (this.readings.length >= avg_of) {
this.last_value = this.readings.reduce((acc, elem) => acc + elem, 0.0) / this.readings.length; // simple mean
Expand All @@ -94,12 +83,13 @@ var TPIndicator = GObject.registerClass(
this.tm_measure = GLib.timeout_add(
GLib.PRIORITY_DEFAULT,
this.settings.get_double('period-sec') * 1000,
Lang.bind(this, this._measure)
this._measure.bind(this),
);
}

_stop() {
GLib.source_remove(this.tm_measure);
this.tm_measure = null;
}
}
);
Expand Down
87 changes: 73 additions & 14 deletions prefs.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
'use strict';

const { Adw, Gio, Gtk } = imports.gi;
const { Gio, Gtk } = imports.gi;

const ExtensionUtils = imports.misc.extensionUtils;
const Me = ExtensionUtils.getCurrentExtension();
Expand All @@ -10,21 +10,86 @@ function init() {
}

function fillPreferencesWindow(window) {
const settings = ExtensionUtils.getSettings('org.gnome.shell.extensions.tp_wattmeter');
// Gnome 42+: GTK5 & libadwaita
const Adw = imports.gi.Adw;

const settings = ExtensionUtils.getSettings(SETTINGS_ID);

const page = new Adw.PreferencesPage();
const group = new Adw.PreferencesGroup();
page.add(group);

// history depth
const avg_row = new Adw.ActionRow({ title: 'Show average of this many measurements' });
const avg_row = new Adw.ActionRow({ title: LBL_AVG });
group.add(avg_row);

const avg_spin = makeAvgOfSpin(settings);
avg_row.add_suffix(avg_spin);
avg_row.activatable_widget = avg_spin;

// measure period
const period_row = new Adw.ActionRow({ title: LBL_PERIOD });
group.add(period_row);

const period_spin = makePeriodSpin(settings);
period_row.add_suffix(period_spin);
period_row.activatable_widget = period_spin;

// done
window.add(page);
}


function buildPrefsWidget() {
// Gnome 41-: GTK4 bare
const settings = ExtensionUtils.getSettings(SETTINGS_ID);

// history
const avg_lbl = new Gtk.Label({label: LBL_AVG});
const avg_spin = makeAvgOfSpin(settings);

const avg_box = new Gtk.Box();
avg_box.set_spacing(10);
avg_box.set_orientation(Gtk.Orientation.HORIZONTAL);
avg_box.prepend(avg_lbl);
avg_box.append(avg_spin);

// period
const period_lbl = new Gtk.Label({label: LBL_PERIOD});
const period_spin = makePeriodSpin(settings);

const period_box = new Gtk.Box();
period_box.set_spacing(10);
period_box.set_orientation(Gtk.Orientation.HORIZONTAL);
period_box.prepend(period_lbl);
period_box.append(period_spin);

// main
const main_box = new Gtk.Box();
main_box.set_spacing(25);
main_box.set_orientation(Gtk.Orientation.VERTICAL);
main_box.append(avg_box);
main_box.append(period_box);

// done
return main_box;
}


/** Common
*/

const SETTINGS_ID = 'org.gnome.shell.extensions.tp_wattmeter';
const LBL_AVG = 'Show average of this many measurements';
const LBL_PERIOD = 'Period between measurements in seconds';


function makeAvgOfSpin(settings) {
const avg_spin = new Gtk.SpinButton({
climb_rate: 1,
digits: 0,
adjustment: new Gtk.Adjustment({
value: settings.get_uint('avg-of'),
value: settings.get_int('avg-of'),
lower: 1,
upper: 25,
step_increment: 1,
Expand All @@ -36,13 +101,11 @@ function fillPreferencesWindow(window) {
'value',
Gio.SettingsBindFlags.DEFAULT
);
avg_row.add_suffix(avg_spin);
avg_row.activatable_widget = avg_spin;
return avg_spin;
}

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

function makePeriodSpin(settings) {
const period_spin = new Gtk.SpinButton({
climb_rate: 1,
digits: 1,
Expand All @@ -59,9 +122,5 @@ function fillPreferencesWindow(window) {
'value',
Gio.SettingsBindFlags.DEFAULT
);
period_row.add_suffix(period_spin);
period_row.activatable_widget = period_spin;

// done
window.add(page);
return period_spin;
}