main.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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: 900,
  48. minHeight: 760,
  49. width: 900,
  50. height: 760,
  51. titleBarStyle:'hidden',
  52. titleBarOverlay:{
  53. color:'rgba(0,0,0,0)',
  54. height:40
  55. },
  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. this.mainWin.setMenu(null)
  66. // 不同环境加载不同文件
  67. if (app.isPackaged) {
  68. this.mainWin.loadFile(join(__dirname, './index.html'))
  69. } else {
  70. this.mainWin.loadURL('http://localhost:6547/')
  71. }
  72. // 事件监听
  73. this.mainWin.on('close', () => { this.mainWin = null })
  74. }
  75. onRead() {
  76. this.createLoading()
  77. this.createWindow()
  78. // 注册调试模式
  79. globalShortcut.register('Ctrl+F12', () => {
  80. this.mainWin.webContents.toggleDevTools()
  81. })
  82. // 系统环境
  83. if (platform === 'win32') {
  84. // 右键
  85. tray.setContextMenu(contextMenu)
  86. // 禁用右键
  87. this.mainWin.hookWindowMessage(278, () => {
  88. this.mainWin.setEnabled(false);//窗口禁用
  89. setTimeout(() => {
  90. this.mainWin.setEnabled(true);
  91. }, 100) //延时太快会立刻启动,太慢会妨碍窗口其他操作,可自行测试最佳时间
  92. return true
  93. })
  94. }
  95. // 通信
  96. ipcMain.on('signal', (_, evt, data) => {
  97. if (evt === 'close-loading') {
  98. if (this.loadingWin) this.loadingWin.close()
  99. this.mainWin.show()
  100. } else if (evt === 'minWin') {
  101. this.mainWin.minimize()
  102. } else if (evt === 'closeWin') {
  103. this.mainWin.close()
  104. } else if (evt === 'maxWin') {
  105. const { width, height } = screen.getPrimaryDisplay().size
  106. if (data) { this.mainWin.setBounds({ x: 0, y: 0, width, height }) } else {
  107. this.mainWin.setBounds({ width: 1300, height: 760 })
  108. this.mainWin.center()
  109. }
  110. }
  111. })
  112. }
  113. };
  114. new MainSerivce()