main.js 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. // Module to control application life.
  12. const app = electron.app;
  13. // Module to create native browser window.
  14. const BrowserWindow = electron.BrowserWindow;
  15. // const yargs = require('yargs');
  16. // console.log('argv', yargs.argv);
  17. const tray = require('./src/modules/tray');
  18. const pref = require('./src/libs/pref');
  19. let user_language = pref.get('user_language') || (app.getLocale() || '').split('-')[0].toLowerCase() || 'en';
  20. // Keep a global reference of the window object, if you don't, the window will
  21. // be closed automatically when the JavaScript object is garbage collected.
  22. let mainWindow;
  23. let contents;
  24. let willQuitApp = false;
  25. let is_tray_initialized;
  26. function createWindow() {
  27. // Create the browser window.
  28. mainWindow = new BrowserWindow({
  29. width: 800, height: 500,
  30. minWidth: 400, minHeight: 250,
  31. fullscreenable: false
  32. });
  33. contents = mainWindow.webContents;
  34. app.mainWindow = mainWindow;
  35. // and load the index.html of the app.
  36. mainWindow.loadURL(`file://${__dirname}/index.html?lang=${user_language}`);
  37. if (process.env && process.env.ENV === 'dev') {
  38. // Open the DevTools.
  39. mainWindow.webContents.openDevTools();
  40. }
  41. mainWindow.on('close', (e) => {
  42. if (willQuitApp) {
  43. /* the user tried to quit the app */
  44. mainWindow = null;
  45. } else {
  46. /* the user only tried to close the window */
  47. e.preventDefault();
  48. mainWindow.hide();
  49. }
  50. });
  51. // Emitted when the window is closed.
  52. mainWindow.on('closed', () => {
  53. // Dereference the window object, usually you would store windows
  54. // in an array if your app supports multi windows, this is the time
  55. // when you should delete the corresponding element.
  56. mainWindow = null;
  57. contents = null;
  58. });
  59. contents.on('did-finish-load', () => {
  60. if (!is_tray_initialized) {
  61. tray.makeTray(app, contents, user_language);
  62. is_tray_initialized = true;
  63. }
  64. });
  65. require('./bg/events').init(app, contents);
  66. }
  67. // This method will be called when Electron has finished
  68. // initialization and is ready to create browser windows.
  69. // Some APIs can only be used after this event occurs.
  70. app.on('ready', () => {
  71. createWindow();
  72. require('./src/modules/mainMenu').init(app, user_language);
  73. });
  74. // Quit when all windows are closed.
  75. app.on('window-all-closed', function () {
  76. // if (process.platform !== 'darwin') {
  77. // app.quit();
  78. // }
  79. });
  80. app.on('show', function () {
  81. if (mainWindow) {
  82. if (mainWindow.isMinimized()) {
  83. mainWindow.restore();
  84. }
  85. mainWindow.show();
  86. } else {
  87. createWindow();
  88. }
  89. });
  90. app.on('activate', function () {
  91. // On OS X it's common to re-create a window in the app when the
  92. // dock icon is clicked and there are no other windows open.
  93. if (mainWindow === null) {
  94. createWindow();
  95. } else if (mainWindow.isMinimized()) {
  96. mainWindow.restore();
  97. } else {
  98. mainWindow.show();
  99. }
  100. });
  101. app.on('before-quit', () => willQuitApp = true);