From 687943f10c5ff98eb6fc4bd794649dc764cef529 Mon Sep 17 00:00:00 2001 From: "Eon S. Jeon" Date: Sat, 8 Dec 2018 01:46:38 +0900 Subject: [PATCH] split `layout.ts` --- src/layouts/ilayout.ts | 28 ++++++ src/layouts/monoclelayout.ts | 33 +++++++ src/layouts/spreadlayout.ts | 68 +++++++++++++ src/layouts/stairlayout.ts | 65 +++++++++++++ src/{layout.ts => layouts/tilelayout.ts} | 118 ----------------------- 5 files changed, 194 insertions(+), 118 deletions(-) create mode 100644 src/layouts/ilayout.ts create mode 100644 src/layouts/monoclelayout.ts create mode 100644 src/layouts/spreadlayout.ts create mode 100644 src/layouts/stairlayout.ts rename src/{layout.ts => layouts/tilelayout.ts} (53%) diff --git a/src/layouts/ilayout.ts b/src/layouts/ilayout.ts new file mode 100644 index 00000000..18712a91 --- /dev/null +++ b/src/layouts/ilayout.ts @@ -0,0 +1,28 @@ +// Copyright (c) 2018 Eon S. Jeon +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the "Software"), +// to deal in the Software without restriction, including without limitation +// the rights to use, copy, modify, merge, publish, distribute, sublicense, +// and/or sell copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. + +interface ILayout { + apply(tiles: Tile[], area: Rect): void; + + handleUserInput(input: UserInput, data?: any): boolean; + /* if true, layout completely overrides the default behavior */ + + isEnabled(): boolean; +} diff --git a/src/layouts/monoclelayout.ts b/src/layouts/monoclelayout.ts new file mode 100644 index 00000000..bf911141 --- /dev/null +++ b/src/layouts/monoclelayout.ts @@ -0,0 +1,33 @@ +// Copyright (c) 2018 Eon S. Jeon +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the "Software"), +// to deal in the Software without restriction, including without limitation +// the rights to use, copy, modify, merge, publish, distribute, sublicense, +// and/or sell copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. + +class MonocleLayout implements ILayout { + public apply = (tiles: Tile[], area: Rect): void => { + tiles.forEach((tile) => tile.geometry.copyFrom(area)); + } + + public handleUserInput(input: UserInput) { + return false; + } + + public isEnabled(): boolean { + return Config.enableMonocleLayout; + } +} diff --git a/src/layouts/spreadlayout.ts b/src/layouts/spreadlayout.ts new file mode 100644 index 00000000..e783e002 --- /dev/null +++ b/src/layouts/spreadlayout.ts @@ -0,0 +1,68 @@ +// Copyright (c) 2018 Eon S. Jeon +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the "Software"), +// to deal in the Software without restriction, including without limitation +// the rights to use, copy, modify, merge, publish, distribute, sublicense, +// and/or sell copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. + +class SpreadLayout implements ILayout { + private space: number; /* in ratio */ + + constructor() { + this.space = 0.07; + } + + public apply = (tiles: Tile[], area: Rect): void => { + let numTiles = tiles.length; + const spaceWidth = Math.floor(area.width * this.space); + let cardWidth = area.width - (spaceWidth * (numTiles - 1)); + + // TODO: define arbitrary constants + const miniumCardWidth = area.width * 0.40; + while (cardWidth < miniumCardWidth) { + cardWidth += spaceWidth; + numTiles -= 1; + } + + for (let i = 0; i < tiles.length; i++) + tiles[i].geometry.set( + area.x + ((i < numTiles) ? spaceWidth * (numTiles - i - 1) : 0), + area.y, + cardWidth, + area.height, + ); + } + + public handleUserInput(input: UserInput) { + switch (input) { + case UserInput.Decrease: + // TODO: define arbitrary constants + this.space = Math.max(0.04, this.space - 0.01); + break; + case UserInput.Increase: + // TODO: define arbitrary constants + this.space = Math.min(0.10, this.space + 0.01); + break; + default: + return false; + } + return true; + } + + public isEnabled(): boolean { + return Config.enableSpreadLayout; + } +} diff --git a/src/layouts/stairlayout.ts b/src/layouts/stairlayout.ts new file mode 100644 index 00000000..8ecea057 --- /dev/null +++ b/src/layouts/stairlayout.ts @@ -0,0 +1,65 @@ +// Copyright (c) 2018 Eon S. Jeon +// +// Permission is hereby granted, free of charge, to any person obtaining a +// copy of this software and associated documentation files (the "Software"), +// to deal in the Software without restriction, including without limitation +// the rights to use, copy, modify, merge, publish, distribute, sublicense, +// and/or sell copies of the Software, and to permit persons to whom the +// Software is furnished to do so, subject to the following conditions: +// +// The above copyright notice and this permission notice shall be included in +// all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL +// THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING +// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER +// DEALINGS IN THE SOFTWARE. + +class StairLayout implements ILayout { + private space: number; /* in PIXELS */ + + constructor() { + this.space = 24; + } + + public apply = (tiles: Tile[], area: Rect): void => { + const len = tiles.length; + const space = this.space; + + // TODO: limit the maximum number of staired windows. + + for (let i = 0; i < len; i++) { + const dx = space * (len - i - 1); + const dy = space * i; + tiles[i].geometry.set( + area.x + dx, + area.y + dy, + area.width - dx, + area.height - dy, + ); + } + } + + public handleUserInput(input: UserInput) { + switch (input) { + case UserInput.Decrease: + // TODO: define arbitrary constants + this.space = Math.max(16, this.space - 8); + break; + case UserInput.Increase: + // TODO: define arbitrary constants + this.space = Math.min(160, this.space + 8); + break; + default: + return false; + } + return true; + } + + public isEnabled(): boolean { + return Config.enableStairLayout; + } +} diff --git a/src/layout.ts b/src/layouts/tilelayout.ts similarity index 53% rename from src/layout.ts rename to src/layouts/tilelayout.ts index 049c6bb9..2f9d8153 100644 --- a/src/layout.ts +++ b/src/layouts/tilelayout.ts @@ -18,15 +18,6 @@ // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER // DEALINGS IN THE SOFTWARE. -interface ILayout { - apply(tiles: Tile[], area: Rect): void; - - handleUserInput(input: UserInput, data?: any): boolean; - /* if true, layout completely overrides the default behavior */ - - isEnabled(): boolean; -} - class TileLayout implements ILayout { private numMaster: number; private masterRatio: number; /* in ratio */ @@ -107,112 +98,3 @@ class TileLayout implements ILayout { return Config.enableTileLayout; } } - -class MonocleLayout implements ILayout { - public apply = (tiles: Tile[], area: Rect): void => { - tiles.forEach((tile) => tile.geometry.copyFrom(area)); - } - - public handleUserInput(input: UserInput) { - return false; - } - - public isEnabled(): boolean { - return Config.enableMonocleLayout; - } -} - -class SpreadLayout implements ILayout { - private space: number; /* in ratio */ - - constructor() { - this.space = 0.07; - } - - public apply = (tiles: Tile[], area: Rect): void => { - let numTiles = tiles.length; - const spaceWidth = Math.floor(area.width * this.space); - let cardWidth = area.width - (spaceWidth * (numTiles - 1)); - - // TODO: define arbitrary constants - const miniumCardWidth = area.width * 0.40; - while (cardWidth < miniumCardWidth) { - cardWidth += spaceWidth; - numTiles -= 1; - } - - for (let i = 0; i < tiles.length; i++) - tiles[i].geometry.set( - area.x + ((i < numTiles) ? spaceWidth * (numTiles - i - 1) : 0), - area.y, - cardWidth, - area.height, - ); - } - - public handleUserInput(input: UserInput) { - switch (input) { - case UserInput.Decrease: - // TODO: define arbitrary constants - this.space = Math.max(0.04, this.space - 0.01); - break; - case UserInput.Increase: - // TODO: define arbitrary constants - this.space = Math.min(0.10, this.space + 0.01); - break; - default: - return false; - } - return true; - } - - public isEnabled(): boolean { - return Config.enableSpreadLayout; - } -} - -class StairLayout implements ILayout { - private space: number; /* in PIXELS */ - - constructor() { - this.space = 24; - } - - public apply = (tiles: Tile[], area: Rect): void => { - const len = tiles.length; - const space = this.space; - - // TODO: limit the maximum number of staired windows. - - for (let i = 0; i < len; i++) { - const dx = space * (len - i - 1); - const dy = space * i; - tiles[i].geometry.set( - area.x + dx, - area.y + dy, - area.width - dx, - area.height - dy, - ); - } - } - - public handleUserInput(input: UserInput) { - switch (input) { - case UserInput.Decrease: - // TODO: define arbitrary constants - this.space = Math.max(16, this.space - 8); - break; - case UserInput.Increase: - // TODO: define arbitrary constants - this.space = Math.min(160, this.space + 8); - break; - default: - return false; - } - return true; - } - - public isEnabled(): boolean { - return Config.enableStairLayout; - } -}