main.js 2.0 KB

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