This repository has been archived by the owner on Aug 23, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
prefs.js
executable file
·594 lines (475 loc) · 15 KB
/
prefs.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
'use strict';
// Imports:
const { Gio, Gtk } = imports.gi;
const ExtensionUtils = imports.misc.extensionUtils;
const Me = ExtensionUtils.getCurrentExtension();
const Config = imports.misc.config;
const Gettext = imports.gettext;
// Init translations
Gettext.textdomain('[email protected]');
Gettext.bindtextdomain(
Me.dir.get_child('locale').get_path(),
);
const _ = Gettext.gettext;
// `extensionManager` may not be available in the current version of GNOME Shell
// but it's necessary in order to change the shell theme
//
// `extensionManager` should be available on gnome-shell >= 3.34
const canChangeShellTheme = parseInt(Config.PACKAGE_VERSION.split('.')[1]) > 32;
/**
* This function is required to make this script work but doesn't do anything
*/
function init() { }
/**
* Build an array of widgets for an entry row in the nighttime section
*
* @param {string} title The row's title, it will be shown as a label
* @param {string} settingsId The identifier used to identify the edited value
* @param {Gio.Settings} settings The extension's settings
*
* @returns {Array<Gtk.Widget>} The array containing all the row's widgets
*/
function buildNighttimeRow(title, settingsId, settings) {
// ROW TITLE
const labelTitle = new Gtk.Label({
label: title,
visible: true,
});
// ENTRIES
const spinHours = new Gtk.SpinButton({
visible: true,
hexpand: true,
});
const spinMinutes = new Gtk.SpinButton({
visible: true,
hexpand: true,
});
// This is the value that's stored inside the settings at the moment the row
// is being built
// The `| 0` part is just here to make type checking easier but doesn't
// actually do anything in that context (same goes with later uses)
const initialValue = settings.get_uint(settingsId) | 0;
// The range should be (min - step, max + step) in order to make a looping
// spinner
spinHours.set_range(-1, 24 /* hours */);
spinHours.set_increments(1 /* hours */, 0);
spinHours.set_value(Math.floor(initialValue / 60));
spinHours.connect(
'value-changed',
(spinHours) => {
let hours = spinHours.get_value() | 0;
// In order to enable looping:
// If the user tries incrementing the hours when it's already at its
// highest value, set it to the lowest one instead
// Also do the same with decrementing
if (hours == 24) {
hours = 0;
spinHours.set_value(hours);
} else if (hours == -1) {
hours = 23;
spinHours.set_value(hours);
}
settings.set_uint(settingsId,
hours * 60 + spinMinutes.get_value());
},
);
// Make it loop! (see `spinHours`'s documentation above)
spinMinutes.set_range(-15, 74 /* minutes */);
spinMinutes.set_increments(15 /* minutes */, 0);
spinMinutes.set_value(initialValue % 60);
spinMinutes.connect(
'value-changed',
(spinMinutes) => {
let minutes = spinMinutes.get_value() | 0;
let hours = spinHours.get_value() | 0;
// In order to loop here, we have to change both the hours and the
// minutes spinner (it wouldn't make any sense from a user's
// perspective to not change the hours spinner)
if (minutes > 59 /* max */) {
// Increment hours by 1 and remove the hour from the minute
// count
minutes -= 60;
++hours;
spinMinutes.set_value(minutes);
spinHours.set_value(hours);
} else if (minutes < 0 /* min */) {
// Decrement hours by 1 and add the hour to the minute count
minutes += 60;
--hours;
spinMinutes.set_value(minutes);
spinHours.set_value(hours);
}
settings.set_uint(settingsId, hours * 60 + minutes);
},
);
// HOURS/MINUTES SEPARATOR
const labelSeparator = new Gtk.Label({
label: ':',
visible: true,
});
return [labelTitle, spinHours, labelSeparator, spinMinutes];
}
/**
* Add a new tab to the `Gtk.Notebook`
*
* @param {Gtk.Notebook} notebook The previously created notebook
* @param {string} name The tab's visible name
* @param {Gtk.Widget} content The widget shown by this new page
*/
function appendPage(notebook, name, content) {
const nameLabel = new Gtk.Label({
label: name,
visible: true,
});
notebook.append_page(content, nameLabel);
}
/**
* @returns {Gtk.Grid} A grid widget with the default properties used here
*/
function buildDefaultGrid() {
return new Gtk.Grid({
margin_start: 18,
margin_end: 18,
margin_top: 18,
margin_bottom: 18,
column_spacing: 12,
row_spacing: 12,
visible: true,
hexpand: true,
vexpand: true,
});
}
/**
* @param {Gio.Settings} settings This extension's settings
*
* @returns {Gtk.Widget} The "Time" page's content
*/
function buildTimePage(settings) {
// Current grid's line
let line = 0;
const prefWidget = buildDefaultGrid();
// NIGHTTIME HEADER
const titleNighttime = new Gtk.Label({
label: '<b>' + _('Nighttime (24h format)') + '</b>',
halign: Gtk.Align.START,
use_markup: true,
visible: true,
});
prefWidget.attach(titleNighttime, 0, line, 4, 1);
++line;
// NIGHTTIME BEGIN
const nighttimeRowBegin = buildNighttimeRow(
_('Start of Nighttime'),
'nighttime-begin',
settings,
);
const nighttimeRowBeginLength = nighttimeRowBegin.length;
for (let i = 0; i < nighttimeRowBeginLength; ++i) {
prefWidget.attach(nighttimeRowBegin[i], i, line, 1, 1);
}
++line;
// NIGHTTIME END
const nighttimeRowEnd = buildNighttimeRow(
_('End of Nighttime'),
'nighttime-end',
settings,
);
const nighttimeRowEndLength = nighttimeRowEnd.length;
for (let i = 0; i < nighttimeRowEndLength; ++i) {
prefWidget.attach(nighttimeRowEnd[i], i, line, 1, 1);
}
++line;
// SEPARATOR
prefWidget.attach(new Gtk.Separator({
orientation: Gtk.Orientation.HORIZONTAL,
visible: true,
}), 0, line, 4, 1);
++line;
// SAME AS NIGHT LIGHT
const labelNightLight = new Gtk.Label({
label: '<b>' + _('Use Night Light\'s Manual Schedule Instead') + '</b>',
halign: Gtk.Align.START,
use_markup: true,
visible: true,
});
prefWidget.attach(labelNightLight, 0, line, 3, 1);
const switchNightLight = new Gtk.Switch({
active: settings.get_boolean('nighttime-from-night-light'),
visible: true,
halign: Gtk.Align.END,
});
prefWidget.attach(switchNightLight, 3, line, 1, 1);
settings.bind(
'nighttime-from-night-light',
switchNightLight,
'active',
Gio.SettingsBindFlags.DEFAULT,
);
return prefWidget;
}
/**
* @param {Gio.Settings} settings This extension's settings
*
* @returns {Gtk.Widget} The "Themes" page's content
*/
function buildThemesPage(settings) {
// Current grid's line
let line = 0;
const prefWidget = buildDefaultGrid();
// THEMES HEADER
const titleThemes = new Gtk.Label({
label: '<b>' + _('Day/Night GTK Themes') + '</b>',
halign: Gtk.Align.START,
use_markup: true,
visible: true,
});
prefWidget.attach(titleThemes, 0, line, 4, 1);
++line;
// THEME DAY
const labelThemeDay = new Gtk.Label({
label: _('Day Theme'),
visible: true,
});
prefWidget.attach(labelThemeDay, 0, line, 1, 1);
const entryThemeDay = new Gtk.Entry({
text: settings.get_string('day-theme'),
visible: true,
hexpand: true,
});
prefWidget.attach(entryThemeDay, 1, line, 3, 1);
settings.bind(
'day-theme',
entryThemeDay,
'text',
Gio.SettingsBindFlags.DEFAULT,
);
++line;
// THEME NIGHT
const labelThemeNight = new Gtk.Label({
label: _('Night Theme'),
visible: true,
});
prefWidget.attach(labelThemeNight, 0, line, 1, 1);
const entryThemeNight = new Gtk.Entry({
text: settings.get_string('night-theme'),
visible: true,
hexpand: true,
});
prefWidget.attach(entryThemeNight, 1, line, 3, 1);
settings.bind(
'night-theme',
entryThemeNight,
'text',
Gio.SettingsBindFlags.DEFAULT,
);
// Don't show shell theme settings if they can't be used
if (canChangeShellTheme) {
++line;
// SEPARATOR
prefWidget.attach(new Gtk.Separator({
orientation: Gtk.Orientation.HORIZONTAL,
visible: true,
}), 0, line, 4, 1);
++line;
// SHELL THEMES HEADER
const titleShellThemes = new Gtk.Label({
label: '<b>' + _('Day/Night Shell Themes') + '</b>',
halign: Gtk.Align.START,
use_markup: true,
visible: true,
});
prefWidget.attach(titleShellThemes, 0, line, 3, 1);
const switchShellTheme = new Gtk.Switch({
active: settings.get_boolean('shell-enabled'),
visible: true,
halign: Gtk.Align.END,
});
prefWidget.attach(switchShellTheme, 3, line, 1, 1);
settings.bind(
'shell-enabled',
switchShellTheme,
'active',
Gio.SettingsBindFlags.DEFAULT,
);
++line;
// SHELL THEME DAY
const labelShellThemeDay = new Gtk.Label({
label: _('Day Theme'),
visible: true,
});
prefWidget.attach(labelShellThemeDay, 0, line, 1, 1);
const entryShellThemeDay = new Gtk.Entry({
text: settings.get_string('day-shell'),
visible: true,
hexpand: true,
});
prefWidget.attach(entryShellThemeDay, 1, line, 3, 1);
settings.bind(
'day-shell',
entryShellThemeDay,
'text',
Gio.SettingsBindFlags.DEFAULT,
);
++line;
// SHELL THEME NIGHT
const labelShellThemeNight = new Gtk.Label({
label: _('Night Theme'),
visible: true,
});
prefWidget.attach(labelShellThemeNight, 0, line, 1, 1);
const entryShellThemeNight = new Gtk.Entry({
text: settings.get_string('night-shell'),
visible: true,
hexpand: true,
});
prefWidget.attach(entryShellThemeNight, 1, line, 3, 1);
settings.bind(
'night-shell',
entryShellThemeNight,
'text',
Gio.SettingsBindFlags.DEFAULT,
);
}
return prefWidget;
}
/**
* @param {Gio.Settings} settings This extension's settings
*
* @returns {Gtk.Widget} The "Extra" page's content
*/
function buildExtraPage(settings) {
// Current grid's line
let line = 0;
const prefWidget = buildDefaultGrid();
// COMMANDS HEADER
const titleCommands = new Gtk.Label({
label: '<b>' + _('Day/Night Commands (executed with /bin/sh)') + '</b>',
halign: Gtk.Align.START,
use_markup: true,
visible: true,
});
prefWidget.attach(titleCommands, 0, line, 3, 1);
const switchCommands = new Gtk.Switch({
active: settings.get_boolean('commands-enabled'),
visible: true,
halign: Gtk.Align.END,
});
prefWidget.attach(switchCommands, 3, line, 1, 1);
settings.bind(
'commands-enabled',
switchCommands,
'active',
Gio.SettingsBindFlags.DEFAULT,
);
++line;
// COMMAND DAY
const labelCommandDay = new Gtk.Label({
label: _('Day Command'),
visible: true,
});
prefWidget.attach(labelCommandDay, 0, line, 1, 1);
const entryCommandDay = new Gtk.Entry({
text: settings.get_string('day-command'),
visible: true,
hexpand: true,
});
prefWidget.attach(entryCommandDay, 1, line, 3, 1);
settings.bind(
'day-command',
entryCommandDay,
'text',
Gio.SettingsBindFlags.DEFAULT,
);
++line;
// COMMAND NIGHT
const labelCommandNight = new Gtk.Label({
label: _('Night Command'),
visible: true,
});
prefWidget.attach(labelCommandNight, 0, line, 1, 1);
const entryCommandNight = new Gtk.Entry({
text: settings.get_string('night-command'),
visible: true,
hexpand: true,
});
prefWidget.attach(entryCommandNight, 1, line, 3, 1);
settings.bind(
'night-command',
entryCommandNight,
'text',
Gio.SettingsBindFlags.DEFAULT,
);
return prefWidget;
}
/**
* @param {Gio.Settings} settings This extension's settings
*
* @returns {Gtk.Widget} The "Advanced" page's content
*/
function buildAdvancedPage(settings) {
// Current grid's line
let line = 0;
const prefWidget = buildDefaultGrid();
// ADVANCED HEADER
const titleAdvanced = new Gtk.Label({
label: '<b>' + _('Advanced') + '</b>',
halign: Gtk.Align.START,
use_markup: true,
visible: true,
});
prefWidget.attach(titleAdvanced, 0, line, 4, 1);
++line;
// TIME CHECK PERIOD
const labelCheckPeriod = new Gtk.Label({
label: _('Time Check Period (in ms)'),
visible: true,
});
prefWidget.attach(labelCheckPeriod, 0, line, 1, 1);
const spinCheckPeriod = new Gtk.SpinButton({
visible: true,
hexpand: true,
});
// Range = [10ms, 30min]
spinCheckPeriod.set_range(10 /* ms */, 1800000 /* ms */);
spinCheckPeriod.set_increments(10 /* ms */, 0);
spinCheckPeriod.set_value(settings.get_uint('time-check-period'));
spinCheckPeriod.connect(
'value-changed',
(spinCheckPeriod) => {
settings.set_uint(
'time-check-period',
spinCheckPeriod.get_value(),
);
},
);
prefWidget.attach(spinCheckPeriod, 1, line, 3, 1);
return prefWidget;
}
/**
* Create the widget and bind its children to the actual values
*
* @returns {Gtk.Widget} The widget containing all the other widgets
*/
function buildPrefsWidget() {
const schema = Gio.SettingsSchemaSource.new_from_directory(
Me.dir.get_child('schemas').get_path(),
Gio.SettingsSchemaSource.get_default(),
false,
);
const settings = new Gio.Settings({
settings_schema: schema.lookup(
true,
),
});
const prefWidget = new Gtk.Notebook({
visible: true,
});
// Create and append all the pages
appendPage(prefWidget, _("Time"), buildTimePage(settings));
appendPage(prefWidget, _("Themes"), buildThemesPage(settings));
appendPage(prefWidget, _("Extra"), buildExtraPage(settings));
appendPage(prefWidget, _("Advanced"), buildAdvancedPage(settings));
return prefWidget;
}