Skip to content

Commit 91c0409

Browse files
authored
Merge pull request #31 from StephWood/preload
PopUp Container refactored
2 parents 29e4029 + e5f6fc4 commit 91c0409

File tree

5 files changed

+144
-123
lines changed

5 files changed

+144
-123
lines changed

main.js

Lines changed: 53 additions & 47 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ const {
1919
REDUX_DEVTOOLS,
2020
} = require("electron-devtools-installer");
2121
// Import Auto-Updater- Swell will update itself
22-
// const { autoUpdater } = require("electron-updater");
22+
const { autoUpdater } = require("electron-updater");
2323
// TouchBarButtons are our nav buttons(ex: Select All, Deselect All, Open Selected, Close Selected, Clear All)
2424
const { TouchBarButton, TouchBarSpacer } = TouchBar;
2525

@@ -41,7 +41,7 @@ const { createHttpLink } = require("apollo-link-http");
4141
const { ApolloLink } = require("apollo-link");
4242

4343
// proto-parser func for parsing .proto files
44-
const protoParserFunc = require('./src/client/protoParser.js');
44+
const protoParserFunc = require("./src/client/protoParser.js");
4545

4646
// require menu file
4747
require("./menu/mainMenu");
@@ -206,10 +206,10 @@ function createWindow() {
206206
webPreferences: {
207207
nodeIntegration: false,
208208
contextIsolation: true,
209-
enableRemoteModule: false,
209+
enableRemoteModule: false,
210210
sandbox: true,
211211
webSecurity: true,
212-
preload: path.resolve(__dirname, 'preload.js')
212+
preload: path.resolve(__dirname, "preload.js"),
213213
},
214214
icon: `${__dirname}/src/assets/icons/64x64.png`,
215215
});
@@ -280,8 +280,6 @@ function createWindow() {
280280
************** EVENT LISTENERS **********
281281
****************************************/
282282

283-
284-
285283
// app.on('ready') will be called when Electron has finished
286284
// initialization and is ready to create browser windows.
287285
// Some APIs can only be used after this event occurs.
@@ -305,10 +303,13 @@ app.on("window-all-closed", () => {
305303
}
306304
});
307305

308-
ipcMain.on('toMain', (e, args) => {
309-
console.log('received from ipcRenderer inside main: ', args);
310-
mainWindow.webContents.send('fromMain', `sending ${args} back to ipcRenderer`);
311-
})
306+
ipcMain.on("toMain", (e, args) => {
307+
console.log("received from ipcRenderer inside main: ", args);
308+
mainWindow.webContents.send(
309+
"fromMain",
310+
`sending ${args} back to ipcRenderer`
311+
);
312+
});
312313

313314
// Auto Updating Functionality
314315
const sendStatusToWindow = (text) => {
@@ -350,9 +351,9 @@ const sendStatusToWindow = (text) => {
350351
// // You could call autoUpdater.quitAndInstall(); immediately
351352
// autoUpdater.quitAndInstall();
352353
// });
353-
// ipcMain.on("quit-and-install", () => {
354-
// autoUpdater.quitAndInstall();
355-
// });
354+
ipcMain.on("quit-and-install", () => {
355+
autoUpdater.quitAndInstall();
356+
});
356357
// App page reloads when user selects "Refresh" from pop-up dialog
357358
ipcMain.on("fatalError", () => {
358359
console.log("received fatal error");
@@ -471,50 +472,55 @@ ipcMain.on("import-collection", (event, args) => {
471472
});
472473

473474
// ============ CONFIRM CLEAR HISTORY / RESPONSE COMMUNICATION ===============
474-
ipcMain.on('confirm-clear-history', (event) => {
475+
ipcMain.on("confirm-clear-history", (event) => {
475476
const opts = {
476477
type: "warning",
477478
buttons: ["Okay", "Cancel"],
478479
message: "Are you sure you want to clear history?",
479480
};
480481

481-
dialog.showMessageBox(null, opts)
482-
.then((response) => {
483-
console.log('response to clear-history : ', response)
484-
mainWindow.webContents.send('clear-history-response', response)
485-
})
486-
.catch(err => console.log(`Error on 'confirm-clear-history': ${err}`));
482+
dialog
483+
.showMessageBox(null, opts)
484+
.then((response) => {
485+
console.log("response to clear-history : ", response);
486+
mainWindow.webContents.send("clear-history-response", response);
487+
})
488+
.catch((err) => console.log(`Error on 'confirm-clear-history': ${err}`));
487489
});
488490

489-
ipcMain.on('import-proto', (event) => {
490-
console.log('import-proto event fired!!')
491+
ipcMain.on("import-proto", (event) => {
492+
console.log("import-proto event fired!!");
491493
let importedProto;
492-
dialog.showOpenDialog({
493-
buttonLabel : "Import Proto File",
494-
properties: ['openFile', 'multiSelections'],
495-
filters: [ { name: 'Protos', extensions: ['proto'] } ]
496-
})
497-
.then(filePaths => {
498-
if (!filePaths) return undefined;
499-
// read uploaded proto file & save protoContent in the store
500-
fs.readFile(filePaths.filePaths[0], 'utf-8', (err, file) => {
501-
// handle read error
502-
if (err) {
503-
return console.log('error reading file : ', err);
504-
};
505-
importedProto = file;
506-
protoParserFunc(importedProto)
507-
.then(protoObj => {
508-
console.log('finished with logic. about to send importedProto : ', importedProto, ' and protoObj : ', protoObj)
509-
mainWindow.webContents.send('proto-info', importedProto, protoObj)
510-
})
494+
dialog
495+
.showOpenDialog({
496+
buttonLabel: "Import Proto File",
497+
properties: ["openFile", "multiSelections"],
498+
filters: [{ name: "Protos", extensions: ["proto"] }],
499+
})
500+
.then((filePaths) => {
501+
if (!filePaths) return undefined;
502+
// read uploaded proto file & save protoContent in the store
503+
fs.readFile(filePaths.filePaths[0], "utf-8", (err, file) => {
504+
// handle read error
505+
if (err) {
506+
return console.log("error reading file : ", err);
507+
}
508+
importedProto = file;
509+
protoParserFunc(importedProto).then((protoObj) => {
510+
console.log(
511+
"finished with logic. about to send importedProto : ",
512+
importedProto,
513+
" and protoObj : ",
514+
protoObj
515+
);
516+
mainWindow.webContents.send("proto-info", importedProto, protoObj);
517+
});
518+
});
519+
})
520+
.catch((err) => {
521+
console.log(err);
511522
});
512-
})
513-
.catch(err => {
514-
console.log(err);
515-
})
516-
517-
})
523+
});
518524

519525
// ipcMain listener that
520526
ipcMain.on("http1-fetch-message", (event, arg) => {

package-lock.json

Lines changed: 21 additions & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

preload.js

Lines changed: 29 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,30 @@
1-
const { ipcRenderer, contextBridge } = require('electron');
1+
const { ipcRenderer, contextBridge } = require("electron");
22

3-
contextBridge.exposeInMainWorld(
4-
'api', {
5-
send: (channel, data) => {
6-
// allowlist channels
7-
const allowedChannels = ['toMain', 'confirm-clear-history', 'import-proto' ];
8-
if (allowedChannels.includes(channel)) {
9-
ipcRenderer.send(channel, data);
10-
}
11-
},
12-
receive: (channel, cb) => {
13-
console.log('listening on channel : ', channel)
14-
// allowlist channels
15-
const allowedChannels = ['fromMain', 'add-collection', 'clear-history-response', 'proto-info'];
16-
if (allowedChannels.includes(channel)){
17-
ipcRenderer.on(channel, (event, ...args) => cb(...args));
18-
}
19-
},
20-
}
21-
)
3+
contextBridge.exposeInMainWorld("api", {
4+
send: (channel, data) => {
5+
// allowlist channels
6+
const allowedChannels = [
7+
"toMain",
8+
"confirm-clear-history",
9+
"import-proto",
10+
"quit-and-install",
11+
];
12+
if (allowedChannels.includes(channel)) {
13+
ipcRenderer.send(channel, data);
14+
}
15+
},
16+
receive: (channel, cb) => {
17+
console.log("listening on channel : ", channel);
18+
// allowlist channels
19+
const allowedChannels = [
20+
"fromMain",
21+
"add-collection",
22+
"clear-history-response",
23+
"proto-info",
24+
"message",
25+
];
26+
if (allowedChannels.includes(channel)) {
27+
ipcRenderer.on(channel, (event, ...args) => cb(...args));
28+
}
29+
},
30+
});

0 commit comments

Comments
 (0)