-
Notifications
You must be signed in to change notification settings - Fork 2
/
windowtiler.js
288 lines (241 loc) · 8.62 KB
/
windowtiler.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
// Let's pollute the global namespace with these two functions so that we can
// keep a normal object-oriented paradigm the rest of the time.
function toArray(obj) {
return Array.prototype.slice.call(obj);
}
/**
* Creates a new window tiler.
* @constructor
*/
WindowTiler = function() {};
/**
* Array of all the screens for this instance of Chrome.
* @type {Array.<Object>}
*/
WindowTiler.prototype.screens;
/**
* Array of windows that currently need repositioning.
* @type {Array.<Object>}
*/
WindowTiler.prototype.windowsToReposition = [];
/**
* Array of windows that currently need verifying.
* @type {Array.<Object>}
*/
WindowTiler.prototype.windowsToVerify = [];
/**
* Starts the whole process of tiling windows.
* @param {chrome.windows.Tab} tab The tab from which the action was triggered.
*/
WindowTiler.prototype.start = function(tab) {
chrome.system.display.getInfo(this.onReceivedDisplayData.bind(this));
};
WindowTiler.prototype.onReceivedDisplayData = function(screens) {
this.screens = screens;
chrome.windows.getAll({"populate" : false},
this.onReceivedWindowsData.bind(this));
};
WindowTiler.prototype.findWindowsOnThisScreen = function(theWindows,
theScreen, allScreens) {
var windowsOnSelectedScreen = [];
for (var eachWindow, i = 0; eachWindow = theWindows[i]; i++) {
var maxOverlap = 0;
var screenWithMaxOverlap;
for (var eachScreen, j = 0; eachScreen = allScreens[j]; j++) {
var overlap = WindowTilerUtils.rectangleOverlap(
eachWindow.top, eachWindow.left, eachWindow.width, eachWindow.height,
eachScreen.bounds.top, eachScreen.bounds.left,
eachScreen.bounds.width, eachScreen.bounds.height);
if (overlap >= maxOverlap) {
maxOverlap = overlap;
screenWithMaxOverlap = eachScreen;
}
}
if (screenWithMaxOverlap == theScreen) {
windowsOnSelectedScreen.push(eachWindow);
}
}
return windowsOnSelectedScreen;
};
/**
* Callback for when we received data about the currently open windows.
* @param {Array.<chrome.windows.Window>} windows The array of open windows.
*/
WindowTiler.prototype.onReceivedWindowsData = function(windowsParam) {
var filters = [];
filters.push(this.windowIsNonMinimized);
var filteredWindows = this.filterWindows(windowsParam, filters);
var mainScreen;
for (var i = 0, eachScreen; eachScreen = this.screens[i]; i++) {
if (eachScreen.isPrimary) {
mainScreen = eachScreen;
break;
}
}
if (!mainScreen) {
alert('I cannot find the main screen! I\'m going to stop here, sorry.');
return;
}
for (var i = 0, eachScreen; eachScreen = this.screens[i]; i++) {
var windowsForThisScreen = this.findWindowsOnThisScreen(filteredWindows,
eachScreen, this.screens);
this.tileWindows(windowsForThisScreen, eachScreen);
}
};
/**
* Callback for when we're finished resizing a window.
* @param {chrome.windows.Window} myWindow The window that has just finished
* resizing.
*/
WindowTiler.prototype.finished = function(myWindow) {
// Do nothing for now.
};
WindowTiler.prototype.windowIsNonMinimized = function(theWindow) {
return theWindow.state != 'minimized';
};
WindowTiler.prototype.filterWindows = function(windowsParam, filters) {
var filtered = [];
for (var i = 0; i < windowsParam.length; i++) {
var shouldAdd = true;
for (var j = 0; j < filters.length; j++) {
shouldAdd &= filters[j](windowsParam[i]);
}
if (shouldAdd) {
filtered.push(windowsParam[i]);
}
}
return filtered;
}
WindowTiler.prototype.processAllWindowRepositioningRequests = function() {
if (this.windowsToReposition.length == 0) {
//this.verifyAllPositions();
this.finished();
return;
}
var tile = this.windowsToReposition.shift();
this.windowsToVerify.push(tile);
this.repositionAndResizeWindow(tile,
this.processAllWindowRepositioningRequests.bind(this));
};
/**
* Utility function to resize a window with the given window ID with the given
* dimensions, and call the given callback function.
* @param {Object} tile An object containing all the necessary information to
* process the request.
* @param {Function} callback The callback function to call once the window is
* resized.
*/
WindowTiler.prototype.repositionAndResizeWindow = function(tile, callback) {
window.console.log('Repositioning window ' + tile.windowId + ' to ' +
tile.width + 'x' + tile.height + ' + (' + tile.left + ', ' + tile.top + ')') ;
chrome.windows.update(tile.windowId, {
'left': tile.left,
'top': tile.top,
'width': tile.width,
'height': tile.height,
'state': 'normal'
}, callback);
};
WindowTiler.prototype.verifyAllPositions = function() {
var allMatch = true;
for (var i = 0, toVerify; toVerify = this.windowsToVerify; i++) {
}
};
WindowTiler.prototype.verifynewWindowPosition = function(tile, callback) {
chrome.windows.get(tile.windowId, undefined, function(theWindow) {
var comparison = WindowTilerUtils.compareAreas(tile, theWindow);
if (comparison == 0) {
console.log('Equal:', tile, ' and ', theWindow);
} else {
console.log('NOT Equal:', tile, ' and ', theWindow);
}
callback();
});
};
/**
* Adds a tile (which contains information about one of the tiles on the screen)
* into the current context (array of computed tiles).
* @param {number} left The left position to use for the added tile.
* @param {number} top The top position to use for the added tile.
* @param {number} width The width to use for the added tile.
* @param {number} height The height to use for the added tile.
* @param {Array.<Object>} tileContext The context to which to add the new tile.
*/
WindowTiler.prototype.pushTileIntoTileContext = function(left, top, width,
height, tileContext) {
tileContext.push({
left: left,
top: top,
width: width,
height: height
});
return tileContext;
};
/**
* Computes the relevant tiles and pushes them into the given tile context, for
* a zone on the screen defined by the arguments, and for the given number of
* windows to tile.
* @param {Array.<Object>} tileContext The tile context to which to add computed
* tiles.
* @param {number} numWindows The number of windows left to tile.
* @param {number} zoneX The X coordinate of the zone remaining to tile.
* @param {number} zoneY The Y coordinate of the zone remaining to tile.
* @param {number} zoneWidth The width of the zone remaining to tile.
* @param {number} zoneHeight The height of the zone remaining to tile.
*/
WindowTiler.prototype.computeTiles = function(tileContext, numWindows, zoneX,
zoneY, zoneWidth, zoneHeight) {
if (window.console) {
window.console.log('Computing tiles: ' + zoneX + ', ' + zoneY + ', ' +
zoneWidth + ', ' + zoneHeight + ' for ' + numWindows + ' windows');
}
if (!numWindows) {
return tileContext;
}
// Base case: only one window remains, we occupy the whole remaining space.
if (numWindows == 1) {
this.pushTileIntoTileContext(zoneX, zoneY, zoneWidth, zoneHeight,
tileContext);
return tileContext;
}
var halfNumWindows = Math.floor(numWindows / 2);
if (zoneWidth > zoneHeight) {
var halfWidth = Math.floor(zoneWidth / 2);
tileContext = this.computeTiles(tileContext, halfNumWindows,
zoneX, zoneY,
halfWidth, zoneHeight);
tileContext = this.computeTiles(tileContext,
numWindows - halfNumWindows,
zoneX + halfWidth + 1, zoneY,
zoneWidth - halfWidth, zoneHeight);
} else {
var halfHeight = Math.floor(zoneHeight / 2);
tileContext = this.computeTiles(tileContext, halfNumWindows,
zoneX, zoneY,
zoneWidth, halfHeight);
tileContext = this.computeTiles(tileContext,
numWindows - halfNumWindows,
zoneX, zoneY + halfHeight + 1,
zoneWidth, zoneHeight - halfHeight);
}
return tileContext;
};
/**
* Tiles the windows given in an array as an argument over the available area
* on the screen.
*/
WindowTiler.prototype.tileWindows = function(theWindows, theScreen) {
var tileContext = [];
window.console.log('Tiling ' + theWindows.length + ' windows on screen ');
window.console.log(theScreen);
// TODO: screen.avail* properties do not work well on Linux/GNOME.
tileContext = this.computeTiles(tileContext, theWindows.length,
theScreen.workArea.left, theScreen.workArea.top,
theScreen.workArea.width, theScreen.workArea.height);
for (var i = 0, tile; i < tileContext.length; i++) {
var tileContextWithWindowId = tileContext[i];
tileContextWithWindowId.windowId = theWindows[i].id;
this.windowsToReposition.push(tileContextWithWindowId);
}
this.processAllWindowRepositioningRequests();
}