main.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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: 1620,
  39. minHeight: 900,
  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. })