diff --git a/app/config.js b/app/config.js index cc059cbb..bed91878 100644 --- a/app/config.js +++ b/app/config.js @@ -84,6 +84,7 @@ exports.setup = () => { exports.getWin = win.get; exports.winRecord = win.recordState; +exports.windowDefaults = win.defaults; const getDeprecatedCSS = function(config) { const deprecated = []; diff --git a/app/config/windows.js b/app/config/windows.js index a8cef764..901f47b2 100644 --- a/app/config/windows.js +++ b/app/config/windows.js @@ -1,14 +1,15 @@ const Config = require('electron-config'); +const defaults = { + windowPosition: [50, 50], + windowSize: [540, 380] +}; + // local storage -const cfg = new Config({ - defaults: { - windowPosition: [50, 50], - windowSize: [540, 380] - } -}); +const cfg = new Config({defaults}); module.exports = { + defaults, get() { const position = cfg.get('windowPosition'); const size = cfg.get('windowSize'); diff --git a/app/index.js b/app/index.js index 63ce56db..8e287057 100644 --- a/app/index.js +++ b/app/index.js @@ -63,10 +63,9 @@ config.setup(); const plugins = require('./plugins'); const {addSymlink, addBinToUserPath} = require('./utils/cli-install'); - const AppMenu = require('./menus/menu'); - const Window = require('./ui/window'); +const windowUtils = require('./utils/window-utils'); const windowSet = new Set([]); @@ -155,6 +154,10 @@ app.on('ready', () => } } + if (!windowUtils.positionIsValid([startX, startY])) { + [startX, startY] = config.windowDefaults.windowPosition; + } + const hwin = new Window({width, height, x: startX, y: startY}, cfg, fn); windowSet.add(hwin); hwin.loadURL(url); diff --git a/app/utils/window-utils.js b/app/utils/window-utils.js new file mode 100644 index 00000000..7ba4eb91 --- /dev/null +++ b/app/utils/window-utils.js @@ -0,0 +1,14 @@ +const electron = require('electron'); + +function positionIsValid(position) { + const displays = electron.screen.getAllDisplays(); + const [x, y] = position; + + return displays.some(({workArea}) => { + return x >= workArea.x && x <= workArea.x + workArea.width && y >= workArea.y && y <= workArea.y + workArea.height; + }); +} + +module.exports = { + positionIsValid +}; diff --git a/package.json b/package.json index 431148be..09f5a304 100644 --- a/package.json +++ b/package.json @@ -236,6 +236,7 @@ "inquirer": "5.0.0", "node-gyp": "3.6.2", "prettier": "1.10.2", + "proxyquire": "1.8.0", "spectron": "3.7.2", "style-loader": "0.19.1", "webpack": "3.10.0" diff --git a/test/unit/window-utils.test.js b/test/unit/window-utils.test.js new file mode 100644 index 00000000..597b942a --- /dev/null +++ b/test/unit/window-utils.test.js @@ -0,0 +1,88 @@ +import test from 'ava'; +const proxyquire = require('proxyquire').noCallThru(); + +test('positionIsValid() returns true when window is on only screen', t => { + const position = [50, 50]; + const windowUtils = proxyquire('../../app/utils/window-utils', { + electron: { + screen: { + getAllDisplays: () => { + return [ + { + workArea: { + x: 0, + y: 0, + width: 500, + height: 500 + } + } + ]; + } + } + } + }); + + const result = windowUtils.positionIsValid(position); + + t.true(result); +}); + +test('positionIsValid() returns true when window is on second screen', t => { + const position = [750, 50]; + const windowUtils = proxyquire('../../app/utils/window-utils', { + electron: { + screen: { + getAllDisplays: () => { + return [ + { + workArea: { + x: 0, + y: 0, + width: 500, + height: 500 + } + }, + { + workArea: { + x: 500, + y: 0, + width: 500, + height: 500 + } + } + ]; + } + } + } + }); + + const result = windowUtils.positionIsValid(position); + + t.true(result); +}); + +test('positionIsValid() returns false when position isnt valid', t => { + const primaryDisplay = { + workArea: { + x: 0, + y: 0, + width: 500, + height: 500 + } + }; + const position = [600, 50]; + const windowUtils = proxyquire('../../app/utils/window-utils', { + electron: { + screen: { + getAllDisplays: () => { + return [primaryDisplay]; + }, + getPrimaryDisplay: () => primaryDisplay + } + } + }); + + const result = windowUtils.positionIsValid(position); + + t.false(result); +}); diff --git a/yarn.lock b/yarn.lock index 46213608..bc3cbfa3 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2760,6 +2760,13 @@ filename-regex@^2.0.0: version "2.0.1" resolved "https://registry.yarnpkg.com/filename-regex/-/filename-regex-2.0.1.tgz#c1c4b9bee3e09725ddb106b75c1e301fe2f18b26" +fill-keys@^1.0.2: + version "1.0.2" + resolved "https://registry.yarnpkg.com/fill-keys/-/fill-keys-1.0.2.tgz#9a8fa36f4e8ad634e3bf6b4f3c8882551452eb20" + dependencies: + is-object "~1.0.1" + merge-descriptors "~1.0.0" + fill-range@^2.1.0: version "2.2.3" resolved "https://registry.yarnpkg.com/fill-range/-/fill-range-2.2.3.tgz#50b77dfd7e469bc7492470963699fe7a8485a723" @@ -3586,7 +3593,7 @@ is-obj@^1.0.0: version "1.0.1" resolved "https://registry.yarnpkg.com/is-obj/-/is-obj-1.0.1.tgz#3e4729ac1f5fde025cd7d83a896dab9f4f67db0f" -is-object@^1.0.1: +is-object@^1.0.1, is-object@~1.0.1: version "1.0.1" resolved "https://registry.yarnpkg.com/is-object/-/is-object-1.0.1.tgz#8952688c5ec2ffd6b03ecc85e769e02903083470" @@ -4137,6 +4144,10 @@ meow@^3.1.0, meow@^3.7.0: redent "^1.0.0" trim-newlines "^1.0.0" +merge-descriptors@~1.0.0: + version "1.0.1" + resolved "https://registry.yarnpkg.com/merge-descriptors/-/merge-descriptors-1.0.1.tgz#b00aaa556dd8b44568150ec9d1b953f3f90cbb61" + micromatch@^2.1.5: version "2.3.11" resolved "https://registry.yarnpkg.com/micromatch/-/micromatch-2.3.11.tgz#86677c97d1720b363431d04d0d15293bd38c1565" @@ -4237,6 +4248,10 @@ mkdirp@0.5.0: dependencies: minimist "0.0.8" +module-not-found-error@^1.0.0: + version "1.0.1" + resolved "https://registry.yarnpkg.com/module-not-found-error/-/module-not-found-error-1.0.1.tgz#cf8b4ff4f29640674d6cdd02b0e3bc523c2bbdc0" + mousetrap@chabou/mousetrap#useCapture: version "1.6.1" resolved "https://codeload.github.com/chabou/mousetrap/tar.gz/c95eeeaafba1131dd8d35bc130d4a79b2ff9261a" @@ -5163,6 +5178,14 @@ protocols@^1.1.0, protocols@^1.4.0: version "1.4.6" resolved "https://registry.yarnpkg.com/protocols/-/protocols-1.4.6.tgz#f8bb263ea1b5fd7a7604d26b8be39bd77678bf8a" +proxyquire@1.8.0: + version "1.8.0" + resolved "https://registry.yarnpkg.com/proxyquire/-/proxyquire-1.8.0.tgz#02d514a5bed986f04cbb2093af16741535f79edc" + dependencies: + fill-keys "^1.0.2" + module-not-found-error "^1.0.0" + resolve "~1.1.7" + prr@~1.0.1: version "1.0.1" resolved "https://registry.yarnpkg.com/prr/-/prr-1.0.1.tgz#d3fc114ba06995a45ec6893f484ceb1d78f5f476" @@ -5586,6 +5609,10 @@ resolve-url@~0.2.1: version "0.2.1" resolved "https://registry.yarnpkg.com/resolve-url/-/resolve-url-0.2.1.tgz#2c637fe77c893afd2a663fe21aa9080068e2052a" +resolve@~1.1.7: + version "1.1.7" + resolved "https://registry.yarnpkg.com/resolve/-/resolve-1.1.7.tgz#203114d82ad2c5ed9e8e0411b3932875e889e97b" + restore-cursor@^2.0.0: version "2.0.0" resolved "https://registry.yarnpkg.com/restore-cursor/-/restore-cursor-2.0.0.tgz#9f7ee287f82fd326d4fd162923d62129eee0dfaf"