Skip to content

Web: Migrate touch input to unified Pointer Events API#2799

Merged
Konstantin (terrakok) merged 3 commits into
jb-mainfrom
web-pointers
Mar 2, 2026
Merged

Web: Migrate touch input to unified Pointer Events API#2799
Konstantin (terrakok) merged 3 commits into
jb-mainfrom
web-pointers

Conversation

@terrakok

@terrakok Konstantin (terrakok) commented Feb 26, 2026

Copy link
Copy Markdown
Member

Refactor and streamline touch handling on Web

  • Replace TouchEvent handling with a more unified Pointer Event approach.
  • Remove obsolete TouchEvent-specific code and tests.
  • Adjust event propagation while maintaining browser compatibility.

Fixes https://youtrack.jetbrains.com/issue/CMP-9745

Release Notes

Fixes - Web

  • Unified touch and pointer event handling for better consistency and performance on Web.

}

keyboardModeState = KeyboardModeState.Hardware
private val activeTouchPointers = mutableMapOf<Int, TouchEventWithContainerOffset>()

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

mutableIntObjectMapOf ??

val composePointers: List<ComposeScenePointer> by lazy {
val coalesced = getCoalescedEvents(event).toList()
val list = coalesced.takeIf { it.isNotEmpty() } ?: listOf(event)
list.map { e ->

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

fastMap

"pointerup",
"pointerleave",
"pointercancel"
).forEach { name ->

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

fastForEach

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

this is not a hot path and is called once

"pointerleave",
"pointercancel"
).forEach { name ->
addTypedEvent<PointerEvent>(name, passive = false) { onPointerEvent(it) }

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Suggested change
addTypedEvent<PointerEvent>(name, passive = false) { onPointerEvent(it) }
addTypedEvent<PointerEvent>(name, passive = false, handler = ::onPointerEvent)

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.

::onPointerEvent would create a new Function instance on every iteration anyway.
If the goal is to reduce allocations, then something like this would do:

val handler = ::onPointerEvent // just one instantiation. 
... fastForEach  {
 addTypedEvent<PointerEvent>(name, passive = false, handler)
}

This particular initialization is one time call though, unlike other palces such as a handler itself.

- Replace `TouchEvent` handling with a more unified Pointer Event approach.
- Remove obsolete `TouchEvent`-specific code and tests.
- Adjust event propagation while maintaining browser compatibility.

Fixes https://youtrack.jetbrains.com/issue/CMP-9745

## Release Notes
### Fixes - Web
- Unified touch and pointer event handling for better consistency and performance on Web.
- Integrate optimized utility methods for PointerEvent processing.
- Replace `map` with `fastMap` and streamline pointer type determination.
- Use `mutableIntObjectMapOf` for active touch pointers to improve performance.
- Introduce reusable touch pointer list to reduce allocations.
- Replace inline pointer collection logic with `getCoalescedTouchPointers` for clarity and performance.
@terrakok Konstantin (terrakok) merged commit c724c68 into jb-main Mar 2, 2026
25 of 27 checks passed
@terrakok Konstantin (terrakok) deleted the web-pointers branch March 2, 2026 15:20
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.

3 participants