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.
This commit is contained in:
Sameh Khamis committed 2026-11-07 03:11:51 -08:00
1 parent 6ee3bb2f7d
commit d928c30337
7 files changed
+153 -87

No files matched your search

+4 -9
View File
@@ -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);
}
+35
View File
@@ -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);
+10 -73
View File
@@ -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<void> } | 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();
},
};
}
+6 -2
View File
@@ -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);