-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
244 lines (216 loc) · 5.12 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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
'use strict'
const app = require('electron').app
const BrowserWindow = require('electron').BrowserWindow
const ipcMain = require('electron').ipcMain
const fs = require('fs')
const Menu = require('menu')
let storage = require('./app/config/storage')
let mainWindow = null
let authWindow = null
let forceQuit = false
let globalSetting = {
lang: 'en',
userDataPath: app.getPath('userData'),
drop: {
maxFilesOnce: 20,
availableType: ['image/png', 'image/jpg', 'image/jpeg', 'image/gif', 'image/svg']
},
qn: storage.get('qn'),
needCompress: storage.get('needCompress') !== null ? storage.get('needCompress') : true,
compressPath: app.getPath('temp'),
compressLevel: 4,
maxSizeToDataURL: 10 * 1024
}
// global setting
global.setting = globalSetting
console.log(app.getPath('userData'))
// set menu
let menu = Menu.buildFromTemplate([
{
label: 'Setting',
submenu: [
{
label: 'Edit Qiniu Auth',
click: function () { initAuth() }
},
{
label: 'Need Compress',
type: 'checkbox',
checked: global.setting.needCompress,
click: function () { setCompress() }
},
{
label: 'Toggle Developer Tools',
accelerator: (function () {
if (process.platform === 'darwin')
return 'Alt+Command+I'
else
return 'Ctrl+Shift+I'
})(),
click: function (item, focusedWindow) {
if (focusedWindow) focusedWindow.webContents.toggleDevTools()
}
},
{
label: 'Quit',
accelerator: 'CmdOrCtrl+Q',
click: function () { app.quit() }
}
]
},
{
label: 'Edit',
submenu: [
{
label: 'Undo',
accelerator: 'CmdOrCtrl+Z',
role: 'undo'
},
{
label: 'Redo',
accelerator: 'Shift+CmdOrCtrl+Z',
role: 'redo'
},
{
type: 'separator'
},
{
label: 'Cut',
accelerator: 'CmdOrCtrl+X',
role: 'cut'
},
{
label: 'Copy',
accelerator: 'CmdOrCtrl+C',
role: 'copy'
},
{
label: 'Paste',
accelerator: 'CmdOrCtrl+V',
role: 'paste'
},
{
label: 'Select All',
accelerator: 'CmdOrCtrl+A',
role: 'selectall'
},
]
},
{
label: 'About',
submenu: [
{
label: 'About',
selector: 'orderFrontStandardAboutPanel:'
},
{
label: 'Learn More',
click: function () { require('electron').shell.openExternal('https://github.com/A-limon/toaster-uploader') }
}
]
},
])
// setting the default language, only support simplified Chinese and English for now
function setDefaultLang () {
if (app.getLocale() === 'zh-CN') {
global.setting.lang = 'zh'
} else {
global.setting.lang = 'en'
}
}
// compress settting
function setCompress () {
global.setting.needCompress = !global.setting.needCompress
storage.set('needCompress', global.setting.needCompress )
}
// clear local cache
function clearCompresseFiles (dirPath) {
try {
var files = fs.readdirSync(dirPath)
}
catch(e) {
console.log(e)
return
}
if (files.length > 0)
for (var i = 0; i < files.length; i++) {
var filePath = dirPath + '/' + files[i]
if (fs.statSync(filePath).isFile())
fs.unlinkSync(filePath)
}
}
// init main window
function initMain () {
mainWindow = new BrowserWindow({
width: 900,
height: 500,
resizable: false,
fullscreenable: false,
hasShadow: true,
title: 'Toaster Uploader',
icon: process.platform === 'darwin' ? __dirname + '/icon.icns' : __dirname + '/icon.png'
})
mainWindow.loadURL('file://' + __dirname + '/index.html')
mainWindow.on('close', (e) => {
if(!forceQuit){
e.preventDefault()
mainWindow.hide()
}
})
}
// init the auth window
function initAuth () {
if (authWindow) return
// tell main window auto info
mainWindow.webContents.send('auth-info', {
code: 'waiting'
})
authWindow = new BrowserWindow({
width: 600,
height: 600,
resizable: false,
fullscreenable: false,
hasShadow: true
})
authWindow.loadURL('file://' + __dirname + '/auth.html')
authWindow.on('closed', () => {
authWindow = null
})
}
// close the auth window when auth finished
ipcMain.on('auth-finish', (event, arg) => {
global.setting.qn = storage.get('qn')
authWindow.close()
// sent auth result to main window
// when update auth info in auth window, the global setting can't be updated as expect
mainWindow.webContents.send('auth-info', {
code: 'success',
auth: arg
})
})
app.on('window-all-closed', () => {
if (process.platform !== 'darwin') {
app.quit()
}
})
app.on('will-quit', () => {
mainWindow = null
authWindow = null
})
app.on('ready', () => {
Menu.setApplicationMenu(menu)
setDefaultLang()
initMain()
if (!global.setting.qn) {
initAuth()
}
if (global.setting.needCompress) {
clearCompresseFiles(global.setting.compressPath)
}
app.on('before-quit', (e) => {
forceQuit = true
})
app.on('activate', (e) => {
mainWindow.show()
})
})