-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
160 lines (146 loc) · 3.94 KB
/
index.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
const {
app,
BrowserWindow,
protocol,
Menu,
dialog,
ipcMain,
} = require("electron");
const url = require("url");
const path = require("path");
const FileType = require("file-type");
const glob = require("glob");
const fs = require("fs");
let win;
const isMac = process.platform === "darwin";
const template = [
// { role: 'fileMenu' }
{
label: "File",
submenu: [
{
label: "Open",
accelerator: "CmdOrCtrl+O",
click: function () {
dialog
.showOpenDialog({ properties: ["openFile", "multiSelections"] })
.then(function (data) {
if (data.canceled == false) {
FileType.fromFile(data.filePaths[0]).then((type) => {
data.type = type["mime"].split("/")[0];
win.webContents.send("FileData", data);
});
}
// console.log(await FileType.fromFile(data));
// win.webContents.send("FileData", data);
});
},
},
isMac ? { role: "close" } : { role: "quit" },
],
},
{
role: "help",
submenu: [
{ role: "toggleDevTools" },
{
label: "About",
click: async () => {
createStartupInfo();
},
},
],
},
];
function createStartupInfo() {
// aka. loading screen
const win2 = new BrowserWindow({
width: 500,
height: 400,
frame: true,
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
},
});
win2.setFullScreen(false);
win2.setAlwaysOnTop(false);
win2.loadFile(path.join(__dirname, "dist/ui_templates/about.html"));
win2.show();
return win2;
}
function createWindow() {
win = new BrowserWindow({
width: 800,
height: 600,
webPreferences: {
nodeIntegration: true,
contextIsolation: false,
},
});
win.loadURL(
url.format({
pathname: path.join(__dirname, "dist/ui_templates/index.html"),
protocol: "file:",
slashes: true,
})
);
// win.webContents.openDevTools();
const menu = Menu.buildFromTemplate(template);
Menu.setApplicationMenu(menu);
}
app.on("ready", function () {
createWindow();
console.log(process.argv)
ipcMain.on("synchronous-message", (event, arg) => {
console.log(arg);
if (arg == "openFile") {
dialog
.showOpenDialog({ properties: ["openFile", "multiSelections"] })
.then(function (data) {
FileType.fromFile(data.filePaths[0]).then((type) => {
data.type = type["mime"].split("/")[0];
console.info(data)
win.webContents.send("FileData", data);
});
});
event.returnValue = "";
} else if (arg == "resize") {
// A really ugly hack to force the window to update, so the canvas shows up
win.setSize(win.getSize()[0] + 1, win.getSize()[1]);
setTimeout(function () {
win.setSize(win.getSize()[0] + 1, win.getSize()[1]);
}, 200);
event.returnValue = "";
} else if (arg.includes("indexFolder")) {
const fold = arg.split(";")[1];
folder = fs.readdirSync(fold);
let result = [];
for (const file in folder) {
const myFile = folder[file];
if (fs.statSync(fold + "/" + myFile).isFile()) {
result.push(myFile)
}
}
event.returnValue = result;
} else if (arg == "finishedLoading") {
const tryOpenFile = process.argv[1];
if (tryOpenFile != undefined) {
console.log("Opening file: ", process.argv[1])
if (fs.existsSync(tryOpenFile)) {
data = {
canceled: false,
filePaths: [process.argv[1]]
};
FileType.fromFile(data.filePaths[0]).then((type) => {
data.type = type["mime"].split("/")[0];
console.log(data)
win.webContents.send("FileData", data);
});
} else {
console.log("Argument file does not exist")
}
}
}
});
});