fix(browser-files): drop the focus fallback once native cancel is seen (Q-6.2-8)

NativeInputFilePicker installed the window-focus fallback on every select,
even in engines that dispatch a native `cancel`. A provider that returns
focus first and dispatches `change` only after fetching the file (Android
cloud providers) was reported as DISMISSED after the grace window. The picker
now remembers that it observed a native `cancel` and stops installing the
focus fallback for later selections.

Verified: new unit test (cancel seen, then focus + fallback timer + late
change) returned DISMISSED before and SELECTED now. Lane gates pass
(tests/component/router.test.tsx timed out once under full-gate load and
passed on rerun).

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
DongHyeonka
2026-09-24 13:39:59 +09:00
co-authored by Claude Opus 5.5
parent 89e90cc990
commit 4c812a4213
2 changed files with 69 additions and 3 deletions
@@ -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);
+54
View File
@@ -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;