-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
203 lines (185 loc) · 6.23 KB
/
app.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
const {app, BrowserWindow, ipcMain, Tray, nativeImage, shell} = require('electron')
const path = require('path-extra')
// Environment
global.POI_VERSION = app.getVersion()
global.ROOT = __dirname
global.EXECROOT = path.join(process.execPath, '..')
global.APPDATA_PATH = path.join(app.getPath('appData'), 'poi')
global.EXROOT = global.APPDATA_PATH
global.DEFAULT_CACHE_PATH = path.join(global.EXROOT, 'MyCache')
global.MODULE_PATH = path.join(global.ROOT, "node_modules")
const {ROOT} = global
const poiIconPath = path.join(ROOT, 'assets', 'icons',
process.platform === 'linux' ? 'poi_32x32.png' : 'poi.ico')
require('./lib/module-path').setAllowedPath([ global.ROOT, global.APPDATA_PATH ])
const config = require('./lib/config')
const proxy = require('./lib/proxy')
const shortcut = require('./lib/shortcut')
const {warn, error} = require('./lib/utils')
const dbg = require('./lib/debug')
if (process.platform !== 'linux') require('./lib/updater')
proxy.setMaxListeners(30)
// Disable HA
if (config.get('poi.disableHA', false)) {
app.disableHardwareAcceleration()
}
// check safe mode config
if (config.get('poi.enterSafeMode', false)) {
warn('Entering SAFE MODE according to config.')
global.isSafeMode = true
config.set('poi.enterSafeMode')
}
// Add shortcut to start menu when os is windows
app.setAppUserModelId('org.poooi.poi')
if (process.platform === 'win32' && config.get('poi.createShortcut', true)) {
const shortcutPath = app.getPath('appData') + "\\Microsoft\\Windows\\Start Menu\\Programs\\poi.lnk"
const targetPath = app.getPath('exe')
const argPath = app.getAppPath()
const option = {
target: targetPath,
args: argPath,
appUserModelId: 'org.poooi.poi',
description: 'poi the KanColle Browser Tool',
}
if (!ROOT.includes('.asar')) {
Object.assign(option, {
icon: path.join(ROOT, 'assets', 'icons', 'poi.ico'),
iconIndex: 0,
})
}
shell.writeShortcutLink(shortcutPath, option)
const safeModeShortcutPath = app.getPath('appData') + "\\Microsoft\\Windows\\Start Menu\\Programs\\poi (safe mode).lnk"
const safeModeOption = Object.assign({}, option)
Object.assign(safeModeOption, {
description: 'poi the KanColle Browser Tool (safe mode)',
args: `${argPath} --safe`,
appUserModelId: 'org.poooi.poi.safe',
})
shell.writeShortcutLink(safeModeShortcutPath, safeModeOption)
}
if (dbg.isEnabled()) {
global.SERVER_HOSTNAME = '127.0.0.1:17027'
} else {
global.SERVER_HOSTNAME = 'poi.0u0.moe'
process.env.NODE_ENV = 'production'
}
require('./lib/flash')
let mainWindow, appIcon
global.mainWindow = mainWindow = null
// Fix confused cursor in HiDPI
// https://github.com/electron/electron/issues/7655#issuecomment-259688853
if (process.platform === 'win32') {
app.commandLine.appendSwitch('enable-use-zoom-for-dsf', 'false')
}
// Test: enable JavaScript experimental features
app.commandLine.appendSwitch('js-flags', "--harmony")
// Cache size
const cacheSize = parseInt(config.get('poi.cacheSize'))
if (Number.isInteger(cacheSize)) {
app.commandLine.appendSwitch('disk-cache-size', `${1048576 * cacheSize}`)
}
app.on ('window-all-closed', () => {
shortcut.unregister()
app.quit()
})
app.on('ready', () => {
const {screen} = require('electron')
shortcut.register()
const {workArea} = screen.getPrimaryDisplay()
let {x, y, width, height} = config.get('poi.window', workArea)
const validate = (n, min, range) => (n != null && n >= min && n < min + range)
const withinDisplay = (d) => {
const wa = d.workArea
return validate(x, wa.x, wa.width) && validate(y, wa.y, wa.height)
}
if (!screen.getAllDisplays().some(withinDisplay)) {
x = workArea.x
y = workArea.y
}
if (width == null) {
width = workArea.width
}
if (height == null) {
height = workArea.height
}
global.mainWindow = mainWindow = new BrowserWindow({
x: x,
y: y,
width: width,
height: height,
title: 'poi',
icon: poiIconPath,
resizable: config.get('poi.content.resizable', true),
alwaysOnTop: config.get('poi.content.alwaysOnTop', false),
//workaround for low-alpha title-bar
titleBarStyle: process.platform === 'darwin' && Number(require('os').release().split('.')[0]) >= 17 ? null : 'hidden',
frame: !config.get('poi.useCustomTitleBar', process.platform === 'win32' || process.platform === 'linux'),
enableLargerThanScreen: true,
maximizable: config.get('poi.content.resizable', true),
fullscreenable: config.get('poi.content.resizable', true),
webPreferences: {
plugins: true,
},
backgroundColor: process.platform === 'darwin' ? '#00000000' : '#222222',
})
// Default menu
mainWindow.reloadArea = 'kan-game webview'
if (process.platform === 'darwin') {
const {touchBar} = require('./lib/touchbar')
mainWindow.setTouchBar(touchBar)
if (/electron$/i.test(process.argv[0])) {
const icon = nativeImage.createFromPath(`${ROOT}/assets/icons/poi.png`)
app.dock.setIcon(icon)
}
} else {
mainWindow.setMenu(null)
}
mainWindow.loadURL(`file://${__dirname}/index.html${dbg.isEnabled() ? '?react_perf' : ''}`)
if (config.get('poi.window.isMaximized', false)) {
mainWindow.maximize()
}
if (config.get('poi.window.isFullScreen', false)) {
mainWindow.setFullScreen(true)
}
if (dbg.isEnabled()) {
mainWindow.openDevTools({
detach: true,
})
}
// Never wants navigate
mainWindow.webContents.on('will-navigate', (e) => {
e.preventDefault()
})
mainWindow.on('closed', () => {
// Close all sub window
require('./lib/window').closeWindows()
mainWindow = null
})
// Tray icon
if (process.platform === 'win32' || process.platform === 'linux') {
global.appIcon = appIcon = new Tray(poiIconPath)
appIcon.on('click', () => {
if (mainWindow.isMinimized()) {
mainWindow.restore()
} else {
mainWindow.show()
}
})
}
})
// http basic auth
app.on('login', (event, webContents, request, authInfo, callback) => {
event.preventDefault()
mainWindow.webContents.send('http-basic-auth', 'login')
ipcMain.once ('basic-auth-info', (event, usr, pwd) => {
callback(usr, pwd)
})
})
ipcMain.on ('refresh-shortcut', () => {
shortcut.unregister()
shortcut.register()
})
// Uncaught error
process.on ('uncaughtException', (e) => {
error(e.stack)
})