forked from tdryer/tandem-raise
-
Notifications
You must be signed in to change notification settings - Fork 0
/
extension.js
84 lines (76 loc) · 2.83 KB
/
extension.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
const Meta = imports.gi.Meta;
let focusWindowNotifyConnection = null;
function init() { }
function enable() {
focusWindowNotifyConnection = global.display.connect(
'notify::focus-window', onFocusWindowNotify
);
}
function disable() {
global.display.disconnect(focusWindowNotifyConnection);
}
function onFocusWindowNotify() {
let focusedWindow = global.display.get_focus_window();
if (focusedWindow) {
let siblingWindow = findSiblingWindow(focusedWindow);
if (siblingWindow) {
// Raise the sibling window
siblingWindow.raise();
// Raise the focused window again so it's still on top of the sibling window
focusedWindow.raise();
}
}
}
function getWorkspaceCompat(window) {
if (window.get_screen) {
// GNOME 3.28
let screen = window.get_screen();
return screen.get_active_workspace();
} else {
// GNOME 3.30
let display = window.get_display();
let workspaceManager = display.get_workspace_manager();
return workspaceManager.get_active_workspace();
}
}
function findSiblingWindow(window) {
// Only vertically maximized windows can have a sibling
if (window.get_maximized() !== Meta.MaximizeFlags.VERTICAL) {
return null;
}
let windowRect = window.get_frame_rect();
let workAreaRect = window.get_work_area_current_monitor();
let windows = getWorkspaceCompat(window).list_windows();
for (let candidateWindow of windows) {
// Only consider other windows
let isOtherWindow = candidateWindow !== window;
// Only consider windows on the same monitor
let isSameMonitor = window.get_monitor() === candidateWindow.get_monitor();
// Only consider vertically maximized windows
let isVerticallyMaximized = candidateWindow.get_maximized() === Meta.MaximizeFlags.VERTICAL;
// Only consider windows that are horizontally tiled
let candidateWindowRect = candidateWindow.get_frame_rect();
let isHorizontallyTiled = horizontallyTiled(workAreaRect, candidateWindowRect, windowRect);
if (isOtherWindow && isSameMonitor && isVerticallyMaximized && isHorizontallyTiled) {
return candidateWindow;
}
}
return null;
}
function horizontallyTiled(workAreaRect, aRect, bRect) {
let leftRect = aRect;
let rightRect = bRect;
if (aRect.x > bRect.x) {
leftRect = bRect;
rightRect = aRect;
}
return (
workAreaRect.x === leftRect.x &&
// Work around this being off-by-one with Ubuntu Dock for some reason
(
leftRect.x + leftRect.width === rightRect.x ||
leftRect.x + leftRect.width === rightRect.x + 1
) &&
rightRect.x + rightRect.width === workAreaRect.x + workAreaRect.width
);
}