forked from tuxor1337/hidetopbar
-
Notifications
You must be signed in to change notification settings - Fork 0
/
intellihide.js
384 lines (325 loc) · 12.5 KB
/
intellihide.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
/**
* This file is part of Hide Top Bar
*
* Copyright 2020 Thomas Vogt
*
* 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 3 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 <https://www.gnu.org/licenses/>.
*/
// Note that the code in this file is taken from the Dash to Dock Gnome Shell
// extension (https://github.com/micheleg/dash-to-dock) with only minor
// modifications. Dash to Dock is distributed under the terms of the GNU
// General Public License, version 2 or later.
import GLib from 'gi://GLib';
import Meta from 'gi://Meta';
import Shell from 'gi://Shell';
import * as Main from 'resource:///org/gnome/shell/ui/main.js';
import * as Signals from 'resource:///org/gnome/shell/misc/signals.js';
import * as Convenience from './convenience.js';
// A good compromise between reactivity and efficiency; to be tuned.
export const INTELLIHIDE_CHECK_INTERVAL = 100;
export const OverlapStatus = {
UNDEFINED: -1,
FALSE: 0,
TRUE: 1
};
export const IntellihideMode = {
ALL_WINDOWS: 0,
FOCUS_APPLICATION_WINDOWS: 1,
MAXIMIZED_WINDOWS : 2
};
// List of windows type taken into account. Order is important (keep the
// original enum order).
export const handledWindowTypes = [
Meta.WindowType.NORMAL,
Meta.WindowType.DOCK,
Meta.WindowType.DIALOG,
Meta.WindowType.MODAL_DIALOG,
Meta.WindowType.TOOLBAR,
Meta.WindowType.MENU,
Meta.WindowType.UTILITY,
Meta.WindowType.SPLASHSCREEN
];
/**
* A rough and ugly implementation of the intellihide behaviour.
* Intallihide object: emit 'status-changed' signal when the overlap of windows
* with the provided targetBoxClutter.ActorBox changes;
*/
export class Intellihide extends Signals.EventEmitter {
constructor(settings, monitorIndex) {
super();
// Load settings
this._settings = settings;
this._monitorIndex = monitorIndex;
this._signalsHandler = new Convenience.GlobalSignalsHandler();
this._tracker = Shell.WindowTracker.get_default();
// The application whose window is focused.
this._focusApp = null;
// The application whose window is on top on the monitor with the panel.
this._topApp = null;
this._isEnabled = false;
this._status = OverlapStatus.UNDEFINED;
this._targetBox = null;
this._checkOverlapTimeoutContinue = false;
this._checkOverlapTimeoutId = 0;
this._trackedWindows = new Map();
// Connect global signals
this._signalsHandler.add([
// Listen for notification banners to appear or disappear
Main.messageTray,
'show',
this._checkOverlap.bind(this)
], [
Main.messageTray,
'hide',
this._checkOverlap.bind(this)
], [
// Add signals on windows created from now on
global.display,
'window-created',
this._windowCreated.bind(this)
], [
// triggered for instance when the window list order changes,
// included when the workspace is switched
global.display,
'restacked',
this._checkOverlap.bind(this)
], [
// when windows are alwasy on top, the focus window can change
// without the windows being restacked. Thus monitor window focus
// change.
this._tracker,
'notify::focus-app',
this._checkOverlap.bind(this)
], [
// updates when monitor changes, for instance in multimonitor, when
// monitors are attached
Convenience.getMonitorManager(),
'monitors-changed',
this._checkOverlap.bind(this)
]);
}
destroy() {
// Disconnect global signals
this._signalsHandler.destroy();
// Remove residual windows signals
this.disable();
}
enable() {
this._isEnabled = true;
this._status = OverlapStatus.UNDEFINED;
global.get_window_actors().forEach(function(wa) {
this._addWindowSignals(wa);
}, this);
this._doCheckOverlap();
}
disable() {
this._isEnabled = false;
for (let wa of this._trackedWindows.keys()) {
this._removeWindowSignals(wa);
}
this._trackedWindows.clear();
if (this._checkOverlapTimeoutId > 0) {
GLib.source_remove(this._checkOverlapTimeoutId);
this._checkOverlapTimeoutId = 0;
}
}
_windowCreated(display, metaWindow) {
this._addWindowSignals(metaWindow.get_compositor_private());
}
_addWindowSignals(wa) {
if (!this._handledWindow(wa))
return;
let signalId = wa.connect(
'notify::allocation', this._checkOverlap.bind(this)
);
this._trackedWindows.set(wa, signalId);
wa.connect('destroy', this._removeWindowSignals.bind(this));
}
_removeWindowSignals(wa) {
if (this._trackedWindows.get(wa)) {
wa.disconnect(this._trackedWindows.get(wa));
this._trackedWindows.delete(wa);
}
}
updateTargetBox(box) {
this._targetBox = box;
this._checkOverlap();
}
forceUpdate() {
this._status = OverlapStatus.UNDEFINED;
this._doCheckOverlap();
}
getOverlapStatus() {
return (this._status == OverlapStatus.TRUE);
}
_checkOverlap() {
if (!this._isEnabled || (this._targetBox == null))
return;
/* Limit the number of calls to the doCheckOverlap function */
if (this._checkOverlapTimeoutId) {
this._checkOverlapTimeoutContinue = true;
return
}
this._doCheckOverlap();
this._checkOverlapTimeoutId = GLib.timeout_add(
GLib.PRIORITY_DEFAULT, INTELLIHIDE_CHECK_INTERVAL, () => {
this._doCheckOverlap();
if (this._checkOverlapTimeoutContinue) {
this._checkOverlapTimeoutContinue = false;
return GLib.SOURCE_CONTINUE;
} else {
this._checkOverlapTimeoutId = 0;
return GLib.SOURCE_REMOVE;
}
});
}
_doCheckOverlap() {
if (!this._isEnabled || (this._targetBox == null))
return;
let overlaps = OverlapStatus.FALSE;
let windows = global.get_window_actors();
if (windows.length > 0) {
/*
* Get the top window on the monitor where the dock is placed.
* The idea is that we dont want to overlap with the windows of the
* topmost application, event is it's not the focused app -- for
* instance because in multimonitor the user select a window in the
* secondary monitor.
*/
let topWindow = null;
for (let i = windows.length - 1; i >= 0; i--) {
let meta_win = windows[i].get_meta_window();
if (
this._handledWindow(windows[i])
&& (meta_win.get_monitor() == this._monitorIndex)
) {
topWindow = meta_win;
break;
}
}
if (topWindow !== null) {
this._topApp = this._tracker.get_window_app(topWindow);
// If there isn't a focused app, use that of the window on top
this._focusApp = this._tracker.focus_app || this._topApp
windows = windows.filter(
this._intellihideFilterInteresting, this,
);
for (let i = 0; i < windows.length; i++) {
let win = windows[i].get_meta_window();
if (win) {
let rect = win.get_frame_rect();
let test = (rect.x < this._targetBox.x2) &&
(rect.x + rect.width > this._targetBox.x1) &&
(rect.y < this._targetBox.y2) &&
(rect.y + rect.height > this._targetBox.y1);
if (test) {
overlaps = OverlapStatus.TRUE;
break;
}
}
}
}
}
// Check if notification banner overlaps
if (Main.messageTray.visible) {
let rect = Main.messageTray._bannerBin.get_allocation_box();
let test = (rect.x1 < this._targetBox.x2) &&
(rect.x2 > this._targetBox.x1) &&
(rect.y1 < this._targetBox.y2) &&
(rect.y2 > this._targetBox.y1);
if (test) overlaps = OverlapStatus.TRUE;
}
if (this._status !== overlaps) {
this._status = overlaps;
this.emit('status-changed', this._status);
}
}
// Filter interesting windows to be considered for intellihide.
// Consider all windows visible on the current workspace.
// Optionally skip windows of other applications
_intellihideFilterInteresting(wa) {
let meta_win = wa.get_meta_window();
if (!this._handledWindow(wa))
return false;
let currentWorkspace = (
global.workspace_manager.get_active_workspace_index()
);
let wksp = meta_win.get_workspace();
let wksp_index = wksp.index();
// Depending on the intellihide mode, exclude non-relevent windows
if (this._settings.get_boolean('enable-active-window')) {
// Skip windows of other apps
if (this._focusApp) {
// The DropDownTerminal extension is not an application per
// se so we match its window by wm class instead
if (meta_win.get_wm_class() == 'DropDownTerminalWindow')
return true;
let currentApp = this._tracker.get_window_app(meta_win);
let focusWindow = global.display.get_focus_window()
// Consider half maximized windows side by side
// and windows which are alwayson top
if(
(currentApp != this._focusApp)
&& (currentApp != this._topApp)
&& !(
focusWindow
&& focusWindow.maximized_vertically
&& !focusWindow.maximized_horizontally
&& meta_win.maximized_vertically
&& !meta_win.maximized_horizontally
&& (
meta_win.get_monitor()
== focusWindow.get_monitor()
)
)
&& !meta_win.is_above()
) {
return false;
}
}
}
if (
wksp_index == currentWorkspace
&& meta_win.showing_on_its_workspace()
) {
return true;
} else {
return false;
}
}
// Filter windows by type
// inspired by Opacify@gnome-shell.localdomain.pl
_handledWindow(wa) {
let metaWindow = wa.get_meta_window();
if (!metaWindow)
return false;
const ignoreApps = [ "com.rastersoft.ding", "com.desktop.ding" ];
const wmApp = metaWindow.get_gtk_application_id();
if (ignoreApps.includes(wmApp) && metaWindow.is_skip_taskbar())
return false;
// The DropDownTerminal extension uses the POPUP_MENU window type hint
// so we match its window by wm class instead
if (metaWindow.get_wm_class() == 'DropDownTerminalWindow')
return true;
let wtype = metaWindow.get_window_type();
for (let i = 0; i < handledWindowTypes.length; i++) {
let hwtype = handledWindowTypes[i];
if (hwtype == wtype)
return true;
else if (hwtype > wtype)
return false;
}
return false;
}
};