From 50cd49cc311720bde87239dd90c4eaf0b8b7de7b Mon Sep 17 00:00:00 2001 From: Sameh Khamis Date: Sat, 14 Nov 2026 01:09:31 -0800 Subject: [PATCH] fix(earthquakes): a double-click follows a disc again The discs were entities, which the default renderer's double-click followed, framed to fit the disc. As draw-port items they took no part in it. --- src/layers/earthquakes/index.js | 43 +++++++++++++++++++++-- src/layers/earthquakes/ownership.test.mjs | 38 ++++++++++++++++++++ 2 files changed, 79 insertions(+), 2 deletions(-) diff --git a/src/layers/earthquakes/index.js b/src/layers/earthquakes/index.js index 3db74cf52..6a601aa44 100644 --- a/src/layers/earthquakes/index.js +++ b/src/layers/earthquakes/index.js @@ -1,5 +1,7 @@ import { fromDegrees } from '../../geo/ellipsoid.js'; -import { drawOf } from '../../render/rendererOf.js'; +import { followOnDoubleClick } from '../../data/stockFollow.js'; +import { inputFor as defaultInputFor } from '../../input/dispatcher.js'; +import { drawOf, findInput } from '../../render/rendererOf.js'; import { EARTHQUAKE_OVERLAY_SOURCE_ID, EARTHQUAKE_OVERLAY_COHORT_LIMIT, @@ -32,13 +34,20 @@ let instances = 0; * Given a Cesium viewer with no renderer bound, the * caller loads the package's `./render/cesium/unbound` export first. */ -export function createEarthquakesLayer({ source, overlayHost } = {}) { +export function createEarthquakesLayer({ + source, + overlayHost, + inputFor = defaultInputFor, +} = {}) { if (typeof source?.getSnapshot !== 'function') throw new TypeError('Earthquakes require a snapshot source'); if (!overlayHost) throw new TypeError('Earthquakes require an overlay host'); let _viewer = null; let _request = null; let _discs = null; + // Each drawn disc's centre and radius, for a double-click to follow. + let _discsById = new Map(); + let _stockFollow = null; // What the discs were drawn from, so analyst reads come from the app, not the drawing. let _events = []; let _count = 0; @@ -60,6 +69,26 @@ export function createEarthquakesLayer({ source, overlayHost } = {}) { show: false, }); _viewer = viewer; + // A double-click follows a disc, framed to fit it, as the default + // renderer followed the discs' entities (a scene that takes no input + // has no double-click). + _stockFollow = + findInput(viewer) && + followOnDoubleClick({ + viewer, + inputFor, + name: 'earthquakes', + enabled: () => _enabled, + targetFor: (hit) => { + const disc = hit.set === _discs?.id && _discsById.get(hit.id); + if (!disc) return null; + return { + key: `${hit.set}:${hit.id}`, + position: () => ({ ...disc.center }), + sphere: { center: disc.center, radius: disc.radius }, + }; + }, + }); _count = 0; _lastUpdate = null; _lastError = null; @@ -80,6 +109,7 @@ export function createEarthquakesLayer({ source, overlayHost } = {}) { _request?.abort(); _request = null; _enabled = false; + _stockFollow?.stop(); if (_discs) _discs.show = false; overlayHost.clearSource(EARTHQUAKE_OVERLAY_SOURCE_ID); overlayHost.setVisible(EARTHQUAKE_OVERLAY_SOURCE_ID, false); @@ -96,6 +126,7 @@ export function createEarthquakesLayer({ source, overlayHost } = {}) { return false; const nextDiscs = []; + const nextById = new Map(); const nextEvents = []; let count = 0; const overlayEntries = []; @@ -119,6 +150,10 @@ export function createEarthquakesLayer({ source, overlayHost } = {}) { const position = fromDegrees(lon, lat); nextEvents.push({ usgsId, mag, place, time, depthKm, lon, lat }); + nextById.set(`earthquake:${stableId}`, { + center: position, + radius: baseRadius, + }); // A filled disc on the ground, with static axes: re-tessellating // clamped ground geometry every frame is what once cost 30 fps. // No outline: clamped to the ground, Cesium never drew one. @@ -145,6 +180,7 @@ export function createEarthquakesLayer({ source, overlayHost } = {}) { } _discs.set(nextDiscs); + _discsById = nextById; _events = nextEvents; if (_enabled) { overlayHost.setEntries( @@ -181,8 +217,11 @@ export function createEarthquakesLayer({ source, overlayHost } = {}) { _enabled = false; overlayHost.clearSource(EARTHQUAKE_OVERLAY_SOURCE_ID); overlayHost.setVisible(EARTHQUAKE_OVERLAY_SOURCE_ID, false); + _stockFollow?.destroy(); + _stockFollow = null; _discs?.dispose(); _discs = null; + _discsById = new Map(); _events = []; _count = 0; _lastUpdate = null; diff --git a/src/layers/earthquakes/ownership.test.mjs b/src/layers/earthquakes/ownership.test.mjs index 45fd35dad..5c8a133b9 100644 --- a/src/layers/earthquakes/ownership.test.mjs +++ b/src/layers/earthquakes/ownership.test.mjs @@ -168,3 +168,41 @@ test('a viewer with nothing to draw with is refused at init, and init can be ret layer.destroy(); unbind(); }); + +test('a double-click follows a disc, framed to fit it, as its entity was', async (t) => { + const { bindRenderer } = await import('../../render/rendererOf.js'); + const { createStubRenderer } = await import('../../render/stub/index.js'); + const { resetPointerOwnership } = await import('../../input/dispatcher.js'); + resetPointerOwnership?.(); + const renderer = createStubRenderer(); + const viewer = { isDestroyed: () => false }; + const unbind = bindRenderer(viewer, renderer); + const layer = createEarthquakesLayer({ + source: { getSnapshot: async () => [row] }, + overlayHost: { setEntries() {}, setVisible() {}, clearSource() {} }, + }); + t.after(() => { + layer.destroy(viewer); + unbind(); + renderer.destroy(); + resetPointerOwnership?.(); + }); + layer.init(viewer); + layer.enable(viewer); + await layer.update(viewer); + const set = renderer.testing + .sets() + .find((id) => id.startsWith('earthquakes:')); + renderer.testing.scriptPicks(() => [ + { set, id: 'earthquake:event-a', surface: false }, + ]); + renderer.testing.pointer('dblclick', 10, 10); + const followed = renderer.camera.followed; + assert.ok(followed, 'the disc is followed'); + assert.equal(followed.key, `${set}:earthquake:event-a`); + const p = followed.position(0); + const lon = (Math.atan2(p.y, p.x) * 180) / Math.PI; + assert.ok(Math.abs(lon - row.lon) < 1e-9, String(lon)); + layer.disable(viewer); + assert.equal(renderer.camera.followed, null, 'hiding the layer stops it'); +});