Skip to content

Commit ab8d56b

Browse files
fix(desktop): preserve onboarding through tracking failures
1 parent 889e30e commit ab8d56b

4 files changed

Lines changed: 63 additions & 16 deletions

File tree

‎apps/desktop/src/snapShot/MacPermissionHelper.test.ts‎

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ const mocks = vi.hoisted(() => ({
1212
send: vi.fn(),
1313
loadURL: vi.fn(),
1414
stopTracking: vi.fn(),
15+
trackingFailed: undefined as (() => void) | undefined,
1516
settingsChanged: undefined as ((state: SettingsWindow) => void) | undefined,
1617
}));
1718
const windows = vi.hoisted(
@@ -81,8 +82,12 @@ vi.mock("./MacSettingsWindow.ts", async (importOriginal) => {
8182
const actual = await importOriginal<typeof import("./MacSettingsWindow.ts")>();
8283
return {
8384
...actual,
84-
watchMacSettingsWindow: (onChange: (state: SettingsWindow) => void) => {
85+
watchMacSettingsWindow: (
86+
onChange: (state: SettingsWindow) => void,
87+
onUnavailable: () => void,
88+
) => {
8589
mocks.settingsChanged = onChange;
90+
mocks.trackingFailed = onUnavailable;
8691
return mocks.stopTracking;
8792
},
8893
};
@@ -232,3 +237,23 @@ it("docks inside Settings and hides when it is covered or closed", async () => {
232237
helper.close();
233238
expect(mocks.stopTracking).toHaveBeenCalledOnce();
234239
});
240+
241+
it("returns to onboarding when the Settings window disappears", async () => {
242+
const owner = new Electron.BrowserWindow({});
243+
owner.hide();
244+
await helper.show("accessibility", "/preload.cjs", owner, iconPaths);
245+
mocks.settingsChanged!({ x: 100, y: 100, width: 723, height: 719, frontmost: true });
246+
mocks.settingsChanged!(null);
247+
expect(owner.show).toHaveBeenCalledOnce();
248+
expect(owner.focus).toHaveBeenCalledOnce();
249+
});
250+
it("hides on tracking failure and resumes on a valid update", async () => {
251+
await open();
252+
const state = { x: 100, y: 100, width: 723, height: 719, frontmost: true };
253+
mocks.settingsChanged!(state);
254+
mocks.trackingFailed!();
255+
expect(windows[0]!.destroyed).toBe(false);
256+
expect(windows[0]!.hide).toHaveBeenCalledOnce();
257+
mocks.settingsChanged!(state);
258+
expect(windows[0]!.showInactive).toHaveBeenCalledTimes(2);
259+
});

‎apps/desktop/src/snapShot/MacPermissionHelper.ts‎

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -139,10 +139,15 @@ export class MacPermissionHelper {
139139
};
140140
let settingsWindow: SettingsWindow = null;
141141
let foundSettings = false;
142+
let trackingAvailable = true;
142143
const syncPosition = () => {
143144
if (window.isDestroyed()) return;
145+
if (!trackingAvailable) {
146+
window.hide();
147+
return;
148+
}
144149
if (!settingsWindow && foundSettings) {
145-
window.close();
150+
finish();
146151
return;
147152
}
148153
if (!settingsWindow || (!settingsWindow.frontmost && !window.isFocused())) {
@@ -181,11 +186,18 @@ export class MacPermissionHelper {
181186
`data:text/html;charset=utf-8,${encodeURIComponent(helperHtml(permission, icon.toDataURL()))}`,
182187
);
183188
if (!window.isDestroyed()) {
184-
stopTracking = watchMacSettingsWindow((current) => {
185-
settingsWindow = current;
186-
if (current) foundSettings = true;
187-
syncPosition();
188-
});
189+
stopTracking = watchMacSettingsWindow(
190+
(current) => {
191+
trackingAvailable = true;
192+
settingsWindow = current;
193+
if (current) foundSettings = true;
194+
syncPosition();
195+
},
196+
() => {
197+
trackingAvailable = false;
198+
syncPosition();
199+
},
200+
);
189201
}
190202
} catch (error) {
191203
if (!window.isDestroyed()) window.destroy();

‎apps/desktop/src/snapShot/MacSettingsWindow.test.ts‎

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,8 @@ it("places the helper inside small and large Settings windows across displays",
2828
});
2929
it("decodes partial updates and stops the one watcher process on disposal", () => {
3030
const changed = vi.fn();
31-
const stop = watchMacSettingsWindow(changed);
31+
const unavailable = vi.fn();
32+
const stop = watchMacSettingsWindow(changed, unavailable);
3233
const state = { x: 10, y: 20, width: 723, height: 719, frontmost: true };
3334
const line = JSON.stringify(state);
3435
child.stdout.emit("data", line.slice(0, 8));
@@ -40,12 +41,15 @@ it("decodes partial updates and stops the one watcher process on disposal", () =
4041
child.emit("exit", 0);
4142
expect(changed).toHaveBeenCalledTimes(2);
4243
});
43-
it("clears the anchor when the watcher fails or emits invalid bounds", () => {
44+
it("distinguishes unavailable tracking from a valid absent window", () => {
4445
const changed = vi.fn();
45-
const stop = watchMacSettingsWindow(changed);
46+
const unavailable = vi.fn();
47+
const stop = watchMacSettingsWindow(changed, unavailable);
4648
child.stdout.emit("data", '{"x":"bad"}\n');
47-
expect(changed).toHaveBeenLastCalledWith(null);
49+
expect(changed).not.toHaveBeenCalled();
50+
expect(unavailable).toHaveBeenCalledOnce();
4851
child.emit("error", new Error("spawn failed"));
49-
expect(changed).toHaveBeenCalledTimes(2);
52+
expect(changed).not.toHaveBeenCalled();
53+
expect(unavailable).toHaveBeenCalledTimes(2);
5054
stop();
5155
});

‎apps/desktop/src/snapShot/MacSettingsWindow.ts‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,10 @@ export function settingsHelperBounds(settings: NonNullable<SettingsWindow>): Ele
7070
}
7171

7272
/** Track only metadata; this does not request Accessibility or Screen Recording. */
73-
export function watchMacSettingsWindow(onChange: (window: SettingsWindow) => void): () => void {
73+
export function watchMacSettingsWindow(
74+
onChange: (window: SettingsWindow) => void,
75+
onUnavailable: () => void,
76+
): () => void {
7477
const child = NodeChildProcess.spawn(
7578
"/usr/bin/osascript",
7679
["-l", "JavaScript", "-e", SETTINGS_WINDOW_SCRIPT],
@@ -88,15 +91,18 @@ export function watchMacSettingsWindow(onChange: (window: SettingsWindow) => voi
8891
const line = pending.slice(0, end);
8992
pending = pending.slice(end + 1);
9093
if (closed) return;
94+
let settings: SettingsWindow;
9195
try {
92-
onChange(decodeSettingsWindow(line));
96+
settings = decodeSettingsWindow(line);
9397
} catch {
94-
onChange(null);
98+
onUnavailable();
99+
continue;
95100
}
101+
onChange(settings);
96102
}
97103
});
98104
const onExit = () => {
99-
if (!closed) onChange(null);
105+
if (!closed) onUnavailable();
100106
};
101107
child.on("error", onExit);
102108
child.on("exit", onExit);

0 commit comments

Comments
 (0)