Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

fix: serialize recording menu creation and popup

Richie McIlroyab4716f09f1b
diff --git a/apps/desktop/src/routes/target-select-overlay.tsx b/apps/desktop/src/routes/target-select-overlay.tsx
index de755f9dd..ee2c72609 100644
--- a/apps/desktop/src/routes/target-select-overlay.tsx
+++ b/apps/desktop/src/routes/target-select-overlay.tsx
@@ -82,6 +82,7 @@ import {
createOptionsQuery,
createOrganizationsQuery,
} from "~/utils/queries";
+import { createRecordingMenuPopup } from "~/utils/recording-menu";
import {
type CanvasControls,
createImageDataWS,
@@ -2121,10 +2122,15 @@ function RecordingControls(props: {
});
};
- function showMenu(menu: Promise<Menu>, e: UIEvent) {
+ const popupRecordingMenu = createRecordingMenuPopup();
+
+ function showMenu(createMenu: () => Promise<Menu>, e: UIEvent) {
e.stopPropagation();
- const rect = (e.target as HTMLDivElement).getBoundingClientRect();
- menu.then((menu) => menu.popup(new LogicalPosition(rect.x, rect.y + 40)));
+ const rect = (e.currentTarget as HTMLElement).getBoundingClientRect();
+ void popupRecordingMenu(
+ createMenu,
+ new LogicalPosition(rect.x, rect.y + 40),
+ ).catch(() => toast.error("Could not open recording menu"));
}
return (
@@ -2195,13 +2201,14 @@ function RecordingControls(props: {
</span>
</div>
</div>
- <div
+ <button
+ type="button"
+ aria-label="Choose recording mode"
class="pl-2.5 pr-3 py-1.5 flex items-center border-l border-white/20 bg-white/5 transition-colors group-hover:bg-white/10"
- onMouseDown={(e) => showMenu(menuModes(), e)}
- onClick={(e) => showMenu(menuModes(), e)}
+ onClick={(e) => showMenu(menuModes, e)}
>
<IconCapCaretDown class="pointer-events-none" />
- </div>
+ </button>
</Popover.Anchor>
<Popover.Portal>
<Popover.Content class="z-200 w-[min(21rem,calc(100vw-1.5rem))] rounded-xl border border-amber-6 bg-gray-1 p-3.5 text-gray-12 shadow-xl outline-hidden data-expanded:animate-in data-expanded:fade-in data-expanded:zoom-in-95">
@@ -2232,13 +2239,14 @@ function RecordingControls(props: {
</Popover.Content>
</Popover.Portal>
</Popover>
- <div
+ <button
+ type="button"
+ aria-label="Recording countdown"
class="flex justify-center items-center rounded-full border transition-opacity bg-gray-6 text-gray-12 size-9 hover:opacity-80"
- onMouseDown={(e) => showMenu(preRecordingMenu(), e)}
- onClick={(e) => showMenu(preRecordingMenu(), e)}
+ onClick={(e) => showMenu(preRecordingMenu, e)}
>
<IconCapGear class="pointer-events-none will-change-transform size-5" />
- </div>
+ </button>
</div>
</div>
<Show when={(rawOptions.mode as string) !== "screenshot"}>
diff --git a/apps/desktop/src/utils/recording-menu.test.ts b/apps/desktop/src/utils/recording-menu.test.ts
new file mode 100644
index 000000000..7620b4333
--- /dev/null
+++ b/apps/desktop/src/utils/recording-menu.test.ts
@@ -0,0 +1,66 @@
+import { LogicalPosition } from "@tauri-apps/api/dpi";
+import { describe, expect, it, vi } from "vitest";
+import { createRecordingMenuPopup } from "./recording-menu";
+
+function deferred() {
+ let resolve!: () => void;
+ const promise = new Promise<void>((finish) => {
+ resolve = finish;
+ });
+ return { promise, resolve: () => resolve() };
+}
+
+describe("recording menu popup", () => {
+ it("blocks competing menu creation until construction and native popup both finish", async () => {
+ const show = createRecordingMenuPopup();
+ const construction = deferred();
+ const popup = deferred();
+ const position = new LogicalPosition(10, 20);
+ const menu = { popup: vi.fn(() => popup.promise) };
+ const firstFactory = vi.fn(async () => {
+ await construction.promise;
+ return menu;
+ });
+ const nextMenu = { popup: vi.fn().mockResolvedValue(undefined) };
+ const nextFactory = vi.fn().mockResolvedValue(nextMenu);
+ const first = show(firstFactory, position);
+
+ await show(nextFactory, position);
+ expect(nextFactory).not.toHaveBeenCalled();
+ expect(menu.popup).not.toHaveBeenCalled();
+
+ construction.resolve();
+ await Promise.resolve();
+ await Promise.resolve();
+ expect(menu.popup).toHaveBeenCalledOnce();
+ expect(menu.popup).toHaveBeenCalledWith(position);
+ await show(nextFactory, position);
+ expect(nextFactory).not.toHaveBeenCalled();
+
+ popup.resolve();
+ await first;
+ await show(nextFactory, position);
+ expect(nextFactory).toHaveBeenCalledOnce();
+ expect(nextMenu.popup).toHaveBeenCalledOnce();
+ expect(nextMenu.popup).toHaveBeenCalledWith(position);
+ });
+
+ it.each(["creation", "popup"])(
+ "allows another menu after %s fails",
+ async (failure) => {
+ const show = createRecordingMenuPopup();
+ const position = new LogicalPosition(10, 20);
+ const error = new Error("Native menu unavailable");
+ const failedFactory = async () => {
+ if (failure === "creation") throw error;
+ return { popup: vi.fn().mockRejectedValue(error) };
+ };
+ await expect(show(failedFactory, position)).rejects.toBe(error);
+
+ const menu = { popup: vi.fn().mockResolvedValue(undefined) };
+ await show(async () => menu, position);
+ expect(menu.popup).toHaveBeenCalledOnce();
+ expect(menu.popup).toHaveBeenCalledWith(position);
+ },
+ );
+});
diff --git a/apps/desktop/src/utils/recording-menu.ts b/apps/desktop/src/utils/recording-menu.ts
new file mode 100644
index 000000000..8b9520bbb
--- /dev/null
+++ b/apps/desktop/src/utils/recording-menu.ts
@@ -0,0 +1,20 @@
+import type { LogicalPosition } from "@tauri-apps/api/dpi";
+import type { Menu } from "@tauri-apps/api/menu";
+
+export function createRecordingMenuPopup() {
+ let active = false;
+
+ return async (
+ createMenu: () => Promise<Pick<Menu, "popup">>,
+ position: LogicalPosition,
+ ) => {
+ if (active) return;
+ active = true;
+ try {
+ const menu = await createMenu();
+ await menu.popup(position);
+ } finally {
+ active = false;
+ }
+ };
+}