Installations: a label box takes a click only when nothing else was hit

The fallback to the label box the shared overlay paints for a site now runs
only when the click hit nothing drawn (the map itself), reads the overlay in
CSS pixels, and accepts the site only when its label is the one painted on
top there: an aircraft or vessel, or another layer's label, over a site's
label keeps its own click. Tested against the overlay's hit rects, not a stub.
This commit is contained in:
Denis Groshev committed 2026-11-17 16:28:04 +01:00
1 parent be326fdcf4
commit 9ed6f11c58
3 files changed
+101 -13

No files matched your search

+7 -9
View File
@@ -204,10 +204,11 @@ export function createControls({
},
/**
* The mapped installation whose label box the shared overlay paints at a
* canvas position: what a click lands on from far out, where the dot is
* 7 px. The dot, outline and fill themselves are query port hits, read
* with {@link findByPick}. Null for anything else.
* The mapped installation whose label box the shared overlay paints on
* top at a canvas position: what a click lands on from far out, where the
* dot is 7 px. Another layer's label painted over it there wins, so this
* answers null. The dot, outline and fill themselves are query port
* hits, read with {@link findByPick}. Null for anything else.
* @param {{x: number, y: number}|null|undefined} position Canvas CSS px.
* @returns {object|null} As {@link getById}.
*/
@@ -216,14 +217,11 @@ export function createControls({
return null;
let hit = null;
try {
hit =
overlayHost?.hitTest?.(position.x, position.y, {
sourceId: LAYER_ID,
}) ?? null;
hit = overlayHost?.hitTest?.(position.x, position.y) ?? null;
} catch {
hit = null;
}
return hit ? methods.getById(hit.entryId) : null;
return hit?.sourceId === LAYER_ID ? methods.getById(hit.entryId) : null;
},
/**
@@ -0,0 +1,84 @@
// src/layers/installations/labelBoxClick.test.mjs — a click on a mapped
// installation's label box finds the site only where that label is the one
// painted on top: another layer's label over it keeps the click.
import assert from 'node:assert/strict';
import test from 'node:test';
import { createControls } from './controls.js';
import { LAYER_ID } from './policy.js';
const RECORD = Object.freeze({
id: 'base',
kind: 'installation',
name: 'Base',
class: 'base',
latitude: 10,
longitude: 20,
});
/** The installations controls over a fake overlay whose hit rects are given. */
function controlsWith(rects) {
const overlayHost = {
// As the world overlay does: the last painted rect under the point wins,
// filtered to a source when one is asked for.
hitTest(x, y, options = {}) {
for (let i = rects.length - 1; i >= 0; i--) {
const rect = rects[i];
if (options.sourceId && rect.sourceId !== options.sourceId) continue;
if (
x < rect.x ||
x > rect.x + rect.w ||
y < rect.y ||
y > rect.y + rect.h
)
continue;
return { sourceId: rect.sourceId, entryId: rect.entryId };
}
return null;
},
};
const controls = createControls({
state: {
enabled: true,
records: [RECORD],
recordById: new Map([[RECORD.id, RECORD]]),
},
services: {},
parts: { rendering: { installationSurfaceHeightM: () => 0 } },
source: {},
overlayHost,
});
return controls.methods;
}
const OWN = {
sourceId: LAYER_ID,
entryId: 'base',
x: 100,
y: 100,
w: 80,
h: 20,
};
test("a click on the site's own label box finds the site", () => {
const methods = controlsWith([OWN]);
assert.equal(methods.findAt({ x: 120, y: 110 })?.id, 'base');
assert.equal(methods.findAt({ x: 10, y: 10 }), null, 'outside every box');
assert.equal(methods.findAt(null), null);
});
test("another layer's label painted over the site's keeps the click", () => {
const methods = controlsWith([
OWN,
{ sourceId: 'flights', entryId: 'abc123', x: 110, y: 105, w: 40, h: 12 },
]);
assert.equal(
methods.findAt({ x: 120, y: 110 }),
null,
'the flight is on top',
);
assert.equal(
methods.findAt({ x: 170, y: 110 })?.id,
'base',
'where the site is on top, it is found',
);
});
+10 -4
View File
@@ -53,16 +53,22 @@ export function createSelection({
layerState.clickSubscription = input.onClick((event) => {
if (!layerState.enabled) return;
// A site's dot, outline or fill, or a named marker: each draw item
// names its installation in its `meta`. Else the label box the shared
// overlay paints for the site (controls.findAt reads its hit rects),
// which is what a click lands on from far out.
// names its installation in its `meta`. Else, only when the click hit
// nothing drawn (the map itself), the label box the shared overlay
// paints for the site on top there (controls.findAt reads its hit
// rects, in CSS px), which is what a click lands on from far out; an
// aircraft or vessel drawn over a label keeps its own click.
const hit = event.drawHit;
const id =
(hit?.set === parts.namedMarkers?.setId ||
parts.rendering.ownsSet(hit?.set)
? hit.meta?.installationId
: null) ??
parts.controls?.methods?.findAt?.(event.position)?.id ??
(event.topHit
? null
: parts.controls?.methods?.findAt?.(
event.screenPosition ?? event.position,
)?.id) ??
null;
if (id && layerState.recordById.has(id) && id !== layerState.selectedId) {
selectRecord(id);