forked from kwin-scripts/kwin-tiling
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tile.js
510 lines (471 loc) · 18.2 KB
/
tile.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
/********************************************************************
KWin - the KDE window manager
This file is part of the KDE project.
Copyright (C) 2012 Mathias Gottschlag <mgottschlag@gmail.com>
Copyright (C) 2013-2017 Fabian Homborg <FHomborg@gmail.com>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
*********************************************************************/
/**
* Class which manages the windows in one tile and handles resize/move and
* property change events.
* @class
*/
function Tile(firstClient, tileIndex) {
try {
/**
* Signal which is triggered whenever the user starts to move the tile.
*/
this.movingStarted = new Signal();
/**
* Signal which is triggered whenever the user stops moving the tile.
*/
this.movingEnded = new Signal();
/**
* Signal which is triggered whenever the geometry changes between
* movingStarted and movingEnded.
*/
this.movingStep = new Signal();
/**
* Signal which is triggered whenever the user starts to resize the tile.
*/
this.resizingStarted = new Signal();
/**
* Signal which is triggered whenever the user stops resizing the tile.
*/
this.resizingEnded = new Signal();
/**
* Signal which is triggered whenever the geometry changes between
* resizingStarted and resizingEnded.
*/
this.resizingStep = new Signal();
/**
* Signal which is triggered whenever the tile is moved to a different
* screen. Two parameters are passed to the handlers, the old and the new
* screen.
*/
this.screenChanged = new Signal();
/**
* Signal which is triggered whenever the tile is moved to a different
* desktop. Two parameters are passed to the handlers, the old and the new
* desktop.
*/
this.desktopChanged = new Signal();
/**
* Signal which is triggered whenever the tile's presence across
* activities changes. Two parameters are passed to the handlers, the
* tile's current desktop and the tile's current screen.
*/
this.activitiesChanged = new Signal();
/**
* List of the clients in this tile.
*/
this.clients = [];
this.originalx = util.middlex(firstClient.geometry);
this.originaly = util.middley(firstClient.geometry);
/**
* Index of this tile in the TileList to which the tile belongs.
*/
this.tileIndex = tileIndex;
/**
* True if this tile is currently moved by the user.
*/
this._moving = false;
/**
* True if this tile is currently moved by the user.
*/
this._resizing = false;
/**
* Stores the current screen of the tile in order to be able to detect
* movement between screens.
*/
this._currentScreen = firstClient.screen;
/**
* On wayland, certain actions cause a segfault if they happen
* more than once per frame. Keep track of the last time an
* action was run to stop the crash
*/
this._lastAction = 0;
/**
* Stores the current desktop as this is needed as a desktopChanged
* parameter.
*/
this._currentDesktop = util.getClientDesktop(firstClient);
this._activities = Array.from(firstClient.activities);
this.rectangle = null;
this.respectMinMax = KWin.readConfig("respectMinMax", true);
var gapSize = KWin.readConfig("gapSize", 0); /* stick to old gaps config by default */
this.windowsGapSizeHeight = KWin.readConfig("windowsGapSizeHeight", gapSize);
this.windowsGapSizeWidth = KWin.readConfig("windowsGapSizeWidth", gapSize);
this.screenGapSizeLeft = KWin.readConfig("screenGapSizeLeft", 0);
this.screenGapSizeRight = KWin.readConfig("screenGapSizeRight", 0);
this.screenGapSizeTop = KWin.readConfig("screenGapSizeTop", 0);
this.screenGapSizeBottom = KWin.readConfig("screenGapSizeBottom", 0);
if (KWin.readConfig("noBorder", false)) {
firstClient.noBorder = true;
}
this.maximized = false;
this._canSetMaximize = (firstClient.setMaximize != null);
this.clients.push(firstClient);
this.syncCustomProperties();
} catch(err) {
print(err, "in Tile");
}
};
/**
* Sets the geometry of the tile. geometryChanged events caused by this function
* are suppressed.
*
* @param geometry New tile geometry.
*/
Tile.prototype.setGeometry = function(geometry) {
try {
if (!geometry) {
return;
}
if (!this.rectangle) {
this.rectangle = util.copyRect(geometry);
} else {
util.setRect(this.rectangle, geometry);
}
for (var i = 0; i < this.clients.length; i++) {
this.setClientGeometry(this.clients[i]);
}
} catch(err) {
print(err, "in Tile.setGeometry");
}
};
Tile.prototype.resetGeometry = function() {
this.setGeometry(this.rectangle);
};
/**
* Returns the currently active client in the tile.
*/
Tile.prototype.getActiveClient = function() {
try {
var active = null;
this.clients.forEach(function(client) {
active = client;
});
return active;
} catch(err) {
print(err, "in Tile.getActiveClient");
}
};
/**
* Synchronizes all custom properties (tileIndex, floating between all clients
* in the tile).
*/
Tile.prototype.syncCustomProperties = function() {
try {
var client = this.getActiveClient();
if (!client) {
client = this.clients[0];
}
if (client) {
client.tiling_tileIndex = this.tileIndex;
}
} catch(err) {
print(err, "in Tile.syncCustomProperties");
}
};
Tile.prototype.onClientGeometryChanged = function(client) {
this.setClientGeometry(client);
};
Tile.prototype.setClientGeometry = function(client) {
try {
if (!client) {
return;
}
if (client.tiling_resize) {
return;
}
if (client.fullScreen) {
return;
}
if (this.maximized) {
return;
}
if (!this.hasClient(client)) {
print("Wrong tile called");
return;
}
// These two should never be reached
if (client.deleted) {
return;
}
if (client.move
|| client.resize
|| !client.resizeable
|| !client.moveable) {
return;
}
if (this._moving || this._resizing) {
return;
}
// This client is bogus
if (client.minSize.width == client.maxSize.width && client.minSize.height == client.maxSize.width) {
return;
}
if (this.rectangle) {
// We set keepBelow here to keep tiling clients below,
// also because that allows us to not set it for floating ones.
client.keepBelow = true;
if (client.screen != this._currentScreen) {
this._currentScreen = client.screen;
}
// Respect min/maxSize
var changedRect = false;
var screenRect = util.getTilingArea(this._currentScreen, this._currentDesktop);
// We cannot accomodate this. The client is wrong.
if (client.minSize.width > screenRect.width
|| client.minSize.height > screenRect.height) {
return;
}
if (client.minSize.width > this.rectangle.width) {
if (this.rectangle.x + this.rectangle.width == screenRect.x + screenRect.width - this.screenGapSizeRight) {
this.rectangle.x = (screenRect.x + screenRect.width - this.screenGapSizeRight) - client.minSize.width;
}
this.rectangle.width = client.minSize.width + this.windowsGapSizeWidth;
changedRect = true;
}
if (client.minSize.height > this.rectangle.height) {
if (this.rectangle.y + this.rectangle.height == screenRect.y + screenRect.height - this.screenGapSizeBottom) {
this.rectangle.y = (screenRect.y + screenRect.height - this.screenGapSizeBottom) - client.minSize.height;
}
this.rectangle.height = client.minSize.height + this.windowsGapSizeHeight;
changedRect = true;
}
if (client.maxSize.width < this.rectangle.width && client.maxSize.width > 0) {
if (this.rectangle.x + this.rectangle.width == screenRect.x + screenRect.width - this.screenGapSizeRight) {
this.rectangle.x = (screenRect.x + screenRect.width - this.screenGapSizeRight) - client.maxSize.width;
}
this.rectangle.width = client.maxSize.width + this.windowsGapSizeWidth;
changedRect = true;
}
if (client.maxSize.height < this.rectangle.height && client.maxSize.height > 0) {
if (this.rectangle.y + this.rectangle.height == screenRect.y + screenRect.height - this.screenGapSizeBottom) {
this.rectangle.y = (screenRect.y + screenRect.height - this.screenGapSizeBottom) - client.maxSize.height;
}
this.rectangle.height = client.maxSize.height + this.windowsGapSizeHeight;
changedRect = true;
}
if (client.shade) {
this.rectangle.height = client.geometry.height;
changedRect = true;
}
// Adjust to the clients basic increments by rounding down to the nearest unit.
// This will result in a new resize event (changedRect = true)
// which will then adjust the other clients to match.
// (it is possible that no perfect fit is possible,
// which should not cause an infinite loop as all tiles are still marked tiling_resize).
// basicUnit is apparently not a thing on wayland, so check if it exists.
if (client.basicUnit) {
if (client.basicUnit.width > 1) {
// floor(deltaWidth / basicUnit) gives the delta in basicUnits.
// The rest recomputes the width.
var newWidth = Math.floor((this.rectangle.width - client.geometry.width) / client.basicUnit.width)
* client.basicUnit.width + client.geometry.width;
// If the right edge changed, change the width to match.
// If the left edge changed, change that.
// Assumption: Either both edges don't change at the same time
// or it doesn't matter which we pick.
// Not doing this causes corruption (e.g. with a scale of 1.5, unfullscreening vlc).
if (client.geometry.x + client.geometry.width != this.rectangle.x + this.rectangle.width) {
this.rectangle.width = newWidth;
} else if (client.geometry.x != this.rectangle.x) {
this.rectangle.x = this.rectangle.x + this.rectangle.width - newWidth;
}
changedRect = true;
}
if (client.basicUnit.height > 1) {
// As above, change the edge that changed.
var newHeight = Math.floor((this.rectangle.height - client.geometry.height) / client.basicUnit.height)
* client.basicUnit.height + client.geometry.height;
if (client.geometry.y + client.geometry.height != this.rectangle.y + this.rectangle.height) {
this.rectangle.height = newHeight;
} else if (client.geometry.y != this.rectangle.y) {
this.rectangle.y = this.rectangle.y + this.rectangle.height - newHeight;
}
changedRect = true;
}
}
client.tiling_resize = true;
client.geometry = util.copyRect(this.rectangle);
if (changedRect) {
this._resizing = true;
this.resizingEnded.emit();
this._resizing = false;
}
client.tiling_resize = false;
} else {
print("No rectangle", client.resourceClass.toString(), client.windowId);
}
} catch(err) {
print(err, "in Tile.setClientGeometry");
}
};
Tile.prototype.onClientActivitiesChanged = function(client) {
try {
var activities = Array.from(client.activities);
var emit = false;
if (this._activities.length == activities.length) {
for (var i = 0; i < activities.length; i++) {
if (!this._activities.includes(activities[i])) {
emit = true;
break;
}
}
} else {
emit = true;
}
if (emit) {
this._activities = activities;
this.activitiesChanged.emit();
}
} catch(err) {
print(err, "in Tile.onClientActivitiesChanged");
}
};
Tile.prototype.onClientDesktopChanged = function(client) {
try {
var oldDesktop = this._currentDesktop;
this._currentDesktop = util.getClientDesktop(client);
this.desktopChanged.emit(oldDesktop, this._currentDesktop);
} catch(err) {
print(err, "in Tile.onClientDesktopChanged");
}
};
Tile.prototype.onClientScreenChanged = function(client) {
try {
var oldScreen = this._currentScreen;
this._currentScreen = client.screen;
this.screenChanged.emit(oldScreen, this._currentScreen);
} catch(err) {
print(err, "in Tile.onClientScreenChanged");
}
};
Tile.prototype.onClientStartUserMovedResized = function(client) {
// Let client stay above the other tilers so the user sees the move
client.keepBelow = false;
};
Tile.prototype.onClientStepUserMovedResized = function(client) {
var ms = (new Date).getTime();
// On wayland if this runs twice in the same frame
// it causes a segfault
// Don't allow this to run more than once per frame
// 16.666 for 60fps, but give some additional time.
if (ms <= this._lastAction+25) return;
this._lastAction = ms;
try {
if (client.resize) {
this._resizing = true;
this.resizingStep.emit();
// This means it gets "animated"
this.resizingEnded.emit();
return;
}
if (client.move) {
this._moving = true;
this.movingStep.emit();
this.movingEnded.emit();
return;
}
} catch(err) {
print(err, "in Tile.onClientStepUserMovedResized");
}
};
Tile.prototype.onClientFinishUserMovedResized = function(client) {
try {
if (this._moving) {
this._moving = false;
this.movingEnded.emit();
} else if (this._resizing) {
this._resizing = false;
this.resizingEnded.emit();
}
// Put the client on the same layer as the other tilers again
client.keepBelow = true;
} catch(err) {
print(err, "in Tile.onClientFinishUserMovedResized");
}
};
Tile.prototype.removeClient = function(client) {
try {
this.clients.splice(this.clients.indexOf(client), 1);
} catch(err) {
print(err, "in Tile.removeClient");
}
};
Tile.prototype.addClient = function(client) {
try {
if (this.clients.indexOf(client) == -1) {
client.keepBelow = true;
if (KWin.readConfig("noBorder", false)) {
client.noBorder = true;
}
this.clients.push(client);
this.syncCustomProperties();
this.setClientGeometry(client);
}
} catch(err) {
print(err, "in Tile.addClient");
}
};
Tile.prototype.onClientMaximizedStateChanged = function(client, h, v) {
try {
// Set keepBelow to keep maximized clients over tiled ones
// TODO: We don't distinguish between horizontal and vertical maximization,
// also there's no way to find that the _user_ caused this.
// So we might want to ignore maximization entirely.
if (h || v) {
client.keepBelow = false;
// We might get a geometryChanged signal before this
// so we need to manually maximize the client.
client.tiling_resize = true;
client.geometry = workspace.clientArea(KWin.MaximizeFullArea, this._currentScreen, this._currentDesktop);
client.tiling_resize = false;
this.maximized = true;
} else {
this.maximized = false;
client.keepBelow = true;
}
} catch(err) {
print(err, "in tile.onClientMaximizedStateChanged");
}
};
Tile.prototype.hasClient = function(client) {
return (this.clients.indexOf(client) > -1);
};
Tile.prototype.getActivities = function() {
return Array.from(this._activities);
};
Tile.prototype.getDesktop = function() {
return this._currentDesktop;
};
Tile.prototype.getScreen = function() {
return this._currentScreen;
};
Tile.prototype.unmaximize = function() {
if (this._canSetMaximize == true && this.maximized == true) {
this.clients.forEach(function(c) {
c.setMaximize(false, false);
});
}
};
Tile.prototype.setKeepBelow = function(setting) {
this.clients.forEach(function(c) {
c.keepBelow = setting;
});
};
Tile.prototype.onAllActivities = function() {
return this._activities.length == 0;
};