diff --git a/docs/CURRENT-STATE.md b/docs/CURRENT-STATE.md index f492bedce2..064fa13e44 100644 --- a/docs/CURRENT-STATE.md +++ b/docs/CURRENT-STATE.md @@ -2067,9 +2067,13 @@ another layer's card or label painted over it, so a quake card or label beside one acts alone, with a mouse or a coarse pointer. Fire and CCTV cards in turn yield to a touch contact whose layer acts on it. The aircraft a layer already tracks and Street Level's position marker are passive picks, since a click on -either does nothing. Fire, CCTV and vessel cards, like the quake's, act over a -passive owner's pick (a flight trail, the directions route); with no card -there, that pick still leaves a selection alone. Context +either does nothing. The pick registry records which owners act on a click's +exact hit and touch contact when the click first reads them +(`rememberClickPickActions`), and every layer asks that record for the rest of +the click, so an aircraft that starts tracking during the click does not leave +it to a later card handler as well. Fire, CCTV and vessel cards, like the +quake's, act over a passive owner's pick (a flight trail, the directions +route); with no card there, that pick still leaves a selection alone. Context (`src/ui/entityContextBridge.js`) names what a click identifies, except under a world-overlay card or label when the item's owner does not act on that hit (`cardKeepsClick` in `src/data/pickedContext.js`): a passive route, or a fire diff --git a/src/data/aisLiveVessels.test.mjs b/src/data/aisLiveVessels.test.mjs index 25747f257c..fc2c09a3f9 100644 --- a/src/data/aisLiveVessels.test.mjs +++ b/src/data/aisLiveVessels.test.mjs @@ -1012,7 +1012,12 @@ function makeCesiumEvent() { }; } -function installWireHarness(picked, stateOverrides = {}, sceneExtras = {}) { +function installWireHarness( + picked, + stateOverrides = {}, + sceneExtras = {}, + firstClick = null, +) { const hadWindow = Object.hasOwn(globalThis, 'window'); const hadDocument = Object.hasOwn(globalThis, 'document'); const priorWindow = globalThis.window; @@ -1041,6 +1046,8 @@ function installWireHarness(picked, stateOverrides = {}, sceneExtras = {}) { scene: { pick: () => picked, ...sceneExtras }, }; const input = fakeInput(viewer); + // A subscriber that runs before the layer's own (an aircraft layer). + if (firstClick) input.input.onClick(firstClick); // The camera port other layers follow through, with no pose to read; its // followChanged event counts who listens. const followChanged = makeCesiumEvent(); @@ -1993,6 +2000,42 @@ test('vessel interaction wire: a sibling-owned pick wins without selection mutat } }); +// The aircraft layer runs first and starts tracking the plane under the +// vessel's card, making it passive. The card reads the owners that acted +// when the click resolved, so it still yields: one action. +test('vessel interaction wire: a card yields to an aircraft that starts tracking in the same click', (t) => { + let tracked = null; + registerPickOwner('test-planes', (pickedId) => pickedId === 'plane:9', { + expanded: true, + passive: (pickedId) => pickedId === tracked, + }); + t.after(() => unregisterPickOwner('test-planes')); + const harness = installWireHarness( + { id: 'plane:9' }, + { selectedRecord: null }, + {}, + (event) => { + if (event.hit?.id === 'plane:9') tracked = 'plane:9'; + }, + ); + const requests = []; + harness.windowTarget.addEventListener(WORLD_FOCUS_REQUEST_EVENT, (event) => + requests.push(event.detail), + ); + _setVesselOverlayHostForTest( + hostWithCardHit(`vessel:${harness.record.mmsi}`), + ); + try { + harness.handler.click({ position: { x: 10, y: 20 } }); + assert.equal(tracked, 'plane:9', 'the aircraft starts tracking'); + assert.equal(requests.length, 0, 'the vessel card yields'); + assert.equal(aisLiveVesselsLayer.getSelectedInfo()?.mmsi ?? null, null); + } finally { + _setVesselOverlayHostForTest(null); + harness.cleanup(); + } +}); + // A trail acts for nobody: the vessel's card painted over it still selects, // and a bare trail pick stays a no-op that keeps the selection. test('vessel interaction wire: a card over a trail selects; a bare trail is a no-op', () => { diff --git a/src/data/firmsInteraction.test.mjs b/src/data/firmsInteraction.test.mjs index 0bdf158f98..026be0a877 100644 --- a/src/data/firmsInteraction.test.mjs +++ b/src/data/firmsInteraction.test.mjs @@ -64,6 +64,7 @@ function harness({ withGlobe = false, feed = null, drilled = null, + firstClick = null, } = {}) { const hadWindow = Object.hasOwn(globalThis, 'window'); const priorWindow = globalThis.window; @@ -137,6 +138,8 @@ function harness({ if (!withGlobe) layer.attachShellServices({ imageryHost: () => ({ kind: 'none' }) }); const input = fakeInput(viewer); + // A subscriber that runs before the layer's own (an aircraft layer). + if (firstClick) input.input.onClick(firstClick); // The LOD rebuild (and with it the context-registration sweep) is inert // without a data source, so tests that drive a real refresh need one. if (withDataSource) layer.init(viewer); @@ -576,6 +579,35 @@ test('a fire card keeps a tap whose contact does not act; bare taps still clear' } }); +// The aircraft layer runs first and starts tracking the plane it was clicked +// on, which makes that plane passive. The fire card painted over it reads +// the owners that acted when the click resolved, so it still yields. +test('a fire card yields to an aircraft that starts tracking in the same click', (t) => { + const fire = makeFire(); + let tracked = null; + registerPickOwner('test-planes', (pickedId) => pickedId === 'plane:9', { + expanded: true, + passive: (pickedId) => pickedId === tracked, + }); + t.after(() => unregisterPickOwner('test-planes')); + const h = harness({ + fires: [fire], + picked: { id: 'plane:9' }, + cardHit: `fire:${fireDetectionKey(fire)}`, + firstClick: (event) => { + if (event.hit?.id === 'plane:9') tracked = 'plane:9'; + }, + }); + try { + h.click(); + assert.equal(tracked, 'plane:9', 'the aircraft starts tracking'); + assert.equal(h.requests.length, 0, 'the fire card yields'); + assert.equal(h.layer.getSelectedInfo(), null); + } finally { + h.cleanup(); + } +}); + test('a stale card id resolves to nothing instead of an arbitrary fire', () => { const h = harness({ picked: { primitive: {}, content: {}, featureId: 0 }, diff --git a/src/data/pickRegistry.js b/src/data/pickRegistry.js index b16264f71f..9b4ef36047 100644 --- a/src/data/pickRegistry.js +++ b/src/data/pickRegistry.js @@ -35,6 +35,8 @@ const _owners = new Map(); const _passive = new Map(); /** Owners whose click handlers also select nearby contacts. */ const _expanded = new Set(); +/** @type {WeakMap>} */ +const clickActions = new WeakMap(); /** * Layers whose shapes are background to any card painted over them: their * click handlers stand aside under a card, so the card's layer may act there. @@ -130,33 +132,58 @@ export function isOwnedByOtherLayer(layerId, pickedId) { return false; } +/** @param {string} pickedId @returns {Generator<[string, boolean]>} */ +function* actingPickOwners(pickedId) { + for (const [ownerId, predicate] of _owners) { + try { + if (!predicate(pickedId)) continue; + const passive = _passive.get(ownerId); + if (passive === true) continue; + if (typeof passive === 'function' && passive(pickedId)) continue; + yield [ownerId, _expanded.has(ownerId)]; + } catch { + // a broken predicate must never break click handling + } + } +} + +/** + * Hold the acting owners as soon as a click resolves a hit. A handler can + * start tracking the hit and make it passive before later handlers run. + * @param {object} click The shared dispatcher event. + * @param {import('../render/contract/query').Hit|null} hit The resolved hit. + */ +export function rememberClickPickActions(click, hit) { + const pickedId = resolvePickId(hit); + if (!pickedId) return; + let actions = clickActions.get(click); + if (!actions) clickActions.set(click, (actions = new Map())); + if (!actions.has(pickedId)) { + actions.set(pickedId, [...actingPickOwners(pickedId)]); + } +} + /** * True when some OTHER registered layer owns the picked id and acts on a * click on it: {@link isOwnedByOtherLayer} without passive owners. A card * painted over a passive pick (a flight trail) is still the card's click. * @param {string} layerId - The asking layer's id (excluded from the scan). * @param {string} pickedId - Picked primitive/entity id. - * @param {{expanded?: boolean}} [options] A nearby rather than exact hit: - * only owners whose handlers select nearby contacts take precedence. + * @param {{expanded?: boolean, click?: object}} [options] For a nearby hit, + * only expanded owners take precedence. Pass the shared click to use the + * acting owners from when it first resolved this hit. * @returns {boolean} */ export function isActedOnByOtherLayer( layerId, pickedId, - { expanded = false } = {}, + { expanded = false, click } = {}, ) { if (!pickedId) return false; - for (const [ownerId, predicate] of _owners) { - if (ownerId === layerId || (expanded && !_expanded.has(ownerId))) continue; - try { - if (!predicate(pickedId)) continue; - const passive = _passive.get(ownerId); - if (passive === true) continue; - if (typeof passive === 'function' && passive(pickedId)) continue; - return true; - } catch { - // a broken predicate must never break click handling - } + const owners = + clickActions.get(click)?.get(pickedId) ?? actingPickOwners(pickedId); + for (const [ownerId, selectsNearby] of owners) { + if (ownerId !== layerId && (!expanded || selectsNearby)) return true; } return false; } diff --git a/src/data/pickedContext.js b/src/data/pickedContext.js index ed23d093ff..803075244a 100644 --- a/src/data/pickedContext.js +++ b/src/data/pickedContext.js @@ -147,5 +147,5 @@ export function cardKeepsClick(click, underCard) { const pickId = resolvePickId(click.hit); if (!pickId) return false; const expanded = pickId !== resolvePickId(click.topHit ?? null); - return !isActedOnByOtherLayer('', pickId, { expanded }); + return !isActedOnByOtherLayer('', pickId, { expanded, click }); } diff --git a/src/data/pickedContext.test.mjs b/src/data/pickedContext.test.mjs index 70ed704b4d..6d147bfeaa 100644 --- a/src/data/pickedContext.test.mjs +++ b/src/data/pickedContext.test.mjs @@ -11,7 +11,7 @@ globalThis.CustomEvent ||= class CustomEvent { globalThis.dispatchEvent ||= () => true; const { registerEntityContext } = await import('./contextStore.js'); -const { registerPickOwner, unregisterPickOwner, findPickOwner } = await import('./pickRegistry.js'); +const { registerPickOwner, unregisterPickOwner, findPickOwner, rememberClickPickActions } = await import('./pickRegistry.js'); const { resolvePickedContext, pickedKindForLayer, cardKeepsClick } = await import('./pickedContext.js'); function manager({ enabled = new Set(['flights', 'local-datacenters']), modules = {} } = {}) { @@ -147,3 +147,23 @@ test('a card keeps a click from items whose layer does not act on that hit', (t) ); assert.equal(cardKeepsClick({ topHit: null, hit: null }, true), false); }); + +// An aircraft layer can start tracking the plane it was clicked on before +// Context runs, making that plane passive. Context reads the owners that +// acted when the click resolved, so the card over it does not keep the click. +test('a card keeps a click by the owners that acted when it resolved', (t) => { + let tracked = null; + registerPickOwner('test-planes', (id) => id === 'plane:9', { + expanded: true, + passive: (id) => id === tracked, + }); + t.after(() => unregisterPickOwner('test-planes')); + const plane = { set: 'planes:1', id: 'plane:9' }; + const click = { topHit: plane, hit: plane }; + rememberClickPickActions(click, plane); + tracked = 'plane:9'; + assert.equal(cardKeepsClick(click, true), false, 'the aircraft acted'); + // A click that resolves now sees the tracked plane as passive. + const later = { topHit: { ...plane }, hit: { ...plane } }; + assert.equal(cardKeepsClick(later, true), true); +}); diff --git a/src/input/dispatcher.js b/src/input/dispatcher.js index 7d4ea04606..6387a7a1c2 100644 --- a/src/input/dispatcher.js +++ b/src/input/dispatcher.js @@ -51,6 +51,7 @@ import { import { findPickOwner, pickContact, + rememberClickPickActions, resolvePickId, } from '../data/pickRegistry.js'; import { isPickedWorldPosition } from '../data/scenePick.js'; @@ -456,7 +457,9 @@ function createClickEvent(viewer, queryOf, at, gesture) { // The adapter's own pick of this click is the renderer's pass, held // now: read later, it would be another, of a scene drawn since. void native()?.native.pick; - return topHitOf(hit, () => native()?.foreign() ?? null); + const top = topHitOf(hit, () => native()?.foreign() ?? null); + rememberClickPickActions(this, top); + return top; }); }, get drawHit() { @@ -467,7 +470,7 @@ function createClickEvent(viewer, queryOf, at, gesture) { return once('hit', () => { const q = query(); if (!q) return this.topHit; - return pickContact(q, position, { + const hit = pickContact(q, position, { picked: this.topHit, // Everything drawn there, as the adapter sees it, so a contact // search stops at what is drawn over a contact outside the draw @@ -480,6 +483,8 @@ function createClickEvent(viewer, queryOf, at, gesture) { : this.drill(limit, width, height); }, }); + rememberClickPickActions(this, hit); + return hit; }); }, get id() { diff --git a/src/layers/cctv/lifecycle.js b/src/layers/cctv/lifecycle.js index 561fc503cc..2e3da9c172 100644 --- a/src/layers/cctv/lifecycle.js +++ b/src/layers/cctv/lifecycle.js @@ -437,7 +437,7 @@ export function createLifecycle({ typeof pickedId === 'string' && Boolean(isActedOnByOtherLayer) && isOwnedByOtherLayer?.('cctv', pickedId) && - !isActedOnByOtherLayer('cctv', pickedId); + !isActedOnByOtherLayer('cctv', pickedId, { click: event }); if (pickedId !== null && !cardBackground && !passiveUnderCard) return; // A coarse tap's touch contact (not the exact pick) that its layer // acts on (an aircraft, a vessel, a radio station) is that layer's @@ -447,7 +447,10 @@ export function createLifecycle({ cardId && contactId && contactId !== pickedId && - isActedOnByOtherLayer?.('cctv', contactId, { expanded: true }) + isActedOnByOtherLayer?.('cctv', contactId, { + expanded: true, + click: event, + }) ) return; if (cardHit && cardHit.sourceId !== CCTV_OVERLAY_SOURCE_ID) return; diff --git a/src/layers/cctv/lifecycle.test.mjs b/src/layers/cctv/lifecycle.test.mjs index ea43a55990..cf1c3f98bd 100644 --- a/src/layers/cctv/lifecycle.test.mjs +++ b/src/layers/cctv/lifecycle.test.mjs @@ -88,6 +88,12 @@ test('world clicks respect the painted top card and registered card backgrounds' }; let drilled = []; const input = fakeInput(viewer); + // An aircraft layer that runs before ours and starts tracking the plane + // it was clicked on, which makes that plane passive mid-click. + let tracked = null; + input.input.onClick((event) => { + if (event.hit?.id === 'plane:9') tracked = 'plane:9'; + }); const ownCard = { sourceId: CCTV_OVERLAY_SOURCE_ID, entryId: 'cam-1' }; const foreignCard = { sourceId: 'earthquake-card', @@ -145,6 +151,7 @@ test('world clicks respect the painted top card and registered card backgrounds' picking.unregisterPickOwner('area-control'); picking.unregisterPickOwner('test-trails'); picking.unregisterPickOwner('test-planes'); + picking.unregisterPickOwner('test-tracking'); picking.unregisterCardBackground('area-fixture'); }); await lifecycle.methods.init(viewer); @@ -225,6 +232,15 @@ test('world clicks respect the painted top card and registered card backgrounds' picking.unregisterPickOwner('test-planes'); if (previousMatch) globalThis.matchMedia = previousMatch; else delete globalThis.matchMedia; + // The live card reads the owners that acted when the click resolved, so it + // yields to the aircraft even though tracking made it passive meanwhile. + picking.registerPickOwner('test-tracking', (id) => id === 'plane:9', { + expanded: true, + passive: (id) => id === tracked, + }); + click({ set: 'flights:1', id: 'plane:9', surface: false }, [ownCard], []); + assert.equal(tracked, 'plane:9', 'the aircraft started tracking'); + picking.unregisterPickOwner('test-tracking'); picking.registerPickOwner('area-control', (id) => id === drone.id); click(drone, [ownCard], []); picking.unregisterPickOwner('area-control'); diff --git a/src/layers/earthquakes/index.js b/src/layers/earthquakes/index.js index 3d3984376b..93e715e03e 100644 --- a/src/layers/earthquakes/index.js +++ b/src/layers/earthquakes/index.js @@ -356,7 +356,8 @@ export function createEarthquakesLayer({ // since the dispatcher may expose distinct objects for the same pick. const expanded = 'topHit' in click && picking.resolvePickId(click.topHit) !== pickId; - if (pickId && actedOn(EARTHQUAKES_LAYER_ID, pickId, { expanded })) return; + if (pickId && actedOn(EARTHQUAKES_LAYER_ID, pickId, { expanded, click })) + return; const cardHit = overlayHost.hitTest?.(at?.x, at?.y, { sourceId: EARTHQUAKE_CARD_SOURCE_ID, }); diff --git a/src/layers/earthquakes/selection.test.mjs b/src/layers/earthquakes/selection.test.mjs index 6a2b3bf808..65f82c8745 100644 --- a/src/layers/earthquakes/selection.test.mjs +++ b/src/layers/earthquakes/selection.test.mjs @@ -701,33 +701,70 @@ test("another layer's card over a quake disc takes the click, and the disc yield } }); -test("another layer's object under the quake card takes the click", async () => { - const h = harness([quake('ak1')]); - picking.registerPickOwner('test-plane', (id) => id === 'plane-9'); - try { +for (const coarse of [false, true]) { + test(`a quake card yields while an aircraft starts tracking (${coarse ? 'coarse' : 'exact'} hit)`, async (t) => { + const previous = globalThis.matchMedia; + globalThis.matchMedia = () => ({ matches: coarse }); + t.after(() => { + if (previous) globalThis.matchMedia = previous; + else delete globalThis.matchMedia; + }); + const plane = { set: 'flights:1', id: 'plane-9' }; + let sceneHit = null; + let nearby = null; + let tracked = null; + let tracks = 0; + let input; + const h = harness([quake('ak1')], { + input(viewer) { + viewer.scene = { + pick: () => (sceneHit ? { id: sceneHit } : null), + drillPick: () => (nearby ? [{ id: nearby }] : []), + }; + input = fakeInput(viewer); + // The aircraft layer runs first and changes its passive predicate + // synchronously after reading the dispatched hit. + input.input.onClick((event) => { + const id = event.hit?.id; + if (id !== plane.id || tracked === id) return; + tracked = id; + tracks++; + }); + return input.input; + }, + }); + picking.registerPickOwner('test-plane', (id) => id === plane.id, { + expanded: true, + passive: (id) => id === tracked, + }); + t.after(() => { + picking.unregisterPickOwner('test-plane'); + h.layer.destroy(); + input?.destroy(); + }); h.layer.enable(h.viewer); await h.layer.update(h.viewer); - h.clickDisc('ak1'); + sceneHit = { set: h.discs().spec.id, id: 'earthquake:ak1' }; + input.click(); const card = h.lastCard()[0]; - // The card is painted over a plane: the flight layer acts on the click, - // so the card neither opens its link nor changes. - overlayHitCard(h, card.id, { set: 'flights:1', id: 'plane-9' }); - assert.deepEqual(h.opened, []); + h.overlayHits.set(EARTHQUAKE_CARD_SOURCE_ID, { + sourceId: EARTHQUAKE_CARD_SOURCE_ID, + entryId: card.id, + }); + sceneHit = coarse ? null : plane; + nearby = coarse ? plane : null; + + input.click(); + assert.equal(tracks, 1, 'the aircraft starts tracking'); + assert.deepEqual(h.opened, [], 'the quake card yields for the whole click'); assert.equal(h.lastCard()[0].id, card.id, 'the card stays'); - // Over open ground the card opens the USGS page. - h.clickCard(card.id); + + input.click(); + assert.equal(tracks, 1, 'already tracking the aircraft'); assert.deepEqual(h.opened, [ 'https://earthquake.usgs.gov/earthquakes/eventpage/ak1', ]); - } finally { - picking.unregisterPickOwner('test-plane'); - h.layer.destroy(); - } -}); - -/** Click this layer's card with `hit` (another layer's object) under it. */ -function overlayHitCard(h, entryId, hit) { - h.clickOverlayWith(EARTHQUAKE_CARD_SOURCE_ID, entryId, hit); + }); } for (const kind of ['card', 'label']) { diff --git a/src/layers/firms/selection.js b/src/layers/firms/selection.js index d6bcdd9cc3..6788cf64f4 100644 --- a/src/layers/firms/selection.js +++ b/src/layers/firms/selection.js @@ -51,7 +51,7 @@ export function createSelection({ if (pickedId && isOwnedByOtherLayer(id, pickedId)) { passivePick = Boolean(isActedOnByOtherLayer) && - !isActedOnByOtherLayer(id, pickedId); + !isActedOnByOtherLayer(id, pickedId, { click: event }); if (!passivePick) return; } } @@ -68,7 +68,7 @@ export function createSelection({ cardHit && contactId && contactId !== resolvePickId(picked ?? null) && - isActedOnByOtherLayer?.(id, contactId, { expanded: true }) + isActedOnByOtherLayer?.(id, contactId, { expanded: true, click: event }) ) return; if (cardHit) { diff --git a/src/layers/vessels/selection.js b/src/layers/vessels/selection.js index 7a4821dc45..1aba2248b5 100644 --- a/src/layers/vessels/selection.js +++ b/src/layers/vessels/selection.js @@ -62,7 +62,9 @@ export function createSelection({ (String(pickedId).startsWith('gev-trail:') || (Boolean(isActedOnByOtherLayer) && isOwnedByOtherLayer('ais-live-vessels', pickedId) && - !isActedOnByOtherLayer('ais-live-vessels', pickedId))); + !isActedOnByOtherLayer('ais-live-vessels', pickedId, { + click: event, + }))); if (passivePick && !ownCardUnder()) return; // A sibling layer already owns this click. Preserve the current vessel @@ -85,6 +87,7 @@ export function createSelection({ resolvePickId?.(event.topHit ?? null) !== pickedId && !isActedOnByOtherLayer('ais-live-vessels', pickedId, { expanded: true, + click: event, }); if (!nearMissOfExactOnly || !ownCardUnder()) return; }