Skip to content
This repository has been archived by the owner on Jul 21, 2020. It is now read-only.

Commit

Permalink
feat(electron): remeber window position (fixes #121)
Browse files Browse the repository at this point in the history
  • Loading branch information
connor4312 committed May 25, 2018
1 parent fa32666 commit 7062cac
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 13 deletions.
65 changes: 65 additions & 0 deletions src/electron-window-state.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { BrowserWindow, BrowserWindowConstructorOptions } from 'electron';
import { fromEvent } from 'rxjs/observable/fromEvent';
import { merge } from 'rxjs/observable/merge';
import { debounceTime, takeUntil } from 'rxjs/operators';

import { FileDataStore, IDataStore } from './server/datastore';

interface IState {
fullscreen: boolean;
maximized: boolean;
options: Partial<BrowserWindowConstructorOptions>;
}

/**
* ElectronWindowState stores and restores window position information.
*/
export class ElectronWindowState {
/**
* Where window position preferences are stored in the IDataStore.
*/
public static readonly storeKey = 'window-position';

constructor(private readonly store: IDataStore = new FileDataStore()) {}

/**
* Loads the stored window configuration.
*/
public async restore(): Promise<Partial<BrowserWindowConstructorOptions>> {
return (await this.loadState()).options;
}

/**
* Watches for position changes in the window, saving preferences when
* the window is moved or closed.
*/
public async watch(window: BrowserWindow) {
const state = await this.loadState();
if (state.maximized) {
window.maximize();
}
if (state.fullscreen) {
window.setFullScreen(true);
}

merge(fromEvent(window, 'resize'), fromEvent(window, 'move'))
.pipe(takeUntil(fromEvent(window, 'closed')), debounceTime(500))
.subscribe(async () => this.saveState(window));
}

private async saveState(window: BrowserWindow): Promise<void> {
await this.store.saveGlobal<IState>(ElectronWindowState.storeKey, {
fullscreen: window.isFullScreen(),
maximized: window.isMaximized(),
options: window.getBounds(),
});
}

private async loadState(): Promise<IState> {
return this.store.loadGlobal<IState>(ElectronWindowState.storeKey, {
fullscreen: false,
maximized: false,
options: {},
});
}
}
54 changes: 41 additions & 13 deletions src/electron.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,42 @@ import * as fixPath from 'fix-path';
import * as path from 'path';
import * as url from 'url';

import { ElectronWindowState } from './electron-window-state';
import { ElectronServer } from './server/electron-server';

autoUpdater.checkForUpdatesAndNotify().catch(err => err);

let window: BrowserWindow | null = null;
let hasOpenWindow = false;
let server: ElectronServer;

const windowState = new ElectronWindowState();
const isDebug = process.argv.includes('--debug');

fixPath();

function createWindow() {
window = new BrowserWindow({
width: 1280,
height: 720,
webPreferences: {
webSecurity: false,
},
});
/**
* Creates a new BrowserWindow, seeding config from the stored state.
*/
async function createBrowserWindow() {
return windowState
.restore()
.catch(() => ({}))
.then(
windowOptions =>
new BrowserWindow({
width: 1280,
height: 720,
...windowOptions,
webPreferences: {
webSecurity: false,
},
}),
);
}

function setupWindow(window: BrowserWindow) {
hasOpenWindow = true;
windowState.watch(window).catch(() => undefined);

if (isDebug) {
// tslint:disable-next-line
Expand Down Expand Up @@ -59,16 +77,26 @@ function createWindow() {
);
}

window.on('closed', () => (window = null));
window.on('closed', () => (hasOpenWindow = false));
window.setMenu(null);

server = new ElectronServer(window);
server.start();
}

app.on('ready', createWindow);
function boot() {
createBrowserWindow()
.then(setupWindow)
.catch(() => undefined);
}

app.on('ready', boot);

app.on('window-all-closed', () => {
if (!server) {
return;
}

server
.stop()
.catch(() => undefined)
Expand All @@ -79,7 +107,7 @@ app.on('window-all-closed', () => {
});

app.on('activate', () => {
if (window === null) {
createWindow();
if (!hasOpenWindow) {
boot();
}
});

0 comments on commit 7062cac

Please sign in to comment.