refactor: replace getCurrentWindow with getCurrentWebviewWindow (#945)

* refactor: replace `getCurrentWindow` with `getCurrentWebviewWindow`

* refactor: update

* refactor: update
This commit is contained in:
ayangweb
2025-10-23 16:48:13 +08:00
committed by GitHub
parent f267df3f71
commit fc7cd165a8
8 changed files with 66 additions and 60 deletions

View File

@@ -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);

View File

@@ -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;

View File

@@ -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);
};

View File

@@ -26,6 +26,7 @@ function MainApp() {
setViewExtensionOpened(payload);
});
}, []);
const { synthesizeItem } = useChatStore();
useSyncStore();

View File

@@ -60,7 +60,6 @@ export interface WindowOperations {
showWindow: () => Promise<void>;
setAlwaysOnTop: (isPinned: boolean) => Promise<void>;
setShadow(enable: boolean): Promise<void>;
getWebviewWindow: () => Promise<any>;
getWindowByLabel: (label: string) => Promise<{
show: () => Promise<void>;
setFocus: () => Promise<void>;
@@ -68,8 +67,6 @@ export interface WindowOperations {
close: () => Promise<void>;
} | null>;
createWindow: (label: string, options: any) => Promise<void>;
getAllWindows: () => Promise<any[]>;
getCurrentWindow: () => Promise<any>;
createWebviewWindow: (label: string, options: any) => Promise<any>;
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<void>;
getWindowTheme: () => Promise<string>;
onThemeChanged: (
callback: (payload: { payload: string }) => void
) => Promise<void>;

View File

@@ -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<void>;
getCurrentWebviewWindow: () => Promise<WebviewWindow>;
getWindowTheme: () => Promise<Theme | null>;
setWindowTheme: (theme: Theme | null) => Promise<void>;
getAllWindows: () => Promise<WebviewWindow[]>;
}
// 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;
},

View File

@@ -8,6 +8,10 @@ export interface WebPlatformAdapter extends BasePlatformAdapter {
metadata: (path: string, options: any) => Promise<Record<string, any>>;
error: (message: string) => void;
openLogDir: () => Promise<void>;
getCurrentWebviewWindow: () => Promise<any>;
getWindowTheme: () => Promise<string>;
setWindowTheme: (theme: string | null) => Promise<void>;
getAllWindows: () => Promise<any[]>;
}
// 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;
},

View File

@@ -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));
}