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

Add defaultX/defaultY options for setting default position of window #67

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
10 changes: 7 additions & 3 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import * as Electron from 'electron'
import * as Electron from 'electron';

declare function windowStateKeeper(opts: windowStateKeeper.Options): windowStateKeeper.State;

Expand All @@ -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;
Expand Down
12 changes: 8 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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
};
}
Expand Down Expand Up @@ -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 {
Expand Down
8 changes: 8 additions & 0 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 13 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});