main.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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 path = require('path')
  11. //const fs = require('fs')
  12. const app = electron.app
  13. const BrowserWindow = electron.BrowserWindow
  14. const paths = require('./server/paths')
  15. const pref = require('./server/pref')
  16. let user_language = pref.get('user_language') || (app.getLocale() || '').split('-')[0].toLowerCase() || 'en'
  17. global.user_language = user_language
  18. require('./server/Server')
  19. const tray = require('./menu/tray')
  20. const svr = require('./server/svr')
  21. const main_menu = require('./menu/main_menu')
  22. const checkUpdate = require('./server/checkUpdate')
  23. const windowStateKeeper = require('electron-window-state')
  24. // Keep a global reference of the window object, if you don't, the window will
  25. // be closed automatically when the JavaScript object is garbage collected.
  26. let mainWindow
  27. let contents
  28. let willQuitApp = false
  29. let is_tray_initialized
  30. let renderer
  31. function createWindow () {
  32. // Load the previous state with fallback to defaults
  33. let mainWindowState = windowStateKeeper({
  34. defaultWidth: 800,
  35. defaultHeight: 600,
  36. path: paths.work_path
  37. })
  38. // Create the browser window.
  39. mainWindow = new BrowserWindow({
  40. width: mainWindowState.width,
  41. height: mainWindowState.height,
  42. x: mainWindowState.x,
  43. y: mainWindowState.y,
  44. minWidth: 400,
  45. minHeight: 250,
  46. fullscreenable: true,
  47. icon: path.join(__dirname, 'assets', 'logo_512.png'),
  48. webPreferences: {
  49. nodeIntegration: true
  50. }
  51. // autoHideMenuBar: true,
  52. // titleBarStyle: 'hiddenInset'
  53. })
  54. // Let us register listeners on the window, so we can update the state
  55. // automatically (the listeners will be removed when the window is closed)
  56. // and restore the maximized or full screen state
  57. mainWindowState.manage(mainWindow)
  58. contents = mainWindow.webContents
  59. app.mainWindow = mainWindow
  60. // and load the index.html of the app.
  61. mainWindow.loadURL(`file://${__dirname}/ui/index.html?lang=${user_language}`)
  62. if (process.env && process.env.ENV === 'dev') {
  63. //require('devtron').install()
  64. // Open the DevTools.
  65. mainWindow.webContents.openDevTools()
  66. }
  67. if (pref.get('hide_at_launch')) {
  68. // mainWindow.minimize();
  69. mainWindow.hide()
  70. }
  71. mainWindow.on('close', (e) => {
  72. if (willQuitApp) {
  73. /* the user tried to quit the app */
  74. mainWindow = null
  75. } else {
  76. /* the user only tried to close the window */
  77. e.preventDefault()
  78. mainWindow.hide()
  79. }
  80. })
  81. // Emitted when the window is closed.
  82. mainWindow.on('closed', () => {
  83. // Dereference the window object, usually you would store windows
  84. // in an array if your app supports multi windows, this is the time
  85. // when you should delete the corresponding element.
  86. mainWindow = null
  87. contents = null
  88. })
  89. contents.on('did-finish-load', () => {
  90. if (!is_tray_initialized) {
  91. tray.makeTray(app, contents, user_language)
  92. is_tray_initialized = true
  93. }
  94. })
  95. //require('./bg/events').init(app, contents)
  96. svr.win = mainWindow
  97. }
  98. const gotTheLock = app.requestSingleInstanceLock()
  99. if (!gotTheLock) {
  100. app.quit()
  101. } else {
  102. app.on('second-instance', (event, commandLine, workingDirectory) => {
  103. if (mainWindow) {
  104. if (mainWindow.isMinimized()) {
  105. mainWindow.restore()
  106. }
  107. mainWindow.focus()
  108. }
  109. })
  110. // Create myWindow, load the rest of the app, etc...
  111. app.on('ready', () => {
  112. })
  113. }
  114. // This method will be called when Electron has finished
  115. // initialization and is ready to create browser windows.
  116. // Some APIs can only be used after this event occurs.
  117. app.on('ready', () => {
  118. createWindow()
  119. main_menu.init(app, user_language)
  120. setTimeout(() => {
  121. if (renderer) {
  122. checkUpdate.check(true)
  123. }
  124. }, 1000)
  125. })
  126. electron.ipcMain.on('reg_renderer', (e) => {
  127. renderer = e.sender
  128. })
  129. electron.ipcMain.on('relaunch', () => {
  130. app.relaunch({args: process.argv.slice(1) + ['--relaunch']})
  131. app.exit(0)
  132. })
  133. // Quit when all windows are closed.
  134. app.on('window-all-closed', function () {
  135. // if (process.platform !== 'darwin') {
  136. // app.quit();
  137. // }
  138. })
  139. app.on('show', function () {
  140. if (mainWindow) {
  141. if (mainWindow.isMinimized()) {
  142. mainWindow.restore()
  143. }
  144. mainWindow.show()
  145. } else {
  146. createWindow()
  147. }
  148. })
  149. app.on('activate', function () {
  150. // On OS X it's common to re-create a window in the app when the
  151. // dock icon is clicked and there are no other windows open.
  152. if (!mainWindow) {
  153. createWindow()
  154. } else if (mainWindow.isMinimized()) {
  155. mainWindow.restore()
  156. } else {
  157. mainWindow.show()
  158. }
  159. })
  160. app.on('before-quit', () => willQuitApp = true)