Skip to content

Visualizer: Refresh scale bar on home button and resource focus#1064

Merged
BioCam merged 1 commit into
PyLabRobot:mainfrom
BioCam:make-visualizer-scale-bar-update-on-home-button
May 26, 2026
Merged

Visualizer: Refresh scale bar on home button and resource focus#1064
BioCam merged 1 commit into
PyLabRobot:mainfrom
BioCam:make-visualizer-scale-bar-update-on-home-button

Conversation

@BioCam
Copy link
Copy Markdown
Collaborator

@BioCam BioCam commented May 26, 2026

Closes #898.

Problem

When the home button is pressed, the scale bar keeps showing the previous zoom's value instead of refreshing to match the reset view. Clicking a resource to focus on it has the same latent bug. The other scale-dependent overlays (bullseye, WRT-bullseye, tooltip, delta lines) update correctly - only the scale bar is stuck.

Root cause

updateScaleBar was defined inside the window.addEventListener("load", ...) handler, so it was closure-scoped and invisible to the top-level fitToViewport and focusOnResource. Both guarded the call with if (typeof updateScaleBar === "function"), which silently evaluated to false in global scope. The wheel-zoom and zoom-button paths worked because their call sites live inside the same closure. The deeper cause is that the five-updater refresh sequence was duplicated at four view-mutation sites; when updateScaleBar was added later, one copy was missed - and focusOnResource carried the same latent miss.

Change

Hoist updateScaleBar to top level next to its sibling updaters, with an if (!stage) return; guard so pre-load callers stay silent. Drop the now-dead typeof checks. Extract the five-updater sequence into refreshScaleOverlays and route fitToViewport, the wheel handler, zoomByFactor, and focusOnResource through it. Any future view-mutation entry point now wires to a single helper instead of remembering five updaters.

Net: +34 / -40 in pylabrobot/visualizer/lib.js. No behaviour change to paths that already worked.

updateScaleBar was defined inside the load handler, so it was closure-
scoped and invisible to the top-level fitToViewport and focusOnResource.
Both guarded the call with `if (typeof updateScaleBar === "function")`,
which silently evaluated to false in global scope. As a result the home
button and resource-focus paths left the scale bar showing the previous
zoom's value, while the wheel-zoom path - which lives inside the same
load handler - worked correctly.

The proximate cause is the scope mismatch; the underlying cause is that
the five-updater refresh sequence (updateScaleBar, updateBullseyeScale,
updateWrtBullseyeScale, updateTooltipScale, updateDeltaLinesScale) was
duplicated at four view-mutation sites. When updateScaleBar was added,
one of the four copies was missed.

Hoist updateScaleBar to top level next to its sibling updaters and add
an `if (!stage) return;` guard so pre-load callers stay silent. Extract
the refresh sequence into refreshScaleOverlays and route fitToViewport,
the wheel handler, zoomByFactor, and focusOnResource through it. The
now-dead `typeof updateScaleBar` guards are removed, and const/let
become var to match sibling style.

Closes PyLabRobot#898.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@BioCam BioCam merged commit 2d9b77f into PyLabRobot:main May 26, 2026
21 checks passed
@BioCam BioCam deleted the make-visualizer-scale-bar-update-on-home-button branch May 26, 2026 10:45
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

New Visualizer scale bar doesn't update correctly when home button is pressed

1 participant