From 02a78bf384897ce3de1e0707285b16f42cfffc39 Mon Sep 17 00:00:00 2001 From: Vladimir Krivosheev Date: Thu, 17 Dec 2015 07:20:02 +0100 Subject: [PATCH] open default windows on OS X of all windows closed and quit on other OS --- src/StateManager.ts | 19 ++++++++++---- src/index.ts | 63 +++++++++++++++++++++++++++++++++++---------- 2 files changed, 64 insertions(+), 18 deletions(-) diff --git a/src/StateManager.ts b/src/StateManager.ts index 265e24d..c61764d 100644 --- a/src/StateManager.ts +++ b/src/StateManager.ts @@ -1,16 +1,25 @@ import ConfigStore = require("configstore") +function defaultWindows() { + return [ + {url: "https://cad.onshape.com/"} + ] +} + export default class StateManager { - private conf = new ConfigStore("onshape-unofficial", { - windows: [ - {url: "https://cad.onshape.com/"} - ]}) + private store = new ConfigStore("onshape-unofficial", {windows: defaultWindows()}) + + restoreWindows() { + this.store.set("windows", defaultWindows()) + } getWindows(): Array { - return this.conf.get("windows") + return this.store.get("windows") } } interface WindowItem { url: string + width?: number + height?: number } \ No newline at end of file diff --git a/src/index.ts b/src/index.ts index d440a79..c4e2a17 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,28 +1,61 @@ import StateManager from "./StateManager" import electron = require("electron") -import BrowserWindow = GitHubElectron.BrowserWindow; +import BrowserWindow = GitHubElectron.BrowserWindow +import BrowserWindowOptions = GitHubElectron.BrowserWindowOptions let stateManager = new StateManager() electron.crashReporter.start() let windows: Array = [] +let app = electron.app -function windowOptions() { - return { - width: 1024, - height: 768, - webPreferences: { - // fix jquery issue (https://github.com/atom/electron/issues/254), and in any case node integration is not required - nodeIntegration: false - } +app.on("window-all-closed", () => { + // restore default set of windows + stateManager.restoreWindows() + // On OS X it is common for applications and their menu bar + // to stay active until the user quits explicitly with Cmd + Q + if (process.platform == 'darwin') { + // reopen initial window + openWindows() } -} + else { + app.quit() + } +}) + +function openWindows() { + let descriptors = stateManager.getWindows() + if (descriptors == null || descriptors.length === 0) { + stateManager.restoreWindows() + descriptors = stateManager.getWindows() + } + + for (var descriptor of descriptors) { + let options: BrowserWindowOptions = { + // to avoid visible maximizing + show: false, + webPreferences: { + // fix jquery issue (https://github.com/atom/electron/issues/254), and in any case node integration is not required + nodeIntegration: false + } + } -electron.app.on("ready", () => { - for (var descriptor of stateManager.getWindows()) { - let window = new electron.BrowserWindow(windowOptions()) + let isMaximized = false + if (descriptor.width != null && descriptor.height != null) { + options.width = descriptor.width + options.height = descriptor.height + } + else { + isMaximized = true + } + + let window = new electron.BrowserWindow(options) + if (isMaximized) { + window.maximize() + } window.loadURL(descriptor.url) + window.show() window.on("closed", () => { var index = windows.indexOf(window) console.assert(index >= 0) @@ -30,4 +63,8 @@ electron.app.on("ready", () => { }) windows.push(window) } +} + +app.on("ready", () => { + openWindows() }) \ No newline at end of file