Skip to content

Fix MouseWheelZoom target cleanup - #17640

Merged
ahocevar merged 5 commits into
openlayers:mainfrom
tayfuryldz:fix/17497-mousewheel-target-cleanup
Sep 19, 2026
Merged

ahocevar merged 5 commits into
openlayers:mainfrom
tayfuryldz:fix/17497-mousewheel-target-cleanup

Conversation

@tayfuryldz

@tayfuryldz tayfuryldz commented Sep 18, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #17497.

MouseWheelZoom now reuses the map's keyboard event handling to track the physical Control key instead of registering its own document listeners. The map also forwards keyup events to interactions.

Validated with:

  • focused MouseWheelZoom browser spec: 7 passed
  • npm run typecheck
  • ESLint on the changed files
  • git diff --check

Listen for map target changes so document-level Ctrl key handlers are removed when a target is cleared and rebound when it changes.

Fixes openlayers#17497
@chiimyen

Copy link
Copy Markdown
Contributor

Two things while reading this:

  1. The ListenerFunction / KeyboardEvent JSDoc casts that were removed in setMap() are what make e.key legal for tsc. ListenerFunction is function((Event|ol/events/Event)): (void|boolean) (src/ol/events.js), and neither of those types has a key property — with checkJs and strict enabled and src/ol/**/*.js in the include list, the arrow functions should now be rejected. Worth rerunning npm run typecheck.

  2. The description mentions rebinding the listeners when the target changes, but the added test only covers the cleared-target case. Switching the target to an element in a different owner document would exercise the rebinding half — and that's the case where the listeners currently stay behind on the old document.

Note also that no checks have reported on this branch yet, so nothing has verified the test/typecheck claims above — if that's a first-time-contributor approval thing, a maintainer may need to kick CI off.

@tayfuryldz

Copy link
Copy Markdown
Contributor Author

Thanks — both points were valid. I restored the ListenerFunction / KeyboardEvent casts and added coverage that moves the map target into an iframe document, verifies the old document no longer changes the Ctrl state, and verifies the new owner document does. Re-ran npm run typecheck, the focused browser spec (9/9), ESLint on both changed files, and git diff --check.

@ahocevar

Copy link
Copy Markdown
Member

Before I review this, please clean up description and code so it becomes human readable.

@github-actions

Copy link
Copy Markdown

@ahocevar ahocevar left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the comments in #17497 hold true, the fix for the problem that this pull request claims to fix is not in the MouseWheelZoom control, but in the Map. When setTarget(null) is called on the map, the target of all controls should be set to null, and when setTarget(target) is called, the target of the controls should be set back to the map.

@tayfuryldz

Copy link
Copy Markdown
Contributor Author

Thanks. To make sure I follow the intended lifecycle: should handleTargetChanged_() detach and reattach both controls and interactions on target changes? MouseWheelZoom is an interaction, so updating controls alone would not release its document listeners. I can move the fix entirely into Map and cover both setTarget(null) and a target switch there.

@ahocevar

Copy link
Copy Markdown
Member

Sorry, I meant interactions, not controls — and it's just a matter of calling setMap(null) and setMap(this)`.

But I took a closer look at the existing code, and it seems the MouseWheelZoom does not need its own keydown and keyup events, because the map registers them already. So a better fix would be for the MouseWheelZoom to reuse them.

@chiimyen

Copy link
Copy Markdown
Contributor

One thing worth checking before reusing the map's keyboard events here: the map registers KEYDOWN and KEYPRESS on keyboardEventTarget (src/ol/Map.js:1488-1504), but not KEYUP.

MouseWheelZoom currently clears ctrlKeyPressed_ on keyup (src/ol/interaction/MouseWheelZoom.js:205-215). The only other place that flag is cleared is the fallback in handleEvent — if (!wheelEvent.ctrlKey) { this.ctrlKeyPressed_ = false; } (:265-267) — which only runs if a further wheel event with ctrlKey === false arrives.

So if the own listeners are dropped without adding a keyup somewhere, the flag stays true after Ctrl is released, and a trackpad pinch that follows (browser-synthesized ctrlKey, no physical key) is no longer recognised as a pinch, so it loses the 3× delta multiplier — until some unrelated non-Ctrl wheel event happens to reset it. src/ol/interaction/MouseWheelZoom.js:207 is currently the only keyup listener in the library, so nothing else would pick this up.

If reuse is the way to go, the map would probably need a KEYUP entry next to those two. Related: keyboardEventTarget defaults to the target element (or the shadow host), not the document (src/ol/Map.js:1477-1486), so Ctrl presses outside the map would no longer be seen — that's a behaviour change for maps using onFocusOnly: false.

@ahocevar
ahocevar force-pushed the fix/17497-mousewheel-target-cleanup branch 2 times, most recently from beb2290 to f30a7d8 Compare September 19, 2026 15:34

@ahocevar ahocevar left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I took the liberty to change things so MapBrowserEvent handles the ctrlKey separate from the existing keydown/keypress handlers. This simplifies things quite a bit.

@ahocevar
ahocevar force-pushed the fix/17497-mousewheel-target-cleanup branch from f30a7d8 to b5fd008 Compare September 19, 2026 15:38
@ahocevar
ahocevar merged commit cb21d9d into openlayers:main Sep 19, 2026
8 checks passed
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.

Memory leak in MouseWheelZoom interaction

3 participants