Skip to content

Commit

Permalink
feature: 支持electron托盘及相关操作
Browse files Browse the repository at this point in the history
  • Loading branch information
SkyJinXX committed Aug 17, 2020
1 parent 06e3845 commit 0a630ec
Show file tree
Hide file tree
Showing 9 changed files with 2,828 additions and 110 deletions.
52 changes: 52 additions & 0 deletions PointGesture.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
const ioHook = require("iohook");
const doAction = require("./action");
const EventRegister = require("./event/EventRegister");

class PointGesture {
constructor() {
this.ioHook = ioHook;
this.register = new EventRegister(this.ioHook);
this.isActive = false;

this.register.regist("holdMouseRBtnMove", (e) => {
switch (e.moveDirection) {
case "up":
doAction("taskView");
break;
case "down":
doAction("taskView");
break;
case "left":
doAction("desktopRight");
break;
case "right":
doAction("desktopLeft");
break;
default:
break;
}
});
}
active() {
this.ioHook.start(false);
this.ioHook.disableClickPropagation();
this.isActive = true;
}
pause() {
this.ioHook.stop();
this.ioHook.enableClickPropagation();
this.isActive = false;
}
/**
* 因为iohook会自动load,所以这里不需要load
*/
start() {
this.active();
}
stop() {
this.pause();
this.ioHook.unload();
}
}

module.exports = PointGesture;
Binary file added assets/images/P.ico
Binary file not shown.
16 changes: 7 additions & 9 deletions event/EventRegister.js
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
const HoldMouseBtnMoveService = require('./HoldMouseBtnMoveService');
const HoldMouseBtnMoveService = require("./HoldMouseBtnMoveService");
const rightBtn = 2;
const leftBtn = 1;
const middleBtn = 3;

class EventRegister {
constructor (ioHook) {
constructor(ioHook) {
this.ioHook = ioHook;
this.holdMouseBtnMoveService = new HoldMouseBtnMoveService(this.ioHook)
this.holdMouseBtnMoveService = new HoldMouseBtnMoveService(this.ioHook);
this.registEvents = {
holdMouseRBtnMove: (handler) => {
this.holdMouseBtnMoveService.add(rightBtn, handler);

}
}
},
};
}
regist (event, handler) {
regist(event, handler) {
this.registEvents[event](handler);
}

}

module.exports = EventRegister;
module.exports = EventRegister;
2 changes: 0 additions & 2 deletions event/HoldMouseBtnMoveService.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,8 +152,6 @@ class HoldMouseBtnMoveService {

add(btnType, handler) {
this.buttonHandler.set(btnType, handler);

this.ioHook.disableClickPropagation();
}

remove(btnType, handler) {
Expand Down
46 changes: 0 additions & 46 deletions index.js

This file was deleted.

31 changes: 31 additions & 0 deletions main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const {app, Menu, Tray} = require('electron');
const path = require('path')
const PointGesture = require('./PointGesture')

app.on('ready', () => {
const pointGesture = new PointGesture()
// const iconUrl = process.env.NODE_ENV === 'development' ? path.join(__dirname, './assets/images/P.ico') : path.join(__dirname, 'static/favicon.ico')
const tray = new Tray(path.join(__dirname,'./assets/images/P.ico'));
const menuTemplate = [
{
id: 'toggle',
label: '暂停',
click: function () {
pointGesture.isActive ? pointGesture.pause() : pointGesture.active();
menuTemplate[0].label = pointGesture.isActive ? '暂停' : '激活';
tray.setContextMenu(Menu.buildFromTemplate(menuTemplate))
}
},
{
label: '退出',
click: function(){
pointGesture.stop();
app.quit();
}
}
]

tray.setToolTip('PointGesture');
tray.setContextMenu(Menu.buildFromTemplate(menuTemplate));
pointGesture.start();
})
Loading

0 comments on commit 0a630ec

Please sign in to comment.