/** * 文件浏览器窗口分离 — 弹出/分离/附加 FileExplorer 独立窗口。 * * 对标 useAiWindow 的 detach/reattach 模式,复用 WebviewWindow API。 * 分离窗口以独立 Tauri 窗口打开 FileExplorerDetached 视图, * 通过 URL query `?projectId=xxx` 传参。 */ const DETACHED_LABEL_PREFIX = 'fe-detached-' function detachedLabel(projectId: string): string { return DETACHED_LABEL_PREFIX + projectId } /** * 将文件浏览器弹出到独立窗口。 * 若该项目的窗口已存在则聚焦,不重复创建。 * * @param projectId 项目 id(传给分离窗口加载工程列表) * @param title 窗口标题(可选,默认"文件浏览器") */ export async function detachFileExplorer( projectId: string, title?: string, ): Promise { if (!projectId) return const { WebviewWindow } = await import('@tauri-apps/api/webviewWindow') const label = detachedLabel(projectId) // 已存在则聚焦 const existing = await WebviewWindow.getByLabel(label) if (existing) { await existing.setFocus() return } const url = window.location.origin + window.location.pathname + `#/file-explorer-detached?projectId=${encodeURIComponent(projectId)}` const win = new WebviewWindow(label, { url, title: title || '文件浏览器', width: 900, height: 600, minWidth: 640, minHeight: 400, center: true, decorations: true, }) win.once('tauri://created', () => { console.log('[FileExplorer] 分离窗口已创建:', label) }) win.once('tauri://error', (e) => { console.error('[FileExplorer] 窗口创建失败:', e) }) } /** * 关闭指定项目的文件浏览器分离窗口。 */ export async function reattachFileExplorer(projectId: string): Promise { if (!projectId) return const { WebviewWindow } = await import('@tauri-apps/api/webviewWindow') const label = detachedLabel(projectId) const existing = await WebviewWindow.getByLabel(label) if (existing) { try { await existing.close() } catch { // 忽略关闭失败 } } }