main.js 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. const { app, BrowserWindow, Menu, ipcMain, globalShortcut } = require('electron');
  2. const path = require('path');
  3. const HID = require('node-hid');
  4. const devices = HID.devices();
  5. const logitech = devices.filter(el => el.manufacturer == 'Logitech');
  6. const data = new HID.HID(logitech[0].vendorId, logitech[0].productId);
  7. class MainSerivce {
  8. constructor() {
  9. Menu.setApplicationMenu(null) // 去掉菜单栏
  10. app.commandLine.appendSwitch('wm-window-animations-disabled') // 拖动闪屏
  11. this.loadingWin = null
  12. this.mainWin = null
  13. app.on('ready', this.onRead.bind(this))
  14. app.on('activate', this.createWindow.bind(this))
  15. app.on('window-all-closed',app.quit)
  16. }
  17. createLoading() {
  18. this.loadingWin = new BrowserWindow({
  19. frame: false, // 无边框(窗口、工具栏等),只包含网页内容
  20. width: 200,
  21. height: 200,
  22. resizable: false,
  23. center: true,
  24. alwaysOnTop: true,
  25. transparent: true // 窗口是否支持透明,如果想做高级效果最好为true
  26. })
  27. this.loadingWin.loadFile('loading.html')
  28. this.loadingWin.on('close', () => {
  29. this.loadingWin = null
  30. })
  31. }
  32. createWindow() {
  33. if (!this.mainWin) {
  34. this.mainWin = new BrowserWindow({
  35. minWidth: 1300,
  36. minHeight: 760,
  37. width: 1300,
  38. height: 760,
  39. frame: false,
  40. transparent: true,
  41. webPreferences: {
  42. contextIsolation: true,
  43. nodeIntegration: true,
  44. webSecurity: false, // 去掉跨越
  45. nodeIntegrationInWorker: true,
  46. preload: path.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. globalShortcut.register('Control+F12', () => {
  65. // win.flashFrame(true)
  66. this.mainWin.webContents.toggleDevTools()
  67. })
  68. ipcMain.once('close-loading', () => {
  69. this.loadingWin.close()
  70. this.mainWin.show()
  71. })
  72. data.on('data', (db) => {
  73. if(this.mainWin.isEnabled()) this.mainWin?.webContents.send('contrlData',db)
  74. })
  75. }
  76. };
  77. new MainSerivce()