From 4fa5464d80ab097e01788359138221f1e4e599b5 Mon Sep 17 00:00:00 2001 From: rhysd Date: Fri, 29 Jan 2021 18:16:33 +0900 Subject: [PATCH 1/3] implement defaultX and defaultY config values --- index.js | 12 ++++++++---- readme.md | 8 ++++++++ 2 files changed, 16 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index 97491eb..73ebdcf 100644 --- a/index.js +++ b/index.js @@ -16,7 +16,9 @@ module.exports = function (options) { file: 'window-state.json', path: app.getPath('userData'), maximize: true, - fullScreen: true + fullScreen: true, + defaultX: 0, + defaultY: 0 }, options); const fullStoreFileName = path.join(config.path, config.file); @@ -39,8 +41,8 @@ module.exports = function (options) { state = { width: config.defaultWidth || 800, height: config.defaultHeight || 600, - x: 0, - y: 0, + x: 'defaultX' in config ? config.defaultX : 0, + y: 'defaultY' in config ? config.defaultY : 0, displayBounds }; } @@ -167,7 +169,9 @@ module.exports = function (options) { // Set state fallback values state = Object.assign({ width: config.defaultWidth || 800, - height: config.defaultHeight || 600 + height: config.defaultHeight || 600, + x: 'defaultX' in config ? config.defaultX : 0, + y: 'defaultY' in config ? config.defaultY : 0 }, state); return { diff --git a/readme.md b/readme.md index b033c7b..79f6595 100644 --- a/readme.md +++ b/readme.md @@ -58,6 +58,14 @@ Note: Don't call this function before the `ready` event is fired. The height that should be returned if no file exists yet. Defaults to `600`. +`defaultX` - Number | Undefined + + The X position that should be returned if no file exists yet. Defaults to `0`. + +`defaultY` - Number | Undefined + + The Y position that should be returned if no file exists yet. Defaults to `0`. + `path` - *String* The path where the state file should be written to. Defaults to From a2e79ff4ee18925b556ca89dcb815cfd9c1d1e40 Mon Sep 17 00:00:00 2001 From: rhysd Date: Fri, 29 Jan 2021 18:17:03 +0900 Subject: [PATCH 2/3] add test for defaultX/defaultY config --- test.js | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test.js b/test.js index 95f4656..a5e8581 100644 --- a/test.js +++ b/test.js @@ -384,3 +384,16 @@ test('Reset state to default values if saved display is unavailable', t => { screen.getPrimaryDisplay.restore(); screen.getAllDisplays.restore(); }); + +test('Set defaultX and defaultY if no state exists', t => { + let state = require('.')({defaultX: 100, defaultY: 200}); + + t.is(state.x, 100); + t.is(state.y, 200); + + state = require('.')({defaultX: undefined, defaultY: undefined}); + + t.is(state.x, undefined); + t.is(state.y, undefined); +}); + From ab9afe82c73d5147eedfe92188767feaf838d896 Mon Sep 17 00:00:00 2001 From: rhysd Date: Fri, 29 Jan 2021 18:18:53 +0900 Subject: [PATCH 3/3] update type definitions for defaultX/defaultY options --- index.d.ts | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/index.d.ts b/index.d.ts index bdd51eb..50d94b8 100644 --- a/index.d.ts +++ b/index.d.ts @@ -1,4 +1,4 @@ -import * as Electron from 'electron' +import * as Electron from 'electron'; declare function windowStateKeeper(opts: windowStateKeeper.Options): windowStateKeeper.State; @@ -15,12 +15,16 @@ declare namespace windowStateKeeper { file?: string; /** Should we automatically maximize the window, if it was last closed maximized. Defaults to `true`. */ maximize?: boolean; + /** The X position that should be returned if no file exists yet. Defaults to `0`. */ + defaultX?: number | undefined; + /** The Y position that should be returned if no file exists yet. Defaults to `0`. */ + defaultY?: number | undefined; } interface State { displayBounds: { - height: number; - width: number; + height: number; + width: number; }; /** The saved height of loaded state. `defaultHeight` if the state has not been saved yet. */ height: number;