feat: 支持快捷键设置;支持超级面板

This commit is contained in:
muwoo
2021-07-02 17:12:38 +08:00
parent 4bac5ac8a2
commit 7d55ef06a6
18 changed files with 586 additions and 172 deletions

View File

@@ -2,4 +2,5 @@ module.exports = () => ({
picker: require("./picker")(),
separator: require("./separate")(),
capture: require("./capture")(),
superPanel: require("./superPanel")(),
});

View File

@@ -0,0 +1,54 @@
const { BrowserWindow, ipcMain, app } = require("electron");
module.exports = () => {
let win;
let init = (mainWindow) => {
if (win === null || win === undefined) {
createWindow();
ipcMain.on('superPanel-hidden', () => {
win.hide();
});
ipcMain.on('superPanel-setSize', (e, height) => {
win.setSize(250, height);
});
ipcMain.on('superPanel-openPlugin', (e, args) => {
mainWindow.webContents.send('superPanel-openPlugin', args);
});
}
};
let createWindow = () => {
win = new BrowserWindow({
frame: false,
autoHideMenuBar: true,
width: 250,
height: 50,
show: false,
alwaysOnTop: true,
webPreferences: {
webSecurity: false,
enableRemoteModule: true,
backgroundThrottling: false,
nodeIntegration: true,
devTools: false,
},
});
win.loadURL(`file://${__static}/plugins/superPanel/index.html`);
win.once('ready-to-show', () => win.show());
win.on("closed", () => {
win = undefined;
});
// 打包后,失焦隐藏
win.on('blur', () => {
win.hide();
});
};
let getWindow = () => win;
return {
init: init,
getWindow: getWindow,
};
};

View File

@@ -4,6 +4,7 @@ import {
BrowserWindow,
clipboard,
Notification,
app,
} from 'electron';
import Api from './api';
import robot from 'robotjs';
@@ -11,7 +12,9 @@ import './config';
const browsers = require("../browsers")();
const mouseEvents = require("osx-mouse");
const {picker, separator} = browsers;
const {picker, separator, superPanel} = browsers;
// 需要在超级面板展示的插件
let optionPlugin = [];
let closePicker = (newColor) => {
if (picker.getWindow()) {
@@ -34,18 +37,88 @@ function registerShortCut(mainWindow) {
});
}
const getSelectedText = () => {
return new Promise((resolve) => {
const lastText = clipboard.readText('clipboard');
const platform = process.platform;
if (platform === 'darwin') {
robot.keyTap('c', 'command');
} else {
robot.keyTap('c', 'control');
}
setTimeout(() => {
const text = clipboard.readText('clipboard') || ''
const fileUrl = clipboard.read('public.file-url');
clipboard.writeText(lastText);
resolve({
text,
fileUrl
})
}, 100);
})
}
export default function init(mainWindow) {
ipcMain.on('optionPlugin', (e, args) => {
optionPlugin = args;
});
const mouseTrack = mouseEvents();
let down_time = 0;
let isPress = false;
mouseTrack.on('right-down', () => {
isPress = true;
down_time = Date.now();
const config = global.opConfig.get();
setTimeout(async () => {
if (isPress) {
const copyResult = await getSelectedText();
let win = superPanel.getWindow();
if (win) {
win.webContents.send('trigger-super-panel', {
...copyResult,
optionPlugin: optionPlugin.plugins,
});
} else {
superPanel.init(mainWindow);
win = superPanel.getWindow();
win.once('ready-to-show', () => {
win.webContents.send('trigger-super-panel', {
...copyResult,
optionPlugin: optionPlugin.plugins,
});
});
}
const pos = robot.getMousePos();
win.setPosition(parseInt(pos.x), parseInt(pos.y));
win.show();
}
}, config.superPanel.mouseDownTime);
})
mouseTrack.on('right-up', () => {
if ((Date.now() - down_time) > 1000) {
new Notification({ title: 'Rubick 通知', body: '长按了' }).show();
isPress = false;
});
// 注册快捷键
registerShortCut(mainWindow);
// 设置开机启动
const config = global.opConfig.get();
app.setLoginItemSettings({
openAtLogin: config.perf.common.start,
openAsHidden: true,
});
mainWindow.once("ready-to-show", () => {
// 非隐藏式启动需要显示主窗口
if (!app.getLoginItemSettings().wasOpenedAsHidden) {
mainWindow.show();
}
});
registerShortCut(mainWindow);
ipcMain.on('re-register', (event, arg) => {
registerShortCut(mainWindow);
@@ -55,10 +128,12 @@ export default function init(mainWindow) {
mainWindow.setSize(arg.width || 800, arg.height);
});
// 打包后,失焦隐藏
mainWindow.on('blur', () => {
mainWindow.hide();
app.isPackaged && mainWindow.hide();
});
// 响应 preload.js 事件
ipcMain.on('msg-trigger', async (event, arg) => {
const window = arg.winId ? BrowserWindow.fromId(arg.winId) : mainWindow
const operators = arg.type.split('.');
@@ -70,6 +145,7 @@ export default function init(mainWindow) {
event.sender.send(`msg-back-${arg.type}`, data);
});
// 窗口分离
ipcMain.on('new-window', (event, arg) => {
const opts = {
...arg,
@@ -78,6 +154,7 @@ export default function init(mainWindow) {
separator.init(opts);
});
// 拾色器
ipcMain.on('start-picker', () => {
const mouseTrack = mouseEvents();
picker.init();

View File

@@ -20,9 +20,15 @@ let defaultConfig = {
search: true,
}
},
superPanel: {
baiduAPI: {
key: '',
appid: '',
},
mouseDownTime: 500
}
}
}
global.opConfig = {
config: null,
get() {
@@ -38,7 +44,6 @@ global.opConfig = {
}
},
set(key, value) {
console.log(opConfig.config);
opConfig.config[key] = value;
fs.writeFileSync(configPath, JSON.stringify(opConfig.config));
}