-
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathmain.js
60 lines (51 loc) · 1.63 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
/**
* trzsz: https://github.com/trzsz/trzsz.js
* Copyright(c) 2022 Lonny Wong <lonnywong@qq.com>
* @license MIT
*/
const nodePty = require("node-pty");
const { app, BrowserWindow, dialog, ipcMain } = require("electron");
const createWindow = () => {
const { screen } = require("electron");
const display = screen.getPrimaryDisplay();
const { width, height } = display.workAreaSize;
const mainWindow = new BrowserWindow({
width: width,
height: height,
webPreferences: {
sandbox: false,
contextIsolation: false,
preload: require("path").join(__dirname, "preload.js"),
},
});
mainWindow.loadFile("index.html");
// node-pty handler
ipcMain.on("pty:spawn", (event, ...args) => {
const pty = nodePty.spawn(...args);
pty.on("exit", (_code, _sig) => process.exit(_code));
pty.on("data", (data) => mainWindow.webContents.send("pty:ondata", data));
ipcMain.on("pty:write", (_event, data) => pty.write(Buffer.from(data)));
ipcMain.on("pty:resize", (_event, columns, rows) => pty.resize(columns, rows));
});
// display native system dialog for opening and saving files.
ipcMain.handle("show-open-dialog-sync", async (event, ...args) => {
const win = BrowserWindow.fromWebContents(event.sender);
return dialog.showOpenDialogSync(win, ...args);
});
if (process.env.DEBUG) {
mainWindow.openDevTools();
}
};
app.whenReady().then(() => {
createWindow();
app.on("activate", () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
});
app.on("window-all-closed", () => {
if (process.platform !== "darwin") {
app.quit();
}
});