Skip to content

armgit / dbx

Public Imported from GitHub
0

25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。

4.0 KB
Raw Blame
import { assertUpdateAllowsInteraction, beginUpdateSensitiveOperation } from "@/lib/app/updatePreparation";
import { detachedWindowLabel, detachedWindowUrl } from "@/lib/app/windowContext";
import { pinDetachedWindowIfMainPinned } from "@/lib/app/windowAlwaysOnTop";
import { isTauriRuntime } from "@/lib/backend/tauriRuntime";

export interface DetachedWindowOpenPosition {
  x: number;
  y: number;
}

export interface DetachedWindowOpenResult {
  opened: boolean;
  error?: string;
}

function errorMessage(error: unknown): string {
  if (typeof error === "string") return error;
  if (error && typeof error === "object" && "message" in error && typeof error.message === "string") return error.message;
  return String(error || "Unknown window creation error");
}

export async function openDetachedTabWindow(tabId: string, title: string, position?: DetachedWindowOpenPosition): Promise<DetachedWindowOpenResult> {
  if (!isTauriRuntime()) return { opened: false, error: "Detached windows are only available in the desktop app." };
  let finishOperation: (() => void) | undefined;
  try {
    finishOperation = beginUpdateSensitiveOperation();
    const { WebviewWindow } = await import("@tauri-apps/api/webviewWindow");
    const label = detachedWindowLabel(tabId);
    const existing = await WebviewWindow.getByLabel(label);
    if (existing) {
      await pinDetachedWindowIfMainPinned(existing);
      await existing.show();
      await existing.setFocus();
      return { opened: true };
    }

    assertUpdateAllowsInteraction();
    const child = new WebviewWindow(label, {
      url: detachedWindowUrl(tabId),
      title,
      width: 1100,
      height: 720,
      minWidth: 760,
      minHeight: 600,
      resizable: true,
      visible: false,
      decorations: false,
      center: false,
    });

    // A caller timeout does not prove native window creation has stopped.
    // Keep this second reservation until a native terminal event arrives.
    const finishNativeCreation = beginUpdateSensitiveOperation();
    return await new Promise((resolve) => {
      let settled = false;
      let timedOut = false;
      let timeout: ReturnType<typeof setTimeout> | undefined;
      const finish = (result: DetachedWindowOpenResult) => {
        if (settled) return;
        settled = true;
        clearTimeout(timeout);
        resolve(result);
      };
      void child.once("tauri://created", async () => {
        if (timedOut) {
          try {
            // The caller already restored its tab; never surface a duplicate.
            await child.destroy();
            finishNativeCreation();
          } catch (error) {
            // Keep the reservation if removal is uncertain, rather than
            // allowing restart to race a still-initializing webview.
            console.error("[DBX][detached-tab:late-create:close-error]", error);
          }
          return;
        }
        try {
          if (position) {
            const { PhysicalPosition } = await import("@tauri-apps/api/dpi");
            await child.setPosition(new PhysicalPosition(Math.round(position.x - 120), Math.round(position.y - 20)));
          }
          await pinDetachedWindowIfMainPinned(child);
          await child.show();
          await child.setFocus();
          finish({ opened: true });
        } catch (error) {
          finish({ opened: false, error: errorMessage(error) });
        } finally {
          finishNativeCreation();
        }
      });
      void child.once("tauri://error", (event) => {
        const error = errorMessage(event?.payload);
        console.error("[DBX][detached-tab:create:error]", error);
        finish({ opened: false, error });
        finishNativeCreation();
      });
      timeout = setTimeout(() => {
        timedOut = true;
        finish({ opened: false, error: "Timed out while creating the detached window." });
      }, 10_000);
    });
  } catch (error) {
    const message = errorMessage(error);
    console.error("[DBX][detached-tab:create:error]", error);
    return { opened: false, error: message };
  } finally {
    finishOperation?.();
  }
}