-
Notifications
You must be signed in to change notification settings - Fork 6
/
prefs.js
337 lines (278 loc) · 13.1 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
'use strict';
import GObject from 'gi://GObject';
import Gio from 'gi://Gio';
import GLib from 'gi://GLib';
import Gtk from 'gi://Gtk';
import GdkWayland from 'gi://GdkWayland';
import Gdk from 'gi://Gdk';
import {ExtensionPreferences, gettext as _} from 'resource:///org/gnome/Shell/Extensions/js/extensions/prefs.js';
import * as FileUtils from './utils/fileUtils.js';
import * as Log from './utils/log.js';
import {prefsUtilsInit, prefsUtilsDestroy, PrefsUtils} from './utils/prefsUtils.js';
import * as StringUtils from './utils/stringUtils.js';
import * as PrefsCloseWindow from './prefsCloseWindow.js';
export default class AnotherWindowSessionManagerPreferences extends ExtensionPreferences {
fillPreferencesWindow(window) {
window.set_default_size(1000, 800);
const settings = this.getSettings('org.gnome.shell.extensions.another-window-session-manager');
this.initUtils(settings);
this._log = new Log.Log();
this.render_ui();
new PrefsCloseWindow.UICloseWindows(this._builder).init();
this._bindSettings();
// Set sensitive AFTER this._bindSettings() to make it work
this._setSensitive();
this._addPages(window);
window.connect('close-request', () => {
this._destroy();
});
}
initUtils(settings) {
prefsUtilsInit(this, settings);
FileUtils.init(this);
}
_addPages(window) {
const pages = [
this._builder.get_object('close_windows_page'),
this._builder.get_object('save_windows_page'),
this._builder.get_object('restore_sessions_page'),
this._builder.get_object('general_page'),
];
pages.forEach(page => window.add(page));
}
_setSensitive() {
const activeOfRestorePrevious = this.restore_previous_switch.get_active();
this.restore_previous_delay_spinbutton.set_sensitive(activeOfRestorePrevious);
const restore_at_startup_switch_state = this.restore_at_startup_switch.get_active();
this.timer_on_the_autostart_dialog_spinbutton.set_sensitive(restore_at_startup_switch_state);
this.restore_at_startup_without_asking_switch.set_sensitive(restore_at_startup_switch_state);
this.timer_on_the_autostart_dialog_spinbutton.set_sensitive(
restore_at_startup_switch_state && !this.restore_at_startup_without_asking_switch.get_active()
);
const display = Gdk.Display.get_default();
if (display instanceof GdkWayland.WaylandDisplay) {
this.stash_and_restore_states_switch.set_sensitive(false);
}
}
_bindSettings() {
PrefsUtils.getSettings().bind(
'debugging-mode',
this.debugging_mode_switch,
'active',
Gio.SettingsBindFlags.DEFAULT
);
PrefsUtils.getSettings().bind(
'verbose-logging',
this.verbose_logging_switch,
'active',
Gio.SettingsBindFlags.DEFAULT
);
PrefsUtils.getSettings().bind(
'show-indicator',
this.show_indicator_switch,
'active',
Gio.SettingsBindFlags.DEFAULT
);
PrefsUtils.getSettings().bind(
'enable-save-session-notification',
this.save_session_notification_switch,
'active',
Gio.SettingsBindFlags.DEFAULT
);
PrefsUtils.getSettings().bind(
'enable-autorestore-sessions',
this.restore_at_startup_switch,
'active',
Gio.SettingsBindFlags.DEFAULT
);
PrefsUtils.getSettings().bind(
'enable-restore-previous-session',
this.restore_previous_switch,
'active',
Gio.SettingsBindFlags.DEFAULT
);
PrefsUtils.getSettings().bind(
'restore-at-startup-without-asking',
this.restore_at_startup_without_asking_switch,
'active',
Gio.SettingsBindFlags.DEFAULT
);
PrefsUtils.getSettings().bind(
'autorestore-sessions-timer',
this.timer_on_the_autostart_dialog_spinbutton,
'value',
Gio.SettingsBindFlags.DEFAULT
);
PrefsUtils.getSettings().bind(
'restore-previous-delay',
this.restore_previous_delay_spinbutton,
'value',
Gio.SettingsBindFlags.DEFAULT
);
PrefsUtils.getSettings().bind(
'restore-session-interval',
this.restore_session_interval_spinbutton,
'value',
Gio.SettingsBindFlags.DEFAULT
);
PrefsUtils.getSettings().bind(
'autostart-delay',
this.autostart_delay_spinbutton,
'value',
Gio.SettingsBindFlags.DEFAULT
);
PrefsUtils.getSettings().bind(
'restore-window-tiling',
this.restore_window_tiling_switch,
'active',
Gio.SettingsBindFlags.DEFAULT
);
PrefsUtils.getSettings().bind(
'raise-windows-together',
this.raise_windows_together_switch,
'active',
Gio.SettingsBindFlags.DEFAULT
);
PrefsUtils.getSettings().bind(
'stash-and-restore-states',
this.stash_and_restore_states_switch,
'active',
Gio.SettingsBindFlags.DEFAULT
);
PrefsUtils.getSettings().bind(
'enable-autoclose-session',
this.auto_close_session_switch,
'active',
Gio.SettingsBindFlags.DEFAULT
);
PrefsUtils.getSettings().bind(
'enable-close-by-rules',
this.close_by_rules_switch,
'active',
Gio.SettingsBindFlags.DEFAULT
);
PrefsUtils.getSettings().connect('changed::enable-autorestore-sessions', (settings) => {
if (PrefsUtils.getSettings().get_boolean('enable-autorestore-sessions')) {
this._installAutostartDesktopFile(FileUtils.desktop_template_path_restore_at_autostart,
FileUtils.autostart_restore_desktop_file_path);
}
});
PrefsUtils.getSettings().connect('changed::enable-restore-previous-session', (settings) => {
if (PrefsUtils.getSettings().get_boolean('enable-restore-previous-session')) {
this._installAutostartDesktopFile(FileUtils.desktop_template_path_restore_previous_at_autostart,
FileUtils.autostart_restore_previous_desktop_file_path);
}
});
PrefsUtils.getSettings().connect('changed::restore-at-startup-without-asking', (settings) => {
this.timer_on_the_autostart_dialog_spinbutton.set_sensitive(
!PrefsUtils.getSettings().get_boolean('restore-at-startup-without-asking')
);
});
PrefsUtils.getSettings().connect('changed::autostart-delay', (settings) => {
this._installAutostartDesktopFile(FileUtils.desktop_template_path_restore_at_autostart,
FileUtils.autostart_restore_desktop_file_path);
this._installAutostartDesktopFile(FileUtils.desktop_template_path_restore_previous_at_autostart,
FileUtils.autostart_restore_previous_desktop_file_path);
});
}
render_ui() {
this._builder = new Gtk.Builder();
this._builder.set_scope(new BuilderScope(this));
this._builder.add_from_file(this.path + '/ui/prefs-gtk4.ui');
this.debugging_mode_switch = this._builder.get_object('debugging_mode_switch');
this.verbose_logging_switch = this._builder.get_object('verbose_logging_switch');
this.show_indicator_switch = this._builder.get_object('show_indicator_switch');
this.save_session_notification_switch = this._builder.get_object('save_session_notification_switch');
this.restore_session_interval_spinbutton = this._builder.get_object('restore_session_interval_spinbutton');
this.timer_on_the_autostart_dialog_spinbutton = this._builder.get_object('timer_on_the_autostart_dialog_spinbutton');
this.autostart_delay_spinbutton = this._builder.get_object('autostart_delay_spinbutton');
this.restore_window_tiling_switch = this._builder.get_object('restore_window_tiling_switch');
this.restore_window_tiling_switch.connect('notify::active', (widget) => {
const active = widget.active;
this.raise_windows_together_switch.set_sensitive(active);
});
this.raise_windows_together_switch = this._builder.get_object('raise_windows_together_switch');
this.stash_and_restore_states_switch = this._builder.get_object('stash_and_restore_states_switch');
this.restore_previous_delay_spinbutton = this._builder.get_object('restore_previous_delay_spinbutton');
this.restore_previous_switch = this._builder.get_object('restore_previous_switch');
this.restore_previous_switch.connect('notify::active', (widget) => {
const active = widget.active;
const activeOfRestoreAtStartup = this.restore_at_startup_switch.get_active();
if (activeOfRestoreAtStartup) {
this.restore_at_startup_switch.set_active(!active);
}
this.restore_previous_delay_spinbutton.set_sensitive(active);
});
this.restore_at_startup_switch = this._builder.get_object('restore_at_startup_switch');
this.restore_at_startup_switch.connect('notify::active', (widget) => {
const active = widget.active;
this.restore_at_startup_without_asking_switch.set_sensitive(active);
const enableTimerSpinButton = active && !PrefsUtils.getSettings().get_boolean('restore-at-startup-without-asking');
if (enableTimerSpinButton) {
this.timer_on_the_autostart_dialog_spinbutton.set_sensitive(true);
} else {
this.timer_on_the_autostart_dialog_spinbutton.set_sensitive(false);
}
const activeOfRestorePrevious = this.restore_previous_switch.get_active();
if (activeOfRestorePrevious) {
this.restore_previous_switch.set_active(!active);
}
});
this.restore_at_startup_without_asking_switch = this._builder.get_object('restore_at_startup_without_asking_switch');
this.restore_at_startup_without_asking_switch.connect('notify::active', (widget) => {
const active = widget.active;
this.timer_on_the_autostart_dialog_spinbutton.set_sensitive(!active);
});
this.close_by_rules_switch = this._builder.get_object('close_by_rules_switch');
this.auto_close_session_switch = this._builder.get_object('auto_close_session_switch');
}
_installAutostartDesktopFile(desktopFileTemplate, targetDesktopFilePath) {
const argument = {
autostartDelay: PrefsUtils.getSettings().get_int('autostart-delay'),
};
const desktopFileContent = StringUtils.format(FileUtils.loadTemplate(desktopFileTemplate), argument);
this._installDesktopFileToAutostartDir(targetDesktopFilePath, desktopFileContent);
}
_installDesktopFileToAutostartDir(desktopFilePath, desktopFileContents) {
const autostart_restore_desktop_file = Gio.File.new_for_path(desktopFilePath);
const autostart_restore_desktop_file_path_parent = autostart_restore_desktop_file.get_parent().get_path();
if (GLib.mkdir_with_parents(autostart_restore_desktop_file_path_parent, 0o744) === 0) {
let [success, tag] = autostart_restore_desktop_file.replace_contents(
desktopFileContents,
null,
false,
Gio.FileCreateFlags.REPLACE_DESTINATION,
null
);
if (success) {
this._log.info(`Installed the autostart desktop file: ${desktopFilePath}!`);
} else {
this._log.error(new Error(`Failed to install the autostart desktop file: ${desktopFilePath}`))
}
} else {
this._log.error(new Error(`Failed to create folder: ${autostart_restore_desktop_file_path_parent}`));
}
}
_destroy() {
prefsUtilsDestroy();
}
}
const BuilderScope = GObject.registerClass({
// Should be a globally unique GType name
GTypeName: "AnotherWindowSessionManagerBuilderScope",
Implements: [Gtk.BuilderScope],
}, class BuilderScope extends GObject.Object {
_init(preferences) {
this._preferences = preferences;
super._init();
}
// Fix: Gtk.BuilderError: Creating closures is not supported by Gjs_BuilderScope
// https://docs.w3cub.com/gtk~4.0/gtkbuilder#gtk-builder-create-closure
vfunc_create_closure(builder, handlerName, flags, connectObject) {
if (flags & Gtk.BuilderClosureFlags.SWAPPED)
throw new Error('Unsupported template signal flag "swapped"');
if (typeof this[handlerName] === 'undefined')
throw new Error(`${handlerName} is undefined`);
return this[handlerName].bind(connectObject || this);
}
});