diff --git a/contents/code/bladelayout.js b/contents/code/bladelayout.js index e09e95eb81ae607f1e61a269cf4cafb571f922d0..f1c6eb5e5db7504ee2228c94e16c23f1259fe8e2 100644 --- a/contents/code/bladelayout.js +++ b/contents/code/bladelayout.js @@ -25,9 +25,13 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. * the left half of the screen. */ function BladeLayout(screenRectangle) { - print("Creating BladeLayout"); - Layout.call(this, screenRectangle); - // TODO + try { + print("Creating BladeLayout"); + Layout.call(this, screenRectangle); + // TODO + } catch(err) { + print(err, "in BladeLayout"); + } } BladeLayout.name = "Blade"; @@ -38,160 +42,180 @@ BladeLayout.prototype = new Layout(); BladeLayout.prototype.constructor = BladeLayout; BladeLayout.prototype.resetTileSizes = function() { - // 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(); - } + 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 BladeLayout.resetTileSizes"); + } } BladeLayout.prototype.addTile = function() { - if (this.tiles.length == 0) { - // The first tile fills the whole screen - var rect = Qt.rect(this.screenRectangle.x, - this.screenRectangle.y, - this.screenRectangle.width, - this.screenRectangle.height); - this._createTile(rect); - return; - } else { - // Divide the screen width evenly between full-height tiles - // FIXME: Screenrectangle for struts is weird - var lastRect = this.tiles[0].rectangle; - var newRect = Qt.rect(this.screenRectangle.x, - lastRect.y, - Math.floor((this.screenRectangle.width + this.screenRectangle.x) / (this.tiles.length + 1)), - this.screenRectangle.height); - // FIXME: Try to keep ratio - for (i = 0; i < this.tiles.length; i++) { - var rect = this.tiles[i].rectangle; - rect.x = newRect.x + newRect.width * i; - rect.width = newRect.width; - this.tiles[i].rectangle = rect; + try { + if (this.tiles.length == 0) { + // The first tile fills the whole screen + var rect = Qt.rect(this.screenRectangle.x, + this.screenRectangle.y, + this.screenRectangle.width, + this.screenRectangle.height); + this._createTile(rect); + return; + } else { + // Divide the screen width evenly between full-height tiles + // FIXME: Screenrectangle for struts is weird + var lastRect = this.tiles[0].rectangle; + var newRect = Qt.rect(this.screenRectangle.x, + lastRect.y, + Math.floor((this.screenRectangle.width + this.screenRectangle.x) / (this.tiles.length + 1)), + this.screenRectangle.height); + // FIXME: Try to keep ratio + for (i = 0; i < this.tiles.length; i++) { + var rect = this.tiles[i].rectangle; + rect.x = newRect.x + newRect.width * i; + rect.width = newRect.width; + this.tiles[i].rectangle = rect; + } + // Adjust tile's width for rounding errors + newRect.x = newRect.x + newRect.width * this.tiles.length; + newRect.width = (this.screenRectangle.width + this.screenRectangle.x) - newRect.x; + // TODO: Move this before setting ratio to simplify + this._createTile(newRect); } - // Adjust tile's width for rounding errors - newRect.x = newRect.x + newRect.width * this.tiles.length; - newRect.width = (this.screenRectangle.width + this.screenRectangle.x) - newRect.x; - // TODO: Move this before setting ratio to simplify - this._createTile(newRect); - } + } catch(err) { + print(err, "in BladeLayout.addTile"); + } } BladeLayout.prototype.removeTile = function(tileIndex) { - // Remove the array entry - var oldrect = this.tiles[tileIndex].rectangle; - 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; + try { + // Remove the array entry + var oldrect = this.tiles[tileIndex].rectangle; + 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; + } + if (this.tiles.length > 1) { + var tileCount = this.tiles.length; + var lastRect = this.tiles[0].rectangle; + var newRect = Qt.rect(this.screenRectangle.x, + this.screenRectangle.y, + Math.floor(this.screenRectangle.width / tileCount), + this.screenRectangle.height); + var lowest = 1; + for (i = 0; i < this.tiles.length; i++) { + var rect = this.tiles[i].rectangle; + rect.x = newRect.x + newRect.width * i; + rect.width = newRect.width; + this.tiles[i].rectangle = rect; + this.tiles[i].hasDirectNeighbour[Direction.Left] = (i > 0); + this.tiles[i].hasDirectNeighbour[Direction.Right] = (i < this.tiles.length - 1); + this.tiles[i].neighbours[Direction.Left] = i - 1; + this.tiles[i].neighbours[Direction.Right] = i + 1; + } + // Adjust rightmost tile's height for rounding errors + this.tiles[this.tiles.length - 1].rectangle.width = (this.screenRectangle.width + this.screenRectangle.x) - this.tiles[this.tiles.length - 1].rectangle.x; + } + } catch(err) { + print(err, "in BladeLayout.removeTile"); } - if (this.tiles.length > 1) { - var tileCount = this.tiles.length; - var lastRect = this.tiles[0].rectangle; - var newRect = Qt.rect(this.screenRectangle.x, - this.screenRectangle.y, - Math.floor(this.screenRectangle.width / tileCount), - this.screenRectangle.height); - var lowest = 1; - for (i = 0; i < this.tiles.length; i++) { +} + +BladeLayout.prototype.resizeTile = function(tileIndex, rectangle) { + try { + // TODO: Mark tile as resized so it can keep its size on tileadd/remove + if (tileIndex < 0 || tileIndex > this.tiles.length) { + print("Tileindex invalid", tileIndex, "/", this.tiles.length); + return; + } + var tile = this.tiles[tileIndex]; + if (tile == null) { + print("No tile"); + return; + } + if (rectangle == null){ + print("No rect"); + return; + } + // Cap rectangle at screenedges + if (rectangle.x < this.screenRectangle.x) { + rectangle.x = this.screenRectangle.x; + } + if (rectangle.y < this.screenRectangle.y) { + rectangle.y = this.screenRectangle.y; + } + if (rectangle.y + rectangle.height > this.screenRectangle.y + this.screenRectangle.height) { + rectangle.height = this.screenRectangle.y + this.screenRectangle.height - rectangle.y; + } + if (rectangle.x + rectangle.width > this.screenRectangle.x + this.screenRectangle.width) { + rectangle.width = this.screenRectangle.x + this.screenRectangle.width - rectangle.x; + } + var newRect = Qt.Rect(this.screenRectangle.x, + this.screenRectangle.y, + Math.floor(rectangle.x / tileIndex), + this.screenRectangle.height); + for(i = 0; i < tileIndex; i++) { var rect = this.tiles[i].rectangle; rect.x = newRect.x + newRect.width * i; rect.width = newRect.width; this.tiles[i].rectangle = rect; - this.tiles[i].hasDirectNeighbour[Direction.Left] = (i > 0); - this.tiles[i].hasDirectNeighbour[Direction.Right] = (i < this.tiles.length - 1); - this.tiles[i].neighbours[Direction.Left] = i - 1; - this.tiles[i].neighbours[Direction.Right] = i + 1; - } - // Adjust rightmost tile's height for rounding errors - this.tiles[this.tiles.length - 1].rectangle.width = (this.screenRectangle.width + this.screenRectangle.x) - this.tiles[this.tiles.length - 1].rectangle.x; - } -} - -BladeLayout.prototype.resizeTile = function(tileIndex, rectangle) { - // TODO: Mark tile as resized so it can keep its size on tileadd/remove - if (tileIndex < 0 || tileIndex > this.tiles.length) { - print("Tileindex invalid", tileIndex, "/", this.tiles.length); - return; - } - var tile = this.tiles[tileIndex]; - if (tile == null) { - print("No tile"); - return; - } - if (rectangle == null){ - print("No rect"); - return; - } - // Cap rectangle at screenedges - if (rectangle.x < this.screenRectangle.x) { - rectangle.x = this.screenRectangle.x; - } - if (rectangle.y < this.screenRectangle.y) { + } + newRect.width = Math.floor((this.screenRectangle.width - rectangle.x) / (this.tiles.length - tileIndex - 1)); + for(i = tileIndex + 1; i < this.tiles.length; i++){ + var rect = this.tiles[i].rectangle; + rect.x = newRect.x + newRect.width * i; + rect.width = newRect.width; + this.tiles[i].rectangle = rect; + } rectangle.y = this.screenRectangle.y; + rectangle.height = this.screenRectangle.height; + this.tiles[tileIndex].rectangle = rectangle; + } catch(err) { + print(err, "in BladeLayout.resizeTile"); } - if (rectangle.y + rectangle.height > this.screenRectangle.y + this.screenRectangle.height) { - rectangle.height = this.screenRectangle.y + this.screenRectangle.height - rectangle.y; - } - if (rectangle.x + rectangle.width > this.screenRectangle.x + this.screenRectangle.width) { - rectangle.width = this.screenRectangle.x + this.screenRectangle.width - rectangle.x; - } - var newRect = Qt.Rect(this.screenRectangle.x, - this.screenRectangle.y, - Math.floor(rectangle.x / tileIndex), - this.screenRectangle.height); - for(i = 0; i < tileIndex; i++) { - var rect = this.tiles[i].rectangle; - rect.x = newRect.x + newRect.width * i; - rect.width = newRect.width; - this.tiles[i].rectangle = rect; - } - newRect.width = Math.floor((this.screenRectangle.width - rectangle.x) / (this.tiles.length - tileIndex - 1)); - for(i = tileIndex + 1; i < this.tiles.length; i++){ - var rect = this.tiles[i].rectangle; - rect.x = newRect.x + newRect.width * i; - rect.width = newRect.width; - this.tiles[i].rectangle = rect; - } - rectangle.y = this.screenRectangle.y; - rectangle.height = this.screenRectangle.height; - this.tiles[tileIndex].rectangle = rectangle; } BladeLayout.prototype._createTile = function(rect) { - // 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 { - tile.hasDirectNeighbour[Direction.Up] = false; - tile.neighbours[Direction.Up] = - 1; + 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 { + 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 BladeLayout._createTile"); } - tile.neighbours[Direction.Down] = - 1; - tile.hasDirectNeighbour[Direction.Down] = false; - tile.index = this.tiles.length; - this.tiles.push(tile); } diff --git a/contents/code/halflayout.js b/contents/code/halflayout.js index 4f6d6d2b9525e645f3ead159f3b79aaf528df40a..b98a11985907813226eb78be141009dfa4d1662c 100644 --- a/contents/code/halflayout.js +++ b/contents/code/halflayout.js @@ -39,245 +39,265 @@ HalfLayout.prototype = new Layout(); HalfLayout.prototype.constructor = HalfLayout; HalfLayout.prototype.resetTileSizes = function() { - // 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(); - } + 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() { - if (this.tiles.length == 0) { - // The first tile fills the whole screen - var rect = Qt.rect(this.screenRectangle.x, - this.screenRectangle.y, - this.screenRectangle.width, - this.screenRectangle.height); - this._createTile(rect); - return; - } - if (this.tiles.length == 1) { - // The second tile fills the right half of the screen - // Also, javascript sucks - var firstRect = Qt.rect(this.tiles[0].rectangle.x, - this.tiles[0].rectangle.y, - this.tiles[0].rectangle.width, - this.tiles[0].rectangle.height); - firstRect.width = Math.floor(this.screenRectangle.width / 2); - this.tiles[0].rectangle = firstRect; - var newRect = Qt.rect(firstRect.x + firstRect.width, - firstRect.y, - 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 = Qt.rect(lastRect.x + lastRect.width, - lastRect.y, - this.screenRectangle.width - lastRect.width, - Math.floor(lastRect.height / (this.tiles.length))); - newRect.y = newRect.y + newRect.height * (this.tiles.length - 1); - // FIXME: Try to keep ratio - for (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; + try { + if (this.tiles.length == 0) { + // The first tile fills the whole screen + var rect = Qt.rect(this.screenRectangle.x, + this.screenRectangle.y, + this.screenRectangle.width, + this.screenRectangle.height); + this._createTile(rect); + return; + } + if (this.tiles.length == 1) { + // The second tile fills the right half of the screen + // Also, javascript sucks + var firstRect = Qt.rect(this.tiles[0].rectangle.x, + this.tiles[0].rectangle.y, + this.tiles[0].rectangle.width, + this.tiles[0].rectangle.height); + firstRect.width = Math.floor(this.screenRectangle.width / 2); + this.tiles[0].rectangle = firstRect; + var newRect = Qt.rect(firstRect.x + firstRect.width, + firstRect.y, + firstRect.width, + firstRect.height) + this._createTile(newRect); + return; } - // 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); - } + if (this.tiles.length > 1) { + // Every other tile separates the right half + var lastRect = this.tiles[0].rectangle; + var newRect = Qt.rect(lastRect.x + lastRect.width, + lastRect.y, + this.screenRectangle.width - lastRect.width, + Math.floor(lastRect.height / (this.tiles.length))); + newRect.y = newRect.y + newRect.height * (this.tiles.length - 1); + // FIXME: Try to keep ratio + for (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) { - //FIXME: There is a crash here - // Remove the array entry - var oldrect = this.tiles[tileIndex].rectangle; - 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; + try { + //FIXME: There is a crash here + // Remove the array entry + var oldrect = this.tiles[tileIndex].rectangle; + 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] = true; + this.tiles[0].hasDirectNeighbour[Direction.Right] = false; 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 = Qt.rect(lastRect.width, - lastRect.y, - lastRect.width, - Math.floor(lastRect.height / tileCount)); - var lowest = 1; - for (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; + 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; } - 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; + var tileCount = this.tiles.length - 1; + var lastRect = this.tiles[0].rectangle; + var newRect = Qt.rect(lastRect.width, + lastRect.y, + lastRect.width, + Math.floor(lastRect.height / tileCount)); + var lowest = 1; + for (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; } - 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; + // Adjust lowest tile's height for rounding errors + this.tiles[lowest].rectangle.height = (this.screenRectangle.y + this.screenRectangle.height) - this.tiles[lowest].rectangle.y; - /* - this.tiles[1].hasDirectNeighbour[Direction.Up] = false; - this.tiles[this.tiles.length - 1].hasDirectNeighbour[Direction.Down] = false; - */ - } + /* + this.tiles[1].hasDirectNeighbour[Direction.Up] = false; + this.tiles[this.tiles.length - 1].hasDirectNeighbour[Direction.Down] = false; + */ + } + } catch(err) { + print(err, "in HalfLayout.removeTile"); + } } HalfLayout.prototype.resizeTile = function(tileIndex, rectangle) { - // TODO: Mark tile as resized so it can keep its size on tileadd/remove - if (tileIndex < 0 || tileIndex > this.tiles.length) { - print("Tileindex invalid", tileIndex, "/", this.tiles.length); - return; - } - var tile = this.tiles[tileIndex]; - if (tile == null) { - print("No tile"); - return; - } - if (rectangle == null){ - print("No rect"); - return; - } - // Cap rectangle at screenedges - if (rectangle.x < this.screenRectangle.x) { - rectangle.x = this.screenRectangle.x; - } - if (rectangle.y < this.screenRectangle.y) { - rectangle.y = this.screenRectangle.y; - } - if (rectangle.y + rectangle.height > this.screenRectangle.y + this.screenRectangle.height) { - rectangle.height = this.screenRectangle.y + this.screenRectangle.height - rectangle.y; - } - if (rectangle.x + rectangle.width > this.screenRectangle.x + this.screenRectangle.width) { - rectangle.width = this.screenRectangle.x + this.screenRectangle.width - rectangle.x; - } - if (tileIndex == 0) { - // Simply adjust width on everything else, no height adjustment - rectangle.x = tile.rectangle.x; - rectangle.y = tile.rectangle.y; - rectangle.height = tile.rectangle.height; - tile.rectangle = rectangle; - for (i = 1; i < this.tiles.length; i++) { - this.tiles[i].rectangle.width = this.screenRectangle.width - rectangle.width; - this.tiles[i].rectangle.x = this.screenRectangle.x + rectangle.width; + try { + // TODO: Mark tile as resized so it can keep its size on tileadd/remove + if (tileIndex < 0 || tileIndex > this.tiles.length) { + print("Tileindex invalid", tileIndex, "/", this.tiles.length); + return; } - } else { - this.tiles[0].rectangle.width = this.screenRectangle.width - rectangle.width; - var belows = new Array(); - var aboves = new Array(); - for (i = 1; i < this.tiles.length; i++) { - if (this.tiles[i].rectangle.y < tile.rectangle.y){ - aboves.push(i); - } - if (this.tiles[i].rectangle.y > tile.rectangle.y){ - belows.push(i); - } + var tile = this.tiles[tileIndex]; + if (tile == null) { + print("No tile"); + return; } - // Dividing by zero sucks - if (aboves.length == 0) { - var newHeightAbove = 0; - } else { - var newHeightAbove = Math.floor((rectangle.y - this.screenRectangle.y) / aboves.length); + if (rectangle == null){ + print("No rect"); + return; } - if (belows.length == 0) { - var newHeightBelow = 0; - } else { - var newHeightBelow = Math.floor((this.screenRectangle.height - rectangle.height) / belows.length); + // Cap rectangle at screenedges + if (rectangle.x < this.screenRectangle.x) { + rectangle.x = this.screenRectangle.x; } - if (belows.length > 0) { - for (i = 0; i < belows.length; i++) { - this.tiles[belows[i]].rectangle.width = rectangle.width; - this.tiles[belows[i]].rectangle.x = rectangle.x; - this.tiles[belows[i]].rectangle.y = rectangle.y + rectangle.height + newHeightBelow * i; - this.tiles[belows[i]].rectangle.height = newHeightBelow; - } + if (rectangle.y < this.screenRectangle.y) { + rectangle.y = this.screenRectangle.y; } - if (aboves.length > 0) { - for (i = 0; i < aboves.length; i++) { - this.tiles[aboves[i]].rectangle.width = rectangle.width; - this.tiles[aboves[i]].rectangle.x = rectangle.x; - this.tiles[aboves[i]].rectangle.y = this.screenRectangle.y + newHeightAbove * i; - this.tiles[aboves[i]].rectangle.height = newHeightAbove; - } + if (rectangle.y + rectangle.height > this.screenRectangle.y + this.screenRectangle.height) { + rectangle.height = this.screenRectangle.y + this.screenRectangle.height - rectangle.y; + } + if (rectangle.x + rectangle.width > this.screenRectangle.x + this.screenRectangle.width) { + rectangle.width = this.screenRectangle.x + this.screenRectangle.width - rectangle.x; } - tile.rectangle = rectangle; - // No reason to set height when we have the full screen - if (this.tiles.length == 2) { - tile.rectangle.height = this.screenRectangle.height; - tile.rectangle.y = this.screenRectangle.y; + if (tileIndex == 0) { + // Simply adjust width on everything else, no height adjustment + rectangle.x = tile.rectangle.x; + rectangle.y = tile.rectangle.y; + rectangle.height = tile.rectangle.height; + tile.rectangle = rectangle; + for (i = 1; i < this.tiles.length; i++) { + this.tiles[i].rectangle.width = this.screenRectangle.width - rectangle.width; + this.tiles[i].rectangle.x = this.screenRectangle.x + rectangle.width; + } + } else { + this.tiles[0].rectangle.width = this.screenRectangle.width - rectangle.width; + var belows = new Array(); + var aboves = new Array(); + for (i = 1; i < this.tiles.length; i++) { + if (this.tiles[i].rectangle.y < tile.rectangle.y){ + aboves.push(i); + } + if (this.tiles[i].rectangle.y > tile.rectangle.y){ + belows.push(i); + } + } + // Dividing by zero sucks + if (aboves.length == 0) { + var newHeightAbove = 0; + } else { + var newHeightAbove = Math.floor((rectangle.y - this.screenRectangle.y) / aboves.length); + } + if (belows.length == 0) { + var newHeightBelow = 0; + } else { + var newHeightBelow = Math.floor((this.screenRectangle.height - rectangle.height) / belows.length); + } + if (belows.length > 0) { + for (i = 0; i < belows.length; i++) { + this.tiles[belows[i]].rectangle.width = rectangle.width; + this.tiles[belows[i]].rectangle.x = rectangle.x; + this.tiles[belows[i]].rectangle.y = rectangle.y + rectangle.height + newHeightBelow * i; + this.tiles[belows[i]].rectangle.height = newHeightBelow; + } + } + if (aboves.length > 0) { + for (i = 0; i < aboves.length; i++) { + this.tiles[aboves[i]].rectangle.width = rectangle.width; + this.tiles[aboves[i]].rectangle.x = rectangle.x; + this.tiles[aboves[i]].rectangle.y = this.screenRectangle.y + newHeightAbove * i; + this.tiles[aboves[i]].rectangle.height = newHeightAbove; + } + } + tile.rectangle = rectangle; + // No reason to set height when we have the full screen + if (this.tiles.length == 2) { + tile.rectangle.height = this.screenRectangle.height; + tile.rectangle.y = this.screenRectangle.y; + } } + } catch(err) { + print(err, "in HalfLayout.resizeTile"); } } HalfLayout.prototype._createTile = function(rect) { - // 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 { - tile.hasDirectNeighbour[Direction.Up] = false; - tile.neighbours[Direction.Up] = - 1; + 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 { + 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"); } - tile.neighbours[Direction.Down] = - 1; - tile.hasDirectNeighbour[Direction.Down] = false; - tile.index = this.tiles.length; - this.tiles.push(tile); } diff --git a/contents/code/tile.js b/contents/code/tile.js index 37b07c232f037679bf969c923155d7e0a19cb583..116296e34c0861cf02514534b2fa630caff1cade 100644 --- a/contents/code/tile.js +++ b/contents/code/tile.js @@ -24,80 +24,84 @@ along with this program. If not, see <http://www.gnu.org/licenses/>. * @class */ function Tile(firstClient, tileIndex) { - /** - * Signal which is triggered whenever the user starts to move the tile. - */ - this.movingStarted = new Signal(); - /** - * Signal which is triggered whenever the user stops moving the tile. - */ - this.movingEnded = new Signal(); - /** - * Signal which is triggered whenever the geometry changes between - * movingStarted and movingEnded. - */ - this.movingStep = new Signal(); - /** - * Signal which is triggered whenever the user starts to resize the tile. - */ - this.resizingStarted = new Signal(); - /** - * Signal which is triggered whenever the user stops resizing the tile. - */ - this.resizingEnded = new Signal(); - /** - * Signal which is triggered whenever the geometry changes between - * resizingStarted and resizingEnded. - */ - this.resizingStep = new Signal(); - /** - * Signal which is triggered when the geometry of the tile changes because - * of something different to a user move or resize action. - */ - this.geometryChanged = new Signal(); - /** - * Signal which is triggered whenever the tile is moved to a different - * screen. Two parameters are passed to the handlers, the old and the new - * screen. - */ - this.screenChanged = new Signal(); - /** - * Signal which is triggered whenever the tile is moved to a different - * desktop. Two parameters are passed to the handlers, the old and the new - * desktop. - */ - this.desktopChanged = new Signal(); - /** - * List of the clients in this tile. - */ - this.clients = [firstClient]; - /** - * Index of this tile in the TileList to which the tile belongs. - */ - this.tileIndex = tileIndex; - /** - * True if this tile is currently moved by the user. - */ - this._moving = false; - /** - * True if this tile is currently moved by the user. - */ - this._resizing = false; - /** - * Stores the current screen of the tile in order to be able to detect - * movement between screens. - */ - this._currentScreen = firstClient.screen; - - /** - * Stores the current desktop as this is needed as a desktopChanged - * parameter. - */ - this._currentDesktop = firstClient.desktop; - - this.rectangle = null; - - this.syncCustomProperties(); + try { + /** + * Signal which is triggered whenever the user starts to move the tile. + */ + this.movingStarted = new Signal(); + /** + * Signal which is triggered whenever the user stops moving the tile. + */ + this.movingEnded = new Signal(); + /** + * Signal which is triggered whenever the geometry changes between + * movingStarted and movingEnded. + */ + this.movingStep = new Signal(); + /** + * Signal which is triggered whenever the user starts to resize the tile. + */ + this.resizingStarted = new Signal(); + /** + * Signal which is triggered whenever the user stops resizing the tile. + */ + this.resizingEnded = new Signal(); + /** + * Signal which is triggered whenever the geometry changes between + * resizingStarted and resizingEnded. + */ + this.resizingStep = new Signal(); + /** + * Signal which is triggered when the geometry of the tile changes because + * of something different to a user move or resize action. + */ + this.geometryChanged = new Signal(); + /** + * Signal which is triggered whenever the tile is moved to a different + * screen. Two parameters are passed to the handlers, the old and the new + * screen. + */ + this.screenChanged = new Signal(); + /** + * Signal which is triggered whenever the tile is moved to a different + * desktop. Two parameters are passed to the handlers, the old and the new + * desktop. + */ + this.desktopChanged = new Signal(); + /** + * List of the clients in this tile. + */ + this.clients = [firstClient]; + /** + * Index of this tile in the TileList to which the tile belongs. + */ + this.tileIndex = tileIndex; + /** + * True if this tile is currently moved by the user. + */ + this._moving = false; + /** + * True if this tile is currently moved by the user. + */ + this._resizing = false; + /** + * Stores the current screen of the tile in order to be able to detect + * movement between screens. + */ + this._currentScreen = firstClient.screen; + + /** + * Stores the current desktop as this is needed as a desktopChanged + * parameter. + */ + this._currentDesktop = firstClient.desktop; + + this.rectangle = null; + + this.syncCustomProperties(); + } catch(err) { + print(err, "in Tile"); + } } /** @@ -107,12 +111,16 @@ function Tile(firstClient, tileIndex) { * @param geometry New tile geometry. */ Tile.prototype.setGeometry = function(geometry) { - if (geometry == null) { - return; - } - this.rectangle = geometry; - for(i = 0; i < this.clients.length; i++) { - this.clients[i].geometry = geometry; + try { + if (geometry == null) { + return; + } + this.rectangle = geometry; + for(i = 0; i < this.clients.length; i++) { + this.clients[i].geometry = geometry; + } + } catch(err) { + print(err, "in Tile.setGeometry"); } }; @@ -135,7 +143,11 @@ Tile.prototype.saveGeometry = function() { * Restores the previously saved geometry. */ Tile.prototype.restoreGeometry = function() { - this.clients[0].geometry = this._savedGeometry; + try { + this.clients[0].geometry = this._savedGeometry; + } catch(err) { + print(err, "in restoreGeometry"); + } // TODO: Inhibit geometryChanged events? }; @@ -143,13 +155,17 @@ Tile.prototype.restoreGeometry = function() { * Returns the currently active client in the tile. */ Tile.prototype.getActiveClient = function() { - var active; - this.clients.forEach(function(client) { - if (client.isCurrentTab) { - active = client; - } - }); - return active; + try { + var active; + this.clients.forEach(function(client) { + if (client.isCurrentTab) { + active = client; + } + }); + return active; + } catch(err) { + print(err, "in Tile.getActiveClient"); + } }; /** @@ -157,75 +173,90 @@ Tile.prototype.getActiveClient = function() { * in the tile). */ Tile.prototype.syncCustomProperties = function() { - this.clients[0].tiling_tileIndex = this.tileIndex; - this.clients[0].syncTabGroupFor("tiling_tileIndex", true); - this.clients[0].syncTabGroupFor("tiling_floating", true); - this.clients[0].syncTabGroupFor("fullScreen", true); + try { + this.clients[0].tiling_tileIndex = this.tileIndex; + this.clients[0].syncTabGroupFor("tiling_tileIndex", true); + this.clients[0].syncTabGroupFor("tiling_floating", true); + this.clients[0].syncTabGroupFor("fullScreen", true); + } catch(err) { + print(err, "in Tile.syncCustomProperties"); + } }; Tile.prototype.onClientGeometryChanged = function(client) { - if (!client.isCurrentTab) { - return; - } - // If the screen has changed, send an event and reset the saved geometry - if (client.screen != this._currentScreen) { - this._currentScreen = client.screen; - this._savedGeometry = null; - this.screenChanged.emit(); - } - if (client.move || client.resize) { - return; - } - if (this._moving || this.resizing) { - return; - } - if (this.rectangle != null) { - client.geometry.x = this.rectangle.x; - client.geometry.y = this.rectangle.y; - client.geometry.width = this.rectangle.width; - client.geometry.height = this.rectangle.height; + try { + if (!client.isCurrentTab) { + return; + } + // If the screen has changed, send an event and reset the saved geometry + if (client.screen != this._currentScreen) { + this._currentScreen = client.screen; + this._savedGeometry = null; + this.screenChanged.emit(); + } + if (client.move || client.resize) { + return; + } + if (this._moving || this.resizing) { + return; + } + if (this.rectangle != null) { + // Workaround an infinite signal loop by setting this in pieces + client.geometry.x = this.rectangle.x; + client.geometry.y = this.rectangle.y; + client.geometry.width = this.rectangle.width; + client.geometry.height = this.rectangle.height; + } + // TODO: Check whether we caused the geometry change + this.geometryChanged.emit(); + } catch(err) { + print(err, "in Tile.onClientGeometryChanged"); } - // TODO: Check whether we caused the geometry change - this.geometryChanged.emit(); }; Tile.prototype.onClientDesktopChanged = function(client) { - if (!client.isCurrentTab) { - return; - } - var oldDesktop = this._currentDesktop; - this._currentDesktop = client.desktop; - this.desktopChanged.emit(oldDesktop, this._currentDesktop); + try { + if (!client.isCurrentTab) { + return; + } + var oldDesktop = this._currentDesktop; + this._currentDesktop = client.desktop; + this.desktopChanged.emit(oldDesktop, this._currentDesktop); + } catch(err) { + print(err, "in Tile.onClientDesktopChanged"); + } }; Tile.prototype.onClientStartUserMovedResized = function(client) { - /* - // We want to distinguish between moving and resizing, so we have to wait - // for the first geometry change - this._lastGeometry = client.geometry; - */ }; Tile.prototype.onClientStepUserMovedResized = function(client) { - var newGeometry = client.geometry; - if (client.resize) { - this.resizingStep.emit(); - this._resizing = true; - return; - } - if (client.move) { - this.movingStep.emit(); - this._moving = true; - return; + try { + if (client.resize) { + this.resizingStep.emit(); + this._resizing = true; + return; + } + if (client.move) { + this.movingStep.emit(); + this._moving = true; + return; + } + } catch(err) { + print(err, "in Tile.onClientStepUserMovedResized"); } }; Tile.prototype.onClientFinishUserMovedResized = function(client) { - if (this._moving) { - this.movingEnded.emit(); - this._moving = false; - } else if (this._resizing) { - this.resizingEnded.emit(); - this._resizing = false; - } + try { + if (this._moving) { + this.movingEnded.emit(); + this._moving = false; + } else if (this._resizing) { + this.resizingEnded.emit(); + this._resizing = false; + } + } catch(err) { + print(err, "in Tile.onClientFinishUserMovedResized"); + } }; diff --git a/contents/code/tiling.js b/contents/code/tiling.js index 1dc1eabbc6a0e8b7749efcfba6768057f6a8f2c6..a17eefb7f550136c6c6a5ebb11a54f45e6cad5c6 100644 --- a/contents/code/tiling.js +++ b/contents/code/tiling.js @@ -72,38 +72,50 @@ Tiling.prototype.setLayoutArea = function(area) { } Tiling.prototype.addTile = function(tile, x, y) { - this.layout.addTile(); - // If a position was specified, we insert the tile at the specified position - if (x != null && y != null) { - var index = this._getTileIndex(x, y); - if (index == -1) { - this.tiles.push(tile); - } else { - this.tiles.splice(index, 0, tile); - } - } else { - this.tiles.push(tile); - } - this._updateAllTiles(); - // TODO: Register tile callbacks + try { + this.layout.addTile(); + // If a position was specified, we insert the tile at the specified position + if (x != null && y != null) { + var index = this._getTileIndex(x, y); + if (index == -1) { + this.tiles.push(tile); + } else { + this.tiles.splice(index, 0, tile); + } + } else { + this.tiles.push(tile); + } + this._updateAllTiles(); + // TODO: Register tile callbacks + } catch(err) { + print(err, "in Tiling.addTile"); + } } Tiling.prototype.removeTile = function(tile) { - var tileIndex = this.tiles.indexOf(tile); - this.tiles.splice(tileIndex, 1); - this.layout.removeTile(tileIndex); - // TODO: Unregister tile callbacks - this._updateAllTiles(); + try { + var tileIndex = this.tiles.indexOf(tile); + this.tiles.splice(tileIndex, 1); + this.layout.removeTile(tileIndex); + // TODO: Unregister tile callbacks + this._updateAllTiles(); + } catch(err) { + print(err, "in Tiling.removeTile"); + } } Tiling.prototype.swapTiles = function(tile1, tile2) { - if (tile1 != tile2) { - var index1 = this.tiles.indexOf(tile1); - var index2 = this.tiles.indexOf(tile2); - this.tiles[index1] = tile2; - this.tiles[index2] = tile1; - } - this._updateAllTiles(); + try { + if (tile1 != tile2) { + var index1 = this.tiles.indexOf(tile1); + var index2 = this.tiles.indexOf(tile2); + this.tiles[index1] = tile2; + this.tiles[index2] = tile1; + } + this._updateAllTiles(); + } catch(err) { + print(err, "in Tiling.swapTiles"); + } } Tiling.prototype.activate = function() { @@ -140,34 +152,46 @@ Tiling.prototype.resetTileSizes = function() { } Tiling.prototype.getTile = function(x, y) { - var index = this._getTileIndex(x, y); - if (index != -1) { - return this.tiles[index]; - } else { - return null; - } + try { + var index = this._getTileIndex(x, y); + if (index != -1) { + return this.tiles[index]; + } else { + return null; + } + } catch(err) { + print(err, "in Tiling.getTile"); + } } Tiling.prototype.getTileGeometry = function(x, y) { - var index = this._getTileIndex(x, y); - if (index != -1) { - return this.layout.tiles[index]; - } else { - return null; - } + try { + var index = this._getTileIndex(x, y); + if (index != -1) { + return this.layout.tiles[index]; + } else { + return null; + } + } catch(err) { + print(err, "in Tiling.getTileGeometry"); + } } Tiling.prototype._getTileIndex = function(x, y) { - for (var i = 0; i < this.layout.tiles.length; i++) { - var tile = this.layout.tiles[i]; - if (tile.rectangle.x <= x + try { + for (var i = 0; i < this.layout.tiles.length; i++) { + var tile = this.layout.tiles[i]; + if (tile.rectangle.x <= x && tile.rectangle.y <= y && tile.rectangle.x + tile.rectangle.width > x && tile.rectangle.y + tile.rectangle.height > y) { - return i; - } - } - return -1; + return i; + } + } + return -1; + } catch(err) { + print(err, "in Tiling._getTileIndex"); + } } Tiling.prototype.getTiles = function() { @@ -189,11 +213,15 @@ Tiling.prototype.getAdjacentTile = function(from, direction, directOnly) { } Tiling.prototype.resizeTile = function(tile){ - if (tile != null) { - var tileIndex = this.tiles.indexOf(tile); - var client = tile.clients[0]; - this.layout.resizeTile(tileIndex, client.geometry); - this._updateAllTiles(); + try { + if (tile != null) { + var tileIndex = this.tiles.indexOf(tile); + var client = tile.clients[0]; + this.layout.resizeTile(tileIndex, client.geometry); + this._updateAllTiles(); + } + } catch(err) { + print(err, "in Tiling.resizeTile"); } }