chore: finalize electron desktop migration
This commit is contained in:
@@ -0,0 +1,27 @@
|
||||
import { getDesktopHost } from "@/desktop/host";
|
||||
|
||||
export type DesktopEventUnlisten = () => void;
|
||||
|
||||
type EventEnvelope = {
|
||||
payload?: unknown;
|
||||
};
|
||||
|
||||
export async function listenToDesktopEvent<TPayload>(
|
||||
event: string,
|
||||
handler: (payload: TPayload) => void
|
||||
): Promise<DesktopEventUnlisten> {
|
||||
const listen = getDesktopHost()?.events?.on;
|
||||
if (typeof listen !== "function") {
|
||||
throw new Error("Desktop event API is unavailable.");
|
||||
}
|
||||
|
||||
const unlisten = await listen(event, (rawEvent: unknown) => {
|
||||
const payload =
|
||||
typeof rawEvent === "object" && rawEvent !== null && "payload" in rawEvent
|
||||
? (rawEvent as EventEnvelope).payload
|
||||
: rawEvent;
|
||||
handler(payload as TPayload);
|
||||
});
|
||||
|
||||
return typeof unlisten === "function" ? unlisten : () => {};
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
import type { DesktopHostBridge } from "@/desktop/host";
|
||||
|
||||
export function getElectronHost(): DesktopHostBridge | null {
|
||||
if (typeof window === "undefined") {
|
||||
return null;
|
||||
}
|
||||
const host = window.paseoDesktop;
|
||||
if (!host || typeof host !== "object") {
|
||||
return null;
|
||||
}
|
||||
return host;
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
import { getDesktopHost } from "@/desktop/host";
|
||||
|
||||
export async function invokeDesktopCommand<T>(
|
||||
command: string,
|
||||
args?: Record<string, unknown>
|
||||
): Promise<T> {
|
||||
const invoke = getDesktopHost()?.invoke;
|
||||
if (typeof invoke !== "function") {
|
||||
throw new Error("Desktop invoke() is unavailable in this environment.");
|
||||
}
|
||||
return (await invoke(command, args)) as T;
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
import { getDesktopHost, type DesktopWindowBridge } from "@/desktop/host";
|
||||
|
||||
export function getDesktopWindow(): DesktopWindowBridge | null {
|
||||
const getter = getDesktopHost()?.window?.getCurrentWindow;
|
||||
if (typeof getter !== "function") {
|
||||
return null;
|
||||
}
|
||||
try {
|
||||
return getter() ?? null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
export async function startDesktopDragging(): Promise<void> {
|
||||
const win = getDesktopWindow();
|
||||
if (!win || typeof win.startDragging !== "function") {
|
||||
return;
|
||||
}
|
||||
await win.startDragging();
|
||||
}
|
||||
|
||||
export async function toggleDesktopMaximize(): Promise<void> {
|
||||
const win = getDesktopWindow();
|
||||
if (!win || typeof win.toggleMaximize !== "function") {
|
||||
return;
|
||||
}
|
||||
await win.toggleMaximize();
|
||||
}
|
||||
|
||||
export async function isDesktopFullscreen(): Promise<boolean> {
|
||||
const win = getDesktopWindow();
|
||||
if (!win || typeof win.isFullscreen !== "function") {
|
||||
return false;
|
||||
}
|
||||
return await win.isFullscreen();
|
||||
}
|
||||
Reference in New Issue
Block a user