forked from paperwm/PaperWM
-
Notifications
You must be signed in to change notification settings - Fork 0
/
overviewlayout.js
284 lines (233 loc) · 10.1 KB
/
overviewlayout.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
import * as Workspace from 'resource:///org/gnome/shell/ui/workspace.js';
import * as Util from 'resource:///org/gnome/shell/misc/util.js';
import * as Params from 'resource:///org/gnome/shell/misc/params.js';
import { Settings, Tiling } from './imports.js';
/**
* Gnome 45's UnalignedLayoutStrategy is not exported. Hence, we recreate this class
* with modifications to ensure window ordering reflects tiling window order in overview.
*
* See https://gitlab.gnome.org/GNOME/gnome-shell/-/blob/gnome-45/js/ui/workspace.js
*/
export class UnalignedLayoutStrategy extends Workspace.LayoutStrategy {
_newRow() {
// Row properties:
//
// * x, y are the position of row, relative to area
//
// * width, height are the scaled versions of fullWidth, fullHeight
//
// * width also has the spacing in between windows. It's not in
// fullWidth, as the spacing is constant, whereas fullWidth is
// meant to be scaled
//
// * neither height/fullHeight have any sort of spacing or padding
return {
x: 0, y: 0,
width: 0, height: 0,
fullWidth: 0, fullHeight: 0,
windows: [],
};
}
// Computes and returns an individual scaling factor for @window,
// to be applied in addition to the overall layout scale.
_computeWindowScale(window) {
// Since we align windows next to each other, the height of the
// thumbnails is much more important to preserve than the width of
// them, so two windows with equal height, but maybe differering
// widths line up.
let ratio = window.boundingBox.height / this._monitor.height;
// The purpose of this manipulation here is to prevent windows
// from getting too small. For something like a calculator window,
// we need to bump up the size just a bit to make sure it looks
// good. We'll use a multiplier of 1.5 for this.
// Map from [0, 1] to [1.5, 1]
return Util.lerp(1.5, 1, ratio);
}
_computeRowSizes(layout) {
let { rows, scale } = layout;
for (let i = 0; i < rows.length; i++) {
let row = rows[i];
row.width = row.fullWidth * scale + (row.windows.length - 1) * this._columnSpacing;
row.height = row.fullHeight * scale;
}
}
_keepSameRow(row, window, width, idealRowWidth) {
// enforce a minimum number of windows per overview row
if (row.windows.length < Settings.prefs.overview_min_windows_per_row) {
return true;
}
if (row.fullWidth + width <= idealRowWidth)
return true;
let oldRatio = row.fullWidth / idealRowWidth;
let newRatio = (row.fullWidth + width) / idealRowWidth;
if (Math.abs(1 - newRatio) < Math.abs(1 - oldRatio))
return true;
return false;
}
computeLayout(windows, layoutParams) {
layoutParams = Params.parse(layoutParams, {
numRows: 0,
});
if (layoutParams.numRows === 0)
throw new Error(`${this.constructor.name}: No numRows given in layout params`);
let numRows = layoutParams.numRows;
let rows = [];
let totalWidth = 0;
for (let i = 0; i < windows.length; i++) {
let window = windows[i];
let s = this._computeWindowScale(window);
totalWidth += window.boundingBox.width * s;
}
let idealRowWidth = totalWidth / numRows;
let sortedWindows = windows.slice();
// sorting needs to be done here to address moved windows
sortedWindows.sort(sortWindows);
let windowIdx = 0;
for (let i = 0; i < numRows; i++) {
let row = this._newRow();
rows.push(row);
for (; windowIdx < sortedWindows.length; windowIdx++) {
let window = sortedWindows[windowIdx];
let s = this._computeWindowScale(window);
let width = window.boundingBox.width * s;
let height = window.boundingBox.height * s;
row.fullHeight = Math.max(row.fullHeight, height);
// either new width is < idealWidth or new width is nearer from idealWidth then oldWidth
if (this._keepSameRow(row, window, width, idealRowWidth) || (i === numRows - 1)) {
row.windows.push(window);
row.fullWidth += width;
} else {
break;
}
}
}
let gridHeight = 0;
let maxRow;
for (let i = 0; i < numRows; i++) {
let row = rows[i];
if (!maxRow || row.fullWidth > maxRow.fullWidth)
maxRow = row;
gridHeight += row.fullHeight;
}
return {
numRows,
rows,
maxColumns: maxRow.windows.length,
gridWidth: maxRow.fullWidth,
gridHeight,
};
}
computeScaleAndSpace(layout, area) {
let hspacing = (layout.maxColumns - 1) * this._columnSpacing;
let vspacing = (layout.numRows - 1) * this._rowSpacing;
let spacedWidth = area.width - hspacing;
let spacedHeight = area.height - vspacing;
let horizontalScale = spacedWidth / layout.gridWidth;
let verticalScale = spacedHeight / layout.gridHeight;
// Thumbnails should be less than 70% of the original size
let scale = Math.min(
horizontalScale, verticalScale, Settings.prefs.overview_max_window_scale);
let scaledLayoutWidth = layout.gridWidth * scale + hspacing;
let scaledLayoutHeight = layout.gridHeight * scale + vspacing;
let space = (scaledLayoutWidth * scaledLayoutHeight) / (area.width * area.height);
layout.scale = scale;
return [scale, space];
}
computeWindowSlots(layout, area) {
this._computeRowSizes(layout);
let { rows, scale } = layout;
let slots = [];
// Do this in three parts.
let heightWithoutSpacing = 0;
for (let i = 0; i < rows.length; i++) {
let row = rows[i];
heightWithoutSpacing += row.height;
}
let verticalSpacing = (rows.length - 1) * this._rowSpacing;
let additionalVerticalScale = Math.min(1, (area.height - verticalSpacing) / heightWithoutSpacing);
// keep track how much smaller the grid becomes due to scaling
// so it can be centered again
let compensation = 0;
let y = 0;
for (let i = 0; i < rows.length; i++) {
let row = rows[i];
// If this window layout row doesn't fit in the actual
// geometry, then apply an additional scale to it.
let horizontalSpacing = (row.windows.length - 1) * this._columnSpacing;
let widthWithoutSpacing = row.width - horizontalSpacing;
let additionalHorizontalScale = Math.min(1, (area.width - horizontalSpacing) / widthWithoutSpacing);
if (additionalHorizontalScale < additionalVerticalScale) {
row.additionalScale = additionalHorizontalScale;
// Only consider the scaling in addition to the vertical scaling for centering.
compensation += (additionalVerticalScale - additionalHorizontalScale) * row.height;
} else {
row.additionalScale = additionalVerticalScale;
// No compensation when scaling vertically since centering based on a too large
// height would undo what vertical scaling is trying to achieve.
}
row.x = area.x + (Math.max(area.width - (widthWithoutSpacing * row.additionalScale + horizontalSpacing), 0) / 2);
row.y = area.y + (Math.max(area.height - (heightWithoutSpacing + verticalSpacing), 0) / 2) + y;
y += row.height * row.additionalScale + this._rowSpacing;
}
compensation /= 2;
for (let i = 0; i < rows.length; i++) {
const row = rows[i];
const rowY = row.y + compensation;
const rowHeight = row.height * row.additionalScale;
let x = row.x;
for (let j = 0; j < row.windows.length; j++) {
let window = row.windows[j];
let s = scale * this._computeWindowScale(window) * row.additionalScale;
let cellWidth = window.boundingBox.width * s;
let cellHeight = window.boundingBox.height * s;
s = Math.min(s, Settings.prefs.overview_max_window_scale);
let cloneWidth = window.boundingBox.width * s;
const cloneHeight = window.boundingBox.height * s;
let cloneX = x + (cellWidth - cloneWidth) / 2;
let cloneY;
// If there's only one row, align windows vertically centered inside the row
if (rows.length === 1)
cloneY = rowY + (rowHeight - cloneHeight) / 2;
// If there are multiple rows, align windows to the bottom edge of the row
else
cloneY = rowY + rowHeight - cellHeight;
// Align with the pixel grid to prevent blurry windows at scale = 1
cloneX = Math.floor(cloneX);
cloneY = Math.floor(cloneY);
slots.push([cloneX, cloneY, cloneWidth, cloneHeight, window]);
x += cellWidth + this._columnSpacing;
}
}
return slots;
}
}
/**
* Ensures windows are sorted correctly in overview (correctly being the tiled order in the space).
*/
export function sortWindows(a, b) {
let aw = a.metaWindow;
let bw = b.metaWindow;
if (!aw && !bw) {
return 0;
}
if (!aw) {
return -1;
}
if (!bw) {
return 1;
}
let spaceA = Tiling.spaces.spaceOfWindow(aw);
let spaceB = Tiling.spaces.spaceOfWindow(bw);
let ia = spaceA.indexOf(aw);
let ib = spaceB.indexOf(bw);
if (ia === -1 && ib === -1) {
return aw.get_stable_sequence() - bw.get_stable_sequence();
}
if (ia === -1) {
return -1;
}
if (ib === -1) {
return 1;
}
return ia - ib;
}