Ensuring that the restored window position is valid (#2459)

Fixing #1869
This commit is contained in:
Steven 2018-03-02 13:11:33 -08:00 committed by CHaBou
parent e17aab656d
commit 24b83615d4
7 changed files with 144 additions and 9 deletions

View file

@ -84,6 +84,7 @@ exports.setup = () => {
exports.getWin = win.get;
exports.winRecord = win.recordState;
exports.windowDefaults = win.defaults;
const getDeprecatedCSS = function(config) {
const deprecated = [];

View file

@ -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');

View file

@ -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);

14
app/utils/window-utils.js Normal file
View file

@ -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
};

View file

@ -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"

View file

@ -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);
});

View file

@ -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"