|
|
@@ -1,9 +1,6 @@
|
|
|
-const { app, BrowserWindow, Menu, ipcMain, globalShortcut } = require('electron');
|
|
|
-const path = require('path');
|
|
|
+const { app, BrowserWindow, Menu, ipcMain, globalShortcut, dialog, screen, Tray } = require('electron');
|
|
|
+const { join } = require('path');
|
|
|
const HID = require('node-hid');
|
|
|
-const devices = HID.devices();
|
|
|
-const logitech = devices.filter(el => el.manufacturer == 'Logitech');
|
|
|
-const data = new HID.HID(logitech[0].vendorId, logitech[0].productId);
|
|
|
|
|
|
class MainSerivce {
|
|
|
constructor() {
|
|
|
@@ -11,9 +8,10 @@ class MainSerivce {
|
|
|
app.commandLine.appendSwitch('wm-window-animations-disabled') // 拖动闪屏
|
|
|
this.loadingWin = null
|
|
|
this.mainWin = null
|
|
|
+ this.icon = join(__dirname, './icon/playGame.png')
|
|
|
app.on('ready', this.onRead.bind(this))
|
|
|
app.on('activate', this.createWindow.bind(this))
|
|
|
- app.on('window-all-closed',app.quit)
|
|
|
+ app.on('window-all-closed', app.quit)
|
|
|
}
|
|
|
|
|
|
createLoading() {
|
|
|
@@ -41,12 +39,13 @@ class MainSerivce {
|
|
|
height: 760,
|
|
|
frame: false,
|
|
|
transparent: true,
|
|
|
+ icon: this.icon,
|
|
|
webPreferences: {
|
|
|
contextIsolation: true,
|
|
|
nodeIntegration: true,
|
|
|
webSecurity: false, // 去掉跨越
|
|
|
nodeIntegrationInWorker: true,
|
|
|
- preload: path.join(__dirname, './preload.js')
|
|
|
+ preload: join(__dirname, './preload.js')
|
|
|
},
|
|
|
show: false
|
|
|
})// 创建一个窗口
|
|
|
@@ -59,7 +58,7 @@ class MainSerivce {
|
|
|
}
|
|
|
|
|
|
// 事件监听
|
|
|
- this.mainWin.on('close', () => {this.mainWin = null})
|
|
|
+ this.mainWin.on('close', () => { this.mainWin = null })
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -67,20 +66,71 @@ class MainSerivce {
|
|
|
this.createLoading()
|
|
|
this.createWindow()
|
|
|
|
|
|
+ // 图标
|
|
|
+ const tray = new Tray(this.icon)
|
|
|
+ const contextMenu = Menu.buildFromTemplate([
|
|
|
+ {
|
|
|
+ label: '退出',
|
|
|
+ click: () => {
|
|
|
+ this.mainWin.close()
|
|
|
+ app.quit()
|
|
|
+ }
|
|
|
+ }
|
|
|
+ ])
|
|
|
+ tray.setContextMenu(contextMenu)
|
|
|
+ tray.setToolTip('控制端')
|
|
|
+ tray.on('click', () => { this.mainWin.show() })
|
|
|
+
|
|
|
// 注册调试模式
|
|
|
globalShortcut.register('Control+F12', () => {
|
|
|
- // win.flashFrame(true)
|
|
|
this.mainWin.webContents.toggleDevTools()
|
|
|
})
|
|
|
|
|
|
- ipcMain.once('close-loading', () => {
|
|
|
- this.loadingWin.close()
|
|
|
- this.mainWin.show()
|
|
|
+ // 禁用右键
|
|
|
+ this.mainWin.hookWindowMessage(278, () => {
|
|
|
+ this.mainWin.setEnabled(false);//窗口禁用
|
|
|
+ setTimeout(() => {
|
|
|
+ this.mainWin.setEnabled(true);
|
|
|
+ }, 100) //延时太快会立刻启动,太慢会妨碍窗口其他操作,可自行测试最佳时间
|
|
|
+ return true
|
|
|
})
|
|
|
|
|
|
- data.on('data', (db) => {
|
|
|
- if(this.mainWin.isEnabled()) this.mainWin?.webContents.send('contrlData',db)
|
|
|
+ // 通信
|
|
|
+ ipcMain.on('signal', (_, evt, data) => {
|
|
|
+ if (evt === 'close-loading') {
|
|
|
+ if (this.loadingWin) this.loadingWin.close()
|
|
|
+ this.mainWin.show()
|
|
|
+ // this.connectLogi()
|
|
|
+ } else if (evt === 'minWin') {
|
|
|
+ this.mainWin.minimize()
|
|
|
+ } else if (evt === 'closeWin') {
|
|
|
+ this.mainWin.close()
|
|
|
+ } else if (evt === 'maxWin') {
|
|
|
+ const { width, height } = screen.getPrimaryDisplay().size
|
|
|
+ if (data) { this.mainWin.setBounds({ x: 0, y: 0, width, height }) } else {
|
|
|
+ this.mainWin.setBounds({ width: 1300, height: 760 })
|
|
|
+ this.mainWin.center()
|
|
|
+ }
|
|
|
+ }
|
|
|
})
|
|
|
}
|
|
|
+
|
|
|
+ connectLogi() {
|
|
|
+ try {
|
|
|
+ const devices = HID.devices();
|
|
|
+ const logitech = devices.filter(el => el.manufacturer == 'Logitech');
|
|
|
+ const data = new HID.HID(logitech[0].vendorId, logitech[0].productId);
|
|
|
+ data.on('data', (db) => {
|
|
|
+ if (this.mainWin) this.mainWin?.webContents.send('contrlData', db)
|
|
|
+ })
|
|
|
+ } catch (error) {
|
|
|
+ dialog.showMessageBox(this.mainWin, { message: '连接方向盘失败', type: 'error', title: '连接错误', detail: '请检查方向盘是否连接' }).then(({ response }) => {
|
|
|
+ if (!response) {
|
|
|
+ this.mainWin.close()
|
|
|
+ app.quit()
|
|
|
+ }
|
|
|
+ })
|
|
|
+ }
|
|
|
+ }
|
|
|
};
|
|
|
new MainSerivce()
|