Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
sagemathinc
GitHub Repository: sagemathinc/wapython
Path: blob/main/desktop/electron/src/index.ts
1067 views
1
import { app, BrowserWindow, ipcMain } from "electron";
2
import getPython, { pythonTerminate } from "./python";
3
import { join } from "path";
4
5
if (require("electron-squirrel-startup")) {
6
// Handle creating/removing shortcuts on Windows when installing/uninstalling.
7
app.quit();
8
}
9
10
declare global {
11
interface Window {
12
electronAPI: {
13
pythonExec: (arg: string) => Promise<void>;
14
pythonRepr: (arg: string) => Promise<string>;
15
pythonStdin: (data: string) => void;
16
pythonTerminal: () => void;
17
onPythonStdout: (cb: (data: string) => void) => void;
18
onPythonStderr: (cb: (data: string) => void) => void;
19
};
20
}
21
}
22
23
let mainWindow: null | BrowserWindow = null;
24
const createWindow = () => {
25
// Create the browser window.
26
mainWindow = new BrowserWindow({
27
width: 1000,
28
height: 800,
29
title: "CoWasm Desktop Python Shell",
30
webPreferences: {
31
preload: join(__dirname, "preload.js"),
32
},
33
});
34
35
mainWindow.loadFile(join(__dirname, "../renderer/index.html"));
36
// mainWindow.webContents.openDevTools();
37
};
38
// Quit when all windows are closed, except on macOS. There, it's common
39
// for applications and their menu bar to stay active until the user quits
40
// explicitly with Cmd + Q.
41
app.on("window-all-closed", () => {
42
if (process.platform !== "darwin") {
43
app.quit();
44
}
45
});
46
47
app.on("activate", () => {
48
if (BrowserWindow.getAllWindows().length === 0) {
49
createWindow();
50
}
51
});
52
app.setMaxListeners(100);
53
54
ipcMain.handle("python:exec", async (_event, arg: string) => {
55
const python = await getPython();
56
return await python.exec(arg);
57
});
58
59
ipcMain.handle("python:repr", async (_event, arg: string) => {
60
const python = await getPython();
61
return await python.repr(arg);
62
});
63
64
ipcMain.on("python:stdin", async (_event, data) => {
65
const python = await getPython();
66
python.kernel.writeToStdin(data);
67
});
68
69
ipcMain.on("python:terminal", async (_event) => {
70
const python = await getPython();
71
python.kernel.on("stdout", (data: Buffer) => {
72
mainWindow?.webContents.send("python:stdout", data);
73
});
74
python.kernel.on("stderr", (data: Buffer) => {
75
mainWindow?.webContents.send("python:stderr", data);
76
});
77
await python.terminal();
78
pythonTerminate();
79
});
80
81
async function main() {
82
await app.whenReady();
83
84
createWindow();
85
}
86
87
main();
88
89