From 542a167066c79fd8a98c6486571c61461aacd65a Mon Sep 17 00:00:00 2001 From: Allen Date: Sat, 17 May 2025 21:34:19 +0800 Subject: [PATCH] =?UTF-8?q?=E5=9C=A8=E4=B8=BB=E8=BF=9B=E7=A8=8B=E5=90=AF?= =?UTF-8?q?=E5=8A=A8=E6=97=B6=EF=BC=8C=E6=8F=90=E5=89=8D=E9=A2=84=E5=8A=A0?= =?UTF-8?q?=E8=BD=BD=E8=8F=9C=E5=8D=95=E6=95=B0=E6=8D=AE=E5=92=8C=E7=BC=A9?= =?UTF-8?q?=E6=94=BE=E9=85=8D=E7=BD=AE=E6=95=B0=E6=8D=AE=EF=BC=8C=E4=BB=8E?= =?UTF-8?q?=E8=80=8C=E5=87=8F=E5=B0=91=E4=B8=BB=E7=95=8C=E9=9D=A2=E7=9A=84?= =?UTF-8?q?=E5=8A=A0=E8=BD=BD=E8=80=97=E6=97=B6=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/IpcMainHandler.ts | 46 +++++++++++++++++++++++++++++++++++++++++ src/index.ts | 4 ++-- src/main.ts | 3 ++- src/preload.ts | 13 ++++++++++++ src/types/electron.d.ts | 12 +++++++++++ 5 files changed, 75 insertions(+), 3 deletions(-) diff --git a/src/IpcMainHandler.ts b/src/IpcMainHandler.ts index 28bcea8..832d092 100644 --- a/src/IpcMainHandler.ts +++ b/src/IpcMainHandler.ts @@ -2,6 +2,7 @@ import { ipcMain, screen, session } from "electron"; import http from 'http'; import https from 'https'; import { URL } from 'url'; +import { ApiResponse, MenuItem, TagResolutionConfig, EIACDesktopApi } from './EIAC_Desktop_Api'; export function initialize(): void { // Get primary display(需要注意:size 是逻辑像素尺寸(logical screen size),已被 DPI 缩放影响,需要除以 DPI 缩放比例才是物理像素尺寸(physical screen size)) @@ -97,5 +98,50 @@ export function initialize(): void { return false; } }); + + // Get menu cache + ipcMain.handle('get-menu-cache', async () => menuData ?? await menuDataReadyPromise); + + // Get config cache + ipcMain.handle('get-config-cache', async () => configData ?? await configDataReadyPromise); } +let menuData: ApiResponse = null; +let menuDataReadyPromise: Promise>; +let configData: ApiResponse = null; +let configDataReadyPromise: Promise>; + +function getMenuAsync(): void { + if (!menuDataReadyPromise) { + menuDataReadyPromise = new Promise((resolve, reject) => { + EIACDesktopApi.Menu.GetMenuAsync() + .then(data => { + menuData = data; + resolve(data); + }) + .catch(err => { + reject(err); + }); + }); + } +} + +function getConfigAsync(): void { + if (!configDataReadyPromise) { + configDataReadyPromise = new Promise((resolve, reject) => { + EIACDesktopApi.Menu.GetConfigAsync() + .then(data => { + configData = data; + resolve(data); + }) + .catch(err => { + reject(err); + }); + }); + } +} + +export function preloadData(): void { + getMenuAsync(); + getConfigAsync(); +} diff --git a/src/index.ts b/src/index.ts index 8c8bb07..30b1b77 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,6 @@ import { PageTitleUpdatedEvent } from 'electron'; import { TabGroup, Tab } from 'electron-tabs'; -import { MenuItem, ApiResponse, EIACDesktopApi } from './EIAC_Desktop_Api'; +import { MenuItem, ApiResponse } from './EIAC_Desktop_Api'; const tabGroup: TabGroup = document.querySelector('tab-group') as TabGroup; @@ -27,7 +27,7 @@ function checkLoginStatus(): void { */ async function getMenuListAsync(): Promise { try { - const response: ApiResponse = await EIACDesktopApi.Menu.GetMenuAsync(); + const response: ApiResponse = await window.electronAPI.getMenuCacheAsync(); if (response.status === 0) { return response.data; } else { diff --git a/src/main.ts b/src/main.ts index 30caada..4f69bc4 100644 --- a/src/main.ts +++ b/src/main.ts @@ -1,7 +1,7 @@ import { app, BrowserWindow, globalShortcut, Menu, Tray } from 'electron'; import path from 'node:path'; import started from 'electron-squirrel-startup'; -import { initialize } from './IpcMainHandler'; +import { initialize, preloadData } from './IpcMainHandler'; const isDevelopment: boolean = process.env.NODE_ENV === 'development'; @@ -151,6 +151,7 @@ app.on('window-all-closed', () => { // initialization and is ready to create browser windows. // Some APIs can only be used after this event occurs. app.whenReady().then(() => { + preloadData(); createWindow(); // 在 macOS 平台上,点击 dock 图标时重新创建窗口 diff --git a/src/preload.ts b/src/preload.ts index 34b8b5a..6be66da 100644 --- a/src/preload.ts +++ b/src/preload.ts @@ -2,8 +2,21 @@ // https://www.electronjs.org/docs/latest/tutorial/process-model#preload-scripts import { contextBridge, ipcRenderer } from 'electron'; +import { ApiResponse, MenuItem, TagResolutionConfig } from './EIAC_Desktop_Api'; contextBridge.exposeInMainWorld('electronAPI', { + /** + * 获取菜单缓存 + * @returns 菜单缓存 + */ + getMenuCacheAsync: () => ipcRenderer.invoke('get-menu-cache') as Promise>, + + /** + * 获取配置缓存 + * @returns 配置缓存 + */ + getConfigCacheAsync: () => ipcRenderer.invoke('get-config-cache') as Promise>, + /** * 在新标签页打开URL * @param callback 回调函数,参数为webContentId和url。其中webContentId是请求打开URL的webview的id。 diff --git a/src/types/electron.d.ts b/src/types/electron.d.ts index 9206fae..685b48c 100644 --- a/src/types/electron.d.ts +++ b/src/types/electron.d.ts @@ -1,4 +1,16 @@ +import { ApiResponse, MenuItem, TagResolutionConfig } from '../EIAC_Desktop_Api'; + export interface ElectronAPI { + /** + * 获取菜单缓存 + * @returns 菜单缓存 + */ + getMenuCacheAsync: () => Promise>; + /** + * 获取配置缓存 + * @returns 配置缓存 + */ + getConfigCacheAsync: () => Promise>; /** * 在新标签页打开URL * @param callback 回调函数,参数为webContentId和url。其中webContentId是请求打开URL的webview的id。