-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
open default windows on OS X of all windows closed and quit on other OS
- Loading branch information
Showing
2 changed files
with
64 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | ||
}) |