Select Git revision
halflayout.js
halflayout.js 7.09 KiB
/********************************************************************
KWin - the KDE window manager
This file is part of the KDE project.
Copyright (C) 2013 Fabian Homborg <FHomborg@gmail.com>
based on spirallayout.js by Matthias Gottschlag
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 2 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 <http://www.gnu.org/licenses/>.
*********************************************************************/
/**
* Class which arranges the windows in a spiral with the largest window filling
* the left half of the screen.
*/
function HalfLayout(screenRectangle) {
print("Creating HalfLayout");
Layout.call(this, screenRectangle);
this.firstWidth = this.screenRectangle.width / 2;
this.master = 0;
};
HalfLayout.name = "Half";
// TODO: Add an image for the layout switcher
HalfLayout.image = null;
HalfLayout.prototype = new Layout();
HalfLayout.prototype.constructor = HalfLayout;
HalfLayout.prototype.resetTileSizes = function() {
try {
// Simply erase all tiles and recreate them to recompute the initial sizes
var tileCount = this.tiles.length;
this.tiles.length = 0;
for (var i = 0; i < tileCount; i++) {
this.addTile();
}
} catch(err) {
print(err, "in HalfLayout.resetTileSizes");
}
};
HalfLayout.prototype.addTile = function() {
try {
if (this.tiles.length == 0) {
// The first tile fills the whole screen
var rect = util.copyRect(this.screenRectangle);
this._createTile(rect);
return;
}
if (this.tiles.length == 1) {
// The second tile fills the right half of the screen
// Also, javascript sucks
var firstRect = util.copyRect(this.tiles[0].rectangle);
firstRect.width = this.firstWidth;
this.tiles[0].rectangle = firstRect;
var newRect = new Qt.rect(firstRect.x + firstRect.width,
firstRect.y,
this.screenRectangle.width - firstRect.width,
firstRect.height)
this._createTile(newRect);
return;
}
if (this.tiles.length > 1) {
// Every other tile separates the right half
var lastRect = this.tiles[0].rectangle;
var newRect = new Qt.rect(lastRect.x + lastRect.width,
lastRect.y,
this.screenRectangle.width - lastRect.width,
lastRect.height / (this.tiles.length));
newRect.y = newRect.y + newRect.height * (this.tiles.length - 1);
// FIXME: Try to keep ratio
for (var i = 1; i < this.tiles.length; i++) {
var rect = this.tiles[i].rectangle;
rect.x = newRect.x;
var offset = newRect.height * (i - 1);
rect.y = lastRect.y + offset;
rect.width = newRect.width;
rect.height = newRect.height;
this.tiles[i].rectangle = rect;
}
// Adjust lowest tile's height for rounding errors
//newRect.y = newRect.y + newRect.width * (this.tiles.length - 1);
newRect.height = (this.screenRectangle.y + this.screenRectangle.height) - newRect.y;
this._createTile(newRect);
}
} catch(err) {
print(err, "in HalfLayout.addTile");
}
};
HalfLayout.prototype.removeTile = function(tileIndex) {
try {
//FIXME: There is a crash here
// Remove the array entry
var oldrect = this.tiles[tileIndex].rectangle;
if (tileIndex < 0 || tileIndex >= this.tiles.length) {
print("Removing invalid tileindex");
return;
}
this.tiles.splice(tileIndex, 1);
// Update the other tiles
if (this.tiles.length == 1) {
this.tiles[0].rectangle = this.screenRectangle;
this.tiles[0].hasDirectNeighbour[Direction.Left] = false;
this.tiles[0].hasDirectNeighbour[Direction.Right] = false;
this.tiles[0].hasDirectNeighbour[Direction.Up] = false;
this.tiles[0].hasDirectNeighbour[Direction.Down] = false;
}
if (this.tiles.length > 1) {
if (tileIndex == 0) {
this.tiles[0].rectangle = oldrect;
this.tiles[0].hasDirectNeighbour[Direction.Left] = false;
this.tiles[0].hasDirectNeighbour[Direction.Right] = true;
this.tiles[0].hasDirectNeighbour[Direction.Up] = false;
this.tiles[0].hasDirectNeighbour[Direction.Down] = false;
this.tiles[0].neighbours[Direction.Right] = 1;
}
var tileCount = this.tiles.length - 1;
var lastRect = this.tiles[0].rectangle;
var newRect = new Qt.rect(lastRect.width,
lastRect.y,
lastRect.width,
lastRect.height / tileCount);
var lowest = 1;
for (var i = 1; i < this.tiles.length; i++) {
var rect = this.tiles[i].rectangle;
rect.y = newRect.y + newRect.height * (i - 1);
rect.height = newRect.height;
this.tiles[i].rectangle = rect;
if (this.tiles[lowest].rectangle.y < this.tiles[i].rectangle.y) {
lowest = i;
}
this.tiles[i].hasDirectNeighbour[Direction.Left] = true;
this.tiles[i].hasDirectNeighbour[Direction.Right] = false;
if (i == 1) {
this.tiles[i].hasDirectNeighbour[Direction.Up] = false;
} else {
this.tiles[i].hasDirectNeighbour[Direction.Up] = true;
}
if (i == this.tiles.length - 1) {
this.tiles[i].hasDirectNeighbour[Direction.Down] = false;
} else {
this.tiles[i].hasDirectNeighbour[Direction.Down] = true;
}
this.tiles[i].neighbours[Direction.Left] = 0;
this.tiles[i].neighbours[Direction.Up] = i - 1;
this.tiles[i].neighbours[Direction.Down] = i + 1;
}
// Adjust lowest tile's height for rounding errors
this.tiles[lowest].rectangle.height = (this.screenRectangle.y + this.screenRectangle.height) - this.tiles[lowest].rectangle.y;
}
} catch(err) {
print(err, "in HalfLayout.removeTile");
}
};
HalfLayout.prototype._createTile = function(rect) {
try {
// Update the last tile in the list
if (this.tiles.length > 1) {
var lastTile = this.tiles[this.tiles.length - 1];
lastTile.neighbours[Direction.Down] = this.tiles.length;
lastTile.hasDirectNeighbour[Direction.Down] = true;
}
if (this.tiles.length == 1) {
var lastTile2 = this.tiles[0];
lastTile2.neighbours[Direction.Right] = 1;
lastTile2.hasDirectNeighbour[Direction.Right] = true;
}
// Create a new tile and add it to the list
var tile = {};
tile.rectangle = rect;
tile.neighbours = [];
tile.hasDirectNeighbour = [];
tile.neighbours[Direction.Left] = 0;
tile.hasDirectNeighbour[Direction.Left] = (this.tiles.length > 0);
tile.neighbours[Direction.Right] = - 1;
tile.hasDirectNeighbour[Direction.Right] = false;
if (this.tiles.length > 1) {
tile.hasDirectNeighbour[Direction.Up] = true;
tile.neighbours[Direction.Up] = this.tiles.length - 1;
} else {
if (this.tiles.length == 0) {
tile.hasDirectNeighbour[Direction.Up] = false;
tile.neighbours[Direction.Up] = - 1;
}
}
tile.neighbours[Direction.Down] = - 1;
tile.hasDirectNeighbour[Direction.Down] = false;
tile.index = this.tiles.length;
this.tiles.push(tile);
} catch(err) {
print(err, "in HalfLayout._createTile");
}
};