diff --git a/src/adapters/browser-files/browser-file-picker.ts b/src/adapters/browser-files/browser-file-picker.ts index cfd74975..7a68dda4 100644 --- a/src/adapters/browser-files/browser-file-picker.ts +++ b/src/adapters/browser-files/browser-file-picker.ts @@ -54,7 +54,8 @@ interface DisposableFilePicker extends FilePickerPort { /** * Focus can return before some engines dispatch the file input change event. * This grace keeps the fallback from misclassifying a real selection as a - * dismissal. The native cancel event remains authoritative and immediate. + * dismissal. The native cancel event remains authoritative and immediate, and + * once a picker has seen it the fallback is no longer installed. */ export const DEFAULT_NATIVE_PICKER_FOCUS_GRACE_MS = 1_000; @@ -97,6 +98,12 @@ export class NativeInputFilePicker implements DisposableFilePicker { #pending = false; #disposed = false; #abortPending: (() => void) | undefined; + /** + * Q-6.2-8. Once this engine has dispatched a native `cancel`, dismissal is + * reported natively, so the focus heuristic would only turn a slow `change` + * (a cloud provider still fetching the file) into a false dismissal. + */ + #nativeCancelObserved = false; constructor(options: NativeInputFilePickerOptions) { this.#input = options.input; @@ -293,7 +300,10 @@ export class NativeInputFilePicker implements DisposableFilePicker { : captured, ); }; - const onCancel = (): void => dismiss(); + const onCancel = (): void => { + this.#nativeCancelObserved = true; + dismiss(); + }; const onAbort = (): void => finish(browserDataFailure("ABORTED", "FILE_SELECT")); const abortPending = (): void => @@ -319,7 +329,9 @@ export class NativeInputFilePicker implements DisposableFilePicker { this.#inputEvents.add("change", onChange, { once: true }); this.#inputEvents.add("cancel", onCancel, { once: true }); signal?.addEventListener("abort", onAbort, { once: true }); - this.#windowEvents?.add("focus", onWindowFocus); + if (!this.#nativeCancelObserved) { + this.#windowEvents?.add("focus", onWindowFocus); + } this.#abortPending = abortPending; try { this.#input.accept = pickerAcceptValue(policy); diff --git a/tests/unit/browser-file-picker.test.ts b/tests/unit/browser-file-picker.test.ts index b43fd79e..d6ee8f81 100644 --- a/tests/unit/browser-file-picker.test.ts +++ b/tests/unit/browser-file-picker.test.ts @@ -263,6 +263,60 @@ describe("browser file pickers", () => { expect(vault.activeReferenceCount).toBe(1); }); + it("stops the focus fallback once a native cancel event was observed (Q-6.2-8)", async () => { + const input = labelledFileInput(); + const selected = new File(["slow"], "slow.txt", { + type: "text/plain", + lastModified: 1, + }); + let onClick = (): void => { + queueMicrotask(() => input.dispatchEvent(new Event("cancel"))); + }; + Object.defineProperty(input, "click", { + configurable: true, + value: vi.fn(() => onClick()), + }); + const scheduled: Array<() => void> = []; + const { policies, vault } = createHarness(policyDefinition, { + createReference: () => "file:slow", + }); + const picker = new NativeInputFilePicker({ + input, + vault, + policies, + userActivation: { isActive: true }, + scheduler: { + setTimeout(callback) { + scheduled.push(callback); + return scheduled.length; + }, + clearTimeout() {}, + }, + }); + + // This engine supports the native cancel event. + expect(await picker.select({ policy })).toEqual({ + ok: true, + value: { kind: "DISMISSED" }, + }); + + // A cloud provider returns focus first and dispatches change only after + // the file has been fetched, well past the focus grace. + onClick = () => window.dispatchEvent(new Event("focus")); + const pending = picker.select({ policy }); + for (const callback of scheduled.splice(0)) callback(); + Object.defineProperty(input, "files", { + configurable: true, + value: [selected], + }); + input.dispatchEvent(new Event("change")); + + expect(await pending).toMatchObject({ + ok: true, + value: { kind: "SELECTED", files: [{ displayName: "slow.txt" }] }, + }); + }); + it("lets the native cancel event settle before the focus fallback", async () => { const input = labelledFileInput(); let fallback: (() => void) | undefined;