-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathpreload.js
More file actions
101 lines (87 loc) · 4 KB
/
Copy pathpreload.js
File metadata and controls
101 lines (87 loc) · 4 KB
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
const { contextBridge, ipcRenderer, webUtils } = require("electron");
const fs = require("node:fs");
const colormap = require("colormap");
const p = require("node:path");
const SunCalc = require("suncalc");
const si = require('systeminformation');
// We need to wait until the UI is ready to receive the message before
// sending the port. We create this promise in the preload, so it's guaranteed
// to register the onload listener before the load event is fired.
const windowLoaded = new Promise((resolve) => {
window.onload = resolve;
});
// We request that the main process sends us a channel we can use to
// communicate with the worker.
// now see if we have files to load
ipcRenderer.send("file-to-load");
ipcRenderer.once("load-results", async (event, args) => {
// make sure our ui is ready to receive the message
await windowLoaded;
console.log("Posting file to UI");
window.postMessage({ args: args }, "/");
});
ipcRenderer.once("provide-worker-channel", async (event) => {
// make sure our ui is ready to receive the message
await windowLoaded;
// Once we receive the reply, we can take the port...
const [port] = event.ports;
// ... register a handler to receive results ...
port.onmessage = (event) => {
console.log("received result:", event.data);
};
// now transfer the port
window.postMessage("provide-worker-channel", "/", event.ports);
});
ipcRenderer.on("error", (event, errorMessage) => {
console.error("Uncaught Exception from main process:", errorMessage);
alert("Uncaught Exception from main process:", errorMessage);
});
// capture intel mac
const isIntelMac = process.platform === 'darwin' && process.arch === 'x64';
contextBridge.exposeInMainWorld("electron", {
showFilePath: (file) => webUtils.getPathForFile(file),
requestWorkerChannel: () => ipcRenderer.invoke("request-worker-channel"),
unsavedRecords: (isTrue) =>
ipcRenderer.send("unsaved-records", { newValue: isTrue }),
onDownloadProgress: (callback) =>
ipcRenderer.on("download-progress", callback),
saveFile: (args) => ipcRenderer.invoke("saveFile", args),
exportData: (args) => ipcRenderer.invoke("exportData", args),
selectDirectory: (path) => ipcRenderer.invoke("selectDirectory", path),
openDialog: (method, config) =>
ipcRenderer.invoke("openFiles", method, config),
getInstallInfo: (date) => ipcRenderer.invoke("getInstallInfo", date),
getPath: () => ipcRenderer.invoke("getPath"),
getAppPath: () => ipcRenderer.invoke("getAppPath"),
getLocale: () => ipcRenderer.invoke("getLocale"),
getTemp: () => ipcRenderer.invoke("getTemp"),
getVersion: () => ipcRenderer.invoke("getVersion"),
getAudio: () => ipcRenderer.invoke("getAudio"),
trialPeriod: () => ipcRenderer.invoke("trialPeriod"),
isMac: () => ipcRenderer.invoke("isMac"),
isIntelMac: () => isIntelMac,
exitApplication: () => ipcRenderer.invoke("exitApplication"),
powerSaveBlocker: (onOff) => ipcRenderer.send("powerSaveControl", onOff),
debugMode: (onOff) => ipcRenderer.send("debug-mode", onOff),
onFileOpen: (callback) => ipcRenderer.on('open-file', (event, filePath) => callback(filePath)),
MEMBERSHIP_API_ENDPOINT: () => process.env.MEMBERSHIP_API_ENDPOINT
});
contextBridge.exposeInMainWorld("module", {fs, colormap, p, SunCalc, si});
// Expose only specific environment variables
contextBridge.exposeInMainWorld("env", {
TEST_ENV: process.env.TEST_ENV,
});
// Listen for messages from the main process
// Function to display update download progress
window.addEventListener("DOMContentLoaded", () => {
const tracking = document.getElementById("update-progress");
const updateProgressBar = document.getElementById("update-progress-bar");
ipcRenderer.on("download-progress", (_event, progressObj) => {
console.log(progressObj.percent); // Log the message to the console
tracking.classList.remove("d-none");
// Update your UI with the progress information
updateProgressBar.value = progressObj.percent;
// Hide progress when done
if (progressObj.percent === 100) tracking.classList.add("d-none");
});
});