main.js 3.7 KB

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