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
// 1. Renderer defines a listener that will, on the user click, | |
// call asynchronously window.electronAPI.openFile(), | |
// which is defined by preload. | |
// Function openFile returns the data | |
// (the file path selected by the user) that the handler in main | |
// obtained from the OS dialog. | |
// The listener updates the html text with this data. | |
// 2. Preload defines function openFile that calls invoke(<channel>) | |
// which will be handled by the main | |
// 3. Main defines a function to hanndle the fileOpen request | |
// by launching the OS File Open dialog, | |
// and attaches it (below) to handle the request comming in | |
// (from renderer) | |
// on success, the function returns the filepath selected by the user | |
// 3a. handleFileOpen is attached here to handle the request on the channel |
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') | |
// 3. Main defines a function to hanndle the fileOpen request | |
// by launching the OS File Open dialog, | |
// and attaches it (below) to handle the request comming in | |
// (from renderer) | |
// on success, the function returns the filepath selected by the user | |
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(() => { | |
// 3a. handleFileOpen is attached here to handle the request on the channel | |
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') | |
// 2. Preload defines function openFile that calls invoke(<channel>) | |
// which will be handled by the main | |
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
// 1. Renderer defines a listener that will, on the user click, | |
// call asynchronously window.electronAPI.openFile(), | |
// which is defined by preload. | |
// Function openFile returns the data | |
// (the file path selected by the user) that the handler in main | |
// obtained from the OS dialog. | |
// The listener updates the html text with this data. | |
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