Nifty ListBox scrollbar position [SOLVED]

Home Forum Troubleshooting GUI Nifty ListBox scrollbar position [SOLVED]

This topic contains 8 replies, has 3 voices, and was last updated by  Florin Bucur 2 years ago.

Viewing 9 posts - 1 through 9 (of 9 total)
  • Author
    Posts
  • #27557

    Florin Bucur
    28p
    Participant

    Hey all…

    I have some trouble in keeping the scroll position on a listbox element that has a vertical scroll bar.
    I think is the same problem described here https://github.com/void256/nifty-gui/issues/52 and fixed in nifty 1.3.2

    Is there any way to read the visible objects from the listbox so I can focus the list back on?

    Thanks in advance!

    #195168
    +1

    void256
    198p
    Participant

    There is no easy way to read the visible objects I think. I’m not saying it’s impossible tho :>

    BUT can you describe the problem a bit more in detail? If it’s a bug or a missing feature I’d like to fix this! :)

    #195169
    +1

    Florin Bucur
    28p
    Participant

    Thanks for the answer @void256 :)
    Here’s a little testcase:

    The number of elements is fixed, it randomly deletes an element and adds a new one every half sec.
    If you run it, you will notice is very hard to select the last element, because the scroll bar jumps up one position whenever a new element is added…

    #195170

    void256
    198p
    Participant

    Thanks for the testcase! I can understand the problem now. Unfortunately I don’t have an idea how to solve that automatically :/

    Here is what is happening:

    1. Let’s assume you’ve scrolled down to the end of the list. The last item is visible.
    2. Calling removeItemByIndex() will remove some element somewhere in the list (not necessarily an element that’s currently visible).
    3. Now the list contains one element less and Nifty will update the Scrollbar with the total number of elements in the list so that the Scrollbar can correctly resize the scrollhandle. Until now everything is fine.
    4. Calling addItem() will add an item somewhere before the last element (or actually before all elements we can currently see). Adding the element however will push the currently visible elements down one element. Which in turn makes the last element disappear.

    I think most text editors with scrollbars work the same. If you scroll so that the last line of the text is visible at the bottom and then you insert new lines of text somewhere above these last lines the end of the document will disappear.

    If for some reason you want to force the last item into view you can do that with the showItem() or showItemByIndex() methods after you’ve modified the list. But as you’ve noticed there currently is no public way to access the visible elements so that you could work around this in a more general way. Something like: remember which element is visible before you remove/add items and make sure it is visible when you’re done with your changes using showItem(). Internally this information is available (viewOffset in the ListBoxImpl class) but it’s not public accessible right now. I’m not sure if this problem justifies making this information a part of the public ListBox API considering it might just be used for this workaround :/

    I guess what you would expect is that the last item is still visible after your changes since the total count of elements has (in total) not changed.

    Not sure how to add that. Any ideas? :)

    #195171

    zarch
    692p
    Keymaster

    I’d suggest that when adding an item to the list nifty remembers the last currently visible item, and then after adding it makes sure that item is still the last currently visible one.

    #195172

    Florin Bucur
    28p
    Participant

    …to complete a little bit @zarch ‘s idea, that could be done in the refresh method, after you remember the last visible index in add method.
    Another suggestion – the add method not to change the float vertical position of the scroll bar; or refresh to reset it to the value before last add method?

    #195173
    +1

    zarch
    692p
    Keymaster

    Why do it in the refresh method at all? Just do it as part of the add process. Remember Position, Add Item, Check Position and Adjust…all one method.

    There is an argument as well for saying that if you are viewing the last item in the list and a new one adds on the end then you should scroll down to view that one too.

    #195174
    +3

    void256
    198p
    Participant

    @zarch:
    That is a valid argument but you’ve convinced me that saving the visible elements before the operation and restoring it after the operation is the better approach. In fact I’ve already added it and it actually looks so much more reasonable :D Here is the related commit (Nifty 1.4) https://github.com/void256/nifty-gui/commit/1dbeb7bfb7e40790f464b1e474dbf6493c24fea5 Thanks for the suggestion to both of you!

    If one adds an element to the end of the list and wants that this element is visible one can simply do that manually. In fact the controls demo already did that with something like:

    I think this is a reasonable approach for this special case.

    #195175
    +1

    Florin Bucur
    28p
    Participant

    @void256 & @zarch thanks guys!

Viewing 9 posts - 1 through 9 (of 9 total)

You must be logged in to reply to this topic.