-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprefs.js
194 lines (177 loc) · 6.75 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
"use strict";
import Adw from "gi://Adw";
import Gio from "gi://Gio";
import GLib from "gi://GLib";
import Gtk from "gi://Gtk";
import { ExtensionPreferences } from "resource:///org/gnome/Shell/Extensions/js/extensions/prefs.js";
import { ServerGroup } from "./serverGroup.js";
import { SettingsParser } from "./settingsParser.js";
/**
* The main preferences class that creates server groups and saves to gsettings.
*/
export default class ServerStatusPreferences extends ExtensionPreferences {
/**
* Called by system when preferences are opened.
*
* @param {Gtk.Window} window
*/
fillPreferencesWindow(window) {
this.window = window;
this.page = new Adw.PreferencesPage();
this.gioSettings = this.getSettings();
this.serverGroups = [];
// destroy on close
window.connect("close-request", () => {
this.serverGroups = null;
this.gioSettings = null;
this.page = null;
});
// instructions/help
const helpBox = new Gtk.Box({
orientation: Gtk.Orientation.VERTICAL,
spacing: 10,
});
// server-bad
const serverInitBox = new Gtk.Box({
orientation: Gtk.Orientation.HORIZONTAL,
spacing: 10,
});
const serverInitImage = new Gtk.Image({
file: this.path + "/assets/server.svg",
pixel_size: 36,
});
const serverInitDesc = new Gtk.Label({
label: "Initializing...",
});
serverInitBox.append(serverInitImage);
serverInitBox.append(serverInitDesc);
helpBox.append(serverInitBox);
// server-down
const serverDownBox = new Gtk.Box({
orientation: Gtk.Orientation.HORIZONTAL,
spacing: 10,
});
const serverDownImage = new Gtk.Image({
file: this.path + "/assets/server-down.svg",
pixel_size: 36,
});
const serverDownDesc = new Gtk.Label({
label: "If you get a server-down indicator, try switching to GET.\nHTTP HEAD is faster but not always supported.",
});
serverDownBox.append(serverDownImage);
serverDownBox.append(serverDownDesc);
helpBox.append(serverDownBox);
// server-bad
const serverBadBox = new Gtk.Box({
orientation: Gtk.Orientation.HORIZONTAL,
spacing: 10,
});
const serverBadImage = new Gtk.Image({
file: this.path + "/assets/server-bad.svg",
pixel_size: 36,
});
const serverBadDesc = new Gtk.Label({
label: "If you get a server-bad indicator, there's something wrong with the URL.\nIt should be of format http[s]://host[:port][/path].",
});
serverBadBox.append(serverBadImage);
serverBadBox.append(serverBadDesc);
helpBox.append(serverBadBox);
// server-up
const serverUpBox = new Gtk.Box({
orientation: Gtk.Orientation.HORIZONTAL,
spacing: 10,
});
const serverUpImage = new Gtk.Image({
file: this.path + "/assets/server-up.svg",
pixel_size: 36,
});
const serverUpDesc = new Gtk.Label({
label: "The desired server-up indicator.",
});
serverUpBox.append(serverUpImage);
serverUpBox.append(serverUpDesc);
helpBox.append(serverUpBox);
// help group
const helpGroup = new Adw.PreferencesGroup({});
helpGroup.add(helpBox);
this.page.add(helpGroup);
// operations group
const operationsGroup = new Adw.PreferencesGroup({});
// add
const addRow = new Adw.ActionRow({
title: "Add a new server",
});
const addButton = Gtk.Button.new_from_icon_name("list-add-symbolic");
addButton.set_css_classes(["suggested-action"]);
addRow.add_suffix(addButton);
addButton.connect("clicked", () => {
// ServerGroup is a wrapper around a PreferenceGroup, returned by getGroup()
const newGroup = new ServerGroup(this, null); // widgets will not be initialized but group will be expanded
newGroup
.getGroup()
.insert_after(operationsGroup.parent, operationsGroup); // add group to top of groups
this.serverGroups.unshift(newGroup); // add to beginning of array
this.save(this, this.serverGroups);
// make name field focused
newGroup.getNameInput().grab_focus();
});
operationsGroup.add(addRow);
this.page.add(operationsGroup);
// create one server group per discovered settings
const parsedSettings = SettingsParser.parseGtkSettings(
this.gioSettings,
);
// add them back reversed, same as they were created, and displayed in indicator
const reversed = parsedSettings.toReversed();
for (const savedSettings of reversed) {
// ServerGroup is a wrapper around a PreferenceGroup, returned by getGroup()
const newGroup = new ServerGroup(this, savedSettings);
newGroup
.getGroup()
.insert_after(operationsGroup.parent, operationsGroup);
this.serverGroups.unshift(newGroup); // add to beginning of array
}
this.window.add(this.page);
}
/**
* Render the displayed groups in their new order.
*
* @param {ExtensionPreferences} preferences
*/
reorder(preferences) {
// remove all Adw.PreferenceGroups related to ServerGroups and...
for (const serverGroup of preferences.serverGroups) {
// remove it from whatever position it's in
preferences.page.remove(serverGroup.getGroup());
}
// ...add them back in new order
for (const serverGroup of preferences.serverGroups) {
// add sequentially
preferences.page.add(serverGroup.getGroup());
}
}
/**
* Save current server settings to gsettings.
*
* @param {ServerStatusPreferences} preferences
*/
save(preferences) {
const serverSettings = [];
for (const serverGroup of preferences.serverGroups) {
const settings = serverGroup.settings;
if (settings) {
settings.name = settings.name.trim();
settings.url = settings.url.trim();
settings.frequency = settings.frequency.toString();
settings.is_get = settings.is_get.toString();
serverSettings.push(settings);
}
}
// persist
preferences.gioSettings.set_value(
"server-settings",
new GLib.Variant("aa{ss}", serverSettings),
);
Gio.Settings.sync();
}
}