Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixing #1869. Ensuring that the restored window position is valid #2459

Merged
merged 13 commits into from
Mar 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions app/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ exports.setup = () => {

exports.getWin = win.get;
exports.winRecord = win.recordState;
exports.windowDefaults = win.defaults;

const getDeprecatedCSS = function(config) {
const deprecated = [];
Expand Down
13 changes: 7 additions & 6 deletions app/config/windows.js
Original file line number Diff line number Diff line change
@@ -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');
Expand Down
7 changes: 5 additions & 2 deletions app/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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([]);

Expand Down Expand Up @@ -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);
Expand Down
14 changes: 14 additions & 0 deletions app/utils/window-utils.js
Original file line number Diff line number Diff line change
@@ -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
};
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"dev": "webpack -w",
"build": "cross-env NODE_ENV=production webpack",
"lint": "eslint .",
"test": "yarn run lint",
"test": "yarn run lint && yarn run test:unit",
"test:unit": "ava test/unit",
"test:unit:watch": "yarn run test:unit -- --watch",
"prepush": "yarn test",
Expand Down Expand Up @@ -218,6 +218,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"
Expand Down
34 changes: 0 additions & 34 deletions test/unit/keymaps-find-command-by-keys.test.js

This file was deleted.

16 changes: 0 additions & 16 deletions test/unit/keymaps-normalize.test.js

This file was deleted.

88 changes: 88 additions & 0 deletions test/unit/window-utils.test.js
Original file line number Diff line number Diff line change
@@ -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);
});
29 changes: 28 additions & 1 deletion yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"

Expand Down Expand Up @@ -4141,6 +4148,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"
Expand Down Expand Up @@ -4241,6 +4252,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"
Expand Down Expand Up @@ -5175,6 +5190,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"
Expand Down Expand Up @@ -5598,6 +5621,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"
Expand Down