From d928c30337005c8d338f8443c89c83773aa28732 Mon Sep 17 00:00:00 2001 From: Sameh Khamis Date: Sat, 7 Nov 2026 03:01:47 -0800 Subject: [PATCH] Move the imagery box preview into the recent imagery renderer The box selection tool no longer builds Cesium entities itself: its fill and dashed outline live in createImageryBoxPreview in the recent imagery rendering module, and the tool takes the preview as a dependency. The cockpit contact card reads the nearest contact's position with the geo helpers. Both files leave the allow-list. Tests now inject the preview with the Cesium double, check that a second destroy returns the same promise, and pin the nose-relative bearing to a known contact. --- src/layers/recentImagery/index.test.mjs | 4 +- src/layers/recentImagery/rendering.js | 95 +++++++++++++++++++++++++ src/render/cesiumAllowList.json | 2 - src/ui/cockpitContext.js | 13 ++-- src/ui/cockpitContext.test.mjs | 35 +++++++++ src/ui/imageryBoxTool.js | 83 +++------------------ src/ui/imageryBoxTool.test.mjs | 8 ++- 7 files changed, 153 insertions(+), 87 deletions(-) diff --git a/src/layers/recentImagery/index.test.mjs b/src/layers/recentImagery/index.test.mjs index 87a5a60a0..1f32e9774 100644 --- a/src/layers/recentImagery/index.test.mjs +++ b/src/layers/recentImagery/index.test.mjs @@ -13,6 +13,7 @@ import { SPLIT_PUBLISH_MS, createRecentImageryLayer, } from './index.js'; +import { createImageryBoxPreview } from './rendering.js'; import { BOX, CANDIDATES, @@ -1399,7 +1400,8 @@ test('with SELECT BOX armed, Escape clears the preview first and the next Escape const cancels = []; const tool = initImageryBoxTool({ viewer: fakes.viewer, - cesium: fakes.cesium, + createPreview: (viewer, dragBox) => + createImageryBoxPreview(viewer, dragBox, { cesium: fakes.cesium }), pickWorld: fakes.pickWorld, documentRef: fakes.documentRef, onBox: (box) => f.layer.setBox(box), diff --git a/src/layers/recentImagery/rendering.js b/src/layers/recentImagery/rendering.js index cce0b8328..da5c0bb1d 100644 --- a/src/layers/recentImagery/rendering.js +++ b/src/layers/recentImagery/rendering.js @@ -11,6 +11,7 @@ // renderer is its Cesium side, so it brings the factories a viewer with no // renderer bound gets those ports from, and marks a tileset host as a base // the globe port drapes on. +import * as Cesium from 'cesium'; import '../../render/cesium/camera.js'; import { markBaseTileset } from '../../render/cesium/globe.js'; import { PRODUCTS, gibsTemplate } from './model.js'; @@ -235,3 +236,97 @@ export function createRecentImageryRenderer({ }, }; } + +/** + * The box tool's preview: the box being dragged, a translucent fill and a + * dashed outline on the ground (the draw port draws no dashed draped + * lines), read from `dragBox()` each frame it draws. + * @param {object} viewer + * @param {() => ({west: number, south: number, east: number, north: number}|null)} dragBox + * Degrees, or null while no drag is live. + * @param {{cesium?: object}} [options] The Cesium namespace (tests pass a double). + * @returns {{setVisible: (visible: boolean) => void, changed: () => void, destroy: () => Promise}} + */ +export function createImageryBoxPreview( + viewer, + dragBox, + { cesium = Cesium } = {}, +) { + const dataSource = new cesium.CustomDataSource( + 'gev-recent-imagery-box-preview', + ); + let attaching = Promise.resolve(viewer.dataSources.add(dataSource)).catch( + () => null, + ); + let destroyed = false; + const stroke = cesium.Color.fromCssColorString('#8be9ff'); + const previewRectangle = () => { + const box = dragBox(); + return box + ? cesium.Rectangle.fromDegrees(box.west, box.south, box.east, box.north) + : undefined; + }; + const previewOutline = () => { + const box = dragBox(); + if (!box) return []; + return cesium.Cartesian3.fromDegreesArray([ + box.west, + box.south, + box.east, + box.south, + box.east, + box.north, + box.west, + box.north, + box.west, + box.south, + ]); + }; + const fill = dataSource.entities.add({ + show: false, + rectangle: { + coordinates: new cesium.CallbackProperty(previewRectangle, false), + material: stroke.withAlpha(0.12), + classificationType: cesium.ClassificationType.BOTH, + }, + }); + // A rectangle graphic cannot draw a dashed edge on the ground, so the + // outline is its own draped polyline traced round the same corners. + const outline = dataSource.entities.add({ + show: false, + polyline: { + positions: new cesium.CallbackProperty(previewOutline, false), + width: 2, + clampToGround: true, + classificationType: cesium.ClassificationType.BOTH, + material: new cesium.PolylineDashMaterialProperty({ + color: stroke.withAlpha(0.95), + dashLength: 12, + }), + }, + }); + const requestRender = () => viewer.scene.requestRender?.(); + return { + setVisible(visible) { + fill.show = visible; + outline.show = visible; + requestRender(); + }, + /** The box moved: draw it again. */ + changed: requestRender, + /** Detach the preview; resolves once it is gone. */ + destroy() { + if (destroyed) return attaching; + destroyed = true; + dataSource.entities.removeAll(); + attaching = attaching.then(() => { + try { + viewer.dataSources.remove(dataSource, true); + } catch { + /* viewer already disposed */ + } + }); + return attaching; + }, + }; +} diff --git a/src/render/cesiumAllowList.json b/src/render/cesiumAllowList.json index 6b2a644ff..0831a9c76 100644 --- a/src/render/cesiumAllowList.json +++ b/src/render/cesiumAllowList.json @@ -111,9 +111,7 @@ "src/sky/starrySky.js", "src/ui/applicationShell.js", "src/ui/cockpitCamera.js", - "src/ui/cockpitContext.js", "src/ui/cockpitController.js", - "src/ui/imageryBoxTool.js", "src/ui/shareFollow.js", "src/voice/gevActions.js" ] diff --git a/src/ui/cockpitContext.js b/src/ui/cockpitContext.js index 1158c632b..1636489a5 100644 --- a/src/ui/cockpitContext.js +++ b/src/ui/cockpitContext.js @@ -1,5 +1,5 @@ /** Contact readouts and nearby-signal presentation for the Cockpit controller. */ -import * as Cesium from 'cesium'; +import { toLonLatH } from '../geo/index.js'; import { formatNumber, t, tn } from '../i18n/index.js'; import { unitLabel } from './unitLabels.js'; import { @@ -151,14 +151,9 @@ export function updateContext(info, heading) { Number.isFinite(info.latitude) && Number.isFinite(info.longitude) ) { - const cartographic = Cesium.Cartographic.fromCartesian(closest.position); - const bearing = cartographic - ? bearingBetweenCoordinates( - info.latitude, - info.longitude, - Cesium.Math.toDegrees(cartographic.latitude), - Cesium.Math.toDegrees(cartographic.longitude), - ) + const at = toLonLatH(closest.position); + const bearing = at + ? bearingBetweenCoordinates(info.latitude, info.longitude, at.lat, at.lon) : null; relative = relativeBearing(bearing, heading); } diff --git a/src/ui/cockpitContext.test.mjs b/src/ui/cockpitContext.test.mjs index 47d3719c7..13229733c 100644 --- a/src/ui/cockpitContext.test.mjs +++ b/src/ui/cockpitContext.test.mjs @@ -11,6 +11,7 @@ import { showContextCard, updateContext, } from './cockpitContext.js'; +import { fromDegrees } from '../geo/index.js'; import { createContextState } from './cockpitContextState.js'; import { syncWeatherToggle } from './cockpitInstruments.js'; import { setContextCollapsed } from './cockpitLayout.js'; @@ -159,6 +160,40 @@ test('a snapshot fills the card and keeps classes other code sets', async (t) => assert.equal(layouts.length, 1); }); +test('when the tracked aircraft is the subject, the bearing points at the nearest contact', async (t) => { + const tracked = { layerId: 'flights', id: 'abc123', label: 'ABC123' }; + const contact = (lon, lat) => + snapshot({ + subject: tracked, + cohorts: [ + { + id: 'flights', + label: 'Flights', + count: 1, + relationship: 'OBSERVED', + nearest: [ + { id: 'N1', distanceM: 96_000, position: fromDegrees(lon, lat) }, + ], + }, + ], + }); + const { text, owner } = fixture(t, contact(-96, 30)); + // Due east of a north-facing nose. + updateContext.call(owner, INFO, 0); + await nextTick(); + assert.equal(text('#cockpit-context-bearing'), 'R 090°'); + // Due north: inside the dead-ahead cone. + owner.snapshot = contact(-97, 31); + updateContext.call(owner, INFO, 0); + await nextTick(); + assert.equal(text('#cockpit-context-bearing'), 'AHEAD'); + // Due west of a nose turned to the north-east. + owner.snapshot = contact(-98, 30); + updateContext.call(owner, INFO, 45); + await nextTick(); + assert.equal(text('#cockpit-context-bearing'), 'L 135°'); +}); + test('a lost contact holds its readout behind the CONTACT LOST cue', async (t) => { const { $, text, owner, layouts } = fixture(t, snapshot()); updateContext.call(owner, INFO, 0); diff --git a/src/ui/imageryBoxTool.js b/src/ui/imageryBoxTool.js index 55d7c0311..8bae8a04a 100644 --- a/src/ui/imageryBoxTool.js +++ b/src/ui/imageryBoxTool.js @@ -5,7 +5,6 @@ * actions are suppressed and given back, and `destroy()` returns everything — * and holds the camera only while a drag is in progress. */ -import * as Cesium from 'cesium'; import { pickWorldFromScreen } from '../annotations/annotationResolver.js'; import { claimPointer, @@ -14,6 +13,7 @@ import { } from '../data/inputOwnership.js'; import { inputFor as defaultInputFor } from '../input/dispatcher.js'; import { validateBox } from '../layers/recentImagery/model.js'; +import { createImageryBoxPreview } from '../layers/recentImagery/rendering.js'; import { msg } from '../i18n/index.js'; /** The id this tool claims the pointer under. */ @@ -21,9 +21,11 @@ export const IMAGERY_BOX_POINTER_OWNER = 'recent-imagery-box'; /** * Wire the box tool. - * @param {{ viewer: object, onBox: (box: object) => void, onCancel?: (reason: string, message?: string, box?: object) => void, onActive?: (active: boolean) => void, onEscape?: () => boolean, cesium?: object, inputFor?: Function, pickWorld?: Function, documentRef?: object }} deps + * @param {{ viewer: object, onBox: (box: object) => void, onCancel?: (reason: string, message?: string, box?: object) => void, onActive?: (active: boolean) => void, onEscape?: () => boolean, createPreview?: Function, inputFor?: Function, pickWorld?: Function, documentRef?: object }} deps * `onEscape` runs before the tool cancels on Escape; returning `true` claims * the key (it is still consumed) and leaves the tool armed. + * `createPreview(viewer, dragBox)` draws the box being dragged + * (`createImageryBoxPreview`). * @returns {{ start: () => boolean, cancel: (reason?: string) => void, isActive: () => boolean, destroy: () => Promise } | null} */ export function initImageryBoxTool({ @@ -32,7 +34,7 @@ export function initImageryBoxTool({ onCancel, onActive, onEscape, - cesium = Cesium, + createPreview = createImageryBoxPreview, inputFor = defaultInputFor, pickWorld = pickWorldFromScreen, documentRef = globalThis.document, @@ -56,65 +58,8 @@ export function initImageryBoxTool({ north: Math.max(anchor.lat, corner.lat), }; - const dataSource = new cesium.CustomDataSource( - 'gev-recent-imagery-box-preview', - ); - let attaching = Promise.resolve(viewer.dataSources.add(dataSource)).catch( - () => null, - ); - const stroke = cesium.Color.fromCssColorString('#8be9ff'); - const previewRectangle = () => { - const box = dragBox(); - return box - ? cesium.Rectangle.fromDegrees(box.west, box.south, box.east, box.north) - : undefined; - }; - const previewOutline = () => { - const box = dragBox(); - if (!box) return []; - return cesium.Cartesian3.fromDegreesArray([ - box.west, - box.south, - box.east, - box.south, - box.east, - box.north, - box.west, - box.north, - box.west, - box.south, - ]); - }; - const fill = dataSource.entities.add({ - show: false, - rectangle: { - coordinates: new cesium.CallbackProperty(previewRectangle, false), - material: stroke.withAlpha(0.12), - classificationType: cesium.ClassificationType.BOTH, - }, - }); - // A rectangle graphic cannot draw a dashed edge on the ground, so the - // outline is its own draped polyline traced round the same corners. - const outline = dataSource.entities.add({ - show: false, - polyline: { - positions: new cesium.CallbackProperty(previewOutline, false), - width: 2, - clampToGround: true, - classificationType: cesium.ClassificationType.BOTH, - material: new cesium.PolylineDashMaterialProperty({ - color: stroke.withAlpha(0.95), - dashLength: 12, - }), - }, - }); - - const requestRender = () => viewer.scene.requestRender?.(); - const setPreview = (visible) => { - fill.show = visible; - outline.show = visible; - requestRender(); - }; + const preview = createPreview(viewer, dragBox); + const setPreview = (visible) => preview.setVisible(visible); const worldAt = (position) => { if (!position) return null; const canvas = viewer.scene.canvas; @@ -157,7 +102,7 @@ export function initImageryBoxTool({ const point = anchor && !destroyed && worldAt(event.endPosition); if (!point) return; corner = point; - requestRender(); + preview.changed(); }; const onUp = (event) => { if (!anchor || destroyed) return; @@ -246,22 +191,14 @@ export function initImageryBoxTool({ }, /** Returns everything; resolves once the preview is detached. */ destroy() { - if (destroyed) return attaching; + if (destroyed) return preview.destroy(); if (active) setActive(false); destroyed = true; releaseScene(); releasePointer(lease); lease = null; thawCamera(); - dataSource.entities.removeAll(); - attaching = attaching.then(() => { - try { - viewer.dataSources.remove(dataSource, true); - } catch { - /* viewer already disposed */ - } - }); - return attaching; + return preview.destroy(); }, }; } diff --git a/src/ui/imageryBoxTool.test.mjs b/src/ui/imageryBoxTool.test.mjs index a5cb5378a..4aa5eea97 100644 --- a/src/ui/imageryBoxTool.test.mjs +++ b/src/ui/imageryBoxTool.test.mjs @@ -11,6 +11,7 @@ import { initImageryBoxTool, } from './imageryBoxTool.js'; import { boxToolFakes } from '../layers/recentImagery/testDoubles.mjs'; +import { createImageryBoxPreview } from '../layers/recentImagery/rendering.js'; function fixture(options = {}) { resetPointerOwnership(); @@ -20,7 +21,8 @@ function fixture(options = {}) { const actives = []; const tool = initImageryBoxTool({ viewer: fakes.viewer, - cesium: fakes.cesium, + createPreview: (viewer, dragBox) => + createImageryBoxPreview(viewer, dragBox, { cesium: fakes.cesium }), pickWorld: fakes.pickWorld, documentRef: fakes.documentRef, onBox: (box) => boxes.push(box), @@ -150,7 +152,9 @@ test('destroy mid-drag restores the camera, releases the claim and detaches the f.tool.start(); f.fire('LEFT_DOWN', { position: { x: 250, y: 125 } }); assert.equal(f.viewer.dataSources.sources.length, 1); - await f.tool.destroy(); + const destroying = f.tool.destroy(); + assert.equal(f.tool.destroy(), destroying); + await destroying; assert.equal(f.camera.enableInputs, true); assert.equal(pointerOwner(), null); assert.equal(f.viewer.dataSources.sources.length, 0);