| 30 | * 应用管理API - 主程序专用 |
| 31 | */ |
| 32 | export class AppsAPI { |
| 33 | private static readonly APP_CACHE_VERSION = 3 |
| 34 | private static readonly APP_CACHE_VERSION_KEY = 'cached-commands-version' |
| 35 | private mainWindow: Electron.BrowserWindow | null = null |
| 36 | private pluginManager: PluginManager | null = null |
| 37 | private launchParam: any = null |
| 38 | private lastMatchState: LastMatchState | null = null |
| 39 | private isLocalAppSearchEnabled = true |
| 40 | private cachedCommandsResult: { commands: any[]; regexCommands: any[]; plugins: any[] } | null = |
| 41 | null |
| 42 | /** 由外部注入,用于在多屏场景下正确显示窗口(跟随光标所在屏幕) */ |
| 43 | private showWindowCallback?: () => void |
| 44 | |
| 45 | public setShowWindowCallback(callback: () => void): void { |
| 46 | this.showWindowCallback = callback |
| 47 | } |
| 48 | |
| 49 | /** |
| 50 | * 安全地向渲染进程发送消息 |
| 51 | */ |
| 52 | private notifyRenderer(channel: string, ...args: any[]): void { |
| 53 | if (this.mainWindow && !this.mainWindow.isDestroyed()) { |
| 54 | this.mainWindow.webContents.send(channel, ...args) |
| 55 | } |
| 56 | } |
| 57 | |
| 58 | public init(mainWindow: Electron.BrowserWindow, pluginManager: PluginManager): void { |
| 59 | this.mainWindow = mainWindow |
| 60 | this.pluginManager = pluginManager |
| 61 | this.setupIPC() |
| 62 | this.loadLastMatchState() |
| 63 | // 异步加载本地应用搜索设置 |
| 64 | this.loadLocalAppSearchSetting() |
| 65 | } |
| 66 | |
| 67 | public getLaunchParam(): any { |
| 68 | return this.launchParam |
| 69 | } |
| 70 | |
| 71 | public invalidateCommandsCache(notifyRenderer = false): void { |
| 72 | this.cachedCommandsResult = null |
| 73 | console.log('[Commands] 指令缓存已清空:', { notifyRenderer }) |
| 74 | |
| 75 | if (notifyRenderer) { |
| 76 | console.log('[Commands] 发送 apps-changed 通知,触发主窗口重载指令与 alias 搜索索引') |
| 77 | this.notifyRenderer('apps-changed') |
| 78 | } |
| 79 | } |
| 80 | |
| 81 | /** |
| 82 | * 根据名称查找直接启动指令(系统应用、系统设置等) |
| 83 | */ |
| 84 | public async findDirectCommandByName(name: string): Promise<any | null> { |
| 85 | const { commands } = await this.getCommands() |
| 86 | return commands.find((cmd: any) => cmd.type === 'direct' && cmd.name === name) || null |
| 87 | } |
| 88 | |
| 89 | private setupIPC(): void { |
nothing calls this directly
no outgoing calls
no test coverage detected