Probable bug: the ScrollTo message sometimes scrolls to the wrong place
Closed this issue ยท 5 comments
It seems like the target scroll rect is offset by the position of the scroll widget.
To reproduce:
- Run
stack run
in this project: monomer-scroll-bug.zip - Play with the buttons. Notice that the "Scroll to bottom" button scrolls to a slightly incorrect position.
- Edit the source code to remove the "spacer" label at the top of the stack, so that the vscroll is now the top widget. Notice that the "Scroll to bottom" button now works correctly.
I could probably send a PR to fix it, if this would be helpful (after some more investigation...).
Thanks!
Hi @Dretch!
The location ScrollTo
expects is absolute, so the x and y coordinates of the child widget need to be considered. You can get those coordinates using nodeInfoFromKey
if the child widget has a key assigned, or you can listen for onChange
in scroll
that provides that information (and more) in the ScrollStatus
type.
This behavior is probably confusing. Maybe adding a ScrollToRelative
message would be a good idea (or a similar name)? Let me know; I can add it if needed. I think removing/changing the behavior of ScrollTo
could be problematic.
Hi @fjvallarino, thanks for explaining.
My use-case is that I have a scroll widget containing data-grid rows, and I want to be able to scroll to a particular row, by index. I know the position and size of each row (relative to the parent scroll widget).
You can get those coordinates using nodeInfoFromKey if the child widget has a key assigned, or you can listen for onChange in scroll that provides that information (and more) in the ScrollStatus type.
Ok, this is useful, thanks. A ScrollToRelative
message sounds like it would useful too (would make it easier for me, at least).
The selectList
widget does something similar to what you mention, providing functionality for showing the currently selected item (this is used by dropdown
to scroll to the current value when its list is shown). If the widget you want to scroll to is your children, you already have their viewport information available without needing to retrieve other data by accessing node ^. L.children
.
You may be interested in these functions:
- https://github.com/fjvallarino/monomer/blob/main/src/Monomer/Widgets/Containers/SelectList.hs#L423
- https://github.com/fjvallarino/monomer/blob/main/src/Monomer/Widgets/Containers/SelectList.hs#L428
A selectList
consists of a vstack
and a bunch of box
es containing user items. It is all wrapped by a scroll
, and since the existence and location in the widget tree is known in advance (one step above), it's easy to know where to send the ScrollTo
message.
Thanks @fjvallarino , this turned out to be pretty easy. I think I was just confused by the different coordinate systems ๐
I'm happy to close this.
Nice! I'll close the issue, then.
I'll add a note in scroll
's documentation.
Thanks!