Skip to content

A cross-platform desktop All-in-One assistant for Claude Code, Codex, OpenCode,DeepSeek Harness and more. Only official website: ccswitch.io

5.9 KB
Raw Blame
import { isTauri } from "@tauri-apps/api/core";
import { getCurrentWindow } from "@tauri-apps/api/window";
import { focusManager } from "@tanstack/react-query";

const HEARTBEAT_INTERVAL_MS = 3000;
const HEARTBEAT_DIM_MS = 300;

let initialized = false;
let heartbeatInterval: number | undefined;
let heartbeatReset: number | undefined;
const windowFocusListeners = new Set<() => void>();

function stopHeartbeat() {
  if (heartbeatInterval !== undefined) {
    window.clearInterval(heartbeatInterval);
    heartbeatInterval = undefined;
  }
  if (heartbeatReset !== undefined) {
    window.clearTimeout(heartbeatReset);
    heartbeatReset = undefined;
  }
  delete document.documentElement.dataset.statusHeartbeat;
}

function startHeartbeat() {
  stopHeartbeat();
  heartbeatInterval = window.setInterval(() => {
    document.documentElement.dataset.statusHeartbeat = "true";
    heartbeatReset = window.setTimeout(() => {
      delete document.documentElement.dataset.statusHeartbeat;
      heartbeatReset = undefined;
    }, HEARTBEAT_DIM_MS);
  }, HEARTBEAT_INTERVAL_MS);
}

function setWindowActive(active: boolean) {
  const previous = document.documentElement.dataset.windowActive;
  if (previous === String(active)) return;
  document.documentElement.dataset.windowActive = String(active);

  if (active) {
    startHeartbeat();
    if (previous === "false") {
      windowFocusListeners.forEach((listener) => listener());
    }
  } else {
    stopHeartbeat();
  }
}

/**
 * 窗口从失焦或隐藏回到激活状态时通知调用方。
 * 运行时复用 initializeWindowActivity 合并后的 DOM focus 与 Tauri onFocusChanged,
 * 避免同一轮回焦重复触发;未初始化时(如单元测试)回退到原生 window focus 事件。
 */
export function subscribeWindowFocus(listener: () => void): () => void {
  windowFocusListeners.add(listener);
  const onFallbackFocus = () => {
    if (!initialized) listener();
  };
  window.addEventListener("focus", onFallbackFocus);
  return () => {
    windowFocusListeners.delete(listener);
    window.removeEventListener("focus", onFallbackFocus);
  };
}

export function initializeWindowActivity() {
  if (initialized) return;
  initialized = true;

  // Visible monitoring windows must keep refreshing while the user works in a
  // terminal. Only hidden/minimized windows pause Query; focus controls the
  // decorative heartbeat. Native visibility covers WebViews that remain
  // document-visible after being hidden.
  focusManager.setEventListener((handleFocus) => {
    const currentWindow = isTauri() ? getCurrentWindow() : undefined;
    let focused = document.hasFocus();
    let nativeVisible = true;
    let focusRevision = 0;
    let visibilityRevision = 0;
    let disposed = false;
    const unlisteners: (() => void)[] = [];
    const update = () => {
      const visible = nativeVisible && document.visibilityState !== "hidden";
      setWindowActive(focused && visible);
      handleFocus(visible);
    };
    const refreshVisibility = async () => {
      if (!currentWindow || disposed) return;
      const revision = ++visibilityRevision;
      try {
        const [visible, minimized] = await Promise.all([
          currentWindow.isVisible(),
          currentWindow.isMinimized(),
        ]);
        if (disposed || visibilityRevision !== revision) return;
        nativeVisible = visible && !minimized;
        update();
      } catch (error) {
        console.error("Failed to read window visibility", error);
      }
    };
    const onVisibilityChange = () => {
      update();
      void refreshVisibility();
    };
    const onFocus = () => {
      focusRevision += 1;
      focused = true;
      onVisibilityChange();
    };
    const onBlur = () => {
      focusRevision += 1;
      focused = false;
      onVisibilityChange();
    };
    window.addEventListener("focus", onFocus);
    window.addEventListener("blur", onBlur);
    document.addEventListener("visibilitychange", onVisibilityChange);
    update();

    if (currentWindow) {
      const observe = async (register: () => Promise<() => void>) => {
        try {
          const off = await register();
          if (disposed) off();
          else unlisteners.push(off);
        } catch (error) {
          console.error("Failed to observe window activity", error);
        }
      };
      void (async () => {
        await Promise.all([
          observe(() =>
            currentWindow.onFocusChanged(({ payload }) => {
              if (disposed) return;
              focusRevision += 1;
              focused = payload;
              onVisibilityChange();
            }),
          ),
          observe(() =>
            currentWindow.onResized(() => void refreshVisibility()),
          ),
          // Closing an already-unfocused window to the tray may emit no blur
          // or resize. The backend notifies after its explicit show/hide calls.
          observe(() =>
            currentWindow.listen("main-window-visibility-changed", () => {
              void refreshVisibility();
            }),
          ),
        ]);
        if (disposed) return;
        // Register before snapshots so transitions during setup cannot be lost.
        // Visibility must not wait for the separate decorative focus snapshot.
        void refreshVisibility();
        const revision = focusRevision;
        try {
          const active = await currentWindow.isFocused();
          if (disposed || focusRevision !== revision) return;
          focused = active;
          update();
        } catch (error) {
          console.error("Failed to read initial window focus", error);
        }
      })();
    }

    return () => {
      disposed = true;
      window.removeEventListener("focus", onFocus);
      window.removeEventListener("blur", onBlur);
      document.removeEventListener("visibilitychange", onVisibilityChange);
      unlisteners.forEach((off) => off());
      stopHeartbeat();
      delete document.documentElement.dataset.windowActive;
    };
  });
}