Sui Chen | b65280f | 2020-06-30 18:14:03 -0700 | [diff] [blame] | 1 | // Modules to control application life and create native browser window |
Sui Chen | b53fa1b | 2022-05-26 00:16:42 -0700 | [diff] [blame] | 2 | const {app, BrowserWindow, dialog, ipcMain } = require('electron'); |
Sui Chen | b65280f | 2020-06-30 18:14:03 -0700 | [diff] [blame] | 3 | const path = require('path'); |
Sui Chen | b53fa1b | 2022-05-26 00:16:42 -0700 | [diff] [blame] | 4 | const {fs} = require('file-system'); |
| 5 | |
| 6 | // Open-file dialog |
| 7 | ipcMain.on('file-request', (event) => { |
| 8 | const options = { |
| 9 | title: 'Open a file or folder', |
| 10 | }; |
| 11 | const x = dialog.showOpenDialogSync(options) + ''; // Convert to string |
| 12 | event.reply("filename", x); |
| 13 | }); |
Sui Chen | b65280f | 2020-06-30 18:14:03 -0700 | [diff] [blame] | 14 | |
| 15 | function createWindow() { |
| 16 | // Create the browser window. |
| 17 | const mainWindow = new BrowserWindow({ |
| 18 | width: 1440, |
| 19 | height: 900, |
| 20 | webPreferences: { |
Sui Chen | b65280f | 2020-06-30 18:14:03 -0700 | [diff] [blame] | 21 | nodeIntegration: |
Sui Chen | 27cf933 | 2021-11-03 16:20:28 -0700 | [diff] [blame] | 22 | true, // For opening file dialog from the renderer process |
Sui Chen | b53fa1b | 2022-05-26 00:16:42 -0700 | [diff] [blame] | 23 | contextIsolation: false, |
Sui Chen | b65280f | 2020-06-30 18:14:03 -0700 | [diff] [blame] | 24 | } |
| 25 | }); |
| 26 | |
| 27 | // and load the index.html of the app. |
| 28 | mainWindow.loadFile('index.html'); |
| 29 | |
| 30 | // Open the DevTools. |
| 31 | // mainWindow.webContents.openDevTools() |
| 32 | } |
| 33 | |
| 34 | // This method will be called when Electron has finished |
| 35 | // initialization and is ready to create browser windows. |
| 36 | // Some APIs can only be used after this event occurs. |
| 37 | app.whenReady().then(createWindow); |
| 38 | |
| 39 | // Quit when all windows are closed. |
| 40 | app.on('window-all-closed', function() { |
| 41 | // On macOS it is common for applications and their menu bar |
| 42 | // to stay active until the user quits explicitly with Cmd + Q |
| 43 | if (process.platform !== 'darwin') app.quit(); |
| 44 | }) |
| 45 | |
| 46 | app.on('activate', function() { |
| 47 | // On macOS it's common to re-create a window in the app when the |
| 48 | // dock icon is clicked and there are no other windows open. |
| 49 | if (BrowserWindow.getAllWindows().length === 0) createWindow(); |
| 50 | }); |
| 51 | |
| 52 | // In this file you can include the rest of your app's specific main process |
| 53 | // code. You can also put them in separate files and require them here. |