main.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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.contrlEvent = null
  12. if (!app.requestSingleInstanceLock({ key: 'contrl' })) {
  13. app.quit()
  14. } else {
  15. app.on('ready', this.onRead.bind(this))
  16. app.on('activate', (e, isVisible) => {
  17. if (!isVisible && this.mainWin) {
  18. // 兼容Mac dock 栏点击
  19. this.mainWin.show()
  20. }
  21. })
  22. app.on('window-all-closed', () => app.quit())
  23. }
  24. }
  25. createLoading() {
  26. const { size: { width, height } } = screen.getPrimaryDisplay()
  27. this.loadingWin = new BrowserWindow({
  28. frame: false, // 无边框(窗口、工具栏等),只包含网页内容
  29. width,
  30. height,
  31. resizable: false,
  32. center: true,
  33. alwaysOnTop: true,
  34. transparent: true // 窗口是否支持透明,如果想做高级效果最好为true
  35. })
  36. if (app.isPackaged) {
  37. this.loadingWin.loadFile(join(__dirname, './loading.html'))
  38. } else {
  39. this.loadingWin.loadFile('loading.html')
  40. }
  41. this.loadingWin.on('close', () => {
  42. this.loadingWin = null
  43. })
  44. }
  45. createWindow() {
  46. this.mainWin = new BrowserWindow({
  47. minWidth: 1300,
  48. minHeight: 760,
  49. width: 1300,
  50. height: 760,
  51. frame: false,
  52. transparent: true,
  53. webPreferences: {
  54. contextIsolation: true,
  55. nodeIntegration: true,
  56. webSecurity: false, // 去掉跨越
  57. nodeIntegrationInWorker: true,
  58. preload: join(__dirname, './preload.js')
  59. },
  60. show: false
  61. })// 创建一个窗口
  62. // 不同环境加载不同文件
  63. if (app.isPackaged) {
  64. this.mainWin.loadFile(join(__dirname, './index.html'))
  65. } else {
  66. this.mainWin.loadURL('http://localhost:6547/')
  67. }
  68. // 事件监听
  69. this.mainWin.on('close', () => { this.mainWin = null })
  70. }
  71. onRead() {
  72. this.createLoading()
  73. this.createWindow()
  74. // 注册调试模式
  75. globalShortcut.register('Ctrl+F12', () => {
  76. this.mainWin.webContents.toggleDevTools()
  77. })
  78. // 系统环境
  79. if (platform === 'win32') {
  80. // 右键
  81. tray.setContextMenu(contextMenu)
  82. // 禁用右键
  83. this.mainWin.hookWindowMessage(278, () => {
  84. this.mainWin.setEnabled(false);//窗口禁用
  85. setTimeout(() => {
  86. this.mainWin.setEnabled(true);
  87. }, 100) //延时太快会立刻启动,太慢会妨碍窗口其他操作,可自行测试最佳时间
  88. return true
  89. })
  90. }
  91. // 通信
  92. ipcMain.on('signal', (_, evt, data) => {
  93. if (evt === 'close-loading') {
  94. if (this.loadingWin) this.loadingWin.close()
  95. this.mainWin.show()
  96. } else if (evt === 'minWin') {
  97. this.mainWin.minimize()
  98. } else if (evt === 'closeWin') {
  99. this.mainWin.close()
  100. } else if (evt === 'maxWin') {
  101. const { width, height } = screen.getPrimaryDisplay().size
  102. if (data) { this.mainWin.setBounds({ x: 0, y: 0, width, height }) } else {
  103. this.mainWin.setBounds({ width: 1300, height: 760 })
  104. this.mainWin.center()
  105. }
  106. }
  107. })
  108. }
  109. };
  110. new MainSerivce()