index.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. import { app, BrowserWindow, ipcMain, Menu, Tray, nativeImage } from 'electron'
  2. let tray = null;
  3. let path = require("path")
  4. let icon
  5. let iconS
  6. /**
  7. * Set `__static` path to static files in production
  8. * https://simulatedgreg.gitbooks.io/electron-vue/content/en/using-static-assets.html
  9. */
  10. if (process.env.NODE_ENV !== 'development') {
  11. icon = nativeImage.createFromPath(path.join(__dirname, '../../../icon.png'))
  12. iconS = nativeImage.createFromPath(path.join(__dirname, '../../../empty.png'))
  13. global.__static = require('path').join(__dirname, '/static').replace(/\\/g, '\\\\')
  14. } else {
  15. icon = nativeImage.createFromPath('build/icons/icon.png')
  16. iconS = nativeImage.createFromPath('build/icons/empty.png')
  17. }
  18. let mainWindow
  19. const winURL = process.env.NODE_ENV === 'development'
  20. ? `http://localhost:9080`
  21. : `file://${__dirname}/index.html`
  22. function createWindow() {
  23. let timer = null
  24. let count = 0;
  25. tray = new Tray(icon);
  26. const trayMenuTemplate = [
  27. {
  28. label: '退出',
  29. click: function () {
  30. if (tray) {
  31. tray.destroy()
  32. }
  33. app.exit(0)
  34. }
  35. }
  36. ]
  37. // 图标的上下文菜单
  38. const contextMenu = Menu.buildFromTemplate(trayMenuTemplate)
  39. // 设置此托盘图标的悬停提示内容
  40. tray.setToolTip('欢迎使大博客服系统')
  41. // 设置此图标的上下文菜单
  42. if (process.platform === 'win32' || process.platform === 'win64') {
  43. tray.setContextMenu(contextMenu)
  44. }
  45. ipcMain.on('haveMessage', (event,arg) => {
  46. mainWindow.flashFrame(true)
  47. timer = setInterval(() => {
  48. count += 1
  49. if (count % 2 === 0) {
  50. tray.setImage(icon)
  51. } else {
  52. tray.setImage(iconS) // 创建一个空的nativeImage实例
  53. }
  54. }, 500)
  55. })
  56. tray.on('click', () => {
  57. mainWindow.flashFrame(false)
  58. if (mainWindow.isVisible()) {
  59. mainWindow.hide()
  60. } else {
  61. mainWindow.show()
  62. tray.setImage(icon)
  63. clearInterval(timer)
  64. timer = null
  65. count = 0
  66. }
  67. })
  68. /**
  69. * Initial window options
  70. */
  71. mainWindow = new BrowserWindow({
  72. height: 563,
  73. useContentSize: true,
  74. width: 1000,
  75. })
  76. mainWindow.loadURL(winURL)
  77. mainWindow.on('closed', () => {
  78. mainWindow = null
  79. })
  80. }
  81. app.on('ready', createWindow)
  82. app.on('window-all-closed', () => {
  83. if (process.platform !== 'darwin') {
  84. app.quit()
  85. }
  86. })
  87. app.on('activate', () => {
  88. if (mainWindow === null) {
  89. createWindow()
  90. }
  91. })
  92. /**
  93. * Auto Updater
  94. *
  95. * Uncomment the following code below and install `electron-updater` to
  96. * support auto updating. Code Signing with a valid certificate is required.
  97. * https://simulatedgreg.gitbooks.io/electron-vue/content/en/using-electron-builder.html#auto-updating
  98. */
  99. /*
  100. import { autoUpdater } from 'electron-updater'
  101. autoUpdater.on('update-downloaded', () => {
  102. autoUpdater.quitAndInstall()
  103. })
  104. app.on('ready', () => {
  105. if (process.env.NODE_ENV === 'production') autoUpdater.checkForUpdates()
  106. })
  107. */