Skip to content

Commit

Permalink
open default windows on OS X of all windows closed and quit on other OS
Browse files Browse the repository at this point in the history
  • Loading branch information
develar committed Dec 17, 2015
1 parent 348ed16 commit 02a78bf
Show file tree
Hide file tree
Showing 2 changed files with 64 additions and 18 deletions.
19 changes: 14 additions & 5 deletions src/StateManager.ts
Original file line number Diff line number Diff line change
@@ -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<WindowItem> {
return this.conf.get("windows")
return this.store.get("windows")
}
}

interface WindowItem {
url: string
width?: number
height?: number
}
63 changes: 50 additions & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,33 +1,70 @@
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<BrowserWindow> = []
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)
windows.splice(index, 1)
})
windows.push(window)
}
}

app.on("ready", () => {
openWindows()
})

0 comments on commit 02a78bf

Please sign in to comment.