-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
71 lines (60 loc) · 1.77 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
import { fileURLToPath } from "url";
import path from "path";
import fs from "fs";
import os from "os";
import { app, BrowserWindow, screen, ipcMain } from "electron";
const __filename = fileURLToPath(import.meta.url);
const __dirname = path.dirname(__filename);
let mainWindow;
function createWindow() {
const { height } = screen.getPrimaryDisplay().workAreaSize;
const windowHeight = height / 2;
const centeredY = (height - windowHeight) / 2;
mainWindow = new BrowserWindow({
width: 250,
height: windowHeight,
webPreferences: {
preload: path.resolve(__dirname, "preload.js"),
contextIsolation: true,
enableRemoteModule: false,
nodeIntegration: false,
},
autoHideMenuBar: true,
resizable: false,
icon: path.join(__dirname, "img/icon.png"),
alwaysOnTop: true,
x: 0,
y: centeredY,
frame: false,
});
mainWindow.loadFile("index.html");
ipcMain.on("close-app", () => {
app.quit();
});
ipcMain.on("minimize-window", () => {
if (mainWindow) {
mainWindow.minimize();
}
});
ipcMain.handle("save-file-to-temp", async (_, { name, data }) => {
const tempDir = os.tmpdir();
const tempFilePath = path.join(tempDir, name);
await fs.promises.writeFile(tempFilePath, data);
return tempFilePath;
});
ipcMain.handle("drag-files", async (_, files) => {
const filePaths = files.map((file) => file.path);
if (filePaths.every((filePath) => fs.existsSync(filePath))) {
mainWindow.webContents.startDrag({
files: filePaths,
icon: path.join(__dirname, "img", "drag-and-drop.png"),
});
return { success: true };
} else {
console.error("One or more files do not have a valid path.");
}
});
}
app.whenReady().then(() => {
createWindow();
});