-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
240 lines (233 loc) · 7.26 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
const { app, BrowserWindow, Menu, ipcMain, dialog, screen, Tray, shell } = require('electron')
const path = require('path');
const fs = require('fs')
const os = require('os')
const createShortcut = require('windows-shortcuts')
const startupFolderPath = path.join(os.homedir(), 'AppData', 'Roaming', 'Microsoft', 'Windows', 'Start Menu', 'Programs', 'Startup');
const prompt = require('electron-prompt');
const Store = require('electron-store');
const { DisableMinimize } = require('electron-disable-minimize');
const store = new Store();
let tray = undefined;
let form = undefined;
var win = undefined;
let template = []
let basePath = app.isPackaged ? './resources/app/' : './'
if (!app.requestSingleInstanceLock({ key: 'classSchedule' })) {
app.quit();
}
const createWindow = () => {
win = new BrowserWindow({
x: 0,
y: 0,
width: screen.getPrimaryDisplay().workAreaSize.width,
height: 200,
frame: false,
transparent: true,
alwaysOnTop: store.get('isWindowAlwaysOnTop', true),
minimizable: false,
maximizable: false,
autoHideMenuBar: true,
resizable: false,
type: 'toolbar',
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
enableRemoteModule: true
},
})
// win.webContents.openDevTools()
win.loadFile('index.html')
if (store.get('isWindowAlwaysOnTop', true))
win.setAlwaysOnTop(true, 'screen-saver', 9999999999999)
}
function setAutoLaunch() {
const shortcutName = '电子课表(请勿重命名).lnk'
app.setLoginItemSettings({ // backward compatible
openAtLogin: false,
openAsHidden: false
})
if (store.get('isAutoLaunch', true)) {
createShortcut.create(startupFolderPath + '/' + shortcutName,
{
target: app.getPath('exe'),
workingDir: app.getPath('exe').split('\\').slice(0, -1).join('\\'),
}, (e) => { e && console.log(e); })
} else {
fs.unlink(startupFolderPath + '/' + shortcutName, () => { })
}
}
app.whenReady().then(() => {
createWindow()
Menu.setApplicationMenu(null)
win.webContents.on('did-finish-load', () => {
win.webContents.send('getWeekIndex');
})
const handle = win.getNativeWindowHandle();
DisableMinimize(handle); // Thank to peter's project https://github.com/tbvjaos510/electron-disable-minimize
setAutoLaunch()
})
ipcMain.on('getWeekIndex', (e, arg) => {
tray = new Tray(basePath + 'image/icon.png')
template = [
{
label: '第一周',
type: 'radio',
click: () => {
win.webContents.send('setWeekIndex', 0)
}
},
{
label: '第二周',
type: 'radio',
click: () => {
win.webContents.send('setWeekIndex', 1)
}
},
{
label: '第三周',
type: 'radio',
click: () => {
win.webContents.send('setWeekIndex', 2)
}
},
{
label: '第四周',
type: 'radio',
click: () => {
win.webContents.send('setWeekIndex', 3)
}
},
{
type: 'separator'
},
{
icon: basePath + 'image/setting.png',
label: '配置课表',
click: () => {
win.webContents.send('openSettingDialog')
}
},
{
icon: basePath + 'image/clock.png',
label: '矫正计时',
click: () => {
win.webContents.send('getTimeOffset')
}
},
{
icon: basePath + 'image/github.png',
label: '源码仓库',
click: () => {
shell.openExternal('https://github.com/EnderWolf006/ElectronClassSchedule');
}
},
{
type: 'separator'
},
{
id: 'countdown',
label: '课上计时',
type: 'checkbox',
checked: store.get('isDuringClassCountdown', true),
click: (e) => {
store.set('isDuringClassCountdown', e.checked)
win.webContents.send('ClassCountdown', e.checked)
}
},
{
label: '窗口置顶',
type: 'checkbox',
checked: store.get('isWindowAlwaysOnTop', true),
click: (e) => {
store.set('isWindowAlwaysOnTop', e.checked)
if (store.get('isWindowAlwaysOnTop', true))
win.setAlwaysOnTop(true, 'screen-saver', 9999999999999)
else
win.setAlwaysOnTop(false)
}
},
{
label: '上课隐藏',
type: 'checkbox',
checked: store.get('isDuringClassHidden', true),
click: (e) => {
store.set('isDuringClassHidden', e.checked)
win.webContents.send('ClassHidden', e.checked)
}
},
{
label: '开机启动',
type: 'checkbox',
checked: store.get('isAutoLaunch', true),
click: (e) => {
store.set('isAutoLaunch', e.checked)
setAutoLaunch()
}
},
{
type: 'separator'
},
{
icon: basePath + 'image/quit.png',
label: '退出程序',
click: () => {
dialog.showMessageBox(win, {
title: '请确认',
message: '你确定要退出程序吗?',
buttons: ['取消', '确定']
}).then((data) => {
if (data.response) app.quit()
})
}
}
]
template[arg].checked = true
form = Menu.buildFromTemplate(template)
tray.setToolTip('电子课表 - by lsl')
function trayClicked() {
tray.popUpContextMenu(form)
}
tray.on('click', trayClicked)
tray.on('right-click', trayClicked)
tray.setContextMenu(form)
win.webContents.send('ClassCountdown', store.get('isDuringClassCountdown', true))
win.webContents.send('ClassHidden', store.get('isDuringClassHidden', true))
})
ipcMain.on('log', (e, arg) => {
console.log(arg);
})
ipcMain.on('setIgnore', (e, arg) => {
if (arg)
win.setIgnoreMouseEvents(true, { forward: true });
else
win.setIgnoreMouseEvents(false);
})
ipcMain.on('dialog', (e, arg) => {
dialog.showMessageBox(win, arg.options).then((data) => {
e.reply(arg.reply, { 'arg': arg, 'index': data.response })
})
})
ipcMain.on('pop', (e, arg) => {
tray.popUpContextMenu(form)
})
ipcMain.on('getTimeOffset', (e, arg) => {
prompt({
title: '计时矫正',
label: '请设置课表计时与系统时间的偏移秒数:',
value: arg.toString(),
inputAttrs: {
type: 'number'
},
type: 'input',
height: 180,
width: 400,
icon: basePath + 'image/clock.png',
}).then((r) => {
if (r === null) {
console.log('[getTimeOffset] User cancelled');
} else {
win.webContents.send('setTimeOffset', Number(r) % 10000000000000)
}
})
})