-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
80 lines (66 loc) · 1.86 KB
/
index.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
'use strict'
const electron = require('electron')
const { app, shell } = require('electron')
const createWindow = require('./app/helpers/window')
// Allow app to be accessible globally.
global.app = app
// adds global logging
require('./app/helpers/logger')
// adds debug features like hotkeys for triggering dev tools and reload
require('electron-debug')({ showDevTools: true })
// prevent window being garbage collected
let windows = []
function onClosed (number) {
if (process.platform !== 'darwin' && !number) {
windows.map((val) => { return null })
app.quit()
}
windows[number] = null
logger.log(`Someone closed window number ${number}`)
}
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit()
}
})
app.on('activate', () => {
if (!windows[0]) {
openWindow('main')
}
})
app.on('ready', () => {
openWindow('main')
electron.globalShortcut.register('CmdOrCtrl+Shift+C', () => {
openWindow('compose')
})
})
function openWindow (file) {
const { width, height } = electron.screen.getPrimaryDisplay().workAreaSize
let index = file === 'main' ? 0 : windows.length
windows[index] = createWindow(file, {
width, height,
icon: 'build/128x128.png',
minWidth: 320,
minHeight: 480,
maximized: true,
frame: false
})
windows[index].loadURL(`file://${__dirname}/app/${file}.html`)
windows[index].on('closed', ((i) => () => { onClosed(i) })(index))
windows[index].webContents.on('new-window', handleURL)
windows[index].webContents.on('will-navigate', handleURL)
}
function handleURL (e, url) {
if (url.indexOf('file://') == '-1') {
e.preventDefault()
shell.openExternal(url)
}
}
electron.ipcMain.on('open', (event, arg) => {
openWindow(arg.file)
})
electron.ipcMain.on('send', (event, arg) => {
logger.log(event)
logger.log(arg)
windows[0].webContents.send('send', arg)
})