-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathelectron.js
85 lines (76 loc) · 2.02 KB
/
electron.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
const electron = require('electron')
const fs = require('fs')
const path = require('path')
const { BrowserWindow, app } = electron
let win
if (fs.existsSync('package.json')) {
try {
const pkg = require(path.resolve('package.json'))
app.setName(pkg.title || pkg.name || 'Nanotron')
} catch (_) {
app.setName('Nanotron')
}
} else {
app.setName('Nanotron')
}
app.allowRendererProcessReuse = false
app.on('ready', function () {
win = new BrowserWindow({
webPreferences: {
nodeIntegration: true,
contextIsolation: false
}
})
win.loadURL('file://' + require.resolve('./index.html'))
win.webContents.on('did-finish-load', () => win.webContents.openDevTools({ mode: 'detach' }))
win.webContents.on('context-menu', onContextMenu)
process.on('SIGHUP', () => {
win.loadURL(win.webContents.getURL())
})
})
if (fs.existsSync('electron.js')) {
require(path.resolve('electron.js'))
}
function onContextMenu (event, params) {
const { editFlags } = params
const hasText = params.selectionText.trim().length > 0
const can = type => editFlags[`can${type}`] && hasText
const menuTpl = [{
type: 'separator'
}, {
id: 'cut',
label: 'Cut',
// Needed because of macOS limitation:
// https://github.com/electron/electron/issues/5860
role: can('Cut') ? 'cut' : '',
enabled: can('Cut'),
visible: params.isEditable
}, {
id: 'copy',
label: 'Copy',
role: can('Copy') ? 'copy' : '',
enabled: can('Copy'),
visible: params.isEditable || hasText
}, {
id: 'paste',
label: 'Paste',
role: editFlags.canPaste ? 'paste' : '',
enabled: editFlags.canPaste,
visible: params.isEditable
}, {
type: 'separator'
}, {
id: 'inspect',
label: 'Inspect Element',
click () {
win.inspectElement(params.x, params.y)
if (win.webContents.isDevToolsOpened()) {
win.webContents.devToolsWebContents.focus()
}
}
}, {
type: 'separator'
}]
const menu = electron.Menu.buildFromTemplate(menuTpl)
menu.popup(win)
}