-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrenderer.js
More file actions
60 lines (49 loc) · 2.54 KB
/
Copy pathrenderer.js
File metadata and controls
60 lines (49 loc) · 2.54 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
// Renderer — runs in the sandboxed, contextIsolated web page. The only way
// to reach the main process is through the whitelisted API that `preload.js`
// exposed on `window.api` (new) and `window.electronAPI` (legacy).
// --- Legacy: version badges + update banner ---------------------------------
document.getElementById('electron-version').textContent = window.electronAPI.versions.electron;
document.getElementById('chrome-version').textContent = window.electronAPI.versions.chrome;
document.getElementById('node-version').textContent = window.electronAPI.versions.node;
window.electronAPI.getAppVersion().then((version) => {
document.getElementById('app-version').textContent = version;
});
window.electronAPI.onUpdateDownloaded((version) => {
const banner = document.getElementById('update-banner');
banner.textContent = `Update v${version} downloaded. Restart to apply.`;
banner.hidden = false;
});
// Surface auto-update failures so the user knows why the app isn't updating
// instead of wondering at a silent failure. The default template logs to the
// console — wire your own banner / settings UI as appropriate.
const unsubscribeUpdateError = window.electronAPI.onUpdateError((payload) => {
console.error('Auto-update error:', payload);
});
// --- New: IPC bridge demo ----------------------------------------------------
// 1. Request/response — one-shot call into the main process.
window.api.getSystemInfo().then((info) => {
document.getElementById('system-info').textContent = JSON.stringify(info, null, 2);
}).catch((err) => {
document.getElementById('system-info').textContent = `error: ${err.message}`;
});
// 2. Event subscription — main-process broadcast fan-out.
const powerLog = document.getElementById('power-log');
const unsubscribePower = window.api.onPowerEvent((event) => {
// Drop the "waiting..." placeholder on first real event.
const placeholder = powerLog.querySelector('.power-empty');
if (placeholder) placeholder.remove();
const li = document.createElement('li');
const time = new Date(event.at).toLocaleTimeString();
li.textContent = `[${time}] ${event.kind}`;
powerLog.prepend(li);
// Cap the list so it cannot grow unbounded during a long session.
while (powerLog.children.length > 20) {
powerLog.removeChild(powerLog.lastElementChild);
}
});
// Always hand the listeners back on teardown. Without this the main process
// keeps broadcasting to a dead WebContents reference until the app quits.
window.addEventListener('beforeunload', () => {
unsubscribePower();
unsubscribeUpdateError();
});