From 67322b4e8dbcba5b62decca20f9ef457aa5b9fad Mon Sep 17 00:00:00 2001 From: KochiyaOcean Date: Sat, 23 Jul 2016 16:30:05 +0800 Subject: [PATCH] Rewrite app --- app.coffee | 134 ----------------------------------------- app.js | 170 +++++++++++++++++++++++++++++++++++++++++++++++++++++ index.js | 8 +-- 3 files changed, 174 insertions(+), 138 deletions(-) delete mode 100644 app.coffee create mode 100644 app.js diff --git a/app.coffee b/app.coffee deleted file mode 100644 index 05fc01327..000000000 --- a/app.coffee +++ /dev/null @@ -1,134 +0,0 @@ -{app, BrowserWindow, ipcMain, Tray, nativeImage} = require 'electron' -path = require 'path-extra' -fs = require 'fs-extra' - -# Environment -global.POI_VERSION = app.getVersion() -global.ROOT = __dirname -global.EXECROOT = path.join(process.execPath, '..') -global.APPDATA_PATH = path.join(app.getPath('appData'), 'poi') -global.EXROOT = global.APPDATA_PATH -global.DEFAULT_CACHE_PATH = path.join(global.EXROOT, 'MyCache') -global.MODULE_PATH = path.join(global.ROOT, "node_modules") - -CONST = require './lib/constant' -config = require './lib/config' -proxy = require './lib/proxy' -proxy.setMaxListeners 30 -update = require './lib/update' -shortcut = require './lib/shortcut' -{log, warn, error} = require './lib/utils' - -poiIconPath = path.join ROOT, 'assets', 'icons', 'poi.ico' - -# Disable HA -disableHA = config.get 'poi.disableHA', false -if disableHA - app.disableHardwareAcceleration() - -# Add shortcut to start menu when os is windows -app.setAppUserModelId 'org.poooi.poi' -if process.platform == 'win32' && config.get 'poi.createShortcut', true - windowsShortcuts = require 'windows-shortcuts-appid' - shortcutPath = app.getPath('appData') + "\\Microsoft\\Windows\\Start Menu\\Programs\\poi.lnk" - targetPath = app.getPath('exe') - argPath = app.getAppPath() - try - fs.accessSync shortcutPath - windowsShortcuts.edit shortcutPath, {target: targetPath, args: argPath}, -> - windowsShortcuts.addAppId shortcutPath, 'org.poooi.poi' - catch error - try - windowsShortcuts.create shortcutPath, {target: targetPath, args: argPath}, -> - windowsShortcuts.addAppId shortcutPath, 'org.poooi.poi' - -if dbg.isEnabled() - global.SERVER_HOSTNAME = '127.0.0.1:17027' -else - global.SERVER_HOSTNAME = 'poi.0u0.moe' - -global.mainWindow = mainWindow = null - -platform_to_paths = - 'win32-ia32': 'win-ia32' - 'win32-x64': 'win-x64' - 'darwin-x64': 'osx-x64' - 'linux-x64': 'linux-x64' - -flashPath1 = path.join ROOT, '..', 'PepperFlash', platform_to_paths["#{process.platform}-#{process.arch}"] -flashPath2 = path.join ROOT, 'PepperFlash', platform_to_paths["#{process.platform}-#{process.arch}"] -require('flash-player-loader').debug( - enable: dbg.isEnabled() - log: dbg._log - error: error -).addSource(flashPath1, '21.0.0.242').addSource(flashPath2, '21.0.0.242').load() - -app.on 'window-all-closed', -> - shortcut.unregister() - app.quit() - -app.on 'ready', -> - shortcut.register() - {screen} = require 'electron' - {workArea} = screen.getPrimaryDisplay() - {x, y, width, height} = config.get 'poi.window', workArea - validate = (n, min, range) -> - n? and n >= min and n < min + range - withinDisplay = (d) -> - wa = d.workArea - validate(x, wa.x, wa.width) and validate(y, wa.y, wa.height) - if not screen.getAllDisplays().some withinDisplay - {x, y} = workArea - width ?= workArea.width - height ?= workArea.height - global.mainWindow = mainWindow = new BrowserWindow - x: x - y: y - width: width - height: height - title: 'poi' - icon: poiIconPath - resizable: config.get 'poi.content.resizeable', true - alwaysOnTop: config.get 'poi.content.alwaysOnTop', false - titleBarStyle: 'hidden' - webPreferences: - plugins: true - enableLargerThanScreen: true - # Default menu - mainWindow.reloadArea = 'kan-game webview' - if process.platform == 'darwin' - if /electron$/i.test process.argv[0] - icon = nativeImage.createFromPath("#{ROOT}/assets/icons/poi.png") - app.dock?.setIcon? icon - else - mainWindow.setMenu null - mainWindow.loadURL "file://#{__dirname}/index.html" - if config.get 'poi.window.isMaximized', false - mainWindow.maximize() - if config.get 'poi.window.isFullScreen', false - mainWindow.setFullScreen(true) - if dbg.isEnabled() - mainWindow.openDevTools - detach: true - # Never wants navigate - mainWindow.webContents.on 'will-navigate', (e) -> - e.preventDefault() - mainWindow.on 'closed', -> - # Close all sub window - require('./lib/window').closeWindows() - mainWindow = null - - # Tray icon - if process.platform == 'win32' - global.appIcon = appIcon = new Tray(poiIconPath) - appIcon.on 'click', -> - win = mainWindow - if win.isMinimized() then win.restore() else win.show() - -ipcMain.on 'refresh-shortcut', -> - shortcut.unregister() - shortcut.register() - -# Uncaught error -process.on 'uncaughtException', (e) -> - error e.stack diff --git a/app.js b/app.js new file mode 100644 index 000000000..30141fd01 --- /dev/null +++ b/app.js @@ -0,0 +1,170 @@ +const {app, BrowserWindow, ipcMain, Tray, nativeImage} = require('electron') +const path = require('path-extra') +const fs = require('fs-extra') + +// Environment +global.POI_VERSION = app.getVersion() +global.ROOT = __dirname +global.EXECROOT = path.join(process.execPath, '..') +global.APPDATA_PATH = path.join(app.getPath('appData'), 'poi') +global.EXROOT = global.APPDATA_PATH +global.DEFAULT_CACHE_PATH = path.join(global.EXROOT, 'MyCache') +global.MODULE_PATH = path.join(global.ROOT, "node_modules") + +const {ROOT} = global +const poiIconPath = path.join(ROOT, 'assets', 'icons', 'poi.ico') + +const config = require('./lib/config') +const proxy = require('./lib/proxy') +const shortcut = require('./lib/shortcut') +const {error} = require('./lib/utils') +const dbg = require('./lib/debug') +proxy.setMaxListeners(30) + +// Disable HA + +if (config.get('poi.disableHA', false)) { + app.disableHardwareAcceleration() +} + +// Add shortcut to start menu when os is windows +app.setAppUserModelId('org.poooi.poi') +if (process.platform === 'win32' && config.get('poi.createShortcut', true)) { + const windowsShortcuts = require('windows-shortcuts-appid') + const shortcutPath = app.getPath('appData') + "\\Microsoft\\Windows\\Start Menu\\Programs\\poi.lnk" + const targetPath = app.getPath('exe') + const argPath = app.getAppPath() + try { + fs.accessSync(shortcutPath) + windowsShortcuts.edit( shortcutPath, {target: targetPath, args: argPath}, () => { + windowsShortcuts.addAppId(shortcutPath, 'org.poooi.poi') + }) + } catch (error) { + try { + windowsShortcuts.create (shortcutPath, {target: targetPath, args: argPath}, () =>{ + windowsShortcuts.addAppId(shortcutPath, 'org.poooi.poi') + }) + } catch (err) { + console.error(err) + } + } +} + +if (dbg.isEnabled()) { + global.SERVER_HOSTNAME = '127.0.0.1:17027' +} else { + global.SERVER_HOSTNAME = 'poi.0u0.moe' +} + +const platform_to_paths = { + 'win32-ia32': 'win-ia32', + 'win32-x64': 'win-x64', + 'darwin-x64': 'osx-x64', + 'linux-x64': 'linux-x64', +} + +const flashPath1 = path.join(ROOT, '..', 'PepperFlash', platform_to_paths[`${process.platform}-${process.arch}`]) +const flashPath2 = path.join(ROOT, 'PepperFlash', platform_to_paths[`${process.platform}-${process.arch}`]) +require('flash-player-loader').debug({ + enable: dbg.isEnabled(), + log: dbg._log, + error: error, +}).addSource(flashPath1, '21.0.0.242').addSource(flashPath2, '21.0.0.242').load() + +let mainWindow, appIcon +global.mainWindow = mainWindow = null + +app.on ('window-all-closed', () => { + shortcut.unregister() + app.quit() +}) + +app.on('ready', () => { + const {screen} = require('electron') + shortcut.register() + const {workArea} = screen.getPrimaryDisplay() + let {x, y, width, height} = config.get('poi.window', workArea) + const validate = (n, min, range) => (n != null && n >= min && n < min + range) + const withinDisplay = (d) => { + const wa = d.workArea + return validate(x, wa.x, wa.width) && validate(y, wa.y, wa.height) + } + if (!screen.getAllDisplays().some(withinDisplay)) { + x = workArea.x + y = workArea.y + } + if (width == null) { + width = workArea.width + } + if (height == null) { + height = workArea.height + } + global.mainWindow = mainWindow = new BrowserWindow({ + x: x, + y: y, + width: width, + height: height, + title: 'poi', + icon: poiIconPath, + resizable: config.get('poi.content.resizeable', true), + alwaysOnTop: config.get('poi.content.alwaysOnTop', false), + titleBarStyle: 'hidden', + webPreferences: { + plugins: true, + enableLargerThanScreen: true, + }, + }) + // Default menu + mainWindow.reloadArea = 'kan-game webview' + if (process.platform === 'darwin') { + if (/electron$/i.test(process.argv[0])) { + const icon = nativeImage.createFromPath(`${ROOT}/assets/icons/poi.png`) + app.dock.setIcon(icon) + } + } else { + mainWindow.setMenu(null) + } + mainWindow.loadURL(`file://${__dirname}/index.html`) + if (config.get('poi.window.isMaximized', false)) { + mainWindow.maximize() + } + if (config.get('poi.window.isFullScreen', false)) { + mainWindow.setFullScreen(true) + } + if (dbg.isEnabled()) { + mainWindow.openDevTools({ + detach: true, + }) + } + // Never wants navigate + mainWindow.webContents.on('will-navigate', (e) => { + e.preventDefault() + }) + mainWindow.on('closed', () => { + // Close all sub window + require('./lib/window').closeWindows() + mainWindow = null + }) + + // Tray icon + if (process.platform === 'win32') { + global.appIcon = appIcon = new Tray(poiIconPath) + appIcon.on('click', () => { + if (mainWindow.isMinimized()) { + mainWindow.restore() + } else { + mainWindow.show() + } + }) + } +}) + +ipcMain.on ('refresh-shortcut', () => { + shortcut.unregister() + shortcut.register() +}) + +// Uncaught error +process.on ('uncaughtException', (e) => { + error(e.stack) +}) diff --git a/index.js b/index.js index d269a8770..f7fd16a0e 100644 --- a/index.js +++ b/index.js @@ -1,4 +1,4 @@ -require('coffee-script/register'); -require('babel-register')(require('./babel.config')); -require('./lib/cl'); -require('./app'); +require('coffee-script/register') +require('babel-register')(require('./babel.config')) +require('./lib/cl') +require('./app')