-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.ts
125 lines (108 loc) · 3.53 KB
/
main.ts
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
const electron = require("electron");
const path = require("path");
const log = require("electron-log");
const { app, BrowserWindow, protocol, globalShortcut, ipcMain } = electron;
const miniSize = [800, 600];
const midSize = [1366, 768];
/* 配置 */
let mainWindow;
// 窗体初始配置按照已登录状态下的midsize
let mainWindowConfig = {
width: miniSize[0],
height: miniSize[1],
minWidth: miniSize[0],
minHeight: miniSize[1],
resizable: true,
maximizable: true,
frame: false,
show: false,
titleBarStyle: "hiddenInset",
// icon: path.join(__dirname, app.isPackaged ? "./build/icon.ico" : "./public/icon.ico"),
webPreferences: {
webgl: true,
webSecurity: false,
nodeIntegration: true,
enableRemoteModule: true,
},
};
let globals = { mainWindow };
/* ---- 单实例锁 ---- */
const gotTheLock = app.requestSingleInstanceLock();
if (!gotTheLock) {
app.quit();
} else {
app.on("second-instance", (event, commandLine, workingDirectory) => {
if (mainWindow) {
if (mainWindow.isMinimized()) {
mainWindow.restore();
}
mainWindow.focus();
mainWindow.show();
}
});
}
/* ---- 启动程序窗口 ---- */
app.whenReady().then(() => {
// 开启file协议, cors(9.0未知原因关闭,这算是workaround)
protocol.registerFileProtocol("file", (request, callback) => {
const pathname = request.url.replace("file:///", "");
callback(pathname);
});
createMainWindow();
});
app.commandLine.appendSwitch("disable-features", "OutOfBlinkCors");
app.on("window-all-closed", function () {
app.quit();
});
/* 主窗口 */
function createMainWindow() {
// 当运行第二个实例时,将会聚焦到mainWindow这个窗口
mainWindow = new BrowserWindow(mainWindowConfig);
globals.mainWindow = mainWindow;
mainWindow.once("ready-to-show", () => {
mainWindow.show();
});
if (app.isPackaged) {
mainWindow.loadURL("file://" + path.join(__dirname, "./dist/index.html"));
mainWindow.setMenu(null);
} else {
mainWindow.loadURL("http://localhost:3344");
mainWindow.webContents.openDevTools(); // 打开开发者工具
}
mainWindow.on("closed", function () {
app.quit();
});
mainWindow.webContents.on("render-process-gone", (e) => {
log.log("[FINAL BREATH]", e);
});
globalShortcut.register("ctrl+d+t", () => mainWindow.webContents.openDevTools());
globalShortcut.register("ctrl+d+r", () => mainWindow.webContents.reload());
globalShortcut.register("ctrl+e", () => electron.shell.showItemInFolder(app.getPath("crashDumps")));
ipcMain.on("window-mini-size", (e) => {
mainWindow.restore(); // 防止maximized 无法修改窗口
mainWindow.setMinimumSize(...miniSize);
mainWindow.setSize(...miniSize);
mainWindow.setResizable(false);
mainWindow.setMaximizable(false);
});
ipcMain.on("window-mid-size", (e) => {
mainWindow.setResizable(true);
mainWindow.setMaximizable(true);
mainWindow.setSize(...midSize);
mainWindow.setMinimumSize(...midSize);
});
ipcMain.on("window-max", (e) => {
mainWindow.setResizable(true);
mainWindow.setMaximizable(true);
mainWindow.maximize();
});
ipcMain.on("window-close", (e) => {
e.sender.send("window-before-close");
mainWindow.close();
});
ipcMain.on("window-minimize", (e) => mainWindow.minimize());
ipcMain.on("window-restore", (e) => mainWindow.restore());
ipcMain.on("window-maximize", (e) => mainWindow.maximize());
ipcMain.on("window-hide", (e) => mainWindow.setOpacity(0));
ipcMain.on("window-show", (e) => mainWindow.setOpacity(1));
}