main.js 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  1. const {app, BrowserWindow, Menu, ipcMain, globalShortcut } = require('electron')// 引入electron
  2. const path = require('path')
  3. let win, loadingWin;
  4. Menu.setApplicationMenu(null) // 去掉菜单栏
  5. app.commandLine.appendSwitch('wm-window-animations-disabled') // 拖动闪屏
  6. // 消息通知
  7. function notifiction(title, options) {
  8. const WebNotification = window.Notification || window.mozNotification || window.webkitNotification
  9. const isTrue = WebNotification.requestPermission(result => {
  10. return (result === 'granted') // granted(允许) || denied(拒绝)
  11. })
  12. const noti = new WebNotification(title, options)
  13. noti.onclick = () => {
  14. console.log('关闭')
  15. }
  16. }
  17. // 创建loading 窗口
  18. const showLoading = () => {
  19. loadingWin = new BrowserWindow({
  20. frame: false, // 无边框(窗口、工具栏等),只包含网页内容
  21. width: 200,
  22. height: 200,
  23. resizable: false,
  24. center: true,
  25. alwaysOnTop: true,
  26. transparent: true // 窗口是否支持透明,如果想做高级效果最好为true
  27. })
  28. loadingWin.loadFile('loading.html')
  29. loadingWin.on('close', () => {
  30. loadingWin = null
  31. })
  32. }
  33. // 创建主程序 窗口
  34. const createWindow = () => {
  35. win = new BrowserWindow({
  36. minWidth: 1300,
  37. minHeight: 760,
  38. width: 1300,
  39. height: 760,
  40. webPreferences: {
  41. contextIsolation: true,
  42. nodeIntegration: true,
  43. webSecurity: false, // 去掉跨越
  44. preload: path.join(__dirname, 'preload.js')
  45. },
  46. show: false
  47. })// 创建一个窗口
  48. // 不同环境加载不同文件
  49. if (app.isPackaged) {
  50. win.loadFile('dist/index.html')
  51. } else {
  52. win.loadURL('http://localhost:6547/')
  53. }
  54. // 事件监听
  55. win.on('close', () => {
  56. // 回收BrowserWindow对象
  57. win = null
  58. })
  59. }
  60. // 事件监听
  61. app.on('ready', async () => {
  62. showLoading()
  63. createWindow()
  64. // 监听渲染进行
  65. ipcMain.once('close-loading', () => {
  66. loadingWin.close()
  67. win.show()
  68. })
  69. // 在BrowserWindow创建完成后,注册全局快捷键
  70. globalShortcut.register('Control+F12', () => {
  71. win.webContents.toggleDevTools()
  72. })
  73. })
  74. app.on('window-all-closed', () => {
  75. app.quit()
  76. })
  77. app.on('activate', () => {
  78. if (win == null) {
  79. createWindow()
  80. }
  81. })