mirror of
https://github.com/infinilabs/coco-app.git
synced 2025-12-16 11:37:47 +01:00
refactor: replace getCurrentWindow with getCurrentWebviewWindow (#945)
* refactor: replace `getCurrentWindow` with `getCurrentWebviewWindow` * refactor: update * refactor: update
This commit is contained in:
@@ -4,7 +4,7 @@ import {
|
|||||||
getCurrent as getCurrentDeepLinkUrls,
|
getCurrent as getCurrentDeepLinkUrls,
|
||||||
onOpenUrl,
|
onOpenUrl,
|
||||||
} from "@tauri-apps/plugin-deep-link";
|
} 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 { useAppStore } from "@/stores/appStore";
|
||||||
import { useConnectStore } from "@/stores/connectStore";
|
import { useConnectStore } from "@/stores/connectStore";
|
||||||
@@ -52,7 +52,7 @@ export function useDeepLinkManager() {
|
|||||||
|
|
||||||
// trigger oauth success event
|
// trigger oauth success event
|
||||||
platformAdapter.emitEvent("oauth_success", { serverId });
|
platformAdapter.emitEvent("oauth_success", { serverId });
|
||||||
getCurrentWindow().setFocus();
|
getCurrentWebviewWindow().setFocus();
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error("Failed to parse OAuth callback URL:", err);
|
console.error("Failed to parse OAuth callback URL:", err);
|
||||||
addError("Invalid OAuth callback URL format: " + err);
|
addError("Invalid OAuth callback URL format: " + err);
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ export const useTauriFocus = (props: Props) => {
|
|||||||
useMount(async () => {
|
useMount(async () => {
|
||||||
if (!isTauri) return;
|
if (!isTauri) return;
|
||||||
|
|
||||||
const appWindow = await platformAdapter.getWebviewWindow();
|
const appWindow = await platformAdapter.getCurrentWebviewWindow();
|
||||||
|
|
||||||
const wait = isMac ? 0 : 100;
|
const wait = isMac ? 0 : 100;
|
||||||
|
|
||||||
|
|||||||
@@ -26,7 +26,7 @@ export const useWindows = () => {
|
|||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const fetchWindow = async () => {
|
const fetchWindow = async () => {
|
||||||
try {
|
try {
|
||||||
const window = await platformAdapter.getCurrentWindow();
|
const window = await platformAdapter.getCurrentWebviewWindow();
|
||||||
setAppWindow(window);
|
setAppWindow(window);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error("Failed to get current window:", error);
|
console.error("Failed to get current window:", error);
|
||||||
@@ -51,7 +51,7 @@ export const useWindows = () => {
|
|||||||
|
|
||||||
const win = await platformAdapter.createWebviewWindow(args.label, args);
|
const win = await platformAdapter.createWebviewWindow(args.label, args);
|
||||||
|
|
||||||
if(win) {
|
if (win) {
|
||||||
win.once("tauri://created", async () => {
|
win.once("tauri://created", async () => {
|
||||||
console.log("tauri://created");
|
console.log("tauri://created");
|
||||||
// if (args.label.includes("main")) {
|
// if (args.label.includes("main")) {
|
||||||
@@ -68,7 +68,6 @@ export const useWindows = () => {
|
|||||||
console.error("error:", error);
|
console.error("error:", error);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
const closeWin = useCallback(async (label: string) => {
|
const closeWin = useCallback(async (label: string) => {
|
||||||
@@ -96,32 +95,44 @@ export const useWindows = () => {
|
|||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
const listenEvents = useCallback(() => {
|
const listenEvents = useCallback(() => {
|
||||||
let unlistenHandlers: { (): void; (): void; (): void; (): void; }[] = [];
|
let unlistenHandlers: { (): void; (): void; (): void; (): void }[] = [];
|
||||||
|
|
||||||
const setupListeners = async () => {
|
const setupListeners = async () => {
|
||||||
const winCreateHandler = await platformAdapter.listenWindowEvent("win-create", (event) => {
|
const winCreateHandler = await platformAdapter.listenWindowEvent(
|
||||||
console.log(event);
|
"win-create",
|
||||||
createWin(event.payload);
|
(event) => {
|
||||||
});
|
console.log(event);
|
||||||
|
createWin(event.payload);
|
||||||
|
}
|
||||||
|
);
|
||||||
unlistenHandlers.push(winCreateHandler);
|
unlistenHandlers.push(winCreateHandler);
|
||||||
|
|
||||||
const winShowHandler = await platformAdapter.listenWindowEvent("win-show", async () => {
|
const winShowHandler = await platformAdapter.listenWindowEvent(
|
||||||
if (!appWindow || !appWindow.label.includes("main")) return;
|
"win-show",
|
||||||
await appWindow.show();
|
async () => {
|
||||||
await appWindow.unminimize();
|
if (!appWindow || !appWindow.label.includes("main")) return;
|
||||||
await appWindow.setFocus();
|
await appWindow.show();
|
||||||
});
|
await appWindow.unminimize();
|
||||||
|
await appWindow.setFocus();
|
||||||
|
}
|
||||||
|
);
|
||||||
unlistenHandlers.push(winShowHandler);
|
unlistenHandlers.push(winShowHandler);
|
||||||
|
|
||||||
const winHideHandler = await platformAdapter.listenWindowEvent("win-hide", async () => {
|
const winHideHandler = await platformAdapter.listenWindowEvent(
|
||||||
if (!appWindow || !appWindow.label.includes("main")) return;
|
"win-hide",
|
||||||
await appWindow.hide();
|
async () => {
|
||||||
});
|
if (!appWindow || !appWindow.label.includes("main")) return;
|
||||||
|
await appWindow.hide();
|
||||||
|
}
|
||||||
|
);
|
||||||
unlistenHandlers.push(winHideHandler);
|
unlistenHandlers.push(winHideHandler);
|
||||||
|
|
||||||
const winCloseHandler = await platformAdapter.listenWindowEvent("win-close", async () => {
|
const winCloseHandler = await platformAdapter.listenWindowEvent(
|
||||||
await appWindow.close();
|
"win-close",
|
||||||
});
|
async () => {
|
||||||
|
await appWindow.close();
|
||||||
|
}
|
||||||
|
);
|
||||||
unlistenHandlers.push(winCloseHandler);
|
unlistenHandlers.push(winCloseHandler);
|
||||||
};
|
};
|
||||||
|
|
||||||
@@ -144,4 +155,4 @@ export const useWindows = () => {
|
|||||||
getWin,
|
getWin,
|
||||||
getAllWin,
|
getAllWin,
|
||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -26,6 +26,7 @@ function MainApp() {
|
|||||||
setViewExtensionOpened(payload);
|
setViewExtensionOpened(payload);
|
||||||
});
|
});
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
const { synthesizeItem } = useChatStore();
|
const { synthesizeItem } = useChatStore();
|
||||||
|
|
||||||
useSyncStore();
|
useSyncStore();
|
||||||
|
|||||||
@@ -60,7 +60,6 @@ export interface WindowOperations {
|
|||||||
showWindow: () => Promise<void>;
|
showWindow: () => Promise<void>;
|
||||||
setAlwaysOnTop: (isPinned: boolean) => Promise<void>;
|
setAlwaysOnTop: (isPinned: boolean) => Promise<void>;
|
||||||
setShadow(enable: boolean): Promise<void>;
|
setShadow(enable: boolean): Promise<void>;
|
||||||
getWebviewWindow: () => Promise<any>;
|
|
||||||
getWindowByLabel: (label: string) => Promise<{
|
getWindowByLabel: (label: string) => Promise<{
|
||||||
show: () => Promise<void>;
|
show: () => Promise<void>;
|
||||||
setFocus: () => Promise<void>;
|
setFocus: () => Promise<void>;
|
||||||
@@ -68,8 +67,6 @@ export interface WindowOperations {
|
|||||||
close: () => Promise<void>;
|
close: () => Promise<void>;
|
||||||
} | null>;
|
} | null>;
|
||||||
createWindow: (label: string, options: any) => Promise<void>;
|
createWindow: (label: string, options: any) => Promise<void>;
|
||||||
getAllWindows: () => Promise<any[]>;
|
|
||||||
getCurrentWindow: () => Promise<any>;
|
|
||||||
createWebviewWindow: (label: string, options: any) => Promise<any>;
|
createWebviewWindow: (label: string, options: any) => Promise<any>;
|
||||||
listenWindowEvent: (
|
listenWindowEvent: (
|
||||||
event: string,
|
event: string,
|
||||||
@@ -88,8 +85,6 @@ export interface ThemeAndEvents {
|
|||||||
event: K,
|
event: K,
|
||||||
callback: (event: { payload: EventPayloads[K] }) => void
|
callback: (event: { payload: EventPayloads[K] }) => void
|
||||||
) => Promise<() => void>;
|
) => Promise<() => void>;
|
||||||
setWindowTheme: (theme: string | null) => Promise<void>;
|
|
||||||
getWindowTheme: () => Promise<string>;
|
|
||||||
onThemeChanged: (
|
onThemeChanged: (
|
||||||
callback: (payload: { payload: string }) => void
|
callback: (payload: { payload: string }) => void
|
||||||
) => Promise<void>;
|
) => Promise<void>;
|
||||||
|
|||||||
@@ -16,6 +16,8 @@ import { useAppearanceStore } from "@/stores/appearanceStore";
|
|||||||
import { copyToClipboard, dispatchEvent, OpenURLWithBrowser } from ".";
|
import { copyToClipboard, dispatchEvent, OpenURLWithBrowser } from ".";
|
||||||
import { useAppStore } from "@/stores/appStore";
|
import { useAppStore } from "@/stores/appStore";
|
||||||
import { unrequitable } from "@/utils";
|
import { unrequitable } from "@/utils";
|
||||||
|
import { WebviewWindow } from "@tauri-apps/api/webviewWindow";
|
||||||
|
import { Theme } from "@tauri-apps/api/window";
|
||||||
|
|
||||||
export interface TauriPlatformAdapter extends BasePlatformAdapter {
|
export interface TauriPlatformAdapter extends BasePlatformAdapter {
|
||||||
openFileDialog: (
|
openFileDialog: (
|
||||||
@@ -24,6 +26,10 @@ export interface TauriPlatformAdapter extends BasePlatformAdapter {
|
|||||||
metadata: typeof metadata;
|
metadata: typeof metadata;
|
||||||
error: typeof error;
|
error: typeof error;
|
||||||
openLogDir: () => Promise<void>;
|
openLogDir: () => Promise<void>;
|
||||||
|
getCurrentWebviewWindow: () => Promise<WebviewWindow>;
|
||||||
|
getWindowTheme: () => Promise<Theme | null>;
|
||||||
|
setWindowTheme: (theme: Theme | null) => Promise<void>;
|
||||||
|
getAllWindows: () => Promise<WebviewWindow[]>;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create Tauri adapter functions
|
// Create Tauri adapter functions
|
||||||
@@ -34,12 +40,12 @@ export const createTauriAdapter = (): TauriPlatformAdapter => {
|
|||||||
},
|
},
|
||||||
|
|
||||||
async hideWindow() {
|
async hideWindow() {
|
||||||
const window = await windowWrapper.getWebviewWindow();
|
const window = await windowWrapper.getCurrentWebviewWindow();
|
||||||
return window?.hide();
|
return window?.hide();
|
||||||
},
|
},
|
||||||
|
|
||||||
async showWindow() {
|
async showWindow() {
|
||||||
const window = await windowWrapper.getWebviewWindow();
|
const window = await windowWrapper.getCurrentWebviewWindow();
|
||||||
window?.show();
|
window?.show();
|
||||||
window?.unminimize();
|
window?.unminimize();
|
||||||
return window?.setFocus();
|
return window?.setFocus();
|
||||||
@@ -77,8 +83,10 @@ export const createTauriAdapter = (): TauriPlatformAdapter => {
|
|||||||
},
|
},
|
||||||
|
|
||||||
async setAlwaysOnTop(isPinned) {
|
async setAlwaysOnTop(isPinned) {
|
||||||
const { getCurrentWindow } = await import("@tauri-apps/api/window");
|
const { getCurrentWebviewWindow } = await import(
|
||||||
const window = getCurrentWindow();
|
"@tauri-apps/api/webviewWindow"
|
||||||
|
);
|
||||||
|
const window = getCurrentWebviewWindow();
|
||||||
return window.setAlwaysOnTop(isPinned);
|
return window.setAlwaysOnTop(isPinned);
|
||||||
},
|
},
|
||||||
|
|
||||||
@@ -164,7 +172,7 @@ export const createTauriAdapter = (): TauriPlatformAdapter => {
|
|||||||
});
|
});
|
||||||
},
|
},
|
||||||
|
|
||||||
async getWebviewWindow() {
|
async getCurrentWebviewWindow() {
|
||||||
const { getCurrentWebviewWindow } = await import(
|
const { getCurrentWebviewWindow } = await import(
|
||||||
"@tauri-apps/api/webviewWindow"
|
"@tauri-apps/api/webviewWindow"
|
||||||
);
|
);
|
||||||
@@ -172,14 +180,14 @@ export const createTauriAdapter = (): TauriPlatformAdapter => {
|
|||||||
},
|
},
|
||||||
|
|
||||||
async setWindowTheme(theme) {
|
async setWindowTheme(theme) {
|
||||||
const window = await this.getWebviewWindow();
|
const window = await this.getCurrentWebviewWindow();
|
||||||
if (window) {
|
if (window) {
|
||||||
return window.setTheme(theme);
|
return window.setTheme(theme);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|
||||||
async getWindowTheme() {
|
async getWindowTheme() {
|
||||||
const window = await this.getWebviewWindow();
|
const window = await this.getCurrentWebviewWindow();
|
||||||
if (window) {
|
if (window) {
|
||||||
return window.theme();
|
return window.theme();
|
||||||
}
|
}
|
||||||
@@ -187,7 +195,7 @@ export const createTauriAdapter = (): TauriPlatformAdapter => {
|
|||||||
},
|
},
|
||||||
|
|
||||||
async onThemeChanged(callback) {
|
async onThemeChanged(callback) {
|
||||||
const window = await this.getWebviewWindow();
|
const window = await this.getCurrentWebviewWindow();
|
||||||
if (window) {
|
if (window) {
|
||||||
window.onThemeChanged(callback);
|
window.onThemeChanged(callback);
|
||||||
}
|
}
|
||||||
@@ -205,13 +213,10 @@ export const createTauriAdapter = (): TauriPlatformAdapter => {
|
|||||||
},
|
},
|
||||||
|
|
||||||
async getAllWindows() {
|
async getAllWindows() {
|
||||||
const { getAllWindows } = await import("@tauri-apps/api/window");
|
const { getAllWebviewWindows } = await import(
|
||||||
return getAllWindows();
|
"@tauri-apps/api/webviewWindow"
|
||||||
},
|
);
|
||||||
|
return getAllWebviewWindows();
|
||||||
async getCurrentWindow() {
|
|
||||||
const { getCurrentWindow } = await import("@tauri-apps/api/window");
|
|
||||||
return getCurrentWindow();
|
|
||||||
},
|
},
|
||||||
|
|
||||||
async createWebviewWindow(label, options) {
|
async createWebviewWindow(label, options) {
|
||||||
@@ -326,7 +331,7 @@ export const createTauriAdapter = (): TauriPlatformAdapter => {
|
|||||||
},
|
},
|
||||||
|
|
||||||
async getCurrentWindowLabel() {
|
async getCurrentWindowLabel() {
|
||||||
const window = await windowWrapper.getWebviewWindow();
|
const window = await windowWrapper.getCurrentWebviewWindow();
|
||||||
return window.label;
|
return window.label;
|
||||||
},
|
},
|
||||||
|
|
||||||
|
|||||||
@@ -8,6 +8,10 @@ export interface WebPlatformAdapter extends BasePlatformAdapter {
|
|||||||
metadata: (path: string, options: any) => Promise<Record<string, any>>;
|
metadata: (path: string, options: any) => Promise<Record<string, any>>;
|
||||||
error: (message: string) => void;
|
error: (message: string) => void;
|
||||||
openLogDir: () => Promise<void>;
|
openLogDir: () => Promise<void>;
|
||||||
|
getCurrentWebviewWindow: () => Promise<any>;
|
||||||
|
getWindowTheme: () => Promise<string>;
|
||||||
|
setWindowTheme: (theme: string | null) => Promise<void>;
|
||||||
|
getAllWindows: () => Promise<any[]>;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create Web adapter functions
|
// Create Web adapter functions
|
||||||
@@ -124,11 +128,6 @@ export const createWebAdapter = (): WebPlatformAdapter => {
|
|||||||
return () => {};
|
return () => {};
|
||||||
},
|
},
|
||||||
|
|
||||||
async getWebviewWindow() {
|
|
||||||
console.log("Web mode simulated get webview window");
|
|
||||||
return null;
|
|
||||||
},
|
|
||||||
|
|
||||||
async setWindowTheme(theme) {
|
async setWindowTheme(theme) {
|
||||||
console.log("Web mode simulated set window theme:", theme);
|
console.log("Web mode simulated set window theme:", theme);
|
||||||
},
|
},
|
||||||
@@ -156,7 +155,7 @@ export const createWebAdapter = (): WebPlatformAdapter => {
|
|||||||
return [];
|
return [];
|
||||||
},
|
},
|
||||||
|
|
||||||
async getCurrentWindow() {
|
async getCurrentWebviewWindow() {
|
||||||
console.log("Web mode simulated get current window");
|
console.log("Web mode simulated get current window");
|
||||||
return null;
|
return null;
|
||||||
},
|
},
|
||||||
@@ -272,7 +271,7 @@ export const createWebAdapter = (): WebPlatformAdapter => {
|
|||||||
async getCurrentWindowLabel() {
|
async getCurrentWindowLabel() {
|
||||||
return "web";
|
return "web";
|
||||||
},
|
},
|
||||||
|
|
||||||
async openLogDir() {
|
async openLogDir() {
|
||||||
console.log("openLogDir is not supported in web environment");
|
console.log("openLogDir is not supported in web environment");
|
||||||
return Promise.resolve();
|
return Promise.resolve();
|
||||||
|
|||||||
@@ -2,12 +2,7 @@ import * as commands from "@/commands";
|
|||||||
|
|
||||||
// Window operations
|
// Window operations
|
||||||
export const windowWrapper = {
|
export const windowWrapper = {
|
||||||
async getCurrentWindow() {
|
async getCurrentWebviewWindow() {
|
||||||
const { getCurrentWindow } = await import("@tauri-apps/api/window");
|
|
||||||
return getCurrentWindow();
|
|
||||||
},
|
|
||||||
|
|
||||||
async getWebviewWindow() {
|
|
||||||
const { getCurrentWebviewWindow } = await import(
|
const { getCurrentWebviewWindow } = await import(
|
||||||
"@tauri-apps/api/webviewWindow"
|
"@tauri-apps/api/webviewWindow"
|
||||||
);
|
);
|
||||||
@@ -16,7 +11,7 @@ export const windowWrapper = {
|
|||||||
|
|
||||||
async setSize(width: number, height: number) {
|
async setSize(width: number, height: number) {
|
||||||
const { LogicalSize } = await import("@tauri-apps/api/dpi");
|
const { LogicalSize } = await import("@tauri-apps/api/dpi");
|
||||||
const window = await this.getWebviewWindow();
|
const window = await this.getCurrentWebviewWindow();
|
||||||
if (window) {
|
if (window) {
|
||||||
await window.setSize(new LogicalSize(width, height));
|
await window.setSize(new LogicalSize(width, height));
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user