index.ts 2.4 KB

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