-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathutils.js
326 lines (272 loc) · 8.92 KB
/
utils.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
import Gio from 'gi://Gio';
export default class PutWindowUtils {
static CENTER_WIDTH_0 = 'center-width';
static CENTER_WIDTH_1 = 'center-width-1';
static CENTER_WIDTH_2 = 'center-width-2';
static CENTER_HEIGHT_0 = 'center-height';
static CENTER_HEIGHT_1 = 'center-height-1';
static CENTER_HEIGHT_2 = 'center-height-2';
static CENTER_KEEP_WIDTH = 'center-keep-width';
static ALWAYS_USE_WIDTHS = 'always-use-widths';
static CORNER_CHANGE = 'corner-changes';
static IGNORE_TOP_PANEL = 'ignore-top-panel';
static MOVE_CENTER_ONLY_TOGGLES = 'move-center-only-toggles';
static ALWAYS_KEEP_WIDTH = 'always-keep-width';
static ALWAYS_KEEP_HEIGHT = 'always-keep-height';
static MOVE_FOCUS_ENABLED = 'move-focus-enabled';
static MOVE_FOCUS_ANIMATION = 'move-focus-animation';
static ENABLE_MOVE_WORKSPACE = 'enable-move-workspace';
static INTELLIGENT_CORNER_MOVEMENT = 'intelligent-corner-movement';
constructor(settingsObject) {
this._changeEventListeners = [];
this._gnomeBindings = null;
this.loadSettings(settingsObject);
// locations is a json strings stored in the gschema. -> reload it after the user saved his changes
this._changeEventListeners.push({
name: 'locations',
fn: this.loadSettings.bind(this),
});
for (let i = 0; i < this._changeEventListeners.length; i++) {
this._changeEventListeners[i].handlerId = this._settingsObject.connect(
`changed::${this._changeEventListeners[i].name}`,
this._changeEventListeners[i].fn
);
}
}
destroy() {
for (let i = 0; i < this._changeEventListeners.length; i++) {
this._settingsObject.disconnect(this._changeEventListeners[i].handlerId);
}
}
getSettingsObject() {
return this._settingsObject;
}
loadSettings(settingsObject) {
this._settingsObject = settingsObject;
this._settings = {
locations: JSON.parse(this._settingsObject.get_string('locations')),
};
}
_loadGlobalBindings() {
this._gnomeBindings = new Gio.Settings({
settings_schema: Gio.SettingsSchemaSource.get_default().lookup('org.gnome.desktop.wm.keybindings', false),
});
this._mutterBindings = new Gio.Settings({
settings_schema: Gio.SettingsSchemaSource.get_default().lookup('org.gnome.mutter.keybindings', false),
});
}
keyboardBindingExists(key, value) {
if (this._gnomeBindings == null) {
this._loadGlobalBindings();
}
let bindingSource = 'PutWindow: ';
// compare with other extension settings
let keys = this._settingsObject.list_keys();
for (let i = 0; i < keys.length; i++) {
// dont compare with our self
if (keys[i] === key) {
continue;
}
if (keys[i].indexOf('move-') !== 0 && keys[i].indexOf('put-') !== 0) {
continue;
}
if (keys[i].length > 7 && keys[i].substring(-7) === 'enabled') {
continue;
}
if (value === this.get_strv(keys[i])) {
return bindingSource + keys[i];
}
}
bindingSource = 'GNOME Shell: ';
keys = this._gnomeBindings.list_keys();
for (let i = 0; i < keys.length; i++) {
if (value === this._gnomeBindings.get_strv(keys[i])) {
return bindingSource + keys[i];
}
}
bindingSource = 'Mutter: ';
keys = this._mutterBindings.list_keys();
for (let i = 0; i < keys.length; i++) {
if (value === this._mutterBindings.get_strv(keys[i])) {
return bindingSource + keys[i];
}
}
return false;
}
saveSettings() {
try {
this._settingsObject.set_string('locations', JSON.stringify(this._settings.locations));
} catch (e) {
this.logErrorMessage('Error saving settings ', e);
}
}
toggleMaximizeOnMoveCenter() {
return this.getBoolean('move-center-only-toggles', false);
}
/**
* ["0", "Both"],
* ["1", "Only height"],
* ["2", "Only width"],
* ["3", "Never change size"]
* ["4", "Nothing on first move, both on second"],
* ["5", "Nothing on first move, Only height on second"],
* ["6", "Nothing on first move, Only width on second"]["4", "Keep size on first move"],
*
* @returns {boolean}
*/
changeCornerHeight() {
const val = this.getNumber(PutWindowUtils.CORNER_CHANGE, 1);
return val === 0 || val === 1 || val === 4 || val === 5;
}
changeCornerWidth() {
const val = this.getNumber(PutWindowUtils.CORNER_CHANGE, 2);
return val === 0 || val === 2 || val === 4 || val === 6;
}
changeCornerFirstTime() {
const val = this.getNumber(PutWindowUtils.CORNER_CHANGE, 0);
return val < 4;
}
changeCornerNever() {
return this.getNumber(PutWindowUtils.CORNER_CHANGE, 0) === 3;
}
changeCornerBoth() {
const val = this.getNumber(PutWindowUtils.CORNER_CHANGE, 0);
return val === 0 || val === 4;
}
getNorthHeights() {
let ret = this._addToArray([], this.getNumber('north-height-0', 50) / 100);
ret = this._addToArray(ret, this.getNumber('north-height-1', 66) / 100);
ret = this._addToArray(ret, this.getNumber('north-height-2', 34) / 100);
return ret;
}
getSouthHeights() {
let ret = this._addToArray([], this.getNumber('south-height-0', 50) / 100);
ret = this._addToArray(ret, this.getNumber('south-height-1', 34) / 100);
ret = this._addToArray(ret, this.getNumber('south-height-2', 66) / 100);
return ret;
}
getWestWidths() {
let ret = this._addToArray([], this.getNumber('left-side-widths-0', 50) / 100);
ret = this._addToArray(ret, this.getNumber('left-side-widths-1', 34) / 100);
ret = this._addToArray(ret, this.getNumber('left-side-widths-2', 66) / 100);
return ret;
}
getEastWidths() {
let ret = this._addToArray([], this.getNumber('right-side-widths-0', 50) / 100);
ret = this._addToArray(ret, this.getNumber('right-side-widths-1', 66) / 100);
ret = this._addToArray(ret, this.getNumber('right-side-widths-2', 34) / 100);
return ret;
}
_addToArray(array, value) {
if (array.indexOf(value) === -1) {
array.push(value);
}
return array;
}
getBoolean(name, defaultValue) {
let ret = defaultValue;
if (name.indexOf('locations') === -1) {
ret = this._settingsObject.get_int(name);
} else {
ret = this.getParameter(name);
}
return ret == 1 || ret === true || ret === 'true' || ret === '1';
}
getNumber(name, defaultValue) {
if (name.indexOf('locations') === -1) {
return this._settingsObject.get_int(name);
}
return this._toNumber(this.getParameter(name, defaultValue), defaultValue);
}
get_strv(name) {
return this._settingsObject.get_strv(name);
}
set_strv(name, value) {
this._settingsObject.set_strv(name, value);
}
getParameter(name, defaultValue) {
const path = name.split('.');
const pathLength = path.length;
let value = this._settings[path[0]];
try {
for (let i = 1; i < pathLength; i++) {
value = value[path[i]];
}
return value;
} catch (e) {
this.logErrorMessage('Error getting parameter!', `Can not get config by name ${name} defaulting to ${defaultValue}'${e.message}`);
return defaultValue;
}
}
unsetParameter(name) {
const path = name.split('.');
const pathLength = path.length - 1;
let conf = this._settings[path[0]];
for (let i = 1; i < pathLength; i++) {
conf = conf[path[i]];
}
if (isNaN(path[pathLength])) {
// normal object
delete conf[path[pathLength]];
} else {
// an array
conf.pop(path[pathLength]);
}
}
setParameter(name, value) {
try {
if (name.indexOf('locations') === -1) {
if (isNaN(value)) {
this._settingsObject.set_string(name, value);
} else {
this._settingsObject.set_int(name, value);
}
return;
}
const path = name.split('.');
const pathLength = path.length - 1;
let conf = this._settings;
for (let i = 0; i < pathLength; i++) {
if (!conf[path[i]]) {
conf[path[i]] = {};
}
conf = conf[path[i]];
}
conf[path[pathLength]] = value;
} catch (e) {
this.logErrorMessage('Error setting parameter!', `Can not set config parameter ${name} ${e.message}`);
}
}
_toNumber(value, defaultValue) {
const valueType = typeof value;
if (valueType === 'undefined') {
return defaultValue;
}
if (isNaN(defaultValue)) {
defaultValue = 0;
}
return !isNaN(value)
? Number(value)
: defaultValue;
}
logErrorMessage(title, message) {
console.log(`ERROR: ${title} ${message}`);
}
getScreen() {
return global.screen || global.display;
}
getWorkspaceManager() {
return global.screen || global.workspace_manager;
}
getMonitorManager() {
// imported here since prefs.js cannot import Meta
const Meta = imports.gi.Meta;
if (global.screen) {
return global.screen;
}
if (Meta.MonitorManager.get) {
return Meta.MonitorManager.get();
}
return global.backend.get_monitor_manager();
}
}