forked from GnomeSnapExtensions/gSnap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
snaptoneighbors.ts
210 lines (178 loc) · 6.17 KB
/
snaptoneighbors.ts
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
/* Edited by Sergey after
https://github.com/tpyl/gssnaptoneighbors
by Timo Pylvanainen <[email protected]>
*/
import { Window } from './gnometypes';
import {log} from './logging';
declare const imports: any;
declare const global: any;
const Meta = imports.gi.Meta;
const WorkspaceManager = global.screen || global.workspace_manager;
const OVERLAP_TOLERANCE = 5;
const SCAN_BOX_SIZE = 50;
/**
* Return all windows on the currently active workspace
*/
function getWindowsOnActiveWorkspace() {
let windows = [];
let windowActors = global.get_window_actors();
let curWorkSpace = WorkspaceManager.get_active_workspace();
for (let i = 0; i < windowActors.length; i++) {
let win = windowActors[i].meta_window;
if(win.located_on_workspace(curWorkSpace) &&
!win.minimized &&
win.get_frame_type() == 0) {
windows.push(win);
}
}
return windows;
}
/**
* Find the maximum horzontal expansion from x and
* returns minx, maxx. The initial maximum x is given
* as argument, and the expansion is never larger than
* that.
*
* The upper and lower limits define the y coordinate
* range to check for overlapping windows.
*/
function expandHorizontally(x, upper, lower, minx, maxx, windows) {
for (let i = 0; i < windows.length; i++) {
let rect = windows[i].get_frame_rect();
let wt = rect.y;
let wb = rect.y + rect.height;
let wl = rect.x;
let wr = rect.x + rect.width;
// Check only if the window overlaps vertically
if(wb > upper && wt < lower) {
if(wr < x && minx < wr) {
minx = wr;
}
if(wl > x && wl < maxx) {
maxx = wl;
}
}
}
return {min: minx, max: maxx};
}
/**
* Find the maximum vertical expansion from y, and
* returns miny, maxy. The initial maximum y is given
* as argument, and the expansion is never larger than
* that.
*
* The left and right limits define the x coordinate
* range to check for overlapping windows.
*/
function expandVertically(y, left, right, miny, maxy, windows) {
for (let i = 0; i < windows.length; i++) {
let rect = windows[i].get_frame_rect();
let wt = rect.y;
let wb = rect.y + rect.height;
let wl = rect.x;
let wr = rect.x + rect.width;
// Check only if the window overlaps horizontally
if(wr > left && wl < right) {
if(wb < y && miny < wb) {
miny = wb;
}
if(wt > y && wt < maxy) {
maxy = wt;
}
}
}
return {min: miny, max: maxy};
}
/**
* Type definition based on
* https://gjs-docs.gnome.org/meta6~6_api/meta.window#method-get_layer and
* https://developer.gnome.org/meta/stable/MetaWindow.html#meta-window-get-work-area-current-monitor.
*/
interface MetaWindow {
readonly maximized_horizontally: boolean;
readonly maximized_vertically: boolean;
get_title(): string;
unmaximize(flags: any): void;
get_work_area_current_monitor(): MetaRectangle;
get_frame_rect(): MetaRectangle;
/**
* https://gjs-docs.gnome.org/meta6~6_api/meta.window#method-move_resize_frame
*/
move_resize_frame(userOp: boolean, rootXNW: number, rootYNW: number, width: number, height: number): void;
}
/**
* Type definition based on https://gjs-docs.gnome.org/meta6~6_api/meta.rectangle.
*/
interface MetaRectangle {
readonly x: number;
readonly y: number;
readonly width: number;
readonly height: number;
}
/**
* Resize & move the *window* so it touches adjacent windows or
* screen edge top, bottom, left and right. The top-left corner
* of the window defines the expansion point.
*
* There is an L-ambiguity where the window could be expanded
* both vertically and horizontally. The expnasion that results
* in closer to 1 aspect ratio is selected.
*/
export function snapToNeighbors(window: Window) {
log("snapToNeighbors " + window.get_title());
// Unmaximize first
if (window.maximized_horizontally || window.maximized_vertically)
window.unmaximize(Meta.MaximizeFlags.HORIZONTAL | Meta.MaximizeFlags.VERTICAL);
let workArea = window.get_work_area_current_monitor();
let myrect = window.get_frame_rect();
let windows = getWindowsOnActiveWorkspace();
// Scan for overlapping windows in a thin bar around the top of the
// window. The vertical height of the window will be adjusted later.
let maxHorizw = expandHorizontally(
myrect.x + Math.min(SCAN_BOX_SIZE, myrect.width / 2),
myrect.y + Math.min(SCAN_BOX_SIZE, myrect.height / 2),
myrect.y + Math.min(SCAN_BOX_SIZE, myrect.height / 2) + SCAN_BOX_SIZE,
workArea.x,
workArea.x + workArea.width,
windows
);
let maxHorizh = expandVertically(
myrect.y + Math.min(SCAN_BOX_SIZE, myrect.height / 2),
maxHorizw.min + OVERLAP_TOLERANCE,
maxHorizw.max - OVERLAP_TOLERANCE,
workArea.y,
workArea.y + workArea.height,
windows)
let maxVerth = expandVertically(
myrect.y + Math.min(SCAN_BOX_SIZE, myrect.height / 2),
myrect.x + Math.min(SCAN_BOX_SIZE, myrect.width / 2),
myrect.x + Math.min(SCAN_BOX_SIZE, myrect.width / 2) + SCAN_BOX_SIZE,
workArea.y,
workArea.y + workArea.height,
windows)
let maxVertw = expandHorizontally(
myrect.x + Math.min(SCAN_BOX_SIZE, myrect.width / 2),
maxVerth.min + OVERLAP_TOLERANCE,
maxVerth.max - OVERLAP_TOLERANCE,
workArea.x,
workArea.x + workArea.width,
windows);
if ((maxHorizw.max - maxHorizw.min) * (maxHorizh.max - maxHorizh.min) >
(maxVertw.max - maxVertw.min) * (maxVerth.max - maxVerth.min)) {
window.move_resize_frame(
true,
maxHorizw.min,
maxHorizh.min,
maxHorizw.max - maxHorizw.min,
maxHorizh.max - maxHorizh.min
);
} else {
window.move_resize_frame(
true,
maxVertw.min,
maxVerth.min,
maxVertw.max - maxVertw.min,
maxVerth.max - maxVerth.min
);
}
}