-
Notifications
You must be signed in to change notification settings - Fork 21
/
Copy pathwindow.js
93 lines (84 loc) · 1.82 KB
/
window.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
const {app, BrowserWindow, Menu} = require('electron')
const path = require('path')
let frame = null
function init() {
frame = new BrowserWindow({
title: 'TODU',
width: 900,
height: 700,
minWidth: 450,
frame: false,
webPreferences: {
experimentalFeatures: true,
},
})
frame.loadURL(`file://${__dirname}/src/index.html`)
frame.on('closed', () => {
frame = null
})
const menu = createMenu()
if (process.platform === 'darwin') {
Menu.setApplicationMenu(menu)
} else {
frame.setMenu(menu)
frame.setMenuBarVisibility(false)
}
// these handler must be binded in main process
transferEvents()
}
function createMenu() {
return Menu.buildFromTemplate([
{
label: app.getName(),
submenu: [
{role: 'toggledevtools'},
{
label: 'Print',
accelerator: 'CommandOrControl+P',
click() {
frame && frame.webContents.print()
}
}
]
}
])
}
function transferEvents() {
frame.on('maximize', () => {
frame.webContents.send('maximize')
})
frame.on('unmaximize', () => {
frame.webContents.send('unmaximize')
})
global.downloads = new Map()
frame.webContents.session.on('will-download', (e, item, webContents) => {
const target = path.join(
process.resourcesPath,
`${item.getFilename()}.download`,
)
item.setSavePath(target)
global.downloads.set(target, item)
webContents.send('will-download', target)
})
}
const second = app.makeSingleInstance((argv, directory) => {
if (frame) {
if (frame.isMinimized()) {
frame.restore()
}
frame.focus()
}
return true
})
if (second) {
app.quit()
}
app.on('ready', init)
app.on('activate', () => {
if (frame === null) {
init()
}
})
app.on('window-all-closed', () => {
app.quit()
})