Browse Source

更新检查版本检查。

oldj 8 years ago
parent
commit
6da36bd07c

+ 25 - 16
app/bg/check_for_update.js

@@ -8,7 +8,6 @@
 const request = require('request');
 const cheerio = require('cheerio');
 const {shell, dialog} = require('electron');
-const release_url = 'https://github.com/oldj/SwitchHosts/releases';
 const current_version = require('../version').version;
 const m_lang = require('../ui/lang');
 const util = require('../ui/libs/util');
@@ -50,17 +49,21 @@ function compareVersion(a, b) {
     }
 }
 
-exports.check = () => {
+exports.check = (is_silent = false, renderer = null) => {
+    let release_url = require('../ui/configs').url_download;
     console.log('start check updates..');
     request(release_url, (err, res, body) => {
         let buttons = [lang.ok];
         if (err) {
             console.log(err);
-            dialog.showMessageBox({
-                type: 'error',
-                message: lang.check_update_err,
-                buttons
-            });
+
+            if (!is_silent) {
+                dialog.showMessageBox({
+                    type: 'error',
+                    message: lang.check_update_err,
+                    buttons
+                });
+            }
             return;
         }
 
@@ -79,20 +82,26 @@ exports.check = () => {
         console.log('cmp', cmp);
         let message;
         if (cmp >= 0) {
+            // 没有发现新版本
             message = m_lang.fill(lang.check_update_nofound, util.formatVersion(current_version));
+
         } else {
+            // 发现新版本
             message = m_lang.fill(lang.check_update_found, last_v);
             buttons.unshift(lang.cancel);
+            renderer.send('update_found', last_v);
         }
 
-        dialog.showMessageBox({
-            type: 'info',
-            message,
-            buttons
-        }, (res) => {
-            if (cmp < 0 && res === 1) {
-                shell.openExternal(release_url);
-            }
-        });
+        if (!is_silent) {
+            dialog.showMessageBox({
+                type: 'info',
+                message,
+                buttons
+            }, (res) => {
+                if (cmp < 0 && res === 1) {
+                    shell.openExternal(release_url);
+                }
+            });
+        }
     });
 };

+ 5 - 1
app/bg/events.js

@@ -6,7 +6,7 @@
 'use strict';
 
 const fs = require('fs');
-const {ipcMain} = require('electron');
+const {ipcMain, shell} = require('electron');
 
 exports.init = (app, contents) => {
 
@@ -47,4 +47,8 @@ exports.init = (app, contents) => {
         app.exit(0);
     });
 
+    ipcMain.on('open_url', (e, url) => {
+        shell.openExternal(url);
+    });
+
 };

File diff suppressed because it is too large
+ 0 - 439
app/build/bundle.js


+ 1 - 1
app/common/lang/cn.js

@@ -27,7 +27,7 @@ exports.content = {
     , edit: '编辑'
     , edit_host: '修改 host'
     , export: '导出'
-    , feedback: '反馈'
+    , feedback: '意见反馈'
     , file: '文件'
     , help: '帮助'
     , hide_at_launch: '启动时隐藏'

+ 11 - 0
app/main.js

@@ -28,6 +28,7 @@ let mainWindow;
 let contents;
 let willQuitApp = false;
 let is_tray_initialized;
+let renderer;
 
 function createWindow() {
     // Create the browser window.
@@ -103,6 +104,16 @@ if (should_quit) {
 app.on('ready', () => {
     createWindow();
     require('./ui/modules/mainMenu').init(app, user_language);
+
+    setTimeout(() => {
+        if (renderer) {
+            require('./bg/check_for_update').check(true, renderer);
+        }
+    }, 1000);
+});
+
+electron.ipcMain.on('reg_renderer', (e) => {
+    renderer = e.sender;
 });
 
 // Quit when all windows are closed.

+ 2 - 0
app/ui/components/app.js

@@ -63,6 +63,8 @@ class App extends React.Component {
                 console.log('imported.');
             })
         });
+
+        ipcRenderer.send('reg_renderer');
     }
 
     setCurrent(host) {

+ 17 - 3
app/ui/components/frame/preferences.js

@@ -7,7 +7,7 @@
 
 import React from 'react';
 import Frame from './frame';
-// import classnames from 'classnames';
+import classnames from 'classnames';
 import './preferences.less';
 import lang from '../../lang';
 import util from '../../libs/util';
@@ -30,7 +30,8 @@ export default class PreferencesPrompt extends React.Component {
             after_cmd: SH_Agent.pref.get('after_cmd') || '',
             choice_mode: choice_mode,
             auto_launch: !!SH_Agent.pref.get(AUTO_LAUNCH),
-            hide_at_launch: !!SH_Agent.pref.get('hide_at_launch')
+            hide_at_launch: !!SH_Agent.pref.get('hide_at_launch'),
+            update_found: false // 发现新版本
         };
 
     }
@@ -46,6 +47,13 @@ export default class PreferencesPrompt extends React.Component {
                 show: true
             });
         });
+
+        ipcRenderer.on('update_found', (v) => {
+            console.log(v);
+            this.setState({
+                update_found: true
+            });
+        });
     }
 
     onOK() {
@@ -193,13 +201,19 @@ export default class PreferencesPrompt extends React.Component {
         )
     }
 
+    openDownloadPage() {
+        ipcRenderer.send('open_url', require('../../configs').url_download);
+    }
+
     body() {
         return (
             <div ref="body">
                 {/*<div className="title">{SH_Agent.lang.host_title}</div>*/}
                 {/*<div className="cnt">*/}
                 {/*</div>*/}
-                <div className="current-version">{util.formatVersion(current_version)}</div>
+                <div className={classnames("current-version", {"update-found": this.state.update_found})}>
+                    <a href="#" onClick={this.openDownloadPage}>{util.formatVersion(current_version)}</a>
+                </div>
                 {this.prefLanguage()}
                 {this.prefChoiceMode()}
                 {this.prefAfterCmd()}

+ 20 - 0
app/ui/components/frame/preferences.less

@@ -11,5 +11,25 @@
     float: right;
     margin-top: -60px;
     color: #999;
+
+    a {
+      color: #999;
+
+      &:hover {
+        color: #000;
+      }
+    }
+
+    &.update-found {
+      &:after {
+        content: '';
+        display: block;
+        float: right;
+        width: 6px;
+        height: 6px;
+        background: #f00;
+        border-radius: 3px;
+      }
+    }
   }
 }

+ 1 - 0
app/ui/configs.js

@@ -10,4 +10,5 @@ const m_ver = require('../version').version;
 
 exports.version = m_ver.slice(0, 3).join('.');
 exports.version_full = m_ver.join('.');
+exports.url_download = 'https://github.com/oldj/SwitchHosts/releases';
 

+ 27 - 13
app/ui/modules/tray.js

@@ -13,6 +13,8 @@ const m_lang = require('../lang');
 const m_chk_update = require('../../bg/check_for_update');
 const pref = require('./../libs/pref');
 const os = process.platform;
+const util = require('../libs/util');
+const current_version = require('../../version').version;
 
 let tray = null;
 
@@ -20,9 +22,15 @@ function makeMenu(app, list, contents, sys_lang) {
     let menu = [];
     let lang = m_lang.getLang(pref.get('user_language', sys_lang));
 
-    menu.push({label: 'SwitchHosts!', type: 'normal', click: () => {
-        app.emit('show');
-    }});
+    menu.push({
+        label: 'SwitchHosts!',
+        type: 'normal',
+        // sublabel: util.formatVersion(current_version), // does not work on Mac
+        click: () => {
+            app.emit('show');
+        }
+    });
+    menu.push({label: util.formatVersion(current_version), type: 'normal', enabled: false});
     menu.push({label: '-', type: 'separator'});
 
     let ac = '1234567890abcdefghijklmnopqrstuvwxyz'.split('');
@@ -40,13 +48,17 @@ function makeMenu(app, list, contents, sys_lang) {
     });
 
     menu.push({type: 'separator'});
-    menu.push({label: lang.feedback, type: 'normal', click: () => {
-        shell.openExternal('https://github.com/oldj/SwitchHosts/issues');
-    }});
+    menu.push({
+        label: lang.feedback, type: 'normal', click: () => {
+            shell.openExternal('https://github.com/oldj/SwitchHosts/issues');
+        }
+    });
 
-    menu.push({label: lang.check_update, type: 'normal', click: () => {
-        m_chk_update.check();
-    }});
+    menu.push({
+        label: lang.check_update, type: 'normal', click: () => {
+            m_chk_update.check();
+        }
+    });
 
     if (os === 'darwin') {
         menu.push({
@@ -63,14 +75,16 @@ function makeMenu(app, list, contents, sys_lang) {
     }
 
     menu.push({type: 'separator'});
-    menu.push({label: lang.quit, type: 'normal', accelerator: 'CommandOrControl+Q', click: () => {
-        app.quit();
-    }});
+    menu.push({
+        label: lang.quit, type: 'normal', accelerator: 'CommandOrControl+Q', click: () => {
+            app.quit();
+        }
+    });
 
     return menu;
 }
 
-function makeTray(app, contents, sys_lang='en') {
+function makeTray(app, contents, sys_lang = 'en') {
     let icon = 'logo.png';
     if (process.platform === 'darwin') {
         icon = 'ilogoTemplate.png';

+ 1 - 1
app/version.js

@@ -1 +1 @@
-exports.version = [3,2,3,4260];
+exports.version = [3,2,3,4264];

+ 1 - 1
package.json

@@ -7,7 +7,7 @@
     "test": "echo \"Error: no test specified\" && exit 1",
     "start": "electron app",
     "dev": "ENV=dev electron app",
-    "build": "gulp ver && webpack",
+    "build": "gulp ver && webpack -p",
     "pack": "gulp pack",
     "pack-mac": "gulp pack --platform=macOS",
     "pack-win": "gulp pack --platform=win64",

Some files were not shown because too many files changed in this diff