diff --git a/ui/rounds.ts b/ui/rounds.ts index 621c7643..e1833313 100644 --- a/ui/rounds.ts +++ b/ui/rounds.ts @@ -86,6 +86,10 @@ let wanted: string | null = null; let drawn = ""; let drawnFull = ""; let redraw = () => {}; +// A pointer press is down. A row selects on `pointerdown`, so the round can open under the press, and its release, outside the dialog, +// would light-dismiss (`closedby="any"`) the dialog it just opened: the dialog waits for the release (`opensAfterPress`). +let pressed = false; +let opensAfterPress = false; // The full-size view's `` (`index.html`), modal over the map. const dialogOf = () => element("round-full") as HTMLDialogElement; @@ -97,6 +101,20 @@ export function startRounds(again: () => void) { const dialog = dialogOf(); dialog.addEventListener("close", closed); lightDismiss(dialog); + document.addEventListener( + "pointerdown", + () => { + pressed = true; + }, + true, + ); + const released = () => { + pressed = false; + // After the `click` that follows the release, which the same task dispatches. + if (opensAfterPress) setTimeout(() => redraw(), 0); + }; + document.addEventListener("pointerup", released, true); + document.addEventListener("pointercancel", released, true); } // Daniel closed the full-size view: the minimized view is back, the focus on the line of the question it showed, since the control that @@ -116,6 +134,7 @@ function closed() { // The view lets go of the full-size view: the round left, or another took it. function shut(dialog: HTMLDialogElement) { full = null; + opensAfterPress = false; drawnFull = ""; if (dialog.open) dialog.close(); dialog.replaceChildren(); @@ -428,7 +447,10 @@ export function drawRoundView(selected: Selection | null): string | null { drawnFull = state; swap(dialog, [fullHeadOf(round, session), bodyOf(round, form, session, message), barOf(round, form, session, copied)]); } - if (!dialog.open) dialog.showModal(); + if (!dialog.open) { + opensAfterPress = pressed; + if (!pressed) dialog.showModal(); + } } return key; } diff --git a/ui/tests/rounds.test.mjs b/ui/tests/rounds.test.mjs index 66f1e202..da7989a6 100644 --- a/ui/tests/rounds.test.mjs +++ b/ui/tests/rounds.test.mjs @@ -8,13 +8,15 @@ import { connect, fakeDocument, Live } from "./dom.mjs"; mock.timers.enable({ apis: ["setInterval", "Date"], now: 1_800_000_000_000 }); const page = new Map(); +// The page's document-level listeners, by event, for the tests that press and release a pointer. +const listeners = new Map(); const byId = (id) => page.get(id) ?? page.set(id, connect(document.createElement("div"))).get(id); globalThis.document = fakeDocument( { getElementById: byId, body: { dataset: {}, classList: { contains: () => true, add() {}, remove() {}, toggle() {} } }, visibilityState: "visible", - addEventListener() {}, + addEventListener: (name, run) => void listeners.set(name, run), }, Live, ); @@ -136,6 +138,19 @@ test("an open round arriving in Decision mode shows the minimized view, one line dialog().close(); }); +// Selecting on a row's `pointerdown` opens the round while the button is still down; a release outside the dialog then light-dismisses +// (`closedby="any"`) the dialog it has just opened. So the dialog waits for the release. +test("a round that opens while a pointer press is down opens after its release, not under it", async () => { + await draw(sessionOf(), [roundOf()]); + listeners.get("pointerdown")?.({ button: 0 }); + openFull(); + assert.equal(dialog().open, false, "not opened under the press"); + listeners.get("pointerup")?.({ button: 0 }); + await new Promise((done) => setTimeout(done, 5)); + assert.equal(dialog().open, true, "opened once the press is over"); + dialog().close(); +}); + // A waiting plan takes the plan view's element from the minimized round: none of the minimized view's layout stays on it. test("a waiting plan that takes the view from a minimized round drops the minimized layout", async () => { await draw(sessionOf({ id: "m1", resume_id: "m1" }), [roundOf({ session_id: "m1" })]);