Skip to content

Record your screen, ship a demo. Free and open-source, GPU-accelerated, no watermarks, no subscriptions. Windows, macOS, Linux. Actively maintained.

fix(macos): bring the permissions window back after Quit & Reopen

Etienne Lescotd687a8f3de1f
diff --git a/electron/permissions/index.ts b/electron/permissions/index.ts
index 0002df9e8..60f0f4898 100644
--- a/electron/permissions/index.ts
+++ b/electron/permissions/index.ts
@@ -32,31 +32,45 @@ const STORE_FILE = "permissions.json";
* the System Settings pane instead, which still works. A lost one costs a request macOS
* silently ignores. Both leave the user with something to act on.
*/
+interface StoreFile {
+ requested?: Partial<Record<NotedKind, string>>;
+ completedAt?: string;
+}
+
function createFileStore(userData: string): PermissionsStore {
const file = path.join(userData, STORE_FILE);
- let requested: Partial<Record<NotedKind, string>> = {};
+ let state: StoreFile = {};
try {
const parsed: unknown = JSON.parse(readFileSync(file, "utf8"));
- if (parsed && typeof parsed === "object" && "requested" in parsed) {
- requested = (parsed as { requested: typeof requested }).requested ?? {};
+ if (parsed && typeof parsed === "object") {
+ state = parsed as StoreFile;
}
} catch {
// Missing or unreadable: nothing has been asked yet.
}
+ const save = (next: StoreFile) => {
+ state = next;
+ const temporary = `${file}.${process.pid}.tmp`;
+ try {
+ writeFileSync(temporary, `${JSON.stringify(state, null, 2)}\n`, "utf8");
+ renameSync(temporary, file);
+ } catch (error) {
+ // Best effort: the in-memory note still gets the rest of this launch right.
+ console.warn("[permissions] failed to persist the permissions note:", error);
+ } finally {
+ rmSync(temporary, { force: true });
+ }
+ };
+
return {
- hasRequested: (kind) => typeof requested[kind] === "string",
- markRequested: (kind) => {
- requested = { ...requested, [kind]: new Date().toISOString() };
- const temporary = `${file}.${process.pid}.tmp`;
- try {
- writeFileSync(temporary, `${JSON.stringify({ requested }, null, 2)}\n`, "utf8");
- renameSync(temporary, file);
- } catch (error) {
- // Best effort: the in-memory note still gets the rest of this launch right.
- console.warn("[permissions] failed to persist the request note:", error);
- } finally {
- rmSync(temporary, { force: true });
+ hasRequested: (kind) => typeof state.requested?.[kind] === "string",
+ markRequested: (kind) =>
+ save({ ...state, requested: { ...state.requested, [kind]: new Date().toISOString() } }),
+ isCompleted: () => typeof state.completedAt === "string",
+ markCompleted: () => {
+ if (typeof state.completedAt !== "string") {
+ save({ ...state, completedAt: new Date().toISOString() });
}
},
};
@@ -115,18 +129,23 @@ export function showPermissionsWindow(): void {
permissionsWindow = createPermissionsWindow();
permissionsWindow.on("closed", () => {
permissionsWindow = null;
+ const permissions = getMacPermissions();
+ void permissions
+ .read()
+ .then((snapshot) => permissions.noteWindowClosed(snapshot))
+ .catch(() => undefined);
});
}
-/** Opens the permissions window when recording cannot work yet. For app launch. */
+/** Opens the permissions window at launch when it belongs there (see shouldShowAtLaunch). */
export async function showPermissionsWindowIfNeeded(): Promise<void> {
// Not in the headless e2e runs either: there is no one to answer, and the probe would
// hold the window open behind every spec.
if (process.platform !== "darwin" || process.env["HEADLESS"] === "true") {
return;
}
- const snapshot = await getMacPermissions().read();
- if (snapshot.screen !== "granted" || snapshot.screenRequiresRelaunch) {
+ const permissions = getMacPermissions();
+ if (permissions.shouldShowAtLaunch(await permissions.read())) {
showPermissionsWindow();
}
}
diff --git a/electron/permissions/macPermissions.test.ts b/electron/permissions/macPermissions.test.ts
index 426bb3b53..61baf213c 100644
--- a/electron/permissions/macPermissions.test.ts
+++ b/electron/permissions/macPermissions.test.ts
@@ -9,6 +9,7 @@ import {
function setup(overrides: Partial<MacPermissionsDeps> = {}) {
const requested = new Set<NotedKind>();
+ const completed = { value: false };
const deps: MacPermissionsDeps = {
platform: "darwin",
macosMajor: 26,
@@ -24,10 +25,14 @@ function setup(overrides: Partial<MacPermissionsDeps> = {}) {
markRequested: (kind) => {
requested.add(kind);
},
+ isCompleted: () => completed.value,
+ markCompleted: () => {
+ completed.value = true;
+ },
},
...overrides,
};
- return { deps, requested, permissions: createMacPermissions(deps) };
+ return { deps, requested, completed, permissions: createMacPermissions(deps) };
}
describe("read", () => {
@@ -172,6 +177,46 @@ describe("request", () => {
});
});
+describe("shouldShowAtLaunch", () => {
+ const granted = { probeScreen: async () => ({ answered: true, granted: true }) as const };
+
+ it("shows while Screen Recording is missing", async () => {
+ const { permissions } = setup();
+ expect(permissions.shouldShowAtLaunch(await permissions.read())).toBe(true);
+ });
+
+ it("comes back after System Settings' Quit & Reopen, mid-onboarding", async () => {
+ // The grant is in and the app was relaunched by System Settings: the other rows are
+ // still waiting, and the window promised to return.
+ const { permissions, requested } = setup({ ...granted, appScreenGranted: () => true });
+ requested.add("screen");
+ expect(permissions.shouldShowAtLaunch(await permissions.read())).toBe(true);
+ });
+
+ it("stays away once the window was closed with the grant in hand", async () => {
+ const { permissions, requested } = setup({ ...granted, appScreenGranted: () => true });
+ requested.add("screen");
+ permissions.noteWindowClosed(await permissions.read());
+ expect(permissions.shouldShowAtLaunch(await permissions.read())).toBe(false);
+ });
+
+ it("does not count a close without the grant as finishing", async () => {
+ const { permissions, completed } = setup();
+ permissions.noteWindowClosed(await permissions.read());
+ expect(completed.value).toBe(false);
+ });
+
+ it("never shows to someone who held the grant before the window existed", async () => {
+ const { permissions } = setup({ ...granted, appScreenGranted: () => true });
+ expect(permissions.shouldShowAtLaunch(await permissions.read())).toBe(false);
+ });
+
+ it("never shows off macOS", async () => {
+ const { permissions } = setup({ platform: "linux" });
+ expect(permissions.shouldShowAtLaunch(await permissions.read())).toBe(false);
+ });
+});
+
describe("permissionSettingsUrl", () => {
it("uses the pane anchors that open on every supported macOS", () => {
expect(permissionSettingsUrl("screen")).toBe(
diff --git a/electron/permissions/macPermissions.ts b/electron/permissions/macPermissions.ts
index b86fd4cf6..14ee5b9be 100644
--- a/electron/permissions/macPermissions.ts
+++ b/electron/permissions/macPermissions.ts
@@ -58,6 +58,9 @@ export type NotedKind = "screen" | "accessibility";
export interface PermissionsStore {
hasRequested(kind: NotedKind): boolean;
markRequested(kind: NotedKind): void;
+ /** The window was closed with Screen Recording granted: the onboarding is over. */
+ isCompleted(): boolean;
+ markCompleted(): void;
}
export interface MacPermissionsDeps {
@@ -195,7 +198,34 @@ export function createMacPermissions(deps: MacPermissionsDeps) {
deps.store.markRequested(kind);
}
- return { read, request, openSettings, noteRequested };
+ /**
+ * Whether the permissions window belongs on screen at launch.
+ *
+ * While recording cannot work, obviously. But also after a relaunch in the middle of
+ * the onboarding: System Settings offers "Quit & Reopen" the moment Screen Recording is
+ * turned on, and the window has promised the user it comes back from that -- with
+ * Accessibility, the microphone and the camera still to go. Someone who already held
+ * the grant before this window existed never raised a prompt through it, so they do
+ * not get it on the first launch after an update.
+ */
+ function shouldShowAtLaunch(snapshot: PermissionsSnapshot): boolean {
+ if (!snapshot.supported) {
+ return false;
+ }
+ if (snapshot.screen !== "granted" || snapshot.screenRequiresRelaunch) {
+ return true;
+ }
+ return deps.store.hasRequested("screen") && !deps.store.isCompleted();
+ }
+
+ /** Called when the window closes: done once Screen Recording is in hand. */
+ function noteWindowClosed(snapshot: PermissionsSnapshot): void {
+ if (snapshot.screen === "granted") {
+ deps.store.markCompleted();
+ }
+ }
+
+ return { read, request, openSettings, noteRequested, shouldShowAtLaunch, noteWindowClosed };
}
export type MacPermissions = ReturnType<typeof createMacPermissions>;