main.ts 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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 http_api from '@main/http'
  7. import * as cron from '@main/libs/cron'
  8. import getIndex from '@main/libs/getIndex'
  9. import isDev from '@main/libs/isDev'
  10. import Tracer from '@main/libs/tracer'
  11. import checkSystemLocale from '@main/ui/checkSystemLocale'
  12. import * as find from '@main/ui/find'
  13. import { makeMainMenu } from '@main/ui/menu'
  14. import '@main/ui/tray'
  15. import version from '@root/version.json'
  16. import { app, BrowserWindow, ipcMain, nativeTheme } from 'electron'
  17. import windowStateKeeper from 'electron-window-state'
  18. import * as path from 'path'
  19. import { v4 as uuid4 } from 'uuid'
  20. import { getSwhDb } from '@main/data'
  21. let win: BrowserWindow | null
  22. const createWindow = async () => {
  23. await getSwhDb()
  24. const configs = await configAll()
  25. let main_window_state = windowStateKeeper({
  26. defaultWidth: 800,
  27. defaultHeight: 480,
  28. })
  29. let linux_icon = {}
  30. if (process.platform === 'linux') {
  31. linux_icon = {
  32. icon: path.join(__dirname, '/assets/icon.png'),
  33. }
  34. }
  35. win = new BrowserWindow({
  36. x: main_window_state.x,
  37. y: main_window_state.y,
  38. width: main_window_state.width,
  39. height: main_window_state.height,
  40. minWidth: 300,
  41. minHeight: 200,
  42. autoHideMenuBar: true,
  43. titleBarStyle: 'hiddenInset',
  44. frame: configs.use_system_window_frame || false,
  45. hasShadow: true,
  46. webPreferences: {
  47. contextIsolation: true,
  48. preload: path.join(__dirname, 'preload.js'),
  49. spellcheck: true,
  50. },
  51. ...linux_icon,
  52. })
  53. main_window_state.manage(win)
  54. const ses = win.webContents.session
  55. // console.log(ses.getUserAgent())
  56. global.ua = ses.getUserAgent()
  57. global.main_win = win
  58. if (configs.hide_at_launch) {
  59. win.hide()
  60. }
  61. let hide_dock_icon = await configGet('hide_dock_icon')
  62. if (hide_dock_icon) {
  63. app.dock && app.dock.hide()
  64. } else {
  65. app.dock && app.dock.show().catch((e) => console.error(e))
  66. }
  67. console.log('isDev: ', isDev())
  68. if (isDev()) {
  69. process.env.ELECTRON_DISABLE_SECURITY_WARNINGS = '1' // eslint-disable-line require-atomic-updates
  70. }
  71. makeMainMenu(configs.locale)
  72. win.loadURL(getIndex()).catch((e) => console.error(e))
  73. if (isDev()) {
  74. // Open DevTools, see https://github.com/electron/electron/issues/12438 for why we wait for dom-ready
  75. win.webContents.once('dom-ready', () => {
  76. win!.webContents.openDevTools()
  77. })
  78. }
  79. win.on('close', (e: Electron.Event) => {
  80. if (global.is_will_quit) {
  81. win = null
  82. } else {
  83. e.preventDefault()
  84. win?.hide()
  85. }
  86. })
  87. win.on('closed', () => {
  88. win = null
  89. })
  90. ipcMain.handle('dark-mode:toggle', () => {
  91. if (nativeTheme.shouldUseDarkColors) {
  92. nativeTheme.themeSource = 'light'
  93. } else {
  94. nativeTheme.themeSource = 'dark'
  95. }
  96. return nativeTheme.shouldUseDarkColors
  97. })
  98. ipcMain.handle('dark-mode:dark', () => {
  99. nativeTheme.themeSource = 'dark'
  100. })
  101. ipcMain.handle('dark-mode:light', () => {
  102. nativeTheme.themeSource = 'light'
  103. })
  104. ipcMain.handle('dark-mode:system', () => {
  105. nativeTheme.themeSource = 'system'
  106. })
  107. }
  108. const gotTheLock = app.requestSingleInstanceLock()
  109. if (!gotTheLock) {
  110. app.quit()
  111. } else {
  112. app.on('second-instance', (event, commandLine, workingDirectory) => {
  113. if (win) {
  114. if (win.isMinimized()) {
  115. win.restore()
  116. }
  117. win.focus()
  118. }
  119. })
  120. }
  121. const onActive = async () => {
  122. if (win === null) {
  123. await createWindow()
  124. } else if (win.isMinimized()) {
  125. await win.restore()
  126. }
  127. win?.show()
  128. }
  129. global.tracer = new Tracer()
  130. app.on('ready', async () => {
  131. console.log(`VERSION: ${version.join('.')}`)
  132. global.session_id = uuid4()
  133. await checkSystemLocale()
  134. await createWindow()
  135. cron.start()
  136. let http_api_on = await configGet('http_api_on')
  137. let http_api_only_local = await configGet('http_api_only_local')
  138. if (http_api_on) {
  139. http_api.start(http_api_only_local)
  140. }
  141. find.makeWindow()
  142. })
  143. app.on('window-all-closed', () => {
  144. if (process.platform !== 'darwin') {
  145. app.quit()
  146. }
  147. })
  148. app.on('before-quit', () => (global.is_will_quit = true))
  149. app.on('activate', onActive)
  150. message.on('active_main_window', onActive)