index.ts 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  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. // 或者在加载前设置 CSP
  18. mainWindow.webContents.on('did-finish-load', () => {
  19. mainWindow.webContents.insertCSS(`
  20. meta[http-equiv="Content-Security-Policy"] {
  21. content: "default-src 'self'; connect-src 'self' ws://127.0.0.1:8899";
  22. }
  23. `)
  24. })
  25. mainWindow.on('ready-to-show', () => {
  26. // 最大化
  27. mainWindow.maximize()
  28. mainWindow.show()
  29. })
  30. mainWindow.webContents.setWindowOpenHandler(details => {
  31. shell.openExternal(details.url)
  32. return { action: 'deny' }
  33. })
  34. // HMR for renderer base on electron-vite cli.
  35. // Load the remote URL for development or the local html file for production.
  36. if (is.dev && process.env['ELECTRON_RENDERER_URL']) {
  37. mainWindow.loadURL(process.env['ELECTRON_RENDERER_URL'])
  38. } else {
  39. mainWindow.loadFile(join(__dirname, '../renderer/index.html'))
  40. }
  41. }
  42. // This method will be called when Electron has finished
  43. // initialization and is ready to create browser windows.
  44. // Some APIs can only be used after this event occurs.
  45. app.whenReady().then(() => {
  46. // Set app user model id for windows
  47. electronApp.setAppUserModelId('com.electron')
  48. // Default open or close DevTools by F12 in development
  49. // and ignore CommandOrControl + R in production.
  50. // see https://github.com/alex8088/electron-toolkit/tree/master/packages/utils
  51. app.on('browser-window-created', (_, window) => {
  52. optimizer.watchWindowShortcuts(window)
  53. })
  54. // IPC test
  55. ipcMain.on('ping', () => console.log('pong'))
  56. createWindow()
  57. app.on('activate', function () {
  58. // On macOS it's common to re-create a window in the app when the
  59. // dock icon is clicked and there are no other windows open.
  60. if (BrowserWindow.getAllWindows().length === 0) createWindow()
  61. })
  62. })
  63. // Quit when all windows are closed, except on macOS. There, it's common
  64. // for applications and their menu bar to stay active until the user quits
  65. // explicitly with Cmd + Q.
  66. app.on('window-all-closed', () => {
  67. if (process.platform !== 'darwin') {
  68. app.quit()
  69. }
  70. })
  71. // In this file you can include the rest of your app's specific main process
  72. // code. You can also put them in separate files and require them here.