Skip to content

Conversation

@v-ein
Copy link
Collaborator

@v-ein v-ein commented Jan 4, 2026


name: Pull Request
about: Create a pull request to help us improve
title: Added a scroll handler
assignees: ''


Description:
This PR adds a scroll handler that fires every time the corresponding widget (a container) is scrolled, that is, when the position returned by get_x_scroll and/or get_y_scroll changes. With this handler, the "traditional" polling on get_y_scroll in item_visible_handler is not needed anymore.

Events for horizontal and vertical scroll are reported separately. If somehow the container has been scrolled in both directions (quite possible with touch screens, I think), there will be two handler calls in the frame.

The handler receives a tuple in app_data with the following contents: (widget, direction, pos, max_pos, is_scrolling).

  • widget: the container being scrolled (i.e. the item to which the handler registry is bound).
  • direction: one of dpg.mvScrollDirection_XAxis or dpg.mvScrollDirection_YAxis - can be used to filter out just one direction (I bet most users will be interested in YAxis only). One can also use dpg.mvScrollDirection_Horizontal and dpg.mvScrollDirection_Vertical, they are just synonyms to X/YAxis.
  • pos: current scroll position in pixels as returned by get_x_scroll or get_y_scroll. Note: this is the scroll position at the time the event occurred; since the handlers are queued and in a heavily loaded application may lag begind rendering, it's quite possible that pos will be different from what the get functions return. In this case, however, there will be more handler calls and they will eventually catch up with the get functions. Technically this applies to every handler, not just the scroll handler.
  • pos_max: max scroll position as returned by get_x_scroll_max or get_y_scroll_max.
  • is_scrolling: currently always False (supposed to be True while the user is holding the scrollbar). Reserved for future use - this needs support on the ImGui side, we'll see if we can add such support (technically it's trivial).

Another change in this PR is the when argument on set_x/y_scroll functions. There are two ways in ImGui to specify the scroll position; one works before rendering the container in the current frame starts, and therefore applies right in the current frame; the other sets scroll position immediately after rendering and therefore only takes effect in the next frame. Depending on how contents changes, one or the other may be necessary. Of course one could always use the former method, combined with split_frame as necessary, but I figured it would be easier to let ImGui do this on its own. The when argument lets the caller choose when to apply scroll position: right in the next frame (the former method) or with a 1-frame delay (the latter method). One can also "kill it twice" and specify mvSetScrollFlags_Both. The default is the Delayed method as it is more reliable; whereas the Now method can be used for flicker-free scrolling.

Concerning Areas:
None.

@hoffstadt hoffstadt merged commit 4f3c50e into hoffstadt:master Jan 4, 2026
4 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.

2 participants