Skip to content

Instantly share code, notes, and snippets.

@rudifa
Last active October 27, 2022 18:32
Show Gist options
  • Select an option

  • Save rudifa/1466eb2b47cf0575a59bf05f2d068669 to your computer and use it in GitHub Desktop.

Select an option

Save rudifa/1466eb2b47cf0575a59bf05f2d068669 to your computer and use it in GitHub Desktop.

Revisions

  1. rudifa revised this gist Oct 27, 2022. No changes.
  2. rudifa renamed this gist Oct 27, 2022. 1 changed file with 0 additions and 0 deletions.
    File renamed without changes.
  3. rudifa revised this gist Oct 27, 2022. 1 changed file with 18 additions and 0 deletions.
    18 changes: 18 additions & 0 deletions Electron
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,18 @@
    // 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
  4. rudifa revised this gist Oct 27, 2022. 3 changed files with 17 additions and 0 deletions.
    6 changes: 6 additions & 0 deletions main.js
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,11 @@
    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) {
    @@ -20,6 +25,7 @@ function createWindow () {
    }

    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 () {
    3 changes: 3 additions & 0 deletions preload.js
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,8 @@
    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')
    })
    8 changes: 8 additions & 0 deletions renderer.js
    Original file line number Diff line number Diff line change
    @@ -1,3 +1,11 @@
    // 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')

  5. rudifa created this gist Oct 27, 2022.
    14 changes: 14 additions & 0 deletions index.html
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,14 @@
    <!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>
    32 changes: 32 additions & 0 deletions main.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,32 @@
    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()
    })
    16 changes: 16 additions & 0 deletions package.json
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    {
    "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"
    }
    }
    5 changes: 5 additions & 0 deletions preload.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,5 @@
    const { contextBridge, ipcRenderer } = require('electron')

    contextBridge.exposeInMainWorld('electronAPI',{
    openFile: () => ipcRenderer.invoke('dialog:openFile')
    })
    7 changes: 7 additions & 0 deletions renderer.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,7 @@
    const btn = document.getElementById('btn')
    const filePathElement = document.getElementById('filePath')

    btn.addEventListener('click', async () => {
    const filePath = await window.electronAPI.openFile()
    filePathElement.innerText = filePath
    })
    3 changes: 3 additions & 0 deletions styles.css
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,3 @@
    /* styles.css */

    /* Add styles here to customize the appearance of your app */