-
Notifications
You must be signed in to change notification settings - Fork 312
/
Copy pathmain.js
185 lines (168 loc) · 4.68 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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
const {
app,
shell,
Menu,
BrowserWindow,
globalShortcut,
session,
} = require('electron') // eslint-disable-line import/no-extraneous-dependencies
const path = require('path')
const url = require('url')
const port = process.env.PORT || 3000
let mainWindow = null
// adapted from https://github.com/chentsulin/electron-react-boilerplate
const installExtensions = () => {
const installer = require('electron-devtools-installer') // eslint-disable-line import/no-extraneous-dependencies
const extensions = ['REACT_DEVELOPER_TOOLS', 'REDUX_DEVTOOLS']
return Promise.all(
extensions.map(name => installer.default(installer[name])),
).catch(console.error)
}
app.on('window-all-closed', () => {
app.quit()
})
app.on('ready', () => {
// https://github.com/electron/electron/blob/master/docs/tutorial/security.md#csp-http-header
session.defaultSession.webRequest.onHeadersReceived((details, callback) => {
callback({ responseHeaders: "default-src 'none'" }) // eslint-disable-line
})
const onAppReady = () => {
mainWindow = new BrowserWindow({
height: 750,
width: 1200,
minHeight: 750,
minWidth: 1200,
titleBarStyle: 'hidden',
show: false,
icon: path.join(__dirname, 'icons/png/64x64.png'),
contextIsolation: true,
webPreferences: {
allowRunningInsecureContent: false,
webSecurity: true,
nodeIntegration: false,
preload: path.join(__dirname, 'preload.js'),
},
})
mainWindow.on('ready-to-show', () => {
mainWindow.show()
mainWindow.focus()
})
// https://discuss.atom.io/t/prevent-window-navigation-when-dropping-a-link/24365
mainWindow.webContents.on('will-navigate', ev => {
ev.preventDefault()
})
if (process.env.NODE_ENV === 'development') {
mainWindow.webContents.once('dom-ready', () => {
mainWindow.webContents.openDevTools()
})
}
if (process.platform !== 'darwin') {
// Windows/Linux Menu
mainWindow.setMenu(null)
} else {
// Menu is required for MacOS
const template = [
{
label: app.getName(),
submenu: [{ role: 'about' }, { type: 'separator' }, { role: 'quit' }],
},
{
label: 'Edit',
submenu: [
{ role: 'undo' },
{ role: 'redo' },
{ type: 'separator' },
{ role: 'cut' },
{ role: 'copy' },
{ role: 'paste' },
],
},
{
label: 'View',
submenu: [{ role: 'toggledevtools' }],
},
{
role: 'help',
submenu: [
{
label: 'City of Zion',
click() {
shell.openExternal('https://cityofzion.io/')
},
},
{
label: 'GitHub',
click() {
shell.openExternal('https://github.com/CityOfZion')
},
},
{
label: 'NEO Reddit',
click() {
shell.openExternal('https://www.reddit.com/r/NEO/')
},
},
{
label: 'Slack',
click() {
shell.openExternal('https://neosmarteconomy.slack.com')
},
},
],
},
]
const menu = Menu.buildFromTemplate(template)
Menu.setApplicationMenu(menu)
}
const inputMenu = Menu.buildFromTemplate([
{
label: 'Paste',
accelerator: 'CmdOrCtrl+V',
click() {
mainWindow.webContents.paste()
},
},
])
mainWindow.webContents.on('context-menu', () => {
inputMenu.popup(mainWindow)
})
if (process.env.START_HOT) {
mainWindow.loadURL(`http://localhost:${port}/dist`)
} else {
mainWindow.loadURL(
url.format({
protocol: 'file',
slashes: true,
pathname: path.join(__dirname, '/app/dist/index.html'),
}),
)
}
mainWindow.on('closed', () => {
mainWindow = null
})
}
// register any shortcuts here
globalShortcut.register('CommandOrControl+M', () => {
mainWindow.minimize()
})
if (process.env.NODE_ENV === 'development') {
installExtensions().then(() => onAppReady())
} else {
onAppReady()
}
})
app.on('web-contents-created', (event, wc) => {
wc.on('before-input-event', (event, input) => {
// Windows/Linux hotkeys
if (process.platform !== 'darwin') {
if (input.key === 'F12') {
mainWindow.webContents.openDevTools()
event.preventDefault()
}
}
})
})
app.on('will-quit', () => {
// Unregister all shortcuts.
globalShortcut.unregisterAll()
})