diff --git a/package.json b/package.json index 4daf13bd..2599eb41 100644 --- a/package.json +++ b/package.json @@ -70,7 +70,6 @@ "md5": "^2.3.0", "mime-types": "^2.1.31", "node-fetch": "^2.6.1", - "osx-mouse": "^2.0.0", "puppeteer-core": "^10.0.0", "puppeteer-in-electron": "^3.0.3", "query-string": "^7.0.0", diff --git a/src/main/browsers/capture.js b/src/main/browsers/capture.js index 53885b81..9aec8474 100644 --- a/src/main/browsers/capture.js +++ b/src/main/browsers/capture.js @@ -30,15 +30,15 @@ module.exports = () => { enableLargerThanScreen: true, hasShadow: false, show: false, + title: 'capture', webPreferences: { enableRemoteModule: true, nodeIntegration: true, webSecurity: false, - // devTools: false, + devTools: false, } }) captureWin.setAlwaysOnTop(true, 'screen-saver') - captureWin.setVisibleOnAllWorkspaces(true) captureWin.setFullScreenable(false) captureWin.loadFile(`${__static}/plugins/capture/index.html`); @@ -50,6 +50,10 @@ module.exports = () => { captureWin.blur() } + captureWin.on("closed", () => { + captureWin = undefined; + }); + captureWin.once('ready-to-show', () => captureWin.show()); return captureWin }); @@ -57,46 +61,43 @@ module.exports = () => { let getWindow = () => captureWins; - let useCapture = () => { - globalShortcut.register('Esc', () => { + const close = () => { + const wins = BrowserWindow.getAllWindows(); + wins.forEach((win) => { + if (win.title === 'capture') win.close(); + captureWins = []; + }) + } + + ipcMain.on('capture-screen', (e, { type = 'start', screenId, winId, x, y } = {}) => { + if (type === 'start') { + init() + } else if (type === 'complete') { if (captureWins) { captureWins.forEach(win => win.close()) captureWins = [] } - }); - - globalShortcut.register('CmdOrCtrl+Shift+S', init) - - ipcMain.on('capture-screen', (e, { type = 'start', screenId, winId, x, y } = {}) => { - if (type === 'start') { - init() - } else if (type === 'complete') { - if (captureWins) { - captureWins.forEach(win => win.close()) - captureWins = [] - } - // nothing - } else if (type === 'select') { - captureWins.forEach(win => win.webContents.send('capture-screen', { type: 'select', screenId })) - } else if (type === 'getAllDisplays') { - const { screen } = require('electron'); - let displays = screen.getAllDisplays(); - const currentScreen = displays.filter(d => d.bounds.x === x && d.bounds.y === y)[0]; - e.sender.send('getAllDisplays', { - screen: { - scaleFactor: currentScreen.scaleFactor, - id: currentScreen.id, - bounds: currentScreen.bounds, - }, - winId, - }); - } - }); - } + // nothing + } else if (type === 'select') { + captureWins.forEach(win => win.webContents.send('capture-screen', { type: 'select', screenId })) + } else if (type === 'getAllDisplays') { + const { screen } = require('electron'); + let displays = screen.getAllDisplays(); + const currentScreen = displays.filter(d => d.bounds.x === x && d.bounds.y === y)[0]; + e.sender.send('getAllDisplays', { + screen: { + scaleFactor: currentScreen.scaleFactor, + id: currentScreen.id, + bounds: currentScreen.bounds, + }, + winId, + }); + } + }); return { init: init, getWindow: getWindow, - useCapture, + close, }; }; diff --git a/src/main/browsers/picker.js b/src/main/browsers/picker.js index 7e8b079a..fa8a32fb 100644 --- a/src/main/browsers/picker.js +++ b/src/main/browsers/picker.js @@ -3,7 +3,7 @@ const { BrowserWindow, nativeImage } = require("electron"); module.exports = () => { let win; - let init = () => { + let init = (x, y) => { if (win === null || win === undefined) { createWindow(); } diff --git a/src/main/common/common.js b/src/main/common/common.js index 0e7d332a..702a2094 100644 --- a/src/main/common/common.js +++ b/src/main/common/common.js @@ -9,17 +9,16 @@ import { import Api from './api'; import robot from 'robotjs'; import './config'; +import ioHook from 'iohook'; const browsers = require("../browsers")(); -const mouseEvents = require("osx-mouse"); -const {picker, separator, superPanel} = browsers; +const {picker, separator, superPanel, capture} = browsers; // 需要在超级面板展示的插件 let optionPlugin = []; -let closePicker = (newColor) => { +let closePicker = () => { if (picker.getWindow()) { ipcMain.removeListener("closePicker", closePicker); - ipcMain.removeListener("pickerRequested", (event) => {}); picker.getWindow().close(); } }; @@ -27,7 +26,7 @@ let closePicker = (newColor) => { function registerShortCut(mainWindow) { const config = global.opConfig.get(); globalShortcut.unregisterAll(); - + // 注册偏好快捷键 globalShortcut.register(config.perf.shortCut.showAndHidden, () => { mainWindow.show(); }); @@ -35,6 +34,18 @@ function registerShortCut(mainWindow) { globalShortcut.register(config.perf.shortCut.separate, () => { mainWindow.webContents.send('new-window'); }); + + // 注册自定义全局快捷键 + config.global.forEach(sc => { + if (!sc.key || !sc.value) return; + globalShortcut.register(sc.key, () => { + mainWindow.webContents.send('global-short-key', sc.value); + }); + }); + + globalShortcut.register('Esc', () => { + capture.close(); + }); } const getSelectedText = () => { @@ -143,23 +154,35 @@ export default function init(mainWindow) { // 拾色器 ipcMain.on('start-picker', () => { - const mouseTrack = mouseEvents(); + // 开启输入侦测 + ioHook.start(false) picker.init(); - picker.getWindow().on("close", () => { - mouseTrack.destroy(); + + picker.getWindow().on('close', () => { + ioHook.stop(); }); - mouseTrack.on('move', (x, y) => { + + + ioHook.on('mousemove', e => { + let x = e.x + let y = e.y if (!picker.getWindow()) return; let color = "#" + robot.getPixelColor(parseInt(x), parseInt(y)); picker.getWindow().setPosition(parseInt(x) - 50, parseInt(y) - 50); picker.getWindow().webContents.send("updatePicker", color); }) - mouseTrack.on("left-up", (x, y) => { - const color = "#" + robot.getPixelColor(parseInt(x), parseInt(y)); - clipboard.writeText("#" + robot.getPixelColor(parseInt(x), parseInt(y))); - new Notification({ title: 'Rubick 通知', body: `${color} 已保存到剪切板` }).show(); - closePicker(); + + ioHook.on('mouseup', e => { + if (e.button === 1) { + let x = e.x + let y = e.y + const color = "#" + robot.getPixelColor(parseInt(x), parseInt(y)); + clipboard.writeText("#" + robot.getPixelColor(parseInt(x), parseInt(y))); + new Notification({ title: 'Rubick 通知', body: `${color} 已保存到剪切板` }).show(); + closePicker(); + } }); + let pos = robot.getMousePos(); picker .getWindow() @@ -173,7 +196,11 @@ export default function init(mainWindow) { ); ipcMain.on("closePicker", closePicker); - mouseTrack.on("right-up", closePicker); + ioHook.on('mouseup', e => { + if (e.button === 3) { + closePicker() + } + }); }) } diff --git a/src/main/index.js b/src/main/index.js index 12d77996..93ee629c 100644 --- a/src/main/index.js +++ b/src/main/index.js @@ -3,7 +3,7 @@ import '../renderer/store' import init from './common/common'; import {autoUpdate} from './common/autoUpdate'; import createTray from './tray'; -const {capture, main} = require("./browsers")(); +const {main} = require("./browsers")(); /** * Set `__static` path to static files in production * https://simulatedgreg.gitbooks.io/electron-vue/content/en/using-static-assets.html @@ -16,7 +16,6 @@ app.allowRendererProcessReuse = false; app.dock.hide(); function createWindow() { - capture.useCapture(); main.init(); init(main.getWindow()); } diff --git a/src/renderer/App.vue b/src/renderer/App.vue index 2bbdbd36..eb544386 100644 --- a/src/renderer/App.vue +++ b/src/renderer/App.vue @@ -27,7 +27,9 @@