Skip to content
This repository has been archived by the owner on Nov 12, 2018. It is now read-only.

创建全局热键用来唤起Chat窗口.和增加一个未读信息的图标.tooltip显示未读信息数量 #188

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Binary file added assets/unread_icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
9 changes: 9 additions & 0 deletions src/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,13 @@ Common.MESSAGE_PREVENT_RECALL = "Blocked a message recall.";
Common.MESSAGE_PREVENT_RECALL_CN = "阻止了一次撤回";
Common.EMOJI_MAXIUM_SIZE = 120;

Common.globalShortcut = [
{
"Name":"ShowChat",
"Shortcut":"CommandOrControl+Alt+Z",
"func":"() => {this.wechatWindow.Toggle()}"
}
];


module.exports = Common;
16 changes: 15 additions & 1 deletion src/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
'use strict';

const path = require('path');
const {app, ipcMain} = require('electron');
const {app, ipcMain, globalShortcut} = require('electron');

const UpdateHandler = require('./handlers/update');
const Common = require('./common');
Expand All @@ -28,6 +28,12 @@ class ElectronicWeChat {
this.createSplashWindow();
this.createWeChatWindow();
this.createTray();

// register global shortcut
let ShortcutList = Common.globalShortcut;
for (var i =0; i< ShortcutList.length; i++) {
globalShortcut.register(ShortcutList[i]['Shortcut'], eval(ShortcutList[i]['func']));
}
});

app.on('activate', () => {
Expand All @@ -48,6 +54,14 @@ class ElectronicWeChat {
} else {
this.tray.setTitle('');
}
} else {
if (num) {
this.tray.setToolTip('Unread meessage: ' + num);
this.tray.IconSet('unread');
} else {
this.tray.setToolTip('');
this.tray.IconSet('normal');
}
}
});

Expand Down
21 changes: 18 additions & 3 deletions src/windows/controllers/app_tray.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,19 +30,34 @@ class AppTray {
{label: 'Exit', click: () => app.exit(0)}
]);
this.tray.setContextMenu(contextMenu);
} else {
this.tray.on('click', () => this.hideSplashAndShowWeChat());
// } else {
// this.tray.on('click', () => this.hideSplashAndShowWeChat());
}
this.tray.on('click', () => this.hideSplashAndShowWeChat());
}

setTitle(title) {
this.tray.setTitle(title);
}

setToolTip(tip) {
this.tray.setToolTip(tip);
}

IconSet(icon){
let img;
if ( icon == 'normal' ) {
img = nativeImage.createFromPath(path.join(__dirname, '../../../assets/icon.png'));
} else {
img = nativeImage.createFromPath(path.join(__dirname, '../../../assets/unread_icon.png'));
}
this.tray.setImage(img);
}

hideSplashAndShowWeChat() {
if (this.splashWindow.isShown) return;
this.wechatWindow.show();
}
}

module.exports = AppTray;
module.exports = AppTray;
23 changes: 22 additions & 1 deletion src/windows/controllers/wechat.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class WeChatWindow {
this.loginState.current = this.loginState.NULL;
this.inervals = {};
this.createWindow();
this.isShown = false;
}

resizeWindow(isLogged, splashWindow) {
Expand Down Expand Up @@ -65,7 +66,8 @@ class WeChatWindow {
this.wechatWindow.on('close', (e) => {
if (this.wechatWindow.isVisible()) {
e.preventDefault();
this.wechatWindow.hide();
//this.wechatWindow.hide();
this.hide();
}
});

Expand All @@ -89,6 +91,11 @@ class WeChatWindow {
event.preventDefault();
shell.openExternal(new MessageHandler().handleRedirectMessage(url));
});

this.wechatWindow.webContents.on('will-navigate', (event, url) => {
if (url.endsWith('/fake')) event.preventDefault();
});

}

loadURL(url) {
Expand All @@ -97,6 +104,20 @@ class WeChatWindow {

show() {
this.wechatWindow.show();
this.isShown = true;
}

hide() {
this.wechatWindow.hide();
this.isShown = false;
}

Toggle() {
if (this.isShown) {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个判断有点不需要吧 isVisible 判断就好了

this.hide();
} else {
this.show();
}
}

connect() {
Expand Down