-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
93 lines (83 loc) · 2.05 KB
/
main.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
"use strict";
const electron = require("electron");
const appConfig = require("electron-settings");
const path = require("path");const fs = require("fs");
const app = electron.app;
const pathtoElectron = process.platform.match(/win/)?path.resolve(process.env.APPDATA, "npm/electron.cmd"):null;
if (process.env.NODE_ENV!=="prod") {
require("electron-reload")(__dirname, {
electron: pathtoElectron,
hardResetMethod: "exit"
});
require("electron-debug")();
}
let mainWindow;
function onClosed() {
mainWindow = null;
}
function createMainWindow() {
const mainWindowStateKeeper = windowStateKeeper("main");
const win = new electron.BrowserWindow({
title: "main",
x: mainWindowStateKeeper.x,
y: mainWindowStateKeeper.y,
width: mainWindowStateKeeper.width,
height: mainWindowStateKeeper.height,
webPreferences: {
nodeIntegration: true,
allowEval: false
}
});
mainWindowStateKeeper.track(win);
win.loadURL(`file://${__dirname}/index.html`);
win.on("closed", onClosed);
return win;
}
app.on("window-all-closed", () => {
if (process.platform !== "darwin") {
app.quit();
}
});
app.on("activate", () => {
if (!mainWindow) {
mainWindow = createMainWindow();
}
});
app.on("ready", () => {
mainWindow = createMainWindow();
});
function windowStateKeeper(windowName) {
let window, windowState;
function setBounds() {
// Restore from appConfig
if (appConfig.has(`windowState.${windowName}`)) {
windowState = appConfig.get(`windowState.${windowName}`);
return;
}
// Default
windowState = {
x: undefined,
y: undefined,
width: 1000,
height: 800,
};
}
function saveState() {
if (!windowState.isMaximized) windowState = window.getBounds();
windowState.isMaximized = window.isMaximized();
appConfig.set(`windowState.${windowName}`, windowState);
}
function track(win) {
window = win;
["resize", "move", "close"].forEach(event => win.on(event, saveState));
}
setBounds();
return({
x: windowState.x,
y: windowState.y,
width: windowState.width,
height: windowState.height,
isMaximized: windowState.isMaximized,
track
});
}