Last active
October 27, 2022 18:32
-
-
Save rudifa/1466eb2b47cf0575a59bf05f2d068669 to your computer and use it in GitHub Desktop.
The Electron IPC DOCS/FIDDLES/IPC/PATTERN-2 (21.2.0) Renderer to main (two-way)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE html> | |
| <html> | |
| <head> | |
| <meta charset="UTF-8"> | |
| <!-- https://developer.mozilla.org/en-US/docs/Web/HTTP/CSP --> | |
| <meta http-equiv="Content-Security-Policy" content="default-src 'self'; script-src 'self'"> | |
| <title>Dialog</title> | |
| </head> | |
| <body> | |
| <button type="button" id="btn">Open a File</button> | |
| File path: <strong id="filePath"></strong> | |
| <script src='./renderer.js'></script> | |
| </body> | |
| </html> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const {app, BrowserWindow, ipcMain, dialog} = require('electron') | |
| const path = require('path') | |
| async function handleFileOpen() { | |
| const { canceled, filePaths } = await dialog.showOpenDialog() | |
| if (canceled) { | |
| return | |
| } else { | |
| return filePaths[0] | |
| } | |
| } | |
| function createWindow () { | |
| const mainWindow = new BrowserWindow({ | |
| webPreferences: { | |
| preload: path.join(__dirname, 'preload.js') | |
| } | |
| }) | |
| mainWindow.loadFile('index.html') | |
| } | |
| app.whenReady().then(() => { | |
| ipcMain.handle('dialog:openFile', handleFileOpen) | |
| createWindow() | |
| app.on('activate', function () { | |
| if (BrowserWindow.getAllWindows().length === 0) createWindow() | |
| }) | |
| }) | |
| app.on('window-all-closed', function () { | |
| if (process.platform !== 'darwin') app.quit() | |
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "name": "real-chairs-curve-0p1c0", | |
| "productName": "real-chairs-curve-0p1c0", | |
| "description": "My Electron application description", | |
| "keywords": [], | |
| "main": "./main.js", | |
| "version": "1.0.0", | |
| "author": "rudifarkas", | |
| "scripts": { | |
| "start": "electron ." | |
| }, | |
| "dependencies": {}, | |
| "devDependencies": { | |
| "electron": "21.2.0" | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const { contextBridge, ipcRenderer } = require('electron') | |
| contextBridge.exposeInMainWorld('electronAPI',{ | |
| openFile: () => ipcRenderer.invoke('dialog:openFile') | |
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const btn = document.getElementById('btn') | |
| const filePathElement = document.getElementById('filePath') | |
| btn.addEventListener('click', async () => { | |
| const filePath = await window.electronAPI.openFile() | |
| filePathElement.innerText = filePath | |
| }) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* styles.css */ | |
| /* Add styles here to customize the appearance of your app */ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment