mirror of
https://github.com/bilawalsidhu/gods-eye-view.git
synced 2026-11-19 09:42:21 +00:00
fix(click): the card or label on top takes the click in every layer
Review fixes for #1781: - One rule for cards: the world-overlay host hands its hit test to the pick registry, and every click handler reads the topmost interactive card or label once per click (`clickCard`, `isUnderOtherCard` in src/data/pickRegistry.js), so all handlers agree in any listener order. A layer whose own card or label is on top acts on it whatever is drawn under it; a layer under another layer's card neither acts nor treats the click as empty space (a tracked aircraft stays tracked, a selection stays selected); Context names nothing under a card. A selected news card over a vessel no longer opens the link and selects the vessel. - With one rule, the per-owner arbitration options are no longer read and are removed: `passive`, `expanded` and `declinesClick` pick owners, `rememberClickPickActions`, `isActedOnByOtherLayer`, card backgrounds and `cardKeepsClick`. The dispatcher is unchanged from main. - A drag that ends on a quake disc, label or card does nothing; opening the USGS page or clearing the selection needs a clean click. - A click on the map under another layer's card opens no Street Level ground image. - A CCTV card that is going away is still not the scene under it. - Docs and changelog describe the new rule.
This commit is contained in:
1 parent
7312280817
commit
bb3bf9858b
87 files changed
+1398
-1775
No files matched your search
+43
-133
@@ -26,29 +26,6 @@ const isSurface = (hit) => !hit || hit.surface === true;
|
||||
|
||||
/** @type {Map<string, (pickedId: string) => boolean>} layerId -> ownership predicate */
|
||||
const _owners = new Map();
|
||||
/**
|
||||
* Owners that claim picks but never act on a click on them (flight trails, a
|
||||
* directions route line): `true` for all of an owner's picks, or a predicate
|
||||
* naming which. Their picks are still not empty space to other layers.
|
||||
* @type {Map<string, true | ((pickedId: string) => boolean)>}
|
||||
*/
|
||||
const _passive = new Map();
|
||||
/** Owners whose click handlers also select nearby contacts. */
|
||||
const _expanded = new Set();
|
||||
/**
|
||||
* Owners whose click handlers act on none of their picks for some clicks
|
||||
* (aircraft layers ignore a dragged tap, or any tap in cockpit mode).
|
||||
* @type {Map<string, (click: object) => boolean>}
|
||||
*/
|
||||
const _declines = new Map();
|
||||
/** @type {WeakMap<object, Map<string, [string, boolean][]>>} */
|
||||
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.
|
||||
* @type {Map<string, (hit: import('../render/contract/query').Hit) => boolean>}
|
||||
*/
|
||||
const _cardBackgrounds = new Map();
|
||||
|
||||
/**
|
||||
* A hit's canonical pick id, for the ownership scan: its item's id. Layers'
|
||||
@@ -70,30 +47,11 @@ export function resolvePickId(hit) {
|
||||
* @param {string} layerId - Owning layer id (e.g. 'flights').
|
||||
* @param {(pickedId: string) => boolean} predicate - Returns true when the
|
||||
* picked primitive/entity id belongs to this layer.
|
||||
* @param {{passive?: boolean | ((pickedId: string) => boolean), expanded?: boolean, declinesClick?: (click: object) => boolean}} [options]
|
||||
* `passive`: the layer never acts on a click on these picks (all of them,
|
||||
* or those the predicate names). See {@link isActedOnByOtherLayer}.
|
||||
* `expanded`: preserve precedence for handlers that select nearby contacts,
|
||||
* through the shared contact hit or their own pick window.
|
||||
* `declinesClick`: true for a click the layer's handler ignores whatever it
|
||||
* picked (a dragged tap); for that click its picks act for nobody.
|
||||
* @returns {void}
|
||||
*/
|
||||
export function registerPickOwner(
|
||||
layerId,
|
||||
predicate,
|
||||
{ passive, expanded = false, declinesClick } = {},
|
||||
) {
|
||||
export function registerPickOwner(layerId, predicate) {
|
||||
if (!layerId || typeof predicate !== 'function') return;
|
||||
_owners.set(layerId, predicate);
|
||||
if (typeof declinesClick === 'function')
|
||||
_declines.set(layerId, declinesClick);
|
||||
else _declines.delete(layerId);
|
||||
if (expanded) _expanded.add(layerId);
|
||||
else _expanded.delete(layerId);
|
||||
if (passive === true || typeof passive === 'function')
|
||||
_passive.set(layerId, passive);
|
||||
else _passive.delete(layerId);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -103,9 +61,6 @@ export function registerPickOwner(
|
||||
*/
|
||||
export function unregisterPickOwner(layerId) {
|
||||
_owners.delete(layerId);
|
||||
_expanded.delete(layerId);
|
||||
_declines.delete(layerId);
|
||||
_passive.delete(layerId);
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -144,67 +99,6 @@ export function isOwnedByOtherLayer(layerId, pickedId) {
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param {string} pickedId
|
||||
* @param {object} [click] The click being arbitrated, if any.
|
||||
* @returns {Generator<[string, boolean]>}
|
||||
*/
|
||||
function* actingPickOwners(pickedId, click) {
|
||||
for (const [ownerId, predicate] of _owners) {
|
||||
try {
|
||||
if (!predicate(pickedId)) continue;
|
||||
if (click && _declines.get(ownerId)?.(click)) 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, click)]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 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, 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, click } = {},
|
||||
) {
|
||||
if (!pickedId) return false;
|
||||
const owners =
|
||||
clickActions.get(click)?.get(pickedId) ?? actingPickOwners(pickedId, click);
|
||||
for (const [ownerId, selectsNearby] of owners) {
|
||||
if (ownerId !== layerId && (!expanded || selectsNearby)) return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resolve a moving-contact click without letting map tiles hide a nearby touch
|
||||
* target. Exact picks always win; only a coarse primary pointer may expand a
|
||||
@@ -253,41 +147,57 @@ export function pickContact(query, position, options = {}) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Registers a layer's unowned shapes that stand aside for a card painted over
|
||||
* them (its click handler returns when a world-overlay entry is on top).
|
||||
* @param {string} layerId - Registering layer id.
|
||||
* @param {(hit: import('../render/contract/query').Hit) => boolean} predicate
|
||||
* True when a click's draw hit is one of this layer's shapes.
|
||||
* The world overlay's hit test: the topmost interactive card or label painted
|
||||
* at CSS-pixel coordinates, or null. Set by the overlay host while it runs.
|
||||
* @type {((x: number, y: number) => {sourceId: string, entryId: string}|null)|null}
|
||||
*/
|
||||
let _overlayHitTest = null;
|
||||
/** @type {WeakMap<object, {sourceId: string, entryId: string}|null>} */
|
||||
const clickCards = new WeakMap();
|
||||
|
||||
/**
|
||||
* Sets (or with null, clears) the hit test that names the card or label
|
||||
* painted at a click. The world-overlay host sets it while it runs.
|
||||
* @param {((x: number, y: number) => object|null)|null} hitTest
|
||||
* @returns {void}
|
||||
*/
|
||||
export function registerCardBackground(layerId, predicate) {
|
||||
if (!layerId || typeof predicate !== 'function') return;
|
||||
_cardBackgrounds.set(layerId, predicate);
|
||||
export function setClickCardHitTest(hitTest) {
|
||||
_overlayHitTest = typeof hitTest === 'function' ? hitTest : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Removes a layer's card-background predicate (call when its clicks stop).
|
||||
* @param {string} layerId - Registering layer id.
|
||||
* @returns {void}
|
||||
* The topmost interactive card or label painted at a click, read once per
|
||||
* click so every handler of it sees the same one whatever ran first.
|
||||
* @param {{screenPosition?: {x: number, y: number}}|null|undefined} click
|
||||
* @returns {{sourceId: string, entryId: string}|null}
|
||||
*/
|
||||
export function unregisterCardBackground(layerId) {
|
||||
_cardBackgrounds.delete(layerId);
|
||||
export function clickCard(click) {
|
||||
if (!click || typeof click !== 'object') return null;
|
||||
if (clickCards.has(click)) return clickCards.get(click);
|
||||
const at = click.screenPosition;
|
||||
let card = null;
|
||||
try {
|
||||
card = _overlayHitTest?.(at?.x, at?.y) ?? null;
|
||||
} catch {
|
||||
// a broken hit test must never break click handling
|
||||
}
|
||||
clickCards.set(click, card);
|
||||
return card;
|
||||
}
|
||||
|
||||
/**
|
||||
* True when a click's draw hit is a shape whose layer stands aside for a card
|
||||
* painted over it, so that card may act on the click.
|
||||
* @param {import('../render/contract/query').Hit|null|undefined} hit
|
||||
* True when another layer's card or label is painted on top at a click. The
|
||||
* card on top takes the click: a scene item under it does not act, and is
|
||||
* not empty space either, so the layer leaves its selection alone.
|
||||
* @param {object} click The shared dispatcher event.
|
||||
* @param {Iterable<string>} [ownSourceIds] The asking layer's overlay
|
||||
* sources: its own card on top is its click.
|
||||
* @returns {boolean}
|
||||
*/
|
||||
export function isCardBackground(hit) {
|
||||
if (isSurface(hit)) return false;
|
||||
for (const predicate of _cardBackgrounds.values()) {
|
||||
try {
|
||||
if (predicate(hit)) return true;
|
||||
} catch {
|
||||
// a broken predicate must never break click handling
|
||||
}
|
||||
}
|
||||
return false;
|
||||
export function isUnderOtherCard(click, ownSourceIds = []) {
|
||||
const card = clickCard(click);
|
||||
if (!card) return false;
|
||||
for (const sourceId of ownSourceIds)
|
||||
if (card.sourceId === sourceId) return false;
|
||||
return true;
|
||||
}
|
||||
Reference in new issue
Block a user