11'use strict' ;
22
3- const { Adw , Gio, Gtk } = imports . gi ;
3+ const { Gio, Gtk } = imports . gi ;
44
55const ExtensionUtils = imports . misc . extensionUtils ;
66const Me = ExtensionUtils . getCurrentExtension ( ) ;
@@ -10,21 +10,86 @@ function init() {
1010}
1111
1212function 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