-
Notifications
You must be signed in to change notification settings - Fork 6
/
windowTilingSupport.js
223 lines (176 loc) · 7.81 KB
/
windowTilingSupport.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
'use strict';
import Shell from 'gi://Shell';
import Meta from 'gi://Meta';
import GObject from 'gi://GObject';
import * as Log from './utils/log.js';
import {PrefsUtils} from './utils/prefsUtils.js';
// Singleton class, all methods are `static`
export class WindowTilingSupport {
static initialize() {
this._log = new Log.Log();
this._settings = PrefsUtils.getSettings();
this._defaultAppSystem = Shell.AppSystem.get_default();
this._signals = new WindowTilingSupportSignals();
// Used for getting another raised signal id to prevent 'too much recursion' due to raising each other.
this._signalsConnectedMap = new Map();
this._grabbedWindowsAboutToUntileMap = new Map();
this._grabOpBeginId = global.display.connect('grab-op-begin', this._grabOpBegin.bind(this));
this._grabOpEndId = global.display.connect('grab-op-end', this._grabOpEnd.bind(this));
}
static prepareToTile(metaWindow, window_tiling) {
if (!window_tiling) return;
if (!this._settings.get_boolean('restore-window-tiling')) return;
const windowAboutToResize = this._getWindowAboutToResize(window_tiling);
if (!windowAboutToResize) return;
metaWindow._tile_match_awsm = windowAboutToResize;
windowAboutToResize._tile_match_awsm = metaWindow;
this._signals.emit('window-tiled', metaWindow, windowAboutToResize);
// Connect `raised` only once and this will prevent `JS ERROR: too much recursion`
if (!this._signalsConnectedMap.get(metaWindow)) {
const raisedId = metaWindow.connect('raised', () => {
const raisedTogether = this._settings.get_boolean('raise-windows-together');
if (raisedTogether) {
const anotherWindowRaisedId = this._signalsConnectedMap.get(windowAboutToResize);
windowAboutToResize.block_signal_handler(anotherWindowRaisedId);
windowAboutToResize.raise();
windowAboutToResize.unblock_signal_handler(anotherWindowRaisedId);
}
});
this._signalsConnectedMap.set(metaWindow, raisedId);
}
}
static _grabOpBegin(display, grabbedWindow, grabOp) {
// Fix `JS ERROR: TypeError: grabbedWindow is null` while `grab-op-begin` by `dash to panel`,
// who emits nullish grabbedWindow.
if (!grabbedWindow) return;
// Check if the grabbed window has been in a tiling state with another window
const windowAboutToResize = grabbedWindow._tile_match_awsm;
if (!windowAboutToResize || windowAboutToResize._tile_match_awsm !== grabbedWindow)
return;
// When position changed
if (grabOp === Meta.GrabOp.MOVING) {
const oldGrabbedWindowRect = grabbedWindow.get_frame_rect().copy();
this._grabbedWindowsAboutToUntileMap.set(grabbedWindow, oldGrabbedWindowRect);
return;
}
if (!this._settings.get_boolean('restore-window-tiling')) return;
this._sizeChangedId = grabbedWindow.connect('size-changed', () => {
const grabbedWindowRect = grabbedWindow.get_frame_rect();
const windowAboutToResizeRect = windowAboutToResize.get_frame_rect();
const grabbedWindowOnLeftSide = grabbedWindowRect.x < windowAboutToResizeRect.x;
let xywh = null;
if (grabbedWindowOnLeftSide) {
xywh = [
grabbedWindowRect.width,
windowAboutToResizeRect.y,
windowAboutToResizeRect.width - (grabbedWindowRect.width - windowAboutToResizeRect.x),
windowAboutToResizeRect.height];
} else {
xywh = [
windowAboutToResizeRect.x,
windowAboutToResizeRect.y,
grabbedWindowRect.x,
windowAboutToResizeRect.height];
}
if (xywh) {
windowAboutToResize.move_resize_frame(false, ...xywh);
}
});
}
static _grabOpEnd(display, grabbedWindow, grabOp) {
// grabbedWindow is null, tested on Fedora 35 with Gnome 41.6 and Wayland,
// by clicking the indicator show and then hide the popup menu
if (!grabbedWindow) return;
const oldGrabbedWindowRect = this._grabbedWindowsAboutToUntileMap.get(grabbedWindow);
const currentRect = grabbedWindow.get_frame_rect();
// Untile if any of x, y, width and height changed
if (oldGrabbedWindowRect &&
(oldGrabbedWindowRect.x !== currentRect.x
|| oldGrabbedWindowRect.y !== currentRect.y
|| oldGrabbedWindowRect.width !== currentRect.width
|| oldGrabbedWindowRect.height !== currentRect.height))
{
const anotherTilingWindow = grabbedWindow._tile_match_awsm;
this._log.debug(`Untiling ${grabbedWindow.get_title()}`);
delete grabbedWindow._tile_match_awsm;
if (anotherTilingWindow) {
this._log.debug(`Untiling ${anotherTilingWindow.get_title()}`);
delete anotherTilingWindow._tile_match_awsm;
}
this._grabbedWindowsAboutToUntileMap.delete(grabbedWindow);
this._disconnectRaisedSignals();
this._signals.emit('window-untiled', grabbedWindow, anotherTilingWindow);
}
if (this._sizeChangedId) {
grabbedWindow.disconnect(this._sizeChangedId);
this._sizeChangedId = 0;
}
}
static _getWindowAboutToResize(window_tiling) {
if (!window_tiling) return null;
const window_tile_for = window_tiling.window_tile_for;
const shellApp = this._defaultAppSystem.lookup_app(window_tile_for.desktop_file_id);
if (!shellApp) return null;
const windows = shellApp.get_windows();
if (!windows || !windows.length) return null;
let windowAboutToResize = null;
if (windows.length === 1) {
windowAboutToResize = windows[0];
} else {
// Get one window by matching title
for (const win of windows) {
if (win.get_title() === window_tile_for.window_title) {
windowAboutToResize = win;
break;
}
}
}
return windowAboutToResize;
}
static connect(signal, func) {
this._signals.connect(signal, func);
}
static disconnect(id) {
this._signals.disconnect(id);
}
static _disconnectRaisedSignals() {
if (this._signalsConnectedMap) {
this._signalsConnectedMap.forEach((id, obj) => {
obj.disconnect(id);
});
this._signalsConnectedMap.clear();
}
}
static destroy() {
if (this._grabbedWindowsAboutToUntileMap) {
this._grabbedWindowsAboutToUntileMap.clear();
this._grabbedWindowsAboutToUntileMap = null;
}
this._disconnectRaisedSignals();
this._signalsConnectedMap = null;
if (this._grabOpBeginId) {
global.display.disconnect(this._grabOpBeginId);
this._grabOpBeginId = 0;
}
if (this._grabOpEndId) {
global.display.disconnect(this._grabOpEndId);
this._grabOpEndId = 0;
}
}
}
const WindowTilingSupportSignals = GObject.registerClass({
Signals: {
'window-tiled': {
param_types: [Meta.Window.$gtype, Meta.Window.$gtype],
flags: GObject.SignalFlags.RUN_LAST,
},
'window-untiled': {
param_types: [Meta.Window.$gtype, Meta.Window.$gtype],
flags: GObject.SignalFlags.RUN_LAST,
},
}
}, class WindowTilingSupportSignals extends GObject.Object{
_init() {
super._init();
}
});