main.js 4.0 KB

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