index.ts 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import { app, shell, BrowserWindow, ipcMain } from 'electron'
  2. import { join } from 'path'
  3. import { electronApp, optimizer, is } from '@electron-toolkit/utils'
  4. import icon from '../../resources/icon.png?asset'
  5. function createWindow(): void {
  6. // Create the browser window.
  7. const mainWindow = new BrowserWindow({
  8. maximizable: true,
  9. show: false,
  10. autoHideMenuBar: true,
  11. ...(process.platform === 'linux' ? { icon } : {}),
  12. webPreferences: {
  13. preload: join(__dirname, '../preload/index.js'),
  14. sandbox: false
  15. }
  16. })
  17. mainWindow.on('ready-to-show', () => {
  18. // 最大化
  19. mainWindow.maximize()
  20. mainWindow.show()
  21. })
  22. mainWindow.webContents.setWindowOpenHandler(details => {
  23. shell.openExternal(details.url)
  24. return { action: 'deny' }
  25. })
  26. // HMR for renderer base on electron-vite cli.
  27. // Load the remote URL for development or the local html file for production.
  28. if (is.dev && process.env['ELECTRON_RENDERER_URL']) {
  29. mainWindow.loadURL(process.env['ELECTRON_RENDERER_URL'])
  30. } else {
  31. mainWindow.loadFile(join(__dirname, '../renderer/index.html'))
  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(() => {
  38. // Set app user model id for windows
  39. electronApp.setAppUserModelId('com.electron')
  40. // Default open or close DevTools by F12 in development
  41. // and ignore CommandOrControl + R in production.
  42. // see https://github.com/alex8088/electron-toolkit/tree/master/packages/utils
  43. app.on('browser-window-created', (_, window) => {
  44. optimizer.watchWindowShortcuts(window)
  45. })
  46. // IPC test
  47. ipcMain.on('ping', () => console.log('pong'))
  48. createWindow()
  49. app.on('activate', function () {
  50. // On macOS it's common to re-create a window in the app when the
  51. // dock icon is clicked and there are no other windows open.
  52. if (BrowserWindow.getAllWindows().length === 0) createWindow()
  53. })
  54. })
  55. // Quit when all windows are closed, except on macOS. There, it's common
  56. // for applications and their menu bar to stay active until the user quits
  57. // explicitly with Cmd + Q.
  58. app.on('window-all-closed', () => {
  59. if (process.platform !== 'darwin') {
  60. app.quit()
  61. }
  62. })
  63. // In this file you can include the rest of your app's specific main process
  64. // code. You can also put them in separate files and require them here.