From fc7cd165a821ac494050117a9efa91cac1f8c700 Mon Sep 17 00:00:00 2001 From: ayangweb <75017711+ayangweb@users.noreply.github.com> Date: Thu, 23 Oct 2025 16:48:13 +0800 Subject: [PATCH] refactor: replace `getCurrentWindow` with `getCurrentWebviewWindow` (#945) * refactor: replace `getCurrentWindow` with `getCurrentWebviewWindow` * refactor: update * refactor: update --- src/hooks/useDeepLinkManager.ts | 4 +-- src/hooks/useTauriFocus.ts | 2 +- src/hooks/useWindows.ts | 55 +++++++++++++++++------------ src/pages/main/index.tsx | 1 + src/types/platform.ts | 5 --- src/utils/tauriAdapter.ts | 37 ++++++++++--------- src/utils/webAdapter.ts | 13 ++++--- src/utils/wrappers/tauriWrappers.ts | 9 ++--- 8 files changed, 66 insertions(+), 60 deletions(-) diff --git a/src/hooks/useDeepLinkManager.ts b/src/hooks/useDeepLinkManager.ts index e88e89c3..ed12cf45 100644 --- a/src/hooks/useDeepLinkManager.ts +++ b/src/hooks/useDeepLinkManager.ts @@ -4,7 +4,7 @@ import { getCurrent as getCurrentDeepLinkUrls, onOpenUrl, } from "@tauri-apps/plugin-deep-link"; -import { getCurrentWindow } from "@tauri-apps/api/window"; +import { getCurrentWebviewWindow } from "@tauri-apps/api/webviewWindow"; import { useAppStore } from "@/stores/appStore"; import { useConnectStore } from "@/stores/connectStore"; @@ -52,7 +52,7 @@ export function useDeepLinkManager() { // trigger oauth success event platformAdapter.emitEvent("oauth_success", { serverId }); - getCurrentWindow().setFocus(); + getCurrentWebviewWindow().setFocus(); } catch (err) { console.error("Failed to parse OAuth callback URL:", err); addError("Invalid OAuth callback URL format: " + err); diff --git a/src/hooks/useTauriFocus.ts b/src/hooks/useTauriFocus.ts index fe62de2e..b3120580 100644 --- a/src/hooks/useTauriFocus.ts +++ b/src/hooks/useTauriFocus.ts @@ -19,7 +19,7 @@ export const useTauriFocus = (props: Props) => { useMount(async () => { if (!isTauri) return; - const appWindow = await platformAdapter.getWebviewWindow(); + const appWindow = await platformAdapter.getCurrentWebviewWindow(); const wait = isMac ? 0 : 100; diff --git a/src/hooks/useWindows.ts b/src/hooks/useWindows.ts index 167d1243..f975aa50 100644 --- a/src/hooks/useWindows.ts +++ b/src/hooks/useWindows.ts @@ -26,7 +26,7 @@ export const useWindows = () => { useEffect(() => { const fetchWindow = async () => { try { - const window = await platformAdapter.getCurrentWindow(); + const window = await platformAdapter.getCurrentWebviewWindow(); setAppWindow(window); } catch (error) { console.error("Failed to get current window:", error); @@ -51,7 +51,7 @@ export const useWindows = () => { const win = await platformAdapter.createWebviewWindow(args.label, args); - if(win) { + if (win) { win.once("tauri://created", async () => { console.log("tauri://created"); // if (args.label.includes("main")) { @@ -68,7 +68,6 @@ export const useWindows = () => { console.error("error:", error); }); } - }, []); const closeWin = useCallback(async (label: string) => { @@ -96,32 +95,44 @@ export const useWindows = () => { }, []); const listenEvents = useCallback(() => { - let unlistenHandlers: { (): void; (): void; (): void; (): void; }[] = []; + let unlistenHandlers: { (): void; (): void; (): void; (): void }[] = []; const setupListeners = async () => { - const winCreateHandler = await platformAdapter.listenWindowEvent("win-create", (event) => { - console.log(event); - createWin(event.payload); - }); + const winCreateHandler = await platformAdapter.listenWindowEvent( + "win-create", + (event) => { + console.log(event); + createWin(event.payload); + } + ); unlistenHandlers.push(winCreateHandler); - const winShowHandler = await platformAdapter.listenWindowEvent("win-show", async () => { - if (!appWindow || !appWindow.label.includes("main")) return; - await appWindow.show(); - await appWindow.unminimize(); - await appWindow.setFocus(); - }); + const winShowHandler = await platformAdapter.listenWindowEvent( + "win-show", + async () => { + if (!appWindow || !appWindow.label.includes("main")) return; + await appWindow.show(); + await appWindow.unminimize(); + await appWindow.setFocus(); + } + ); unlistenHandlers.push(winShowHandler); - const winHideHandler = await platformAdapter.listenWindowEvent("win-hide", async () => { - if (!appWindow || !appWindow.label.includes("main")) return; - await appWindow.hide(); - }); + const winHideHandler = await platformAdapter.listenWindowEvent( + "win-hide", + async () => { + if (!appWindow || !appWindow.label.includes("main")) return; + await appWindow.hide(); + } + ); unlistenHandlers.push(winHideHandler); - const winCloseHandler = await platformAdapter.listenWindowEvent("win-close", async () => { - await appWindow.close(); - }); + const winCloseHandler = await platformAdapter.listenWindowEvent( + "win-close", + async () => { + await appWindow.close(); + } + ); unlistenHandlers.push(winCloseHandler); }; @@ -144,4 +155,4 @@ export const useWindows = () => { getWin, getAllWin, }; -}; \ No newline at end of file +}; diff --git a/src/pages/main/index.tsx b/src/pages/main/index.tsx index 808b9077..ac49d6c2 100644 --- a/src/pages/main/index.tsx +++ b/src/pages/main/index.tsx @@ -26,6 +26,7 @@ function MainApp() { setViewExtensionOpened(payload); }); }, []); + const { synthesizeItem } = useChatStore(); useSyncStore(); diff --git a/src/types/platform.ts b/src/types/platform.ts index 3d924431..8b1c9deb 100644 --- a/src/types/platform.ts +++ b/src/types/platform.ts @@ -60,7 +60,6 @@ export interface WindowOperations { showWindow: () => Promise; setAlwaysOnTop: (isPinned: boolean) => Promise; setShadow(enable: boolean): Promise; - getWebviewWindow: () => Promise; getWindowByLabel: (label: string) => Promise<{ show: () => Promise; setFocus: () => Promise; @@ -68,8 +67,6 @@ export interface WindowOperations { close: () => Promise; } | null>; createWindow: (label: string, options: any) => Promise; - getAllWindows: () => Promise; - getCurrentWindow: () => Promise; createWebviewWindow: (label: string, options: any) => Promise; listenWindowEvent: ( event: string, @@ -88,8 +85,6 @@ export interface ThemeAndEvents { event: K, callback: (event: { payload: EventPayloads[K] }) => void ) => Promise<() => void>; - setWindowTheme: (theme: string | null) => Promise; - getWindowTheme: () => Promise; onThemeChanged: ( callback: (payload: { payload: string }) => void ) => Promise; diff --git a/src/utils/tauriAdapter.ts b/src/utils/tauriAdapter.ts index f9c2656d..0ccbd824 100644 --- a/src/utils/tauriAdapter.ts +++ b/src/utils/tauriAdapter.ts @@ -16,6 +16,8 @@ import { useAppearanceStore } from "@/stores/appearanceStore"; import { copyToClipboard, dispatchEvent, OpenURLWithBrowser } from "."; import { useAppStore } from "@/stores/appStore"; import { unrequitable } from "@/utils"; +import { WebviewWindow } from "@tauri-apps/api/webviewWindow"; +import { Theme } from "@tauri-apps/api/window"; export interface TauriPlatformAdapter extends BasePlatformAdapter { openFileDialog: ( @@ -24,6 +26,10 @@ export interface TauriPlatformAdapter extends BasePlatformAdapter { metadata: typeof metadata; error: typeof error; openLogDir: () => Promise; + getCurrentWebviewWindow: () => Promise; + getWindowTheme: () => Promise; + setWindowTheme: (theme: Theme | null) => Promise; + getAllWindows: () => Promise; } // Create Tauri adapter functions @@ -34,12 +40,12 @@ export const createTauriAdapter = (): TauriPlatformAdapter => { }, async hideWindow() { - const window = await windowWrapper.getWebviewWindow(); + const window = await windowWrapper.getCurrentWebviewWindow(); return window?.hide(); }, async showWindow() { - const window = await windowWrapper.getWebviewWindow(); + const window = await windowWrapper.getCurrentWebviewWindow(); window?.show(); window?.unminimize(); return window?.setFocus(); @@ -77,8 +83,10 @@ export const createTauriAdapter = (): TauriPlatformAdapter => { }, async setAlwaysOnTop(isPinned) { - const { getCurrentWindow } = await import("@tauri-apps/api/window"); - const window = getCurrentWindow(); + const { getCurrentWebviewWindow } = await import( + "@tauri-apps/api/webviewWindow" + ); + const window = getCurrentWebviewWindow(); return window.setAlwaysOnTop(isPinned); }, @@ -164,7 +172,7 @@ export const createTauriAdapter = (): TauriPlatformAdapter => { }); }, - async getWebviewWindow() { + async getCurrentWebviewWindow() { const { getCurrentWebviewWindow } = await import( "@tauri-apps/api/webviewWindow" ); @@ -172,14 +180,14 @@ export const createTauriAdapter = (): TauriPlatformAdapter => { }, async setWindowTheme(theme) { - const window = await this.getWebviewWindow(); + const window = await this.getCurrentWebviewWindow(); if (window) { return window.setTheme(theme); } }, async getWindowTheme() { - const window = await this.getWebviewWindow(); + const window = await this.getCurrentWebviewWindow(); if (window) { return window.theme(); } @@ -187,7 +195,7 @@ export const createTauriAdapter = (): TauriPlatformAdapter => { }, async onThemeChanged(callback) { - const window = await this.getWebviewWindow(); + const window = await this.getCurrentWebviewWindow(); if (window) { window.onThemeChanged(callback); } @@ -205,13 +213,10 @@ export const createTauriAdapter = (): TauriPlatformAdapter => { }, async getAllWindows() { - const { getAllWindows } = await import("@tauri-apps/api/window"); - return getAllWindows(); - }, - - async getCurrentWindow() { - const { getCurrentWindow } = await import("@tauri-apps/api/window"); - return getCurrentWindow(); + const { getAllWebviewWindows } = await import( + "@tauri-apps/api/webviewWindow" + ); + return getAllWebviewWindows(); }, async createWebviewWindow(label, options) { @@ -326,7 +331,7 @@ export const createTauriAdapter = (): TauriPlatformAdapter => { }, async getCurrentWindowLabel() { - const window = await windowWrapper.getWebviewWindow(); + const window = await windowWrapper.getCurrentWebviewWindow(); return window.label; }, diff --git a/src/utils/webAdapter.ts b/src/utils/webAdapter.ts index 4257156f..618c2489 100644 --- a/src/utils/webAdapter.ts +++ b/src/utils/webAdapter.ts @@ -8,6 +8,10 @@ export interface WebPlatformAdapter extends BasePlatformAdapter { metadata: (path: string, options: any) => Promise>; error: (message: string) => void; openLogDir: () => Promise; + getCurrentWebviewWindow: () => Promise; + getWindowTheme: () => Promise; + setWindowTheme: (theme: string | null) => Promise; + getAllWindows: () => Promise; } // Create Web adapter functions @@ -124,11 +128,6 @@ export const createWebAdapter = (): WebPlatformAdapter => { return () => {}; }, - async getWebviewWindow() { - console.log("Web mode simulated get webview window"); - return null; - }, - async setWindowTheme(theme) { console.log("Web mode simulated set window theme:", theme); }, @@ -156,7 +155,7 @@ export const createWebAdapter = (): WebPlatformAdapter => { return []; }, - async getCurrentWindow() { + async getCurrentWebviewWindow() { console.log("Web mode simulated get current window"); return null; }, @@ -272,7 +271,7 @@ export const createWebAdapter = (): WebPlatformAdapter => { async getCurrentWindowLabel() { return "web"; }, - + async openLogDir() { console.log("openLogDir is not supported in web environment"); return Promise.resolve(); diff --git a/src/utils/wrappers/tauriWrappers.ts b/src/utils/wrappers/tauriWrappers.ts index 7eb1955f..11940ef5 100644 --- a/src/utils/wrappers/tauriWrappers.ts +++ b/src/utils/wrappers/tauriWrappers.ts @@ -2,12 +2,7 @@ import * as commands from "@/commands"; // Window operations export const windowWrapper = { - async getCurrentWindow() { - const { getCurrentWindow } = await import("@tauri-apps/api/window"); - return getCurrentWindow(); - }, - - async getWebviewWindow() { + async getCurrentWebviewWindow() { const { getCurrentWebviewWindow } = await import( "@tauri-apps/api/webviewWindow" ); @@ -16,7 +11,7 @@ export const windowWrapper = { async setSize(width: number, height: number) { const { LogicalSize } = await import("@tauri-apps/api/dpi"); - const window = await this.getWebviewWindow(); + const window = await this.getCurrentWebviewWindow(); if (window) { await window.setSize(new LogicalSize(width, height)); }