From 7dfa51f5dc0724d94039346db9950571d1b2890c Mon Sep 17 00:00:00 2001 From: zdg Date: Wed, 7 Aug 2024 10:34:34 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BE=A7=E8=BE=B9=E5=B7=A5=E5=85=B7=E6=A0=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/renderer/src/plugins/shareStore.js | 9 +- src/renderer/src/utils/tool.js | 24 +-- .../src/views/tool/components/side.vue | 137 ++++++++++++++++++ .../src/views/tool/directive/ignore.js | 39 +++++ src/renderer/src/views/tool/sphere.vue | 33 ++--- 5 files changed, 211 insertions(+), 31 deletions(-) create mode 100644 src/renderer/src/views/tool/components/side.vue create mode 100644 src/renderer/src/views/tool/directive/ignore.js diff --git a/src/renderer/src/plugins/shareStore.js b/src/renderer/src/plugins/shareStore.js index 746332e..dab8505 100644 --- a/src/renderer/src/plugins/shareStore.js +++ b/src/renderer/src/plugins/shareStore.js @@ -1,7 +1,8 @@ /** * 共享数据状态-多窗口 */ -const { ipcRenderer } = require('electron') // app使用 +// const { ipcRenderer } = require('electron') // app使用 +const { ipcRenderer } = window.electron || {} export function shareStorePlugin({store}) { store.$subscribe(() => { // 自动同步 // 在存储变化的时候执行 @@ -20,14 +21,14 @@ function stateSync(store) { const storeName = store.$id const jsonStr = JSON.stringify(store.$state) // 通知主线程更新 - ipcRenderer.invoke('pinia-state-change', storeName, jsonStr) + ipcRenderer?.invoke('pinia-state-change', storeName, jsonStr) } // 同步数据-接收主线程消息 function stateChange(store) { const storeName = store.$id - ipcRenderer.on('pinia-state-set', (e, sName, jsonStr) => { + ipcRenderer?.on('pinia-state-set', (e, sName, jsonStr) => { if (sName == storeName) { // 更新对应数据 - console.log('state-set', jsonStr, sName) + // console.log('state-set', jsonStr, sName) const curJson = JSON.stringify(store.$state) // 当前数据 const isUp = curJson != jsonStr // 不同的时候才写入,不然会导致触发数据变化监听,导致死循环 if (!isUp) return diff --git a/src/renderer/src/utils/tool.js b/src/renderer/src/utils/tool.js index b51a6bb..62102e5 100644 --- a/src/renderer/src/utils/tool.js +++ b/src/renderer/src/utils/tool.js @@ -39,15 +39,19 @@ export const getStaticUrl = (url = '', type = 'app', exitPath = '', isFile = fal * url:路由地址,width:窗口宽度,height:窗口高度,option:自定义选项 * @returns */ -export function ipcMsgSend(key, data) { - return new Promise((resolve) => { - // 返回结果-监听 - ipcRenderer.once(`${key}-reply`, (e, res) => { - resolve(res) +export function ipcMsgSend(key, data, type) { + if (type == 'old') { + return new Promise((resolve) => { + // 返回结果-监听 + ipcRenderer.once(`${key}-reply`, (e, res) => { + resolve(res) + }) + // 发送消息 + ipcRenderer.send(key, data) }) - // 发送消息 - ipcRenderer.send(key, data) - }) + } else { // 新版本-默认 + return ipcRenderer.invoke(key, data) + } } /** * 创建-窗口 调用该方法 @@ -173,13 +177,13 @@ const eventHandles = (type, win) => { case 'tool-sphere': { // 创建-悬浮球 // 监听设置穿透 const setIgnore = (_, ignore) => {win.setIgnoreMouseEvents(ignore, {forward: true})} - Remote.ipcMain.on('tool-sphere:set:ignore', setIgnore) + Remote.ipcMain.handle('tool-sphere:set:ignore', setIgnore) // 异步 // 关闭窗口 Remote.ipcMain.once('tool-sphere:close', () => { win&&win.destroy() }) // 放大监听-测试 Remote.ipcMain.once('maximize-window', () => {win&&win.destroy()}) const on = { - onClosed: () => {Remote.ipcMain.off('tool-sphere:set:ignore', setIgnore)} + onClosed: () => {Remote.ipcMain.removeHandler('tool-sphere:set:ignore', setIgnore)} } publicMethods(on) // 加载公共方法 break} diff --git a/src/renderer/src/views/tool/components/side.vue b/src/renderer/src/views/tool/components/side.vue new file mode 100644 index 0000000..63b766e --- /dev/null +++ b/src/renderer/src/views/tool/components/side.vue @@ -0,0 +1,137 @@ + + + \ No newline at end of file diff --git a/src/renderer/src/views/tool/directive/ignore.js b/src/renderer/src/views/tool/directive/ignore.js new file mode 100644 index 0000000..449a658 --- /dev/null +++ b/src/renderer/src/views/tool/directive/ignore.js @@ -0,0 +1,39 @@ +/** + * 处理元素是否穿透-窗口 + */ +import { ipcMsgSend } from '@/utils/tool' +class Ignore { + el // 绑定元素 + binding // 绑定对象(参数) + value // 绑定值 + isAuto // 是否自动控制 + constructor(el, binding) { + this.el = el + this.binding = binding + this.value = binding.value + this.isAuto = !(this.value && this.value instanceof Boolean) + } + // 设置是否穿透 + send(bool) { + ipcMsgSend('tool-sphere:set:ignore', bool) + } + // 监听元素移入移出,自动设置 + mounted() { + this.el.addEventListener('mouseenter', () => { // 进入 + this.send(false) // 元素不穿透,鼠标有效 + }) + this.el.addEventListener('mouseleave', () => { // 离开 + this.send(true) // 元素穿透,鼠标无效 + }) + } +} +export default { + mounted(el, binding) { + const ignore = new Ignore(el, binding) + if (ignore.isAuto) { // 自动控制 + ignore.mounted() + } else { // 手动设置 + ignore.send(ignore.value) + } + } +} \ No newline at end of file diff --git a/src/renderer/src/views/tool/sphere.vue b/src/renderer/src/views/tool/sphere.vue index 546067c..c6b9b9f 100644 --- a/src/renderer/src/views/tool/sphere.vue +++ b/src/renderer/src/views/tool/sphere.vue @@ -1,9 +1,11 @@