index.ts 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. import { handleFile } from './files'
  6. const net = require('net')
  7. function createWindow(): void {
  8. // Create the browser window.
  9. const mainWindow = new BrowserWindow({
  10. width: 1920,
  11. height: 1080,
  12. show: false,
  13. autoHideMenuBar: true,
  14. ...(process.platform === 'linux' ? { icon } : {}),
  15. webPreferences: {
  16. preload: join(__dirname, '../preload/index.js'),
  17. sandbox: false
  18. }
  19. })
  20. mainWindow.removeMenu()
  21. mainWindow.on('ready-to-show', () => {
  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, '../renderer/index.html'))
  34. }
  35. // 开发者工具
  36. if (is.dev) {
  37. mainWindow.webContents.openDevTools()
  38. }
  39. // pipe命名管道通信
  40. ipcMain.on('connect-pipe', () => {
  41. try {
  42. const pipe = net.connect('\\\\.\\Pipe\\MyPipeServer')
  43. pipe.on('connect', () => {
  44. mainWindow.webContents.send('pipe-connected', pipe)
  45. })
  46. pipe.on('data', (data) => {
  47. mainWindow.webContents.send('pipe-message', data.toString())
  48. })
  49. pipe.on('error', (data) => {
  50. mainWindow.webContents.send('pipe-error', data.toString())
  51. console.log('pipe error:', data)
  52. })
  53. ipcMain.on('pipe-end', () => {
  54. pipe.end()
  55. })
  56. } catch (error) {
  57. console.error('Error connecting to pipe:', error)
  58. }
  59. })
  60. }
  61. // This method will be called when Electron has finished
  62. // initialization and is ready to create browser windows.
  63. // Some APIs can only be used after this event occurs.
  64. app.whenReady().then(() => {
  65. // Set app user model id for windows
  66. electronApp.setAppUserModelId('com.electron')
  67. // Default open or close DevTools by F12 in development
  68. // and ignore CommandOrControl + R in production.
  69. // see https://github.com/alex8088/electron-toolkit/tree/master/packages/utils
  70. app.on('browser-window-created', (_, window) => {
  71. optimizer.watchWindowShortcuts(window)
  72. })
  73. // IPC test
  74. ipcMain.on('ping', () => console.log('pong'))
  75. // 文件处理
  76. handleFile()
  77. createWindow()
  78. app.on('activate', function () {
  79. // On macOS it's common to re-create a window in the app when the
  80. // dock icon is clicked and there are no other windows open.
  81. if (BrowserWindow.getAllWindows().length === 0) createWindow()
  82. })
  83. })
  84. // Quit when all windows are closed, except on macOS. There, it's common
  85. // for applications and their menu bar to stay active until the user quits
  86. // explicitly with Cmd + Q.
  87. app.on('window-all-closed', () => {
  88. if (process.platform !== 'darwin') {
  89. app.quit()
  90. }
  91. })
  92. // In this file you can include the rest of your app's specific main process
  93. // code. You can also put them in separate files and require them here.