main.js 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. const { app, BrowserWindow, Menu, ipcMain, globalShortcut, dialog, screen, Tray } = require('electron');
  2. const { join } = require('path');
  3. const { fork } = require('child_process');
  4. const { platform } = require('process');
  5. class MainSerivce {
  6. constructor() {
  7. Menu.setApplicationMenu(null) // 去掉菜单栏
  8. app.commandLine.appendSwitch('wm-window-animations-disabled') // 拖动闪屏
  9. this.loadingWin = null
  10. this.mainWin = null
  11. this.icon = join(__dirname, './icon/playGame.png')
  12. app.on('ready', this.onRead.bind(this))
  13. app.on('activate', this.createWindow.bind(this))
  14. app.on('window-all-closed', app.quit)
  15. }
  16. createLoading() {
  17. this.loadingWin = new BrowserWindow({
  18. frame: false, // 无边框(窗口、工具栏等),只包含网页内容
  19. width: 200,
  20. height: 200,
  21. resizable: false,
  22. center: true,
  23. alwaysOnTop: true,
  24. transparent: true // 窗口是否支持透明,如果想做高级效果最好为true
  25. })
  26. this.loadingWin.loadFile('loading.html')
  27. this.loadingWin.on('close', () => {
  28. this.loadingWin = null
  29. })
  30. }
  31. createWindow() {
  32. if (!this.mainWin) {
  33. this.mainWin = new BrowserWindow({
  34. minWidth: 1300,
  35. minHeight: 760,
  36. width: 1300,
  37. height: 760,
  38. frame: false,
  39. transparent: true,
  40. icon: this.icon,
  41. webPreferences: {
  42. contextIsolation: true,
  43. nodeIntegration: true,
  44. webSecurity: false, // 去掉跨越
  45. nodeIntegrationInWorker: true,
  46. preload: join(__dirname, './preload.js')
  47. },
  48. show: false
  49. })// 创建一个窗口
  50. // 不同环境加载不同文件
  51. if (app.isPackaged) {
  52. this.mainWin.loadFile('dist/index.html')
  53. } else {
  54. this.mainWin.loadURL('http://localhost:6547/')
  55. }
  56. // 事件监听
  57. this.mainWin.on('close', () => { this.mainWin = null })
  58. }
  59. }
  60. onRead() {
  61. this.createLoading()
  62. this.createWindow()
  63. // 图标
  64. const tray = new Tray(this.icon)
  65. const contextMenu = Menu.buildFromTemplate([
  66. {
  67. label: '退出',
  68. click: () => {
  69. this.mainWin.close()
  70. app.quit()
  71. }
  72. }
  73. ])
  74. tray.setContextMenu(contextMenu)
  75. tray.setToolTip('控制端')
  76. tray.on('click', () => { this.mainWin.show() })
  77. // 注册调试模式
  78. globalShortcut.register('Control+F12', () => {
  79. this.mainWin.webContents.toggleDevTools()
  80. })
  81. // 禁用右键
  82. if(platform === 'win32') this.mainWin.hookWindowMessage(278, () => {
  83. this.mainWin.setEnabled(false);//窗口禁用
  84. setTimeout(() => {
  85. this.mainWin.setEnabled(true);
  86. }, 100) //延时太快会立刻启动,太慢会妨碍窗口其他操作,可自行测试最佳时间
  87. return true
  88. })
  89. // 通信
  90. ipcMain.on('signal', (_, evt, data) => {
  91. if (evt === 'close-loading') {
  92. if (this.loadingWin) this.loadingWin.close()
  93. this.mainWin.show()
  94. if(this.mainWin.isVisible()) this.connectLogi()
  95. } else if (evt === 'minWin') {
  96. this.mainWin.minimize()
  97. } else if (evt === 'closeWin') {
  98. this.mainWin.close()
  99. } else if (evt === 'maxWin') {
  100. const { width, height } = screen.getPrimaryDisplay().size
  101. if (data) { this.mainWin.setBounds({ x: 0, y: 0, width, height }) } else {
  102. this.mainWin.setBounds({ width: 1300, height: 760 })
  103. this.mainWin.center()
  104. }
  105. }
  106. })
  107. }
  108. connectLogi() {
  109. const signal = fork(join(__dirname, './logiControl.js'))
  110. signal.on('message',msg=>{
  111. if(msg.type === 'err'){
  112. dialog.showMessageBox(this.mainWin, { message: '连接方向盘失败', type: 'error', title: '连接错误', detail: '请检查方向盘是否连接' }).then(({ response }) => {
  113. if (!response) {
  114. this.mainWin.close()
  115. app.quit()
  116. }
  117. })
  118. }else{
  119. console.log(msg.data);
  120. if (this.mainWin && !this.mainWin.isDestroyed()) this.mainWin?.webContents.send('contrlData', msg.data)
  121. }
  122. })
  123. }
  124. };
  125. new MainSerivce()