| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160 |
- /**
- * SwitchHosts!
- *
- * @author oldj
- * @blog https://oldj.net
- * @homepage https://oldj.github.io/SwitchHosts/
- * @source https://github.com/oldj/SwitchHosts
- */
- const electron = require('electron')
- const fs = require('fs')
- const app = electron.app
- const BrowserWindow = electron.BrowserWindow
- const pref = require('./server/pref')
- let user_language = pref.get('user_language') || (app.getLocale() || '').split('-')[0].toLowerCase() || 'en'
- global.user_language = user_language
- require('./server/Server')
- const tray = require('./menu/tray')
- const svr = require('./server/svr')
- const main_menu = require('./menu/main_menu')
- const checkUpdate = require('./server/checkUpdate')
- // Keep a global reference of the window object, if you don't, the window will
- // be closed automatically when the JavaScript object is garbage collected.
- let mainWindow
- let contents
- let willQuitApp = false
- let is_tray_initialized
- let renderer
- function createWindow () {
- // Create the browser window.
- mainWindow = new BrowserWindow({
- width: 800
- , height: 500
- , minWidth: 400
- , minHeight: 250
- , fullscreenable: true
- //, autoHideMenuBar: true
- })
- contents = mainWindow.webContents
- app.mainWindow = mainWindow
- // and load the index.html of the app.
- mainWindow.loadURL(`file://${__dirname}/ui/index.html?lang=${user_language}`)
- if (process.env && process.env.ENV === 'dev') {
- // Open the DevTools.
- mainWindow.webContents.openDevTools()
- }
- if (pref.get('hide_at_launch')) {
- // mainWindow.minimize();
- mainWindow.hide()
- }
- mainWindow.on('close', (e) => {
- if (willQuitApp) {
- /* the user tried to quit the app */
- mainWindow = null
- } else {
- /* the user only tried to close the window */
- e.preventDefault()
- mainWindow.hide()
- }
- })
- // Emitted when the window is closed.
- mainWindow.on('closed', () => {
- // Dereference the window object, usually you would store windows
- // in an array if your app supports multi windows, this is the time
- // when you should delete the corresponding element.
- mainWindow = null
- contents = null
- })
- contents.on('did-finish-load', () => {
- if (!is_tray_initialized) {
- tray.makeTray(app, contents, user_language)
- is_tray_initialized = true
- }
- })
- //require('./bg/events').init(app, contents)
- svr.win = mainWindow
- }
- const should_quit = app.makeSingleInstance((commandLine, workingDirectory) => {
- // Someone tried to run a second instance, we should focus our window.
- if (mainWindow) {
- if (mainWindow.isMinimized()) {
- mainWindow.restore()
- }
- mainWindow.show()
- // mainWindow.focus();
- }
- })
- if (should_quit) {
- app.quit()
- }
- // This method will be called when Electron has finished
- // initialization and is ready to create browser windows.
- // Some APIs can only be used after this event occurs.
- app.on('ready', () => {
- createWindow()
- main_menu.init(app, user_language)
- setTimeout(() => {
- if (renderer) {
- checkUpdate.check(true)
- }
- }, 1000)
- })
- electron.ipcMain.on('reg_renderer', (e) => {
- renderer = e.sender
- })
- electron.ipcMain.on('relaunch', () => {
- app.relaunch({args: process.argv.slice(1) + ['--relaunch']})
- app.exit(0)
- })
- // Quit when all windows are closed.
- app.on('window-all-closed', function () {
- // if (process.platform !== 'darwin') {
- // app.quit();
- // }
- })
- app.on('show', function () {
- if (mainWindow) {
- if (mainWindow.isMinimized()) {
- mainWindow.restore()
- }
- mainWindow.show()
- } else {
- createWindow()
- }
- })
- app.on('activate', function () {
- // On OS X it's common to re-create a window in the app when the
- // dock icon is clicked and there are no other windows open.
- if (!mainWindow) {
- createWindow()
- } else if (mainWindow.isMinimized()) {
- mainWindow.restore()
- } else {
- mainWindow.show()
- }
- })
- app.on('before-quit', () => willQuitApp = true)
|