diff --git a/src/main/index.js b/src/main/index.js index 6064613..653c83c 100644 --- a/src/main/index.js +++ b/src/main/index.js @@ -205,14 +205,8 @@ app.on('ready', () => { ipcMain.on('openWindow', (e, data) => { createLinkWin(data) }) - // 新窗口创建-监听 - ipcMain.on('new-window', (e, data) => { - const { id, type } = data - const win = BrowserWindow.fromId(id) - win.type = type // 绑定独立标识 - remote.enable(win.webContents) // 开启远程服务 - }) - + // zdg: 消息监听 + handleAll() // 打开-登录窗口 createLoginWindow() @@ -229,3 +223,24 @@ app.on('window-all-closed', () => { app.quit() } }) + +// 监听全局事件 +function handleAll() { + // 新窗口创建-监听 + ipcMain.on('new-window', (e, data) => { + const { id, type } = data + const win = BrowserWindow.fromId(id) + win.type = type // 绑定独立标识 + remote.enable(win.webContents) // 开启远程服务 + }) + // 用于监听-状态管理变化-同步所有窗口 + ipcMain.handle('pinia-state-change', (e, storeName, jsonStr) => { + for(const curWin of BrowserWindow.getAllWindows()){ + const id = curWin.webContents.id + const bool = id !== e.sender.id && !curWin.isDestroyed() + if (bool) { // 除了消息发送窗口和销毁的窗口 其他都发送 + curWin.webContents.send('pinia-state-set', storeName, jsonStr) + } + } + }) +} \ No newline at end of file diff --git a/src/renderer/src/plugins/shareStore.js b/src/renderer/src/plugins/shareStore.js new file mode 100644 index 0000000..746332e --- /dev/null +++ b/src/renderer/src/plugins/shareStore.js @@ -0,0 +1,41 @@ +/** + * 共享数据状态-多窗口 + */ +const { ipcRenderer } = require('electron') // app使用 +export function shareStorePlugin({store}) { + store.$subscribe(() => { // 自动同步 + // 在存储变化的时候执行 + const storeName = store.$id + // 用于多窗口共享(需要共享的状态名称) + const names = ['tool'] + if (names.includes(storeName)) stateSync(store) // 需要同步 + }) + // 暴露方法-手动同步 + store.stateSync = () => stateSync(store) + // 监听主线程消息-同步数据 + stateChange(store) +} +// 同步数据-发送给主线程 +function stateSync(store) { + const storeName = store.$id + const jsonStr = JSON.stringify(store.$state) + // 通知主线程更新 + ipcRenderer.invoke('pinia-state-change', storeName, jsonStr) +} +// 同步数据-接收主线程消息 +function stateChange(store) { + const storeName = store.$id + ipcRenderer.on('pinia-state-set', (e, sName, jsonStr) => { + if (sName == storeName) { // 更新对应数据 + console.log('state-set', jsonStr, sName) + const curJson = JSON.stringify(store.$state) // 当前数据 + const isUp = curJson != jsonStr // 不同的时候才写入,不然会导致触发数据变化监听,导致死循环 + if (!isUp) return + const stateJson = JSON.parse(jsonStr) // 新数据 + // 更新状态 + store.$patch(stateJson) + // 您可以通过将其 $state 属性设置为新对象来替换 Store 的整个状态 + // store.$state = stateJson + } + }) +} \ No newline at end of file diff --git a/src/renderer/src/store/index.js b/src/renderer/src/store/index.js index 3871c81..3dfcd54 100644 --- a/src/renderer/src/store/index.js +++ b/src/renderer/src/store/index.js @@ -1,7 +1,9 @@ import { createPinia } from 'pinia' import piniaPluginPersistedstate from 'pinia-plugin-persistedstate' +import { shareStorePlugin } from '@/plugins/shareStore' const pinia = createPinia() //创建pinia实例 pinia.use(piniaPluginPersistedstate) //将插件添加到 pinia 实例上 +pinia.use(shareStorePlugin) // 多窗口数据状态共享 export const store = pinia \ No newline at end of file diff --git a/src/renderer/src/store/modules/tool.js b/src/renderer/src/store/modules/tool.js new file mode 100644 index 0000000..224b5d7 --- /dev/null +++ b/src/renderer/src/store/modules/tool.js @@ -0,0 +1,13 @@ +/** + * 工具类-窗口-状态管理 + */ +import { defineStore } from 'pinia' + +export const useToolState = defineStore('tool', { + state: () => ({ + model: 'select', // 悬浮球-当前模式 + showBoardAll: false, // 全屏画板-是否显示 + }), + actions: { + } +}) \ No newline at end of file diff --git a/src/renderer/src/views/resource/index.vue b/src/renderer/src/views/resource/index.vue index 4f66881..6438cbb 100644 --- a/src/renderer/src/views/resource/index.vue +++ b/src/renderer/src/views/resource/index.vue @@ -23,7 +23,6 @@ -