From df0afa82e14db793260c85c588482ce9a346e9a2 Mon Sep 17 00:00:00 2001 From: Sameh Khamis Date: Sat, 14 Nov 2026 01:34:13 -0800 Subject: [PATCH] fix(library): a double-click follows a view dot and zooms to a view area or coverage border again A view-bounded layer's features and a tile entry's coverage outline were entities, which the default renderer's double-click followed (points) or zoomed to (lines and areas); a new view replaced them and ended the follow. As draw-port items they took no part in it, and the outline was drawn unpickable. It is picked again, as its entities were. --- src/library/layers.js | 93 +++++++++++++++++++++++++++++----- src/library/tileLayer.test.mjs | 2 + src/library/viewLayer.test.mjs | 89 ++++++++++++++++++++++++++++++++ 3 files changed, 172 insertions(+), 12 deletions(-) diff --git a/src/library/layers.js b/src/library/layers.js index a36a49520..e69963fa9 100644 --- a/src/library/layers.js +++ b/src/library/layers.js @@ -18,8 +18,10 @@ import { drawOf, findCamera, findGlobe, + findInput, findQuery, } from '../render/rendererOf.js'; +import { drawnItemTarget, followOnDoubleClick } from '../data/stockFollow.js'; import { cssColor } from '../render/color.js'; import { markBaseTileset } from '../render/baseTilesets.js'; import { fromDegrees, toLonLatH } from '../geo/ellipsoid.js'; @@ -88,6 +90,28 @@ function libraryRowControls(getExtent, onZoom, onRemove, legend = []) { * given; otherwise the manifest's static `display.legend` (a tile layer's * colour scale). Labels travel with the colours, so colour is never alone. */ +/** + * A double-click on one of a library layer's drawn items follows it (a + * point) or zooms to it (a line or area), as the default renderer did for + * the entities they were; null on a scene that takes no input. + * @param {object} viewer + * @param {string} name The layer, for warnings. + * @param {() => boolean} enabled + * @param {(hit: {set: string, id: string}) => object|null|undefined} itemFor + * The item a hit names, if it is the layer's. + */ +function libraryDoubleClick(viewer, name, enabled, itemFor) { + const input = viewer ? findInput(viewer) : null; + if (!input) return null; + return followOnDoubleClick({ + viewer, + inputFor: () => input, + name, + enabled, + targetFor: (hit) => drawnItemTarget(hit.set, itemFor(hit)), + }); +} + export function colorByLegend(display, counts = null) { const colorBy = display?.colorBy; if (!colorBy) @@ -483,9 +507,13 @@ export function createLibraryTileLayer( let outlineCount = 0; let outlineEpoch = 0; let outlineBuilds = 0; + /** The outline's rings by item id, for a double-click to zoom to. */ + let outlineItems = new Map(); + let outlineZoom = null; function removeOutline() { outlineEpoch += 1; + outlineItems = new Map(); try { outline?.dispose(); } catch { @@ -510,27 +538,27 @@ export function createLibraryTileLayer( ); if (epoch !== outlineEpoch || !enabled || !rings.length) return; const color = cssColor(OUTLINE_COLOR); + // Pickable, as the polyline entities were: a double-click on a + // border zooms to it. const set = drawOf(viewer).lines({ id: `${id}:outline:${++outlineBuilds}`, - pickable: false, }); + const items = rings.map((ring, index) => ({ + id: String(index), + positions: ring.map(([lon, lat]) => fromDegrees(lon, lat, 0)), + widthPx: OUTLINE_WIDTH_PX, + mode: 'draped', + clampTo: 'both', + style: { kind: 'solid', color }, + })); try { - set.set( - rings.map((ring, index) => ({ - id: String(index), - positions: ring.map(([lon, lat]) => fromDegrees(lon, lat, 0)), - widthPx: OUTLINE_WIDTH_PX, - mode: 'draped', - clampTo: 'both', - style: { kind: 'solid', color }, - pickable: false, - })), - ); + set.set(items); } catch (error) { set.dispose(); throw error; } outline = set; + outlineItems = new Map(items.map((item) => [item.id, item])); outlineCount = rings.length; if (outlineMode !== 'bbox') registerDynamicCredit(viewer, NATURAL_EARTH_CREDIT); @@ -626,6 +654,13 @@ export function createLibraryTileLayer( const off = mapStackController?.subscribe?.(followMap); unsubscribeMap = typeof off === 'function' ? off : null; void drawOutline(); + if (outlineMode && !outlineZoom) + outlineZoom = libraryDoubleClick( + viewer, + id, + () => enabled, + (hit) => (hit.set === outline?.id ? outlineItems.get(hit.id) : null), + ); requestRender(`library-tiles:${entry.id}`); }, async disable() { @@ -634,6 +669,8 @@ export function createLibraryTileLayer( unsubscribeMap = null; detach(); removeOutline(); + outlineZoom?.destroy(); + outlineZoom = null; requestRender(`library-tiles:${entry.id}`); }, async destroy() { @@ -642,6 +679,8 @@ export function createLibraryTileLayer( unsubscribeMap = null; detach(); removeOutline(); + outlineZoom?.destroy(); + outlineZoom = null; viewer = null; }, getStats() { @@ -728,6 +767,9 @@ export function createLibraryViewLayer( let enabled = false; /** The sets drawing the current view's features, or null. */ let drawn = null; + /** Each build's items by set id, then item id, for a double-click. */ + const itemsOf = new WeakMap(); + let stockFollow = null; let builds = 0; let controller = null; let timer = null; @@ -758,6 +800,8 @@ export function createLibraryViewLayer( } function clear() { + // Removing the entities ended the default renderer's follow of one. + stockFollow?.stop(); disposeSets(drawn); drawn = null; loadedKey = null; @@ -829,6 +873,19 @@ export function createLibraryViewLayer( sets.fills.set(fills); sets.lines.set(lines); sets.dots.set(dots); + itemsOf.set( + sets, + new Map( + [ + [sets.fills.id, fills], + [sets.lines.id, lines], + [sets.dots.id, dots], + ].map(([setId, list]) => [ + setId, + new Map(list.map((item) => [item.id, item])), + ]), + ), + ); } catch (caught) { disposeSets(sets); throw caught; @@ -921,6 +978,8 @@ export function createLibraryViewLayer( const previous = drawn; drawn = sets; for (const set of Object.values(sets)) set.show = true; + // Replacing the entities ended the default renderer's follow of one. + if (previous) stockFollow?.stop(); disposeSets(previous); count = result.features.length; truncated = result.truncated; @@ -972,18 +1031,28 @@ export function createLibraryViewLayer( registerDynamicCredit(viewer, libraryCredit(entry)); removeMoveEnd?.(); removeMoveEnd = findCamera(viewer)?.on('moveEnd', schedule) ?? null; + stockFollow ??= libraryDoubleClick( + viewer, + id, + () => enabled, + (hit) => (drawn ? itemsOf.get(drawn)?.get(hit.set)?.get(hit.id) : null), + ); await refresh(); }, async disable() { enabled = false; stop(); clear(); + stockFollow?.destroy(); + stockFollow = null; render(); }, async destroy() { enabled = false; stop(); clear(); + stockFollow?.destroy(); + stockFollow = null; viewer = null; }, getStats() { diff --git a/src/library/tileLayer.test.mjs b/src/library/tileLayer.test.mjs index 8543ca615..79de97451 100644 --- a/src/library/tileLayer.test.mjs +++ b/src/library/tileLayer.test.mjs @@ -157,6 +157,8 @@ test('a bbox outline draws on the ground with the drape, and goes with it', asyn const [ring] = [...set.items.values()]; assert.equal(ring.mode, 'draped'); assert.equal(ring.positions.length, 5); + assert.notEqual(set.spec.pickable, false, 'picked, as its entity was'); + assert.notEqual(ring.pickable, false); assert.equal(layer.getDiagnostics().outlineRings, 1); await layer.disable(); assert.equal(set.disposed, true); diff --git a/src/library/viewLayer.test.mjs b/src/library/viewLayer.test.mjs index 3c4886521..69f0fd6af 100644 --- a/src/library/viewLayer.test.mjs +++ b/src/library/viewLayer.test.mjs @@ -324,3 +324,92 @@ test('features draw as the GeoJSON loader drew them: rhumb edges and their own s assert.ok(line.positions.length > 40); await layer.destroy(viewer); }); + +test('a double-click follows a dot and zooms to an area, as their entities were; a new view ends the follow', async (t) => { + const point = { + type: 'Feature', + geometry: { type: 'Point', coordinates: [-77.0295, 38.8905] }, + properties: { OCC_CLS: 'Government' }, + }; + const layer = createLibraryViewLayer(entry, { + source: { + loadDataset: async () => ({ + features: [polygon, point], + truncated: false, + }), + }, + debounceMs: 0, + }); + const viewer = { canvas: { clientWidth: 800, clientHeight: 600 } }; + const camera = fakeCameraPort(); + const follows = []; + const flights = []; + // A getter, defined so: Object.assign would copy its value once. + Object.defineProperty(camera.port, 'followed', { + get: () => follows.at(-1) ?? null, + }); + Object.assign(camera.port, { + follow: (target) => follows.push(target), + flyToBounds: (sphere, options) => flights.push({ sphere, options }), + controls: { minDistance: 1, maxDistance: Infinity }, + }); + let lon = -77.03; + const centre = () => fromDegrees(lon, 38.89, 0); + camera.setPose({ + lon, + lat: 38.89, + height: 2000, + position: fromDegrees(lon, 38.89, 2000), + }); + let pickAt = () => null; + let dblclick = null; + const { draw, unbind } = bindFakeDraw(viewer, { + camera: camera.port, + query: { pickEllipsoid: centre, pick: (x, y) => pickAt(x, y) }, + input: { + onPointer: (type, fn) => { + assert.equal(type, 'dblclick'); + dblclick = fn; + return () => (dblclick = null); + }, + stockEnabled: (name) => name === 'dblclickFollow', + }, + }); + t.after(unbind); + await layer.init(viewer); + await layer.enable(viewer); + const live = (role) => + [...draw.sets.values()].find( + (s) => !s.disposed && s.spec.id.endsWith(role), + ); + const at = { position: { x: 1, y: 1 } }; + + const fills = live(':fills'); + pickAt = () => ({ set: fills.spec.id, id: [...fills.items.keys()][0] }); + dblclick(at); + assert.equal(flights.length, 1, 'an area is zoomed to'); + + const dots = live(':dots'); + pickAt = () => ({ set: dots.spec.id, id: [...dots.items.keys()][0] }); + dblclick(at); + assert.equal(follows.length, 1, 'a dot is followed'); + assert.ok(camera.port.followed); + const expected = fromDegrees(-77.0295, 38.8905, 0); + const got = follows[0].position(); + assert.ok( + Math.hypot(got.x - expected.x, got.y - expected.y, got.z - expected.z) < + 1e-6, + ); + + // The camera settles elsewhere: the view is drawn anew, which ended a + // follow of an entity it replaced. + lon = -77.2; + camera.setPose({ lon, position: fromDegrees(lon, 38.89, 2000) }); + camera.emit('moveEnd'); + await new Promise((resolve) => setTimeout(resolve, 5)); + assert.equal(camera.port.followed, null); + + await layer.disable(viewer); + assert.equal(dblclick, null, 'let go when disabled'); + await layer.destroy(viewer); +});