forked from OpenEnergyTools/scl-editor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
168 lines (150 loc) · 3.99 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
import { app, BrowserWindow, Menu, dialog, ipcMain, shell } from "electron";
import { promises as fs } from "fs";
import { fileURLToPath } from "url";
import * as path from "path";
const __filename = fileURLToPath(import.meta.url); // get the resolved path to the file
const __dirname = path.dirname(__filename); // get the name of the directory
const resourcesPath = app.isPackaged
? process.resourcesPath
: path.join(__dirname);
console.log(resourcesPath);
const packageInfo = JSON.parse(
await fs.readFile(path.join(resourcesPath, "package.json"), "utf8")
);
let mainWindow;
// development vs. production has different process.argv
// https://github.com/electron/electron/issues/4690
if (app.isPackaged) {
process.argv.unshift(null);
}
function createWindow() {
mainWindow = new BrowserWindow({
show: false,
width: 1920,
height: 1080,
webPreferences: {
contextIsolation: true,
nodeIntegration: false,
enableRemoteModule: false,
preload: path.join(__dirname, "preload.js"),
},
});
mainWindow.loadFile("index.html");
mainWindow.webContents.on("before-input-event", (event, input) => {
if (input.control && input.key.toLowerCase() === "i") {
mainWindow.webContents.toggleDevTools();
event.preventDefault();
}
});
}
function createAppMenu() {
const template = [
{
label: "File",
submenu: [
{
label: "Open...",
accelerator: "CmdOrCtrl+O",
click: () => handleFileOpen(),
},
{ type: "separator" },
{ role: "quit" },
],
},
{
role: "help",
submenu: [
{
label: "Learn More",
click: async () => {
await shell.openExternal(
"https://github.com/transpower-nz/open-scd"
);
},
},
{
label: "Documentation",
click: async () => {
await shell.openExternal(
"https://transpower-nz.github.io/open-scd-docs"
);
},
},
{
label: "About",
click: () => {
dialog.showMessageBoxSync({
title: "About",
message: `${packageInfo.build?.productName ?? "Unknown"} v${
packageInfo.version ?? "Unknown"
}`,
detail: `${
packageInfo.description ?? "Unknown"
}\n\nCopyright © ${new Date().getFullYear()} ${
packageInfo.author ?? "Unknown"
}`,
buttons: ["OK"],
icon: "./build/icons/png/512x512.png",
});
},
},
],
},
];
const menu = Menu.buildFromTemplate(template);
Menu.setApplicationMenu(menu);
}
async function handleFileOpen() {
const { canceled, filePaths } = await dialog.showOpenDialog(mainWindow, {
properties: ["openFile"],
filters: [
{
name: "SCL Files",
extensions: ["scd", "icd", "iid", "ssd", "cid", "sed"],
},
{ name: "All Files", extensions: ["*"] },
],
});
if (!canceled) {
mainWindow.webContents.send(
"file-opened",
path.basename(filePaths[0]),
filePaths[0]
);
}
}
ipcMain.handle("read-file", async (_, filePath) => {
try {
const content = await fs.readFile(filePath, "utf8");
return content;
} catch (err) {
console.error("Error reading file:", err);
throw err;
}
});
ipcMain.handle("dialog:openFile", handleFileOpen);
app.whenReady().then(() => {
createWindow();
createAppMenu();
const inputFilePath = process.argv[2];
mainWindow.once("ready-to-show", () => {
mainWindow.maximize();
if (inputFilePath) {
mainWindow.webContents.send(
"file-opened",
path.basename(inputFilePath),
inputFilePath
);
}
});
app.on("activate", () => {
if (BrowserWindow.getAllWindows().length === 0) {
createWindow();
}
});
});
app.on("window-all-closed", () => {
if (process.platform !== "darwin") {
app.quit();
}
});