main.js 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  1. // Modules to control application life and create native browser window
  2. const { app, BrowserWindow, ipcMain, dialog, shell } = require('electron/main')
  3. // Keep a global reference of the window object, if you don't, the window will
  4. // be closed automatically when the JavaScript object is garbage collected.
  5. let mainWindow
  6. function createWindow () {
  7. // Create the browser window.
  8. mainWindow = new BrowserWindow({
  9. width: 800,
  10. height: 600,
  11. webPreferences: {
  12. contextIsolation: false,
  13. nodeIntegration: true
  14. }
  15. })
  16. // and load the index.html of the app.
  17. mainWindow.loadFile('index.html')
  18. // Open the DevTools.
  19. // mainWindow.webContents.openDevTools()
  20. // Emitted when the window is closed.
  21. mainWindow.on('closed', function () {
  22. // Dereference the window object, usually you would store windows
  23. // in an array if your app supports multi windows, this is the time
  24. // when you should delete the corresponding element.
  25. mainWindow = null
  26. })
  27. // Open external links in the default browser
  28. mainWindow.webContents.on('will-navigate', (event, url) => {
  29. event.preventDefault()
  30. shell.openExternal(url)
  31. })
  32. }
  33. // This method will be called when Electron has finished
  34. // initialization and is ready to create browser windows.
  35. // Some APIs can only be used after this event occurs.
  36. app.whenReady().then(createWindow)
  37. // Quit when all windows are closed.
  38. app.on('window-all-closed', function () {
  39. // On macOS it is common for applications and their menu bar
  40. // to stay active until the user quits explicitly with Cmd + Q
  41. if (process.platform !== 'darwin') {
  42. app.quit()
  43. }
  44. })
  45. app.on('activate', function () {
  46. // On macOS it is common to re-create a window in the app when the
  47. // dock icon is clicked and there are no other windows open.
  48. if (mainWindow === null) {
  49. createWindow()
  50. }
  51. })
  52. ipcMain.on('open-file-dialog', event => {
  53. dialog.showOpenDialog(
  54. {
  55. properties: ['openFile', 'openDirectory']
  56. },
  57. files => {
  58. if (files) {
  59. event.sender.send('selected-directory', files)
  60. }
  61. }
  62. )
  63. })
  64. // In this file you can include the rest of your app's specific main process
  65. // code. You can also put them in separate files and require them here.