Skip to content

Commit f3365cb

Browse files
committed
Add restart button
1 parent 242b28c commit f3365cb

File tree

4 files changed

+21
-1
lines changed

4 files changed

+21
-1
lines changed

index.html

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ <h1>Hello World!</h1>
1313
Chromium <span id="chrome-version"></span>,
1414
and Electron <span id="electron-version"></span>.
1515

16+
<div>
17+
<button id="btn" type="button">Restart</button>
18+
</div>
19+
1620
<!-- You can also require other files to run in this process -->
1721
<script src="./renderer.js"></script>
1822
</body>

main.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
// Modules to control application life and create native browser window
2-
const { app, BrowserWindow } = require('electron')
2+
const { app, BrowserWindow, ipcMain } = require('electron')
33
const path = require('node:path')
44

5+
function handleRestart () {
6+
app.relaunch()
7+
app.quit()
8+
}
9+
510
function createWindow () {
611
// Create the browser window.
712
const mainWindow = new BrowserWindow({
@@ -23,6 +28,7 @@ function createWindow () {
2328
// initialization and is ready to create browser windows.
2429
// Some APIs can only be used after this event occurs.
2530
app.whenReady().then(() => {
31+
ipcMain.on('restart', handleRestart)
2632
createWindow()
2733

2834
app.on('activate', function () {

preload.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
const { contextBridge, ipcRenderer } = require('electron/renderer')
12
/**
23
* The preload script runs before `index.html` is loaded
34
* in the renderer. It has access to web APIs as well as
@@ -16,3 +17,7 @@ window.addEventListener('DOMContentLoaded', () => {
1617
replaceText(`${type}-version`, process.versions[type])
1718
}
1819
})
20+
21+
contextBridge.exposeInMainWorld('electronAPI', {
22+
restart: () => ipcRenderer.send('restart')
23+
})

renderer.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,8 @@
55
* `contextIsolation` is turned on. Use the contextBridge API in `preload.js`
66
* to expose Node.js functionality from the main process.
77
*/
8+
9+
const button = document.getElementById('btn')
10+
button.addEventListener('click', () => {
11+
window.electronAPI.restart()
12+
})

0 commit comments

Comments
 (0)