diff --git a/src/render/cesium/credits.js b/src/render/cesium/credits.js index 2c3259cde..7fc1007f3 100644 --- a/src/render/cesium/credits.js +++ b/src/render/cesium/credits.js @@ -193,7 +193,53 @@ export function createCesiumCredits({ display, scene, report, requestRender }) { .map((credit) => `${credit.id}:${credit.showOnScreen ? 1 : 0}`) .join(','); let lastSignature = signature(); + + /** + * Everything the last frame's credits were listed from, value for value: + * the display's logo, static credits and the frame's own (each with its + * count and placement), and which of them are the page's. A frame that owes + * the same, as most do, is told without listing them. + */ + const lastInputs = []; + /** Counts each change to which credits are the page's. */ + let coreVersion = 0; + function inputsHeld() { + const { statics, screen, lightbox, logo } = creditDisplayContents( + displayLive() ? display : undefined, + ); + let same = true; + let at = 0; + const put = (value) => { + if (!Object.is(lastInputs[at], value)) { + same = false; + lastInputs[at] = value; + } + at += 1; + }; + const putElements = (elements) => { + put(elements.length); + for (const element of elements) { + put(element.credit); + put(element.count); + put(element.credit?.showOnScreen); + } + }; + put(coreVersion); + put(logo); + put(logo?.showOnScreen); + put(statics.length); + for (const credit of statics) { + put(credit); + put(credit.showOnScreen); + } + putElements(screen); + putElements(lightbox); + return same; + } + inputsHeld(); + const afterFrame = () => { + if (inputsHeld()) return; const now = signature(); if (now === lastSignature) return; lastSignature = now; @@ -210,6 +256,7 @@ export function createCesiumCredits({ display, scene, report, requestRender }) { entry = { credit: new Cesium.Credit(markup, inline), count: 0 }; shown.set(key, entry); coreCredits.add(entry.credit); + coreVersion += 1; if (displayLive()) display.addStaticCredit(entry.credit); } entry.count += 1; @@ -223,6 +270,7 @@ export function createCesiumCredits({ display, scene, report, requestRender }) { if (entry.count > 0) return; shown.delete(key); coreCredits.delete(entry.credit); + coreVersion += 1; if (displayLive()) display.removeStaticCredit(entry.credit); } diff --git a/src/render/cesium/credits.test.mjs b/src/render/cesium/credits.test.mjs index 618b28a4a..84b7ecf1f 100644 --- a/src/render/cesium/credits.test.mjs +++ b/src/render/cesium/credits.test.mjs @@ -117,6 +117,88 @@ test('per-frame credits come and go with the frames, with one change each', () = assert.equal(lists.length, 2); }); +test('a frame owing what the last one owed lists nothing; a change in order, placement or the page’s credits does', () => { + const t = setup(); + const lists = []; + t.port.on('change', (list) => lists.push(list.map((c) => c.text))); + const layer = new Cesium.Credit('Layer data', false); + let listed = 0; + layer.isIon = () => { + listed += 1; + return false; + }; + t.display.addStaticCredit(layer); + const a = new Cesium.Credit('Tiles A', false); + const b = new Cesium.Credit('Tiles B', false); + const draw = (counts) => { + t.display.draw(); + for (const [credit, count] of counts) + t.display._currentFrameCredits.lightboxCredits.values.push({ + credit, + count, + }); + for (const cb of [...t.listeners]) cb(); + }; + draw([ + [a, 2], + [b, 1], + ]); + assert.deepEqual(lists, [['Layer data', 'Tiles A', 'Tiles B']]); + listed = 0; + draw([ + [a, 2], + [b, 1], + ]); + assert.equal(listed, 0, 'the same frame again is not listed'); + assert.equal(lists.length, 1); + // B now owed by more tiles: listed first. + draw([ + [a, 1], + [b, 3], + ]); + assert.deepEqual(lists.at(-1), ['Layer data', 'Tiles B', 'Tiles A']); + // A credit moved onto the credit line. + b.showOnScreen = true; + draw([ + [a, 1], + [b, 3], + ]); + assert.equal(lists.length, 3); + // The page's credit of the same markup as a frame credit: listed as the page's. + const off = t.port.addStatic({ text: 'Page data' }); + assert.equal(lists.length, 4); + draw([ + [a, 1], + [b, 3], + ]); + const settled = lists.length; + draw([ + [a, 1], + [b, 3], + ]); + assert.equal(lists.length, settled, 'unchanged once drawn'); + off(); + draw([ + [a, 1], + [b, 3], + ]); + assert.ok(!lists.at(-1).includes('Page data')); + // The logo moved off the credit line. + const logo = new Cesium.Credit('Logo', true); + t.display._currentCesiumCredit = logo; + draw([ + [a, 1], + [b, 3], + ]); + const withLogo = lists.length; + logo.showOnScreen = false; + draw([ + [a, 1], + [b, 3], + ]); + assert.equal(lists.length, withLogo + 1); +}); + test('a static credit added outside the port is listed at once and changes on the next frame', () => { const t = setup(); const lists = [];