Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.
TypeScript cross-platformelectronffmpegopen-sourceproduct-demorustscreen-capturescreen-recorderscreen-recordingscreen-studioscreencastspeech-to-texttypescriptvideo-editingvideo-editorwaylandwgpuwhisper
refactor: share the editable cursor check and format a fixed shortcut's first binding
EtienneLescot24bc56c7bd37
diff --git a/src/components/launch/LaunchWindow.tsx b/src/components/launch/LaunchWindow.tsxindex a97ab04cc..d92ebe7f4 100644--- a/src/components/launch/LaunchWindow.tsx+++ b/src/components/launch/LaunchWindow.tsx@@ -1,6 +1,7 @@import { useCallback, useEffect, useLayoutEffect, useMemo, useRef, useState } from "react";import { useI18n, useScopedT } from "@/contexts/I18nContext";import { getAvailableLocales, getLocaleName } from "@/i18n/loader";+import { offersEditableCursor } from "@/lib/editableCursorAvailability";import { loadUserPreferences, saveUserPreferences } from "@/lib/userPreferences";import { nativeBridgeClient } from "@/native";import { canRecordMicrophone } from "@/utils/platformUtils";@@ -244,31 +245,14 @@ export function LaunchWindow() {// you get two cursors. Verified against a real recording at the// time. The helper is what makes the control mean something,// because it owns the video and never asks WebRTC for anything.- const offersCursorMode =- platform === "win32" || platform === "darwin" || platform === "linux";setIsLinuxHud(platform === "linux");// Without its native helper, macOS or Linux records through the browser, which- // always bakes in the system cursor (`effectiveBrowserCursorMode`). Switching- // to the editable cursor then restores nothing: an option that changes nothing- // is not shown, so the toggle waits for the helper's answer, and a failed- // answer keeps it hidden. Windows has no such fallback and no probe.- const probe =- platform === "darwin"- ? window.electronAPI?.isNativeMacCaptureAvailable- : platform === "linux"- ? window.electronAPI?.isNativeLinuxCaptureAvailable- : undefined;- if (!probe) {- setSupportsCursorModeToggle(offersCursorMode);- return;- }- probe()- .then((result) => {- if (!cancelled) setSupportsCursorModeToggle(result.success && result.available);- })- .catch(() => {- if (!cancelled) setSupportsCursorModeToggle(false);- });+ // always bakes in the system cursor: an option that changes nothing is not+ // shown, so the toggle waits for the helper's answer, and a failed answer keeps+ // it hidden. The editor's Record mode asks the same question, in one place.+ void offersEditableCursor(platform, window.electronAPI).then((offered) => {+ if (!cancelled) setSupportsCursorModeToggle(offered);+ });}}).catch(() => {diff --git a/src/hooks/useEditableCursorAvailable.ts b/src/hooks/useEditableCursorAvailable.tsnew file mode 100644index 000000000..0042d7701--- /dev/null+++ b/src/hooks/useEditableCursorAvailable.ts@@ -0,0 +1,25 @@+import { useEffect, useState } from "react";+import { offersEditableCursor } from "@/lib/editableCursorAvailability";+import { getPlatform } from "@/utils/platformUtils";++/**+ * Whether the editable cursor is worth offering here (see `offersEditableCursor`).+ *+ * `false` until the answer arrives, so the row waits for the native helper's answer and a failed+ * one keeps it hidden: showing a control that does nothing is the worse mistake.+ */+export function useEditableCursorAvailable(): boolean {+ const [available, setAvailable] = useState(false);++ useEffect(() => {+ let cancelled = false;+ void offersEditableCursor(getPlatform(), window.electronAPI).then((offered) => {+ if (!cancelled) setAvailable(offered);+ });+ return () => {+ cancelled = true;+ };+ }, []);++ return available;+}diff --git a/src/lib/editableCursorAvailability.test.ts b/src/lib/editableCursorAvailability.test.tsnew file mode 100644index 000000000..903642732--- /dev/null+++ b/src/lib/editableCursorAvailability.test.ts@@ -0,0 +1,58 @@+import { describe, expect, it, vi } from "vitest";+import { offersEditableCursor } from "./editableCursorAvailability";++const answer = (available: boolean, success = true) => vi.fn(async () => ({ success, available }));++describe("offersEditableCursor", () => {+ // Windows has no browser fallback, so there is nothing to probe.+ it("offers it on Windows without asking a helper", async () => {+ expect(await offersEditableCursor("win32", {})).toBe(true);+ expect(await offersEditableCursor("win32", undefined)).toBe(true);+ });++ it("offers it on macOS and Linux only when the native helper answers yes", async () => {+ expect(+ await offersEditableCursor("darwin", { isNativeMacCaptureAvailable: answer(true) }),+ ).toBe(true);+ expect(+ await offersEditableCursor("linux", { isNativeLinuxCaptureAvailable: answer(true) }),+ ).toBe(true);+ });++ // Without the helper the browser records, and it always draws the system cursor into the+ // video: switching to the editable cursor restores nothing, so it is not offered.+ it("hides it when the helper is missing, or its answer is no", async () => {+ expect(+ await offersEditableCursor("darwin", { isNativeMacCaptureAvailable: answer(false) }),+ ).toBe(false);+ expect(+ await offersEditableCursor("linux", { isNativeLinuxCaptureAvailable: answer(true, false) }),+ ).toBe(false);+ });++ it("hides it when the probe itself fails", async () => {+ const failing = vi.fn(async () => {+ throw new Error("ipc down");+ });+ expect(await offersEditableCursor("darwin", { isNativeMacCaptureAvailable: failing })).toBe(+ false,+ );+ });++ it("asks the probe of its own platform", async () => {+ const mac = answer(true);+ const linux = answer(false);+ const api = { isNativeMacCaptureAvailable: mac, isNativeLinuxCaptureAvailable: linux };+ expect(await offersEditableCursor("darwin", api)).toBe(true);+ expect(await offersEditableCursor("linux", api)).toBe(false);+ expect(mac).toHaveBeenCalledTimes(1);+ expect(linux).toHaveBeenCalledTimes(1);+ });++ // A build with no preload (the browser shim) has no probe: the platform decides.+ it("falls back to the platform when there is no probe to ask", async () => {+ expect(await offersEditableCursor("darwin", undefined)).toBe(true);+ expect(await offersEditableCursor("linux", {})).toBe(true);+ expect(await offersEditableCursor("freebsd", {})).toBe(false);+ });+});diff --git a/src/lib/editableCursorAvailability.ts b/src/lib/editableCursorAvailability.tsnew file mode 100644index 000000000..4424b1ac0--- /dev/null+++ b/src/lib/editableCursorAvailability.ts@@ -0,0 +1,38 @@+/** The slice of `window.electronAPI` the check below needs. */+type NativeCaptureProbe = {+ isNativeMacCaptureAvailable?: () => Promise<{ success: boolean; available: boolean }>;+ isNativeLinuxCaptureAvailable?: () => Promise<{ success: boolean; available: boolean }>;+};++/**+ * Whether choosing the editable cursor changes anything on this machine.+ *+ * Every platform with a native capture helper can leave the system cursor out of the pixels and+ * describe it separately (`captureCursor` on Windows, `hideSystemCursor` on macOS, the portal's+ * METADATA mode on Linux). Without its helper, macOS or Linux records through the browser, which+ * always draws the system cursor into the video (`effectiveBrowserCursorMode`): switching then+ * restores nothing, and an option that changes nothing is not shown. Windows has no such+ * fallback and no probe. A probe that fails counts as no helper.+ *+ * The one answer for every surface that offers the choice (the HUD, and the editor's Record+ * mode), so they cannot disagree about when it exists.+ */+export async function offersEditableCursor(+ platform: string,+ api: NativeCaptureProbe | undefined,+): Promise<boolean> {+ const offersCursorMode = platform === "win32" || platform === "darwin" || platform === "linux";+ const probe =+ platform === "darwin"+ ? api?.isNativeMacCaptureAvailable+ : platform === "linux"+ ? api?.isNativeLinuxCaptureAvailable+ : undefined;+ if (!probe) return offersCursorMode;+ try {+ const result = await probe();+ return result.success && result.available;+ } catch {+ return false;+ }+}diff --git a/src/lib/shortcuts.test.ts b/src/lib/shortcuts.test.tsindex 6c0c2b3ca..511d7dfff 100644--- a/src/lib/shortcuts.test.ts+++ b/src/lib/shortcuts.test.ts@@ -4,6 +4,7 @@ import {FIXED_SHORTCUTS,type FixedShortcut,findConflict,+ formatFirstFixedBinding,formatFixedShortcut,mergeWithDefaults,SHORTCUT_ACTIONS,@@ -72,3 +73,17 @@ describe("formatFixedShortcut", () => {expect(formatFixedShortcut(fixed("cycleAnnotationsBackward"), true)).toBe("Shift + Tab");});});++describe("formatFirstFixedBinding", () => {+ // A tooltip shows one chip. Redo is bound twice, and the dialog row that lists both is too long.+ it("names the first binding of undo and redo, with ⌘ on macOS", () => {+ expect(formatFirstFixedBinding("undo", false)).toBe("Ctrl + Z");+ expect(formatFirstFixedBinding("redo", false)).toBe("Ctrl + Shift + Z");+ expect(formatFirstFixedBinding("redo", true)).toBe("⌘ + ⇧ + Z");+ });++ it("has nothing to show for a row without a binding, or an unknown row", () => {+ expect(formatFirstFixedBinding("panTimeline", false)).toBeUndefined();+ expect(formatFirstFixedBinding("nope", false)).toBeUndefined();+ });+});diff --git a/src/lib/shortcuts.ts b/src/lib/shortcuts.tsindex ee63a8358..03655ef11 100644--- a/src/lib/shortcuts.ts+++ b/src/lib/shortcuts.ts@@ -193,6 +193,16 @@ export function formatFixedShortcut(shortcut: FixedShortcut, isMac: boolean): streturn shortcut.bindings.map((binding) => formatBinding(binding, isMac)).join(" / ");}+/**+ * The chip a tooltip shows for a fixed shortcut: its first binding only. Redo has two+ * (Ctrl + Shift + Z, Ctrl + Y), and a tooltip names one, so the label a dialog row shows+ * (`formatFixedShortcut`) would be too long for it. `undefined` when the row has no binding.+ */+export function formatFirstFixedBinding(i18nKey: string, isMac: boolean): string | undefined {+ const first = FIXED_SHORTCUTS.find((shortcut) => shortcut.i18nKey === i18nKey)?.bindings[0];+ return first ? formatBinding(first, isMac) : undefined;+}+export function mergeWithDefaults(partial: Partial<ShortcutsConfig>): ShortcutsConfig {const merged = { ...DEFAULT_SHORTCUTS };for (const action of SHORTCUT_ACTIONS) {