main.js 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // Modules to control application life and create native browser window
  2. const { app, BrowserWindow, ipcMain, shell, dialog } = require('electron/main')
  3. const path = require('node:path')
  4. let mainWindow
  5. if (process.defaultApp) {
  6. if (process.argv.length >= 2) {
  7. app.setAsDefaultProtocolClient('electron-fiddle', process.execPath, [path.resolve(process.argv[1])])
  8. }
  9. } else {
  10. app.setAsDefaultProtocolClient('electron-fiddle')
  11. }
  12. const gotTheLock = app.requestSingleInstanceLock()
  13. if (!gotTheLock) {
  14. app.quit()
  15. } else {
  16. app.on('second-instance', (event, commandLine, workingDirectory) => {
  17. // Someone tried to run a second instance, we should focus our window.
  18. if (mainWindow) {
  19. if (mainWindow.isMinimized()) mainWindow.restore()
  20. mainWindow.focus()
  21. }
  22. dialog.showErrorBox('Welcome Back', `You arrived from: ${commandLine.pop().slice(0, -1)}`)
  23. })
  24. // Create mainWindow, load the rest of the app, etc...
  25. app.whenReady().then(() => {
  26. createWindow()
  27. })
  28. app.on('open-url', (event, url) => {
  29. dialog.showErrorBox('Welcome Back', `You arrived from: ${url}`)
  30. })
  31. }
  32. function createWindow () {
  33. // Create the browser window.
  34. mainWindow = new BrowserWindow({
  35. width: 800,
  36. height: 600,
  37. webPreferences: {
  38. preload: path.join(__dirname, 'preload.js')
  39. }
  40. })
  41. mainWindow.loadFile('index.html')
  42. }
  43. // Quit when all windows are closed, except on macOS. There, it's common
  44. // for applications and their menu bar to stay active until the user quits
  45. // explicitly with Cmd + Q.
  46. app.on('window-all-closed', function () {
  47. if (process.platform !== 'darwin') app.quit()
  48. })
  49. // Handle window controls via IPC
  50. ipcMain.on('shell:open', () => {
  51. const pageDirectory = __dirname.replace('app.asar', 'app.asar.unpacked')
  52. const pagePath = path.join('file://', pageDirectory, 'index.html')
  53. shell.openExternal(pagePath)
  54. })