-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
237 lines (203 loc) · 6.06 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
/*
Ayase-Client
(C) 2022 kawashiro-ryofu & thr Ayase developers
Licenced Under MPL2.0
*/
// Modules to control application life and create native browser window
const {app, BrowserWindow} = require('electron')
const path = require('path')
//const jquery = require('jquery')
const { dialog } = require('electron')
const os = require('os')
const process = require('node:process')
const shell = require('shelljs')
const log = require('electron-log')
const package = require('./package.json')
const { error } = require('console')
const { ipcMain } = require('electron')
// 日志·版权信息
// <!> 日志规则:仅英文(防止读取日志时出现文字编码问题)
log.info(`Ayase-Client v${package.version}`)
log.info(`(C) ${ new Date().getFullYear()} kawashiro-ryofu & the Ayase Developers`)
log.info(`Licenced Under Mozilla Public License Version 2.0`)
try{
Object.assign(console, log.functions);
}catch(err){
log.error(`Initing Log: ${err}`)
}
try{
shell.config.execPath = shell.which('node').toString()
}catch(err){
log.error(`Initing Shelljs: ${err}`)
}
require('@electron/remote/main').initialize();
// Win32通知修复
if (process.platform === 'win32')
{
app.setAppUserModelId('Ayase');
}
//设置页 锁 防止多开
var settingsLock = false
//渲染器 IPC 接收
ipcMain.on('asynchronous-message', (event, arg) => {
// 渲染进程 IPC调用
var rendererFunc = {
// 打开<del>设置界面</del> 配置文件
// to-do 设置界面
settings: function(){
if(!settingsLock){
settingsLock = true
const settingsWindow = new BrowserWindow({
width: 800,
height: 600,
minWidth: 640,
minHeight: 480,
icon: 'favicon.ico',
backgroundColor: '#000000',
autoHideMenuBar: true,
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
contextIsolation: false,
nodeIntegration: true
},
})
settingsWindow.loadFile('conf/index.html')
log.info('Started Settings Window')
settingsWindow.on('closed',()=>{
settingsLock = false
log.info('Closed Settings Window')
})
}
},
// 阅读器
reader: function(args){
// args:
// e.g. {filepath: "/path/of/page/which/render/export/"}
const readerWindow = new BrowserWindow({
width: 1024,
height: 768,
minWidth: 640,
minHeight: 480,
icon: 'favicon.ico',
autoHideMenuBar: true,
backgroundColor: '#000000',
webPreferences: {
/*preload: path.join(__dirname, 'preload.js'),*/
contextIsolation: false,
nodeIntegration: true,
devTools: false
},
})
try{
readerWindow.loadFile(args.filepath)
readerWindow.on('closed', ()=>{
shell.rm(args.filepath)
})
}catch(err){
log.error(`Reader: ${err}`)
}
},
run: function(args){
// args:
// cmd 要运行的程序、网页、命令
try{
switch(process.platform){
case "darwin":
shell.exec(`open ${ args.cmd }`)
break;
case "win32":
shell.exec(`start ${ args.cmd }`)
break;
default:
shell.exec(`xdg-open ${ args.cmd }`)
}
}catch(err){
log.error(`Calling Shell: ${err}`)
}
}
}
// 解析 渲染器 IPC 接收
var r = JSON.parse(arg)
if(r.argsjson != undefined) rendererFunc[r.command](JSON.parse(r.argsjson))
else rendererFunc[r.command]()
})
function sideNBar (scrwidth, scrheight) {
// Create the browser window.
const mainWindow = new BrowserWindow({
width: 300,
height: scrheight,
x: scrwidth-300,
y: 0,
minHeight: scrheight,
minWidth: 300,
backgroundColor: '#000000',
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
contextIsolation: false,
nodeIntegration: true,
/*devTools: true*/
},
frame: false,
minimizable: false,
maximizable: false,
closable: false,
skipTaskbar: true,
icon: 'favicon.ico',
})
// and load the index.html of the app.
mainWindow.loadFile('index.html')
log.info(`Loaded Sidebar Window`)
//mainWindow.setSkipTaskbar(true)
// Open the DevTools.
//mainWindow.webContents.openDevTools()
require("@electron/remote/main").enable(mainWindow.webContents)
// 崩溃处理
// (有待完善)
mainWindow.webContents.on('crash', function(){
//
const options = {
type: 'error',
title: '进程崩溃了',
message: '这个进程已经崩溃.',
buttons: ['重载', '退出'],
};
dialog.showMessageBox(options)
//
})
}
// This method will be called when Electron has finished
// initialization and is ready to create browser windows.
// Some APIs can only be used after this event occurs.
app.whenReady().then(() => {
const { screen } = require('electron')
const primaryDisplay = screen.getPrimaryDisplay()
const { width, height } = primaryDisplay.workAreaSize
sideNBar(width, height)
app.on('activate', function () {
// On macOS it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
log.info('Ready')
if (BrowserWindow.getAllWindows().length === 0) sideNBar()
})
})
// Quit when all windows are closed, except on macOS. There, it's common
// for applications and their menu bar to stay active until the user quits
// explicitly with Cmd + Q.
app.on('window-all-closed', function () {
if (process.platform !== 'darwin') app.quit()
log.info('Quit')
})
app.on('will-quit', function(){
log.info('Quit')
})
// In this file you can include the rest of your app's specific main process
// code. You can also put them in separate files and require them here.
process.on('uncaughtException', async function(error){
var title = 'Unexpected Error Occurred'
var content = title + `: ${error.toString()}`
log.error(content)
dialog.showErrorBox({
title: title,
content: content
})
})