forked from SabakiHQ/Sabaki
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
186 lines (150 loc) · 5.05 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
186
const {app, shell, dialog, ipcMain, BrowserWindow, Menu} = require('electron')
const fs = require('fs')
const setting = require('./modules/setting')
const updater = require('./modules/updater')
let windows = []
let openfile = null
let isReady = false
function newWindow(path) {
let window = new BrowserWindow({
icon: process.platform === 'linux' ? `${__dirname}/logo.png` : null,
title: app.getName(),
useContentSize: true,
width: setting.get('window.width'),
height: setting.get('window.height'),
minWidth: setting.get('window.minwidth'),
minHeight: setting.get('window.minheight'),
autoHideMenuBar: !setting.get('view.show_menubar'),
backgroundColor: '#111111',
show: false,
webPreferences: {
zoomFactor: setting.get('app.zoom_factor')
}
})
windows.push(window)
buildMenu()
window.webContents.setAudioMuted(!setting.get('sound.enable'))
window.webContents.on('did-finish-load', () => {
if (path) window.webContents.send('load-file', path)
}).on('new-window', evt => {
evt.preventDefault()
})
window.on('closed', () => {
window = null
})
window.loadURL(`file://${__dirname}/index.html`)
if (setting.get('debug.dev_tools')) {
window.openDevTools()
}
return window
}
function buildMenu(disableAll = false) {
let template = require('./data/menu').clone()
// Process menu items
let processMenu = items => {
items.forEach(item => {
if ('click' in item) {
item.click = () => {
let window = BrowserWindow.getFocusedWindow()
if (!window) return
window.webContents.send(`menu-click-${item.id}`)
}
}
if ('clickMain' in item) {
let key = item.clickMain
item.click = () => ({
newWindow,
checkForUpdates: () => checkForUpdates(true)
})[key]()
delete item.clickMain
}
if ('checked' in item) {
item.type = 'checkbox'
item.checked = !!setting.get(item.checked)
}
if (disableAll && !item.enabled && !('submenu' in item || 'role' in item)) {
item.enabled = false
}
if ('submenu' in item) {
processMenu(item.submenu)
}
})
}
processMenu(template)
// Build
Menu.setApplicationMenu(Menu.buildFromTemplate(template))
// Create dock menu
if (process.platform === 'darwin') {
app.dock.setMenu(Menu.buildFromTemplate([{
label: 'New Window',
click: () => newWindow()
}]))
}
}
function checkForUpdates(showFailDialogs) {
updater.check(`yishn/${app.getName()}`, (err, info) => {
if (err) return showFailDialogs && dialog.showMessageBox({
type: 'warning',
buttons: ['OK'],
title: app.getName(),
message: 'An error occurred when checking for updates.'
})
if (info.hasUpdates) {
dialog.showMessageBox({
type: 'info',
buttons: ['Download Update', 'Not Now'],
title: app.getName(),
message: `${app.getName()} v${info.latestVersion} is available now.`,
noLink: true,
cancelId: 1
}, response => response === 0 ? shell.openExternal(info.url) : null)
} else if (showFailDialogs) {
dialog.showMessageBox({
type: 'info',
buttons: ['OK'],
title: 'No update available',
message: `Sabaki v${app.getVersion()} is the latest version.`
}, () => {})
}
})
}
ipcMain.on('new-window', (evt, ...args) => newWindow(...args))
ipcMain.on('build-menu', (evt, ...args) => buildMenu(...args))
ipcMain.on('check-for-updates', (evt, ...args) => checkForUpdates(...args))
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit()
} else {
buildMenu(true)
}
})
app.on('ready', () => {
isReady = true
if (!openfile && process.argv.length >= 2)
openfile = process.argv[1]
newWindow(openfile)
if (setting.get('app.startup_check_updates')) {
setTimeout(() => checkForUpdates(), setting.get('app.startup_check_updates_delay'))
}
})
app.on('activate', (evt, hasVisibleWindows) => {
if (!hasVisibleWindows) newWindow()
})
app.on('open-file', (evt, path) => {
evt.preventDefault()
if (!isReady) {
openfile = path
} else {
newWindow(path)
}
})
process.on('uncaughtException', err => {
dialog.showErrorBox(`${app.getName()} v${app.getVersion()}`, [
'Something weird happened. ',
`${app.getName()} will shut itself down. `,
'If possible, please report this on ',
`${app.getName()}’s repository on GitHub.\n\n`,
err.stack
].join(''))
process.exit(1)
})