-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
194 lines (171 loc) · 6.13 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
// main.js
const { app, BrowserWindow, ipcMain, dialog, globalShortcut, Tray, Menu } = require('electron');
const fs = require('fs');
const path = require('path');
const {exec} = require('child_process');
const homedirHandler = require('./homedirHandler');
const moment = require('moment');
const themes = require("./styles/stylesManager.js");
let mainWindow;
// Tray creation
let tray = null;
app.on('ready', () => {
// check folder
homedirHandler.checkSnapCodeFolder();
// Register the global shortcut...
globalShortcut.register('CommandOrControl+Alt+S', () => {
if (!mainWindow || mainWindow.isDestroyed()) {
mainWindow = new BrowserWindow({
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
contextIsolation: false,
},
autoHideMenuBar: true,
icon: path.join(__dirname, 'icon.ico')
});
mainWindow.loadFile('index.html');
} else {
mainWindow.show();
}
});
// Create a new tray instance
tray = new Tray(path.join(__dirname, 'icon.ico')); // Replace 'icon.png' with the path to your icon
// Create a context menu for the tray icon
const contextMenu = Menu.buildFromTemplate([
{ label: 'Show App', click: function(){
mainWindow.show();
} },
{ label: 'Quit', click: function(){
app.isQuiting = true;
app.quit();
} }
]);
tray.on('right-click', () => {
tray.popUpContextMenu(contextMenu);
});
});
app.on('ready', () => {
mainWindow = new BrowserWindow({
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
contextIsolation: false, // Enable contextIsolation for better security
},
autoHideMenuBar: true
});
mainWindow.loadFile('index.html');
mainWindow.on('close', function (event) {
if (!app.isQuiting) {
event.preventDefault();
mainWindow.hide();
}
});
ipcMain.on('open-file', async () => {
const { filePaths } = await dialog.showOpenDialog(mainWindow, { properties: ['openFile'] });
const content = fs.readFileSync(filePaths[0], 'utf-8');
// get the file name
const fileName = path.basename(filePaths[0]);
mainWindow.webContents.send('file-opened', filePaths[0], content, fileName);
});
ipcMain.on('save-file', async (event, filePath, content) => {
if (!filePath) {
const { filePath } = await dialog.showSaveDialog(mainWindow, {});
fs.writeFileSync(filePath, content);
// get the file name
const fileName = path.basename(filePath);
mainWindow.webContents.send('file-saved', filePath, fileName);
}
else {
fs.writeFileSync(filePath, content);
// get the file name
const fileName = path.basename(filePath);
mainWindow.webContents.send('file-saved', filePath, fileName);
}
});
ipcMain.on('rename-file', async (event, oldFilePath) => {
// Show the save dialog and get the new file path
const { filePath: newFilePath } = await dialog.showSaveDialog(mainWindow, {});
if (newFilePath) {
// Rename the file
fs.rename(oldFilePath, newFilePath, (err) => {
if (err) {
console.error('Error renaming file:', err);
} else {
// Send a message back to the renderer process with the new file path
// get the new filename first
const fileName = path.basename(newFilePath);
event.reply('file-renamed', newFilePath, fileName);
}
});
}
});
// styles handler
ipcMain.on('get-styles', (event) => {
event.reply("got-style", themes)
});
// open with code
ipcMain.on("open-with-code", (event, filePath) => {
console.log(filePath);
exec(`code "${filePath}"`, (err, stdout, stderr) => {
if (err) {
console.log(err);
}
});
})
// execute the code
ipcMain.on("execute-code", (event, lang, fileLoc) => {
const outputWindow = new BrowserWindow({
webPreferences: {
preload: path.join(__dirname, 'preload.js'),
contextIsolation: false,
},
width: 1000,
height: 600,
autoHideMenuBar: true,
icon: path.join(__dirname, 'icon.ico')
});
outputWindow.loadFile('shellExecutor.html');
event.reply("executing-code");
let command = "";
if (lang === "python") {
command = `python "${fileLoc}"`;
}
else if (lang === "javascript") {
command = `node "${fileLoc}"`;
}
else if (lang === "cpp") {
command = `gcc "${fileLoc}" -o "${fileLoc}.exe" && "${fileLoc}.exe"`;
}
else if (lang === "java") {
command = `javac "${fileLoc}" && java "${fileLoc}"`;
}
else {
event.reply("code-executed", undefined);
}
const commandProcess = exec(command, (err, stdout, stderr) => {
if (err) {
console.log(err);
event.reply("code-executed", err);
}
});
commandProcess.stdout.on('data', (data) => {
console.log(data);
outputWindow.webContents.send("output", data.toString());
});
commandProcess.stderr.on('data', (data) => {
console.log(data);
outputWindow.webContents.send("output", data.toString());
});
commandProcess.on('close', (code) => {
event.reply("code-executed", code);
});
ipcMain.on("input", (event, data) => {
commandProcess.stdin.write(data + "\n");
});
});
});
app.on('window-all-closed', function () {
console.log('window-all-closed');
});
app.on('activate', function () {
if (BrowserWindow.getAllWindows().length === 0) mainWindow.show();
});