|
1 | | -// Modules to control application life and create native browser window |
2 | | -const { app, BrowserWindow } = require('electron') |
| 1 | +// Modules to control application life, create windows, and handle IPC |
| 2 | +const { app, BrowserWindow, ipcMain } = require('electron') |
3 | 3 | const path = require('node:path') |
4 | 4 |
|
5 | | -function createWindow () { |
6 | | - // Create the browser window. |
| 5 | +// Function to create the main browser window |
| 6 | +function createWindow() { |
7 | 7 | const mainWindow = new BrowserWindow({ |
8 | 8 | width: 800, |
9 | 9 | height: 600, |
10 | 10 | webPreferences: { |
11 | | - preload: path.join(__dirname, 'preload.js') |
| 11 | + // Preload script exposes safe APIs to renderer |
| 12 | + preload: path.join(__dirname, 'preload.js'), |
| 13 | + contextIsolation: true, // keep renderer isolated for security |
| 14 | + nodeIntegration: false // disable Node access in renderer |
12 | 15 | } |
13 | 16 | }) |
14 | 17 |
|
15 | | - // and load the index.html of the app. |
16 | | - mainWindow.loadFile('index.html') |
17 | | - |
18 | | - // Open the DevTools. |
19 | | - // mainWindow.webContents.openDevTools() |
| 18 | + mainWindow.loadFile('index.html') // load UI |
| 19 | + // mainWindow.webContents.openDevTools() // optional for debugging |
20 | 20 | } |
21 | 21 |
|
22 | | -// This method will be called when Electron has finished |
23 | | -// initialization and is ready to create browser windows. |
24 | | -// Some APIs can only be used after this event occurs. |
| 22 | +// Called when Electron has finished initialization |
25 | 23 | app.whenReady().then(() => { |
26 | 24 | createWindow() |
27 | 25 |
|
28 | | - app.on('activate', function () { |
29 | | - // On macOS it's common to re-create a window in the app when the |
30 | | - // dock icon is clicked and there are no other windows open. |
| 26 | + // macOS: re-create window when dock icon clicked and no windows open |
| 27 | + app.on('activate', () => { |
31 | 28 | if (BrowserWindow.getAllWindows().length === 0) createWindow() |
32 | 29 | }) |
33 | 30 | }) |
34 | 31 |
|
35 | | -// Quit when all windows are closed, except on macOS. There, it's common |
36 | | -// for applications and their menu bar to stay active until the user quits |
37 | | -// explicitly with Cmd + Q. |
38 | | -app.on('window-all-closed', function () { |
| 32 | +// Quit app when all windows are closed (except macOS) |
| 33 | +app.on('window-all-closed', () => { |
39 | 34 | if (process.platform !== 'darwin') app.quit() |
40 | 35 | }) |
41 | 36 |
|
42 | | -// In this file you can include the rest of your app's specific main process |
43 | | -// code. You can also put them in separate files and require them here. |
| 37 | +// ---------------------- |
| 38 | +// IPC: listen for time requests from renderer |
| 39 | +// Sends back local system time as string |
| 40 | +ipcMain.on('get-time', (event) => { |
| 41 | + const now = new Date() // get local system time |
| 42 | + event.reply('time-response', now.toLocaleTimeString()) // send formatted time back |
| 43 | +}) |
0 commit comments