main.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. import { configAll, configGet } from '@main/actions'
  2. import '@main/core/agent'
  3. import * as message from '@main/core/message'
  4. import '@main/core/popupMenu'
  5. import '@main/data'
  6. import * as cron from '@main/libs/cron'
  7. import getIndex from '@main/libs/getIndex'
  8. import isDev from '@main/libs/isDev'
  9. import { makeMainMenu } from '@main/libs/menu'
  10. import '@main/http'
  11. import '@main/tray'
  12. import version from '@root/version.json'
  13. import { app, BrowserWindow } from 'electron'
  14. import windowStateKeeper from 'electron-window-state'
  15. import * as path from 'path'
  16. import { v4 as uuid4 } from 'uuid'
  17. let win: BrowserWindow | null
  18. let is_will_quit: boolean = false
  19. const createWindow = async () => {
  20. const configs = await configAll()
  21. let main_window_state = windowStateKeeper({
  22. defaultWidth: 800,
  23. defaultHeight: 480,
  24. })
  25. win = new BrowserWindow({
  26. x: main_window_state.x,
  27. y: main_window_state.y,
  28. width: main_window_state.width,
  29. height: main_window_state.height,
  30. minWidth: 300,
  31. minHeight: 200,
  32. autoHideMenuBar: true,
  33. titleBarStyle: 'hiddenInset',
  34. frame: false,
  35. webPreferences: {
  36. contextIsolation: true,
  37. preload: path.join(__dirname, 'preload.js'),
  38. spellcheck: true,
  39. },
  40. })
  41. main_window_state.manage(win)
  42. const ses = win.webContents.session
  43. // console.log(ses.getUserAgent())
  44. global.ua = ses.getUserAgent()
  45. global.main_win = win
  46. if (configs.hide_at_launch) {
  47. win.hide()
  48. }
  49. let hide_dock_icon = await configGet('hide_dock_icon')
  50. if (hide_dock_icon) {
  51. app.dock.hide()
  52. }
  53. console.log('isDev: ', isDev())
  54. if (isDev()) {
  55. process.env.ELECTRON_DISABLE_SECURITY_WARNINGS = '1' // eslint-disable-line require-atomic-updates
  56. }
  57. makeMainMenu(configs.locale)
  58. win.loadURL(getIndex())
  59. .catch(e => console.error(e))
  60. if (isDev()) {
  61. // Open DevTools, see https://github.com/electron/electron/issues/12438 for why we wait for dom-ready
  62. win.webContents.once('dom-ready', () => {
  63. win!.webContents.openDevTools()
  64. })
  65. }
  66. win.on('close', (e: Electron.Event) => {
  67. if (is_will_quit) {
  68. win = null
  69. } else {
  70. e.preventDefault()
  71. win?.hide()
  72. }
  73. })
  74. win.on('closed', () => {
  75. win = null
  76. })
  77. }
  78. const gotTheLock = app.requestSingleInstanceLock()
  79. if (!gotTheLock) {
  80. app.quit()
  81. } else {
  82. app.on('second-instance', (event, commandLine, workingDirectory) => {
  83. if (win) {
  84. if (win.isMinimized()) {
  85. win.restore()
  86. }
  87. win.focus()
  88. }
  89. })
  90. }
  91. const onActive = async () => {
  92. if (win === null) {
  93. await createWindow()
  94. } else if (win.isMinimized()) {
  95. await win.restore()
  96. }
  97. win?.show()
  98. }
  99. app.on('ready', async () => {
  100. console.log(`VERSION: ${version.join('.')}`)
  101. global.session_id = uuid4()
  102. await createWindow()
  103. cron.start()
  104. })
  105. app.on('window-all-closed', () => {
  106. if (process.platform !== 'darwin') {
  107. app.quit()
  108. }
  109. })
  110. app.on('before-quit', () => is_will_quit = true)
  111. app.on('activate', onActive)
  112. message.on('active_main_window', onActive)