From 9ed6f11c580a17ff81e357c0445be0b8f97bdde6 Mon Sep 17 00:00:00 2001 From: Denis Groshev Date: Tue, 17 Nov 2026 16:28:04 +0100 Subject: [PATCH] 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. --- src/layers/installations/controls.js | 16 ++-- .../installations/labelBoxClick.test.mjs | 84 +++++++++++++++++++ src/layers/installations/selection.js | 14 +++- 3 files changed, 101 insertions(+), 13 deletions(-) create mode 100644 src/layers/installations/labelBoxClick.test.mjs diff --git a/src/layers/installations/controls.js b/src/layers/installations/controls.js index 393298c254..de14fc73d1 100644 --- a/src/layers/installations/controls.js +++ b/src/layers/installations/controls.js @@ -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; }, /** diff --git a/src/layers/installations/labelBoxClick.test.mjs b/src/layers/installations/labelBoxClick.test.mjs new file mode 100644 index 0000000000..d720453aa6 --- /dev/null +++ b/src/layers/installations/labelBoxClick.test.mjs @@ -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', + ); +}); diff --git a/src/layers/installations/selection.js b/src/layers/installations/selection.js index f031d0b065..906bf4d169 100644 --- a/src/layers/installations/selection.js +++ b/src/layers/installations/selection.js @@ -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);