1
0

main.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. /**
  2. * SwitchHosts!
  3. *
  4. * @author oldj
  5. * @blog https://oldj.net
  6. * @homepage https://oldj.github.io/SwitchHosts/
  7. * @source https://github.com/oldj/SwitchHosts
  8. */
  9. const electron = require('electron')
  10. const fs = require('fs')
  11. const app = electron.app
  12. const BrowserWindow = electron.BrowserWindow
  13. const pref = require('./server/pref')
  14. let user_language = pref.get('user_language') || (app.getLocale() || '').split('-')[0].toLowerCase() || 'en'
  15. global.user_language = user_language
  16. require('./server/Server')
  17. const tray = require('./menu/tray')
  18. const svr = require('./server/svr')
  19. const main_menu = require('./menu/main_menu')
  20. const checkUpdate = require('./server/checkUpdate')
  21. // Keep a global reference of the window object, if you don't, the window will
  22. // be closed automatically when the JavaScript object is garbage collected.
  23. let mainWindow
  24. let contents
  25. let willQuitApp = false
  26. let is_tray_initialized
  27. let renderer
  28. function createWindow () {
  29. // Create the browser window.
  30. mainWindow = new BrowserWindow({
  31. width: 800
  32. , height: 500
  33. , minWidth: 400
  34. , minHeight: 250
  35. , fullscreenable: true
  36. //, autoHideMenuBar: true
  37. })
  38. contents = mainWindow.webContents
  39. app.mainWindow = mainWindow
  40. // and load the index.html of the app.
  41. mainWindow.loadURL(`file://${__dirname}/ui/index.html?lang=${user_language}`)
  42. if (process.env && process.env.ENV === 'dev') {
  43. // Open the DevTools.
  44. mainWindow.webContents.openDevTools()
  45. }
  46. if (pref.get('hide_at_launch')) {
  47. // mainWindow.minimize();
  48. mainWindow.hide()
  49. }
  50. mainWindow.on('close', (e) => {
  51. if (willQuitApp) {
  52. /* the user tried to quit the app */
  53. mainWindow = null
  54. } else {
  55. /* the user only tried to close the window */
  56. e.preventDefault()
  57. mainWindow.hide()
  58. }
  59. })
  60. // Emitted when the window is closed.
  61. mainWindow.on('closed', () => {
  62. // Dereference the window object, usually you would store windows
  63. // in an array if your app supports multi windows, this is the time
  64. // when you should delete the corresponding element.
  65. mainWindow = null
  66. contents = null
  67. })
  68. contents.on('did-finish-load', () => {
  69. if (!is_tray_initialized) {
  70. tray.makeTray(app, contents, user_language)
  71. is_tray_initialized = true
  72. }
  73. })
  74. //require('./bg/events').init(app, contents)
  75. svr.win = mainWindow
  76. }
  77. const should_quit = app.makeSingleInstance((commandLine, workingDirectory) => {
  78. // Someone tried to run a second instance, we should focus our window.
  79. if (mainWindow) {
  80. if (mainWindow.isMinimized()) {
  81. mainWindow.restore()
  82. }
  83. mainWindow.show()
  84. // mainWindow.focus();
  85. }
  86. })
  87. if (should_quit) {
  88. app.quit()
  89. }
  90. // This method will be called when Electron has finished
  91. // initialization and is ready to create browser windows.
  92. // Some APIs can only be used after this event occurs.
  93. app.on('ready', () => {
  94. createWindow()
  95. main_menu.init(app, user_language)
  96. setTimeout(() => {
  97. if (renderer) {
  98. checkUpdate.check(true)
  99. }
  100. }, 1000)
  101. })
  102. electron.ipcMain.on('reg_renderer', (e) => {
  103. renderer = e.sender
  104. })
  105. electron.ipcMain.on('relaunch', () => {
  106. app.relaunch({args: process.argv.slice(1) + ['--relaunch']})
  107. app.exit(0)
  108. })
  109. // Quit when all windows are closed.
  110. app.on('window-all-closed', function () {
  111. // if (process.platform !== 'darwin') {
  112. // app.quit();
  113. // }
  114. })
  115. app.on('show', function () {
  116. if (mainWindow) {
  117. if (mainWindow.isMinimized()) {
  118. mainWindow.restore()
  119. }
  120. mainWindow.show()
  121. } else {
  122. createWindow()
  123. }
  124. })
  125. app.on('activate', function () {
  126. // On OS X it's common to re-create a window in the app when the
  127. // dock icon is clicked and there are no other windows open.
  128. if (!mainWindow) {
  129. createWindow()
  130. } else if (mainWindow.isMinimized()) {
  131. mainWindow.restore()
  132. } else {
  133. mainWindow.show()
  134. }
  135. })
  136. app.on('before-quit', () => willQuitApp = true)