main.js 4.0 KB

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