admin管理员组

文章数量:1221823

I'm working on a multi-modal chat application where users send text requests, and the chatbot responds with text, images, and thumbnails. One of the requirements is to send context information about what is currently visible on the screen to the backend, including the bounds of visible elements.

Requirements for Bounds Calculation

  • The bounds should be in density-independent pixels (dp).

  • The coordinate system originates at the top-left corner of the chat window.

  • If an element is partially visible, its bounds should be clipped to only the visible portion.

  • The reported bounds should be an axis-aligned rectangle enclosing the visible part of the element.

Composable Hierarchy

ChatScreenComposable 
    ├── UIFragmentsComposable 
    │   ├── RequestFragmentsComposable  (User request) 
    │   ├── ResponseFragmentsComposable (Bot responses) 
    │       ├── ChildFragmentsComposable (Bot images, thumbnails, etc.)
  • A root fragment represents a user request.
  • A response fragment contains one or more bot responses (text). A root fragment can contain multiple responses.
  • A child fragment contains response images, thumbnails, etc. A response fragment can contain multiple child(ren) fragments.

Current Approach (Using onGloballyPositioned)

Currently, I use Modifier.onGloballyPositioned on:

  • Request fragments
  • Response fragments
  • Child fragments (inside response fragments)

I store these bounds in a map and use them later when generating the JSON payload to send to the backend.

Problem

A reviewer pointed out that onGloballyPositioned has previously caused lag in scrolling performance.

What I've Tried

  • onPlaced: Considered, but it also has performance concerns.
  • LazyListState: It provides visibility info for root fragments but does not track nested fragments (child elements inside responses).
  • onSizeChanged: Tracks size efficiently but does not provide position.

Question

What are better-performing alternatives to track visible bounds of nested elements in Jetpack Compose, without using onGloballyPositioned?

Any insights on efficient ways to track position and size while avoiding expensive recompositions would be appreciated.

本文标签: