Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"dev": "vite",
"build": "tsc && vite build",
"test:desktop-reopen": "node --experimental-strip-types --test tests/desktopReopen.test.ts",
"test:browser-runtime": "node --experimental-strip-types --test tests/browserRuntime.test.ts",
"test:close-preference": "node --experimental-strip-types --test tests/codexClosePreference.test.ts",
"test:reset-credits": "node --experimental-strip-types --test tests/resetCredits.test.ts",
"test:updater": "node --experimental-strip-types --test tests/updateInstallation.test.ts tests/updaterToolchain.test.ts",
Expand Down
18 changes: 10 additions & 8 deletions src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { useState, useEffect, useCallback, useMemo, useRef } from "react";
import { getCurrentWindow } from "@tauri-apps/api/window";
import { useAccounts } from "./hooks/useAccounts";
import { useDesktopReopen } from "./hooks/useDesktopReopen";
import { useCodexClosePreference } from "./hooks/useCodexClosePreference";
Expand Down Expand Up @@ -42,6 +41,7 @@ import {
type AutoWarmupWindow,
type AutoWarmupWindowKind,
} from "./lib/autoWarmupPolicy";
import { getTauriWindow } from "./lib/tauriWindow";
import "./App.css";

const AUTO_WARMUP_CHECK_INTERVAL_MS = 30 * 1000;
Expand All @@ -65,7 +65,6 @@ type AutoWarmupLedger = Record<
lastAutoWindowKind?: AutoWarmupWindowKind;
}
>;
const appWindow = getCurrentWindow();
const isMacOs =
typeof navigator !== "undefined" &&
/(Mac|iPhone|iPod|iPad)/i.test(navigator.userAgent);
Expand Down Expand Up @@ -388,14 +387,14 @@ function App() {
const handleTitlebarDrag = useCallback(
(event: React.MouseEvent<HTMLDivElement>) => {
if (!isTauriRuntime() || event.button !== 0) return;
void appWindow.startDragging();
void getTauriWindow()?.startDragging();
},
[]
);

const handleTitlebarDoubleClick = useCallback(() => {
if (!isTauriRuntime()) return;
void appWindow.toggleMaximize();
void getTauriWindow()?.toggleMaximize();
}, []);

const toggleMask = (accountId: string) => {
Expand Down Expand Up @@ -522,6 +521,9 @@ function App() {
useEffect(() => {
if (!isTauriRuntime() || isMacOs) return;

const appWindow = getTauriWindow();
if (!appWindow) return;

let unlisten: (() => void) | undefined;

const syncMaximizedState = async () => {
Expand Down Expand Up @@ -1354,11 +1356,11 @@ function App() {
onDoubleClick={handleTitlebarDoubleClick}
className={`h-full flex-1 select-none cursor-default ${isMacOs ? "ml-18 mr-2" : "mr-3"}`}
/>
{!isMacOs && (
{isTauriRuntime() && !isMacOs && (
<div className="flex items-center gap-1">
<button
onClick={() => {
void appWindow.minimize();
void getTauriWindow()?.minimize();
}}
className="flex h-8 w-8 items-center justify-center rounded-md text-gray-500 transition-colors hover:bg-gray-100 hover:text-gray-900 dark:text-gray-400 dark:hover:bg-gray-800 dark:hover:text-gray-100"
title="Minimize"
Expand All @@ -1369,7 +1371,7 @@ function App() {
</button>
<button
onClick={() => {
void appWindow.toggleMaximize();
void getTauriWindow()?.toggleMaximize();
}}
className="flex h-8 w-8 items-center justify-center rounded-md text-gray-500 transition-colors hover:bg-gray-100 hover:text-gray-900 dark:text-gray-400 dark:hover:bg-gray-800 dark:hover:text-gray-100"
title={isWindowMaximized ? "Restore" : "Maximize"}
Expand All @@ -1387,7 +1389,7 @@ function App() {
</button>
<button
onClick={() => {
void appWindow.close();
void getTauriWindow()?.close();
}}
className="flex h-8 w-8 items-center justify-center rounded-md text-gray-500 transition-colors hover:bg-red-500 hover:text-white dark:text-gray-400 dark:hover:bg-red-500 dark:hover:text-white"
title="Close"
Expand Down
14 changes: 14 additions & 0 deletions src/lib/tauriWindow.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { getCurrentWindow, type Window as TauriWindow } from "@tauri-apps/api/window";

let cachedWindow: TauriWindow | null = null;

/**
* Resolve the Tauri window only inside the Tauri runtime. Browser/LAN mode
* must be able to evaluate the React bundle without touching Tauri internals.
*/
export function getTauriWindow(): TauriWindow | null {
if (typeof window === "undefined" || !("__TAURI_INTERNALS__" in window)) {
return null;
}
return (cachedWindow ??= getCurrentWindow());
}
21 changes: 21 additions & 0 deletions tests/browserRuntime.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import assert from "node:assert/strict";
import test from "node:test";
import { getTauriWindow } from "../src/lib/tauriWindow.ts";

test("browser runtime does not resolve a Tauri window", () => {
assert.equal(getTauriWindow(), null);
});

test("a plain browser-like window without Tauri internals remains safe", () => {
const previous = Object.getOwnPropertyDescriptor(globalThis, "window");
Object.defineProperty(globalThis, "window", {
configurable: true,
value: {},
});
try {
assert.equal(getTauriWindow(), null);
} finally {
if (previous) Object.defineProperty(globalThis, "window", previous);
else delete (globalThis as { window?: unknown }).window;
}
});
Loading