Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix(desktop): harden teleprompter window lifecycle on Windows

Richie McIlroy1c0c86eb5674
diff --git a/apps/desktop/src/routes/teleprompter.tsx b/apps/desktop/src/routes/teleprompter.tsx
index fc0b5a168..2bc2bf280 100644
--- a/apps/desktop/src/routes/teleprompter.tsx
+++ b/apps/desktop/src/routes/teleprompter.tsx
@@ -131,6 +131,7 @@ export default function Teleprompter() {
.then(async () => {
await commands.setTeleprompterWindowLevel(true);
await currentWindow.show();
+ if (isWindows) await commands.refreshWindowContentProtection();
await currentWindow.setFocus();
})
.catch((error) => {
diff --git a/apps/desktop/src/utils/teleprompter.test.ts b/apps/desktop/src/utils/teleprompter.test.ts
new file mode 100644
index 000000000..a5e0df72b
--- /dev/null
+++ b/apps/desktop/src/utils/teleprompter.test.ts
@@ -0,0 +1,132 @@
+import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
+
+type TestEvent = { payload: unknown };
+type TestListener = (event: TestEvent) => void;
+type CreatedWindow = {
+ label: string;
+ options: Record<string, unknown>;
+ listeners: Map<string, TestListener>;
+};
+
+const mocks = vi.hoisted(() => {
+ const calls: string[] = [];
+ const existingWindow = {
+ innerPosition: vi.fn(async () => {
+ calls.push("innerPosition");
+ }),
+ unminimize: vi.fn(async () => {
+ calls.push("unminimize");
+ }),
+ show: vi.fn(async () => {
+ calls.push("show");
+ }),
+ setFocus: vi.fn(async () => {
+ calls.push("focus");
+ }),
+ destroy: vi.fn(async () => {
+ calls.push("destroy");
+ }),
+ };
+ const openerWindow = {
+ setFocus: vi.fn(async () => {
+ calls.push("openerFocus");
+ }),
+ };
+
+ return {
+ calls,
+ existingWindow,
+ openerWindow,
+ state: {
+ existingWindow: null as typeof existingWindow | null,
+ createdWindow: undefined as CreatedWindow | undefined,
+ },
+ getCurrent: vi.fn(() => openerWindow),
+ getByLabel: vi.fn(async () => null as typeof existingWindow | null),
+ osType: vi.fn(() => "macos"),
+ refreshWindowContentProtection: vi.fn(async () => {
+ calls.push("refresh");
+ return null;
+ }),
+ };
+});
+
+vi.mock("@tauri-apps/api/webviewWindow", () => ({
+ WebviewWindow: class {
+ static getCurrent = mocks.getCurrent;
+ static getByLabel = vi.fn(async () => mocks.state.existingWindow);
+
+ label: string;
+ options: Record<string, unknown>;
+ listeners = new Map<string, TestListener>();
+
+ constructor(label: string, options: Record<string, unknown> = {}) {
+ this.label = label;
+ this.options = options;
+ mocks.state.createdWindow = this;
+ }
+
+ once(event: string, listener: TestListener) {
+ this.listeners.set(event, listener);
+ return Promise.resolve(() => undefined);
+ }
+ },
+}));
+
+vi.mock("@tauri-apps/plugin-os", () => ({ type: mocks.osType }));
+
+vi.mock("./tauri", () => ({
+ commands: {
+ refreshWindowContentProtection: mocks.refreshWindowContentProtection,
+ },
+}));
+
+describe("openTeleprompter on macOS", () => {
+ beforeEach(() => {
+ vi.resetModules();
+ vi.clearAllMocks();
+ mocks.calls.length = 0;
+ mocks.state.existingWindow = null;
+ mocks.state.createdWindow = undefined;
+ mocks.osType.mockReturnValue("macos");
+ vi.stubGlobal("window", { __CAP__: {} });
+ });
+
+ afterEach(() => {
+ vi.unstubAllGlobals();
+ });
+
+ it("preserves the existing-window refresh and focus sequence", async () => {
+ mocks.state.existingWindow = mocks.existingWindow;
+ const { openTeleprompter } = await import("./teleprompter");
+
+ await openTeleprompter();
+
+ expect(mocks.calls).toEqual(["refresh", "unminimize", "show", "focus"]);
+ expect(mocks.existingWindow.innerPosition).not.toHaveBeenCalled();
+ expect(mocks.existingWindow.destroy).not.toHaveBeenCalled();
+ expect(mocks.getCurrent).not.toHaveBeenCalled();
+ });
+
+ it("preserves creation focus and content-protection timing", async () => {
+ const { openTeleprompter } = await import("./teleprompter");
+
+ await openTeleprompter();
+
+ const createdWindow = mocks.state.createdWindow;
+ expect(createdWindow?.options).toMatchObject({
+ focus: true,
+ visible: false,
+ decorations: true,
+ additionalBrowserArgs: undefined,
+ });
+ expect(mocks.getCurrent).not.toHaveBeenCalled();
+ expect(mocks.refreshWindowContentProtection).not.toHaveBeenCalled();
+
+ createdWindow?.listeners.get("tauri://created")?.({ payload: null });
+
+ await vi.waitFor(() => {
+ expect(mocks.calls).toEqual(["refresh"]);
+ });
+ });
+});
diff --git a/apps/desktop/src/utils/teleprompter.ts b/apps/desktop/src/utils/teleprompter.ts
index 0701df936..39487c068 100644
--- a/apps/desktop/src/utils/teleprompter.ts
+++ b/apps/desktop/src/utils/teleprompter.ts
@@ -1,57 +1,87 @@
-import { LogicalPosition } from "@tauri-apps/api/dpi";
import { WebviewWindow } from "@tauri-apps/api/webviewWindow";
import { type as ostype } from "@tauri-apps/plugin-os";
import { commands } from "./tauri";
+import { getTeleprompterWindowOptions } from "./teleprompter-window-options";
export const TELEPROMPTER_WINDOW_LABEL = "teleprompter";
let creatingWindow: WebviewWindow | undefined;
+async function cleanupFailedWindow(
+ teleprompterWindow: WebviewWindow,
+ openerWindow: WebviewWindow,
+) {
+ if (creatingWindow === teleprompterWindow) creatingWindow = undefined;
+ await teleprompterWindow.destroy().catch(() => undefined);
+ await openerWindow.setFocus().catch(() => undefined);
+}
+
export async function openTeleprompter() {
const existingWindow = await WebviewWindow.getByLabel(
TELEPROMPTER_WINDOW_LABEL,
);
if (existingWindow) {
- await commands.refreshWindowContentProtection();
- await existingWindow.unminimize();
- await existingWindow.show();
- await existingWindow.setFocus();
- return;
+ if (ostype() !== "windows") {
+ await commands.refreshWindowContentProtection();
+ await existingWindow.unminimize();
+ await existingWindow.show();
+ await existingWindow.setFocus();
+ return;
+ }
+
+ const openerWindow = WebviewWindow.getCurrent();
+ try {
+ await existingWindow.innerPosition();
+ await existingWindow.unminimize();
+ await existingWindow.show();
+ await commands.refreshWindowContentProtection();
+ await existingWindow.setFocus();
+ return;
+ } catch (error) {
+ console.error("Discarding unusable teleprompter window:", error);
+ await cleanupFailedWindow(existingWindow, openerWindow);
+ }
}
if (creatingWindow) return;
- const isMacOS = ostype() === "macos";
-
- const teleprompterWindow = new WebviewWindow(TELEPROMPTER_WINDOW_LABEL, {
- url: "/teleprompter",
- title: "Cap Teleprompter",
- width: 560,
- height: 320,
- minWidth: 420,
- minHeight: 220,
- center: true,
- focus: true,
- visible: false,
- resizable: true,
- decorations: isMacOS,
- titleBarStyle: isMacOS ? "overlay" : undefined,
- hiddenTitle: isMacOS,
- trafficLightPosition: isMacOS ? new LogicalPosition(14, 14) : undefined,
- transparent: true,
- shadow: true,
- alwaysOnTop: true,
- visibleOnAllWorkspaces: true,
- skipTaskbar: true,
- });
+ const platform = ostype();
+ const isWindows = platform === "windows";
+ const openerWindow = isWindows ? WebviewWindow.getCurrent() : undefined;
+
+ const teleprompterWindow = new WebviewWindow(
+ TELEPROMPTER_WINDOW_LABEL,
+ getTeleprompterWindowOptions(
+ platform,
+ window.__CAP__?.windowsWebview2BrowserArgs,
+ ),
+ );
creatingWindow = teleprompterWindow;
void teleprompterWindow.once("tauri://created", () => {
- creatingWindow = undefined;
- void commands.refreshWindowContentProtection();
+ if (!isWindows) {
+ creatingWindow = undefined;
+ void commands.refreshWindowContentProtection();
+ return;
+ }
+ if (!openerWindow) return;
+
+ void teleprompterWindow
+ .innerPosition()
+ .then(() => {
+ if (creatingWindow === teleprompterWindow) creatingWindow = undefined;
+ })
+ .catch(async (error) => {
+ console.error("Teleprompter window has no native handle:", error);
+ await cleanupFailedWindow(teleprompterWindow, openerWindow);
+ });
});
void teleprompterWindow.once("tauri://error", (event) => {
- creatingWindow = undefined;
console.error("Failed to create teleprompter window:", event.payload);
+ if (isWindows && openerWindow) {
+ void cleanupFailedWindow(teleprompterWindow, openerWindow);
+ } else {
+ creatingWindow = undefined;
+ }
});
}