Repository navigation
Expand file tree
/
Copy pathextension.js
More file actions
270 lines (242 loc) · 8.13 KB
/
Copy pathextension.js
File metadata and controls
270 lines (242 loc) · 8.13 KB
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
"use strict";
import { Extension } from "resource:///org/gnome/shell/extensions/extension.js";
import Gio from "gi://Gio";
import { WindowManager } from "./lib/window-manager.js";
import { createGSettingsStorage } from "./lib/state-session.js";
import { debug, setDebugEnabled } from "./lib/utils.js";
import {
DBUS_NAME,
DBUS_PATH,
DBUS_INTERFACE,
SECONDS_PER_HOUR,
getActionId,
parseOverrides,
} from "./common.js";
import { migrateSettings } from "./migrations.js";
// Declarative settings handlers for cleaner change management
const SETTING_HANDLERS = {
"debug-logging": {
type: "boolean",
apply: (value) => setDebugEnabled(value),
},
"max-unseen-age": {
type: "int",
field: "_maxUnseenAge",
apply: (value, ext) => ext._cleanupStaleEntries(),
},
"freeze-saves": {
type: "boolean",
field: "_freezeSaves",
apply: (value, ext) => {
if (ext._helper?.getSession()) {
ext._helper.getSession()._readOnly = value;
}
},
},
"activate-workspace": {
type: "boolean",
field: "_activateWorkspace",
apply: (value, ext) => {
if (ext._helper?.getExecutor()) {
ext._helper.getExecutor()._config.activate_on_move = value;
}
},
},
"ignore-position": { type: "boolean", field: "_ignorePosition" },
"ignore-workspace": { type: "boolean", field: "_ignoreWorkspace" },
"ignore-monitor": { type: "boolean", field: "_ignoreMonitor" },
"sync-mode": {
type: "enum",
field: "_syncMode",
transform: (value) => getActionId(value),
apply: (value, ext) => ext._helper.updateConfig({ DEFAULT_SYNC_MODE: value }),
},
"match-threshold": {
type: "double",
field: "_matchThreshold",
apply: (value, ext) => ext._helper.updateConfig({ DEFAULT_MATCH_THRESHOLD: value }),
},
"overrides": {
type: "string",
field: "_overrides",
transform: (value) => parseOverrides(value),
apply: (value, ext) => ext._helper.updateConfig({ OVERRIDES: value }),
},
"saved-windows": {
type: "string",
apply: (value, ext) => {
try {
const newState = JSON.parse(value || "{}");
debug("extension", "saved-windows changed: reloading tracker state");
ext._helper.getTracker()?.restoreFromState(newState);
} catch (e) {
debug("extension", `Error restoring state from GSettings: ${e.message}`, true);
}
},
},
};
class SmartAutoMoveService {
constructor(extension) {
this._extension = extension;
}
ListWindows() {
debug("extension", "ListWindows D-Bus method called");
if (
this._extension._helper &&
this._extension._helper.getExecutor()
) {
const windows = this._extension._helper
.getExecutor()
.listNormalWindows();
debug("extension", `ListWindows found ${windows.length} windows`);
return JSON.stringify(windows);
}
return JSON.stringify([]);
}
RefreshFromCurrentActors() {
debug("extension", "RefreshFromCurrentActors D-Bus method called");
if (this._extension._helper) {
this._extension._helper.refreshWindowState();
}
}
}
export default class SmartAutoMove extends Extension {
enable() {
this._setupDBus();
this._settings = this.getSettings();
this._migrateSettingsIfNeeded();
this._loadExtensionSettings();
setDebugEnabled(this._settings.get_boolean("debug-logging"));
this._helper = new WindowManager({
config: this._createConfigFromSettings(),
stateLoader: createGSettingsStorage(this._settings, "saved-windows").stateLoader,
stateSaver: (state) => this._saveState(state),
readOnly: this._freezeSaves,
executorConfig: { activate_on_move: this._activateWorkspace },
operationFilter: (op) => this._operationFilter(op)
});
this._helper.enable();
this._cleanupStaleEntries();
this._settingsId = this._settings.connect("changed", (settings, key) => {
this._handleSettingChanged(key);
});
debug("extension", "SmartAutoMove extension enabled");
}
_setupDBus() {
this._dbusImpl = new SmartAutoMoveService(this);
this._dbusExport = Gio.DBusExportedObject.wrapJSObject(DBUS_INTERFACE, this._dbusImpl);
this._ownerId = Gio.bus_own_name(
Gio.BusType.SESSION,
DBUS_NAME,
Gio.BusNameOwnerFlags.NONE,
() => {},
(connection, name) => {
debug("extension", `D-Bus name ${name} acquired, exporting service`);
this._dbusExport.export(connection, DBUS_PATH);
},
() => {
debug("extension", "D-Bus name lost");
if (this._dbusExport) this._dbusExport.unexport();
}
);
}
_saveState(state) {
// remember the exact value we write so _handleSettingChanged can ignore the
// asynchronous dconf echo of our own write; only genuine external edits differ
const json = JSON.stringify(state);
this._lastSavedWindows = json;
try {
this._settings.set_string("saved-windows", json);
} catch (e) {
debug("extension", `Error saving state: ${e.message}`, true);
}
}
disable() {
debug("extension", "SmartAutoMove extension disabled");
if (this._settingsId) {
this._settings.disconnect(this._settingsId);
this._settingsId = null;
}
if (this._helper) {
this._helper.disable();
this._helper = null;
}
if (this._dbusExport) {
this._dbusExport.unexport();
this._dbusExport = null;
}
if (this._ownerId) {
Gio.bus_unown_name(this._ownerId);
this._ownerId = null;
}
this._dbusImpl = null;
this._settings = null;
this._lastSavedWindows = null;
}
_migrateSettingsIfNeeded() {
migrateSettings(this._settings);
}
_cleanupStaleEntries() {
if (this._helper && this._maxUnseenAge > 0) {
const hours = this._maxUnseenAge / SECONDS_PER_HOUR;
this._helper.getSession().cleanupStaleEntries(hours);
}
}
_operationFilter(op) {
if (this._ignorePosition && (op.type === "Place" || op.type === "Move")) return false;
if (this._ignoreWorkspace && op.type === "MoveToWorkspace") return false;
if (this._ignoreMonitor && op.type === "MoveToMonitor") return false;
return true;
}
_loadExtensionSettings() {
for (const [key, handler] of Object.entries(SETTING_HANDLERS)) {
if (handler.field) {
const rawValue = this._getSettingValue(key, handler.type);
this[handler.field] = handler.transform ? handler.transform(rawValue) : rawValue;
}
}
}
_getSettingValue(key, type) {
switch (type) {
case "boolean": return this._settings.get_boolean(key);
case "int": return this._settings.get_int(key);
case "double": return this._settings.get_double(key);
case "enum": return this._settings.get_enum(key);
case "string": return this._settings.get_string(key);
default: return this._settings.get_string(key);
}
}
_handleSettingChanged(key) {
const handler = SETTING_HANDLERS[key];
if (!handler) {
this._helper.updateConfig(this._createConfigFromSettings());
return;
}
const rawValue = this._getSettingValue(key, handler.type);
// ignore the asynchronous dconf echo of our own saved-windows write; genuine
// external edits (e.g. from the prefs dialog) differ and are still applied
if (key === "saved-windows" && rawValue === this._lastSavedWindows) {
return;
}
const value = handler.transform ? handler.transform(rawValue) : rawValue;
if (handler.field) {
this[handler.field] = value;
}
if (handler.apply) {
handler.apply(value, this);
}
}
_createConfigFromSettings() {
return {
SETTLE_IDLE_TIMEOUT: this._settings.get_int("new-window-title-stability-ms"),
SETTLE_MAX_WAIT: this._settings.get_int("new-window-max-wait-ms"),
GENERIC_TITLE_EXTENDED_WAIT: this._settings.get_int("generic-title-extended-wait"),
MIN_SCORE_SPREAD: this._settings.get_double("min-score-spread"),
AMBIGUOUS_SIMILARITY_THRESHOLD: this._settings.get_double("ambiguity-threshold"),
MIN_SPECIFIC_TITLE_LENGTH: this._settings.get_int("generic-title-max-length"),
OVERRIDES: this._overrides,
DEFAULT_SYNC_MODE: this._syncMode,
DEFAULT_MATCH_THRESHOLD: this._matchThreshold,
};
}
}