diff --git a/bokehforge/src/bokehforge/review/static/app.js b/bokehforge/src/bokehforge/review/static/app.js index f038be0..b54b446 100644 --- a/bokehforge/src/bokehforge/review/static/app.js +++ b/bokehforge/src/bokehforge/review/static/app.js @@ -41,7 +41,7 @@ let catalogRevision = -1, lastDownload, rateTimeout, startingDownload = false; let catalogProgress = { phase: "connecting" }, catalogNotice = null; let outputLoading = false; let runOptions = "", outputState = "", candidateState = ""; -let candidateScore = null; +let candidateScore = null, candidatePreview = null; let activeImage = null, imageVersion = ""; const readyThumbs = new Map(); const panelImages = new WeakMap(); @@ -203,6 +203,7 @@ function prefetchImage(params) { } function clearImageRequest(well) { + if (well === $("candidate-image")) candidateOverlay.cancelPresentation("image"); const request = panelImages.get(well); panelImages.delete(well); if (request && !request.image.isConnected) request.image.removeAttribute("src"); @@ -241,37 +242,47 @@ function showImage(well, title, params, priority = "high") { well.append(button); } const image = previewElement(params, "panel", { title, priority }); - const request = { src, button, image, title, params, shown: null }; + const request = { src, button, image, title, shown: null, decoded: false }; panelImages.set(well, request); + const candidate = well === $("candidate-image"); const reveal = (ready) => { if (panelImages.get(well) !== request) return; - ready.classList.add("pane-preview"); - ready.alt = request.title; - const old = button.querySelector("img"); - if (old) old.replaceWith(ready); - else button.prepend(ready); - request.shown = ready; - button.classList.add("image-ready"); - button.removeAttribute("aria-disabled"); - button.setAttribute("aria-label", "Inspect " + request.title); - button.onclick = () => openLightbox(request.title, params, request.shown); - if (well === $("candidate-image")) paintCandidateOverlay(); + request.decoded = true; + const show = () => { + ready.classList.add("pane-preview"); + ready.alt = request.title; + const old = button.querySelector("img"); + if (old) old.replaceWith(ready); + else button.prepend(ready); + request.shown = ready; + button.classList.add("image-ready"); + button.removeAttribute("aria-disabled"); + button.setAttribute("aria-label", "Inspect " + request.title); + button.onclick = () => openLightbox(request.title, params, request.shown); + if (candidate) { + candidatePreview = { src, params, image: ready, host: button, contain: true }; + paintCandidateOverlay(); + } + }; + if (candidate) candidateOverlay.stage("image", () => candidateScore, show); + else show(); }; const thumbnail = readyThumbs.get(imageURL(params, "thumb")); if (thumbnail) { const fallback = copyPreview(thumbnail); fallback.decode().then(() => { - if (!request.shown) reveal(fallback); + if (!request.decoded) reveal(fallback); }, () => {}); } image.decode().then(() => reveal(image), () => { - if (panelImages.get(well) === request && !request.shown) { + if (panelImages.get(well) === request && !request.decoded) { placeholder(well, "Image unavailable", "Stackroom could not render this image."); } }); } function cancelLightboxRequests(state) { + lightboxOverlay.cancelPresentation("image"); for (const image of state.requests) image.removeAttribute("src"); state.requests.clear(); } @@ -420,19 +431,18 @@ function scheduleLightboxHoverReset(gallery) { if (!gallery.hover) return; gallery.hoverTimer = setTimeout(() => { if (activeImage?.gallery !== gallery || !$("image-dialog").open) return; - clearLightboxHover(gallery); - paintLightboxOverlays(); + restoreLightboxBase(gallery); }, LIGHTBOX_HOVER_EXIT_DELAY); } function previewLightboxEntry(entry, thumbnail) { const { gallery } = activeImage; - const shown = gallery.hover?.shown; - clearLightboxHover(gallery); if (lightboxImageKey(entry) === lightboxImageKey(activeImage)) { - paintLightboxOverlays(); + if (gallery.hover) restoreLightboxBase(gallery); return; } + const shown = gallery.hover?.shown; + clearLightboxHover(gallery); const hover = { entry, shown, request: null, events: new AbortController(), unavailable: false }; gallery.hover = hover; const { signal } = hover.events; @@ -440,9 +450,8 @@ function previewLightboxEntry(entry, thumbnail) { if (hover.shown?.entry === entry || !thumbnail.naturalWidth) return; const image = copyPreview(thumbnail.currentSrc || thumbnail.src); image.decode().then(() => { - if (signal.aborted || hover.shown?.entry === entry) return; - hover.shown = { entry, image }; - paintLightboxOverlays(); + if (signal.aborted || hover.decoded || hover.shown?.entry === entry) return; + stageLightboxHover(gallery, hover, image); }, () => {}); }; if (thumbnail.complete) useThumbnail(); @@ -454,7 +463,7 @@ function previewLightboxEntry(entry, thumbnail) { function requestLightboxHover() { const state = activeImage; const hover = state.gallery.hover; - if (!hover) return; + if (!hover || hover.events.signal.aborted) return; const variant = state.actual || state.scale > 1 ? "native" : "fit"; if (hover.variant === variant || hover.variant === "native") return; hover.variant = variant; @@ -465,8 +474,8 @@ function requestLightboxHover() { const { signal } = hover.events; image.decode().then(() => { if (signal.aborted || hover.request !== image) return; - hover.shown = { entry: hover.entry, image }; - paintLightboxOverlays(); + hover.decoded = true; + stageLightboxHover(state.gallery, hover, image); }, () => { if (signal.aborted || hover.request !== image) return; hover.unavailable = true; @@ -474,14 +483,34 @@ function requestLightboxHover() { }); } -function clearLightboxHover(gallery) { - const hover = gallery.hover; - if (!hover) return; +function stageLightboxHover(gallery, hover, image) { + lightboxOverlay.stage("hover", () => lightboxScore(gallery, hover.entry), () => { + hover.shown = { entry: hover.entry, image }; + paintLightboxOverlays(); + }); +} + +function stopLightboxHover(hover) { + lightboxOverlay.cancelPresentation("hover"); hover.events.abort(); if (hover.request && !hover.request.complete) hover.request.removeAttribute("src"); +} + +function clearLightboxHover(gallery) { + if (!gallery.hover) return; + stopLightboxHover(gallery.hover); gallery.hover = null; } +function restoreLightboxBase(gallery) { + if (!gallery.hover) return; + stopLightboxHover(gallery.hover); + lightboxOverlay.stage("hover", () => lightboxScore(gallery, gallery.baseEntry), () => { + gallery.hover = null; + paintLightboxOverlays(); + }); +} + function setLightboxTitle(title) { const heading = $("image-title"); if (heading.textContent !== title) { @@ -490,13 +519,23 @@ function setLightboxTitle(title) { } } +function lightboxScore(gallery, { params }) { + const recorded = gallery.benchmarks.get(params.stacker); + return { params, benchmark: recorded?.artifact_revision === params.rev ? recorded : null }; +} + function setLightboxImage(state, image) { - if (state.image) state.image.replaceWith(image); - else state.gallery.surface.prepend(image); - state.image = image; - state.gallery.baseEntry = state.entry; - const hover = state.gallery.hover; - if (hover && lightboxImageKey(hover.entry) === lightboxImageKey(state)) clearLightboxHover(state.gallery); + const { gallery, entry } = state; + lightboxOverlay.stage("image", () => lightboxScore(gallery, entry), () => { + const anchor = lightboxAnchor(); + if (state.image) state.image.replaceWith(image); + else gallery.surface.prepend(image); + state.image = image; + gallery.baseEntry = entry; + const hover = gallery.hover; + if (hover && lightboxImageKey(hover.entry) === lightboxImageKey(state)) clearLightboxHover(gallery); + paintLightbox(anchor); + }); } function showLightboxImage(entry, gallery, stage, previewImage, viewport = {}) { @@ -519,7 +558,6 @@ function showLightboxImage(entry, gallery, stage, previewImage, viewport = {}) { state.requests.delete(image); if (activeImage !== state || state.image) return; setLightboxImage(state, image); - paintLightbox(); }, () => state.requests.delete(image)); } requestLightbox(state.actual || state.scale > 1 ? "native" : "fit"); @@ -580,11 +618,9 @@ function requestLightbox(variant) { image.decode().then(() => { state.requests.delete(image); if (activeImage !== state || (!native && state.nativeLoaded)) return; - const anchor = lightboxAnchor(); if (native) state.nativeLoaded = true; state.error = ""; setLightboxImage(state, image); - paintLightbox(anchor); }, () => { state.requests.delete(image); if (activeImage !== state || (!native && state.nativeLoaded)) return; @@ -651,10 +687,9 @@ function paintLightboxOverlays() { divider.hidden = !showing || held; const entry = previewing ? shown.entry : gallery.baseEntry; const viewingReference = held && showing; - const recorded = gallery.benchmarks.get(entry.params.stacker); - const benchmark = recorded?.artifact_revision === entry.params.rev ? recorded : null; + const score = lightboxScore(gallery, entry); setLightboxTitle(viewingReference ? referenceTitle(reference) : entry.title); - renderLightboxBenchmark(viewingReference ? null : benchmark); + renderLightboxBenchmark(viewingReference ? null : score.benchmark); let status = state.error; if (hover) status = hover.unavailable && shown?.entry !== hover.entry ? "Preview unavailable" : ""; if (comparing && !overlay && fit?.failed && (!native || native.failed)) status = "Reference unavailable"; @@ -676,11 +711,9 @@ function paintLightboxOverlays() { clipLeft = edge; } } - const output = state.params.stacker ? { - params: state.params, benchmark: gallery.benchmarks.get(state.params.stacker), - } : null; + const output = state.params.stacker ? lightboxScore(gallery, state.entry) : null; const context = viewingReference ? null : { - params: entry.params, benchmark, host: gallery.surface, + ...score, host: gallery.surface, image: previewing ? shown.image : state.image, contain: previewing, clipLeft, }; lightboxExplanation.update(context); @@ -1050,12 +1083,11 @@ function showBenchmarkReason(button) { } function paintCandidateOverlay() { - const request = panelImages.get($("candidate-image")); - const matches = candidateScore && request?.shown && imageURL(candidateScore.params, "panel") === request.src; - const context = matches && $("comparison-reference").hidden ? { - ...candidateScore, image: request.shown, host: request.button, contain: true, - } : null; - candidateOverlay.update(candidateScore, context); + if (!candidatePreview?.image.isConnected) candidatePreview = null; + if (candidatePreview && candidateScore && candidatePreview.src === imageURL(candidateScore.params, "panel")) { + candidatePreview.benchmark = candidateScore.benchmark; + } + candidateOverlay.update(candidateScore, $("comparison-reference").hidden ? candidatePreview : null); } function renderOutput() { diff --git a/bokehforge/src/bokehforge/review/static/score-overlay.js b/bokehforge/src/bokehforge/review/static/score-overlay.js index bf600c8..7a3d241 100644 --- a/bokehforge/src/bokehforge/review/static/score-overlay.js +++ b/bokehforge/src/bokehforge/review/static/score-overlay.js @@ -71,6 +71,23 @@ function drawAttention(canvas, payload) { overlayContext(canvas, payload).drawImage(source, 0, 0, ...payload.reference_size_wh); } +function heatmapSpec(output, { mode, family, side }) { + const identity = mode === "off" ? "" : scoreExplanationKey(output); + if (!identity) return null; + const { params, benchmark } = output; + const { explanation } = benchmark; + const placement = explanation.placements[0]; + const comparison = placement?.comparison; + const label = family === "all" && comparison ? "All damage" : comparison?.families[family]?.label; + if (mode === "attention" ? !placement || !explanation.attention : !label) return null; + const key = JSON.stringify([identity, mode, ...(mode === "attention" ? [] : [family, side])]); + return { key, identity, params, benchmark, mode, family, side, label }; +} + +function loading(tile) { + return tile?.state === "loading" || tile?.state === "pending"; +} + export class ScoreOverlay { constructor(root, legendHost, settings, changeSettings, requestOverlay) { this.root = root; @@ -91,8 +108,10 @@ export class ScoreOverlay { this.canvas.hidden = true; this.canvas.setAttribute("aria-hidden", "true"); this.families = null; - this.tile = null; + this.requests = new Map(); + this.presentations = new Map(); this.drawn = null; + this.painting = false; this.picker.id = root.id + "-damage-types"; this.damage.setAttribute("popovertarget", this.picker.id); @@ -191,25 +210,35 @@ export class ScoreOverlay { } } + stage(slot, output, reveal) { + this.presentations.set(slot, { output, reveal }); + this.paintHeatmap(); + } + + cancelPresentation(slot) { + this.presentations.delete(slot); + } + scoresChanged(revision) { this.eventRevision = revision; - if (this.tile?.state === "pending") this.request(this.tile.key); + for (const tile of this.requests.values()) { + if (tile.state === "pending") this.request(tile.spec); + } } - request(key) { - this.tile?.controller.abort(); + request(spec) { + const { key, params, benchmark, mode, family, side } = spec; + this.requests.get(key)?.controller.abort(); const controller = new AbortController(); - const tile = { key, state: "loading", controller }; - this.tile = tile; + const tile = { spec, state: "loading", controller }; + this.requests.set(key, tile); const revision = this.eventRevision; - const { params, benchmark } = this.context; - const { mode, family, side } = this.settings; const query = mode === "attention" ? params : { ...params, family, side }; this.requestOverlay(mode, query, controller.signal).then((result) => { - if (this.tile !== tile || controller.signal.aborted) return; + if (controller.signal.aborted) return; if (result.status === "pending") { tile.state = "pending"; - if (this.eventRevision !== revision) this.request(key); + if (this.eventRevision !== revision) this.request(spec); } else if (result.status === "ok" && result.artifact_revision === params.rev && result.reference_id === benchmark.explanation.reference_id && result.inspection_map_sha256 === benchmark.inspection_map_sha256 && @@ -217,17 +246,26 @@ export class ScoreOverlay { : result.family === family && result.grid?.tile_size === side)) { overlayCache.set(key, result); if (overlayCache.size > 12) overlayCache.delete(overlayCache.keys().next().value); - tile.payload = result; tile.state = "ready"; } else { tile.state = result.status === "stale" ? "stale" : "error"; } this.paintHeatmap(); }, () => { - if (this.tile !== tile || controller.signal.aborted) return; + if (controller.signal.aborted) return; tile.state = "error"; this.paintHeatmap(); }); + return tile; + } + + load(spec, needed) { + if (!spec) return null; + const { key } = spec; + const cached = overlayCache.get(key) ?? (this.drawn?.key === key ? this.drawn.payload : null); + if (cached) return { spec, state: "ready", payload: cached }; + needed.add(key); + return this.requests.get(key) ?? this.request(spec); } paint() { @@ -236,26 +274,57 @@ export class ScoreOverlay { } paintHeatmap() { - const { context } = this; - const { mode, family, side, opacity } = this.settings; - const attention = mode === "attention"; - const identity = context?.image && mode !== "off" ? scoreExplanationKey(context) : ""; - const explanation = identity ? context.benchmark.explanation : null; - const placement = explanation?.placements[0]; - const comparison = placement?.comparison; - const familyLabel = family === "all" && comparison ? "All damage" : comparison?.families[family]?.label; - const available = attention ? placement && explanation.attention : familyLabel; - const key = available ? JSON.stringify([identity, mode, ...(attention ? [] : [family, side])]) : ""; - if (this.tile?.key !== key) { - this.tile?.controller.abort(); - this.tile = null; - if (key && !overlayCache.has(key)) this.request(key); + // Revealing a staged image updates the display context synchronously. + if (this.painting) return; + this.painting = true; + try { + const needed = new Set(); + let stagedTile = null; + for (const [slot, { output, reveal }] of this.presentations) { + const keepVisible = this.context?.image && this.drawn && !this.canvas.hidden && this.settings.opacity > 0; + const tile = keepVisible ? this.load(heatmapSpec(output(), this.settings), needed) : null; + if (loading(tile)) { + stagedTile = tile; + } else { + this.presentations.delete(slot); + reveal(); + } + } + const context = this.context; + const spec = context?.image ? heatmapSpec(context, this.settings) : null; + const tile = this.load(spec, needed); + for (const [key, pending] of this.requests) { + if (needed.has(key)) continue; + pending.controller.abort(); + this.requests.delete(key); + } + const statusTile = stagedTile ?? tile; + const preparing = statusTile?.spec.mode === "attention" ? "Preparing attention map…" : "Preparing damage tiles…"; + const text = statusTile && !statusTile.payload ? overlayStatus[statusTile.state] ?? preparing : ""; + if (text !== this.status.textContent) this.status.textContent = text; + this.drawHeatmap(context, tile, stagedTile); + } finally { + this.painting = false; } - const payload = key ? this.tile?.payload ?? overlayCache.get(key) : null; - const preparing = attention ? "Preparing attention map…" : "Preparing damage tiles…"; - const text = key && !payload ? overlayStatus[this.tile?.state] ?? preparing : ""; - if (text !== this.status.textContent) this.status.textContent = text; - if (!payload) { + } + + drawHeatmap(context, tile, stagedTile) { + const { spec, payload } = tile ?? {}; + if (payload) { + if (this.drawn?.key !== spec.key || this.drawn.payload !== payload) { + const { mode, label, side } = spec; + const attention = mode === "attention"; + if (attention) drawAttention(this.canvas, payload); + else drawGrid(this.canvas, payload); + this.drawn = { key: spec.key, identity: spec.identity, payload }; + this.legend.dataset.mode = mode; + this.legend.firstElementChild.textContent = attention ? "Reference attention" : label + " · " + side + " px tiles"; + const maximum = payload.damage_range?.[1] ?? 1; + Array.from(this.legend.querySelector(".damage-legend-values").children).forEach((tick, index) => { + tick.textContent = number.format(maximum * index / 4); + }); + } + } else if ((!loading(tile) && !stagedTile) || this.drawn?.identity !== scoreExplanationKey(context)) { this.canvas.hidden = this.legend.hidden = true; if (this.drawn) { this.canvas.remove(); @@ -264,18 +333,7 @@ export class ScoreOverlay { } return; } - if (this.drawn !== payload) { - if (attention) drawAttention(this.canvas, payload); - else drawGrid(this.canvas, payload); - this.drawn = payload; - this.legend.dataset.mode = mode; - this.legend.firstElementChild.textContent = attention ? "Reference attention" : familyLabel + " · " + side + " px tiles"; - const maximum = payload.damage_range?.[1] ?? 1; - Array.from(this.legend.querySelector(".damage-legend-values").children).forEach((tick, index) => { - tick.textContent = number.format(maximum * index / 4); - }); - } - this.canvas.style.opacity = opacity / 100; + this.canvas.style.opacity = this.settings.opacity / 100; this.placeOverlay(context); }