-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
136 lines (122 loc) · 3.54 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
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
var defaultMenu = require('electron-default-menu')
var WindowState = require('electron-window-state')
var electron = require('electron')
var Menu = electron.Menu
var Path = require('path')
var windows = {}
var quitting = false
electron.app.on('ready', () => {
var menu = defaultMenu(electron.app, electron.shell)
var view = menu.find(x => x.label === 'View')
view.submenu = [
{ role: 'reload' },
{ role: 'toggledevtools' },
{ type: 'separator' },
{ role: 'resetzoom' },
{ role: 'zoomin' },
{ role: 'zoomout' },
{ type: 'separator' },
{ role: 'togglefullscreen' }
]
if (process.platform === 'darwin') {
var win = menu.find(x => x.label === 'Window')
win.submenu = [
{ role: 'minimize' },
{ role: 'zoom' },
{ role: 'close', label: 'Close' },
{ type: 'separator' },
{ role: 'front' }
]
}
Menu.setApplicationMenu(Menu.buildFromTemplate(menu))
startBackgroundProcess()
// wait until server has started before opening main window
electron.ipcMain.once('server-started', function (ev, config) {
openMainWindow()
})
electron.app.on('before-quit', function () {
quitting = true
})
// allow inspecting of background process
electron.ipcMain.on('open-background-devtools', function (ev, config) {
if (windows.background) {
windows.background.webContents.openDevTools({detach: true})
}
})
})
function startBackgroundProcess () {
if (!windows.background) {
windows.background = openWindow(Path.join(__dirname, 'background-process.js'), {
connect: false,
center: true,
fullscreen: false,
fullscreenable: false,
height: 150,
maximizable: false,
minimizable: false,
resizable: false,
show: false,
skipTaskbar: true,
title: 'ticktack-server',
useContentSize: true,
width: 150
})
}
}
function openMainWindow () {
if (!windows.main) {
var windowState = WindowState({
defaultWidth: 1024,
defaultHeight: 768
})
windows.main = openWindow(Path.join(__dirname, 'main.js'), {
minWidth: 800,
x: windowState.x,
y: windowState.y,
width: windowState.width,
height: windowState.height,
autoHideMenuBar: true,
title: 'Ticktack',
show: true,
backgroundColor: '#EEE',
icon: './assets/icon.png'
})
windowState.manage(windows.main)
windows.main.setSheetOffset(40)
windows.main.on('close', function (e) {
if (!quitting && process.platform === 'darwin') {
e.preventDefault()
windows.main.hide()
}
})
windows.main.on('closed', function () {
windows.main = null
if (process.platform !== 'darwin') electron.app.quit()
})
}
}
function openWindow (path, opts) {
var window = new electron.BrowserWindow(opts)
window.webContents.on('dom-ready', function () {
window.webContents.executeJavaScript(`
var electron = require('electron')
var h = require('mutant/h')
electron.webFrame.setZoomLevelLimits(1, 1)
var title = ${JSON.stringify(opts.title || 'Ticktack')}
document.documentElement.querySelector('head').appendChild(
h('title', title)
)
require(${JSON.stringify(path)})
`)
})
window.webContents.on('will-navigate', function (e, url) {
e.preventDefault()
electron.shell.openExternal(url)
})
window.webContents.on('new-window', function (e, url) {
e.preventDefault()
electron.shell.openExternal(url)
})
window.loadURL('file://' + Path.join(__dirname, 'assets', 'base.html'))
return window
}