Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow horizontal scrollbar for ListView items #147

Merged
merged 5 commits into from
Sep 26, 2020
Merged

Conversation

Miv99
Copy link
Contributor

@Miv99 Miv99 commented Sep 24, 2020

These changes allow the horizontal scrollbar to appear to view items too long to fit in a ListView.

When there are no columns, the scrollbar's max value will be enough to view up to the end of the longest item.

When there are 1 or more columns and m_expandLastColumn is false, the ListView will follow old behavior.

When there are 1 or more columns and m_expandLastColumn is true, the scrollbar's max value will be enough to view up to the end of the longest item in the rightmost column. Items in other columns will be clipped by their column's width (like in old behavior).

@texus
Copy link
Owner

texus commented Sep 24, 2020

Thanks for contributing. I'm a bit concerned with the potential performance impact of this change though. With optimizations the code you wrote would be welcome, but it won't be accepted in its current form.

I noticed you are calling updateColumnsMaxItemWidths every time you add a single item. This means that if you add 1000 lines, you need to calculate 500,000 line widths.

There are several optimizations that are probably possible (based on a quick look at the changes):

  • For adding items, you only need to check if that one item is larger than the maximum.
  • For removing items, if the width was smaller than the maximum you don't need to recalculate anything.
  • The line widths could be cached so that you only need to calculate them once. If you do need to loop over all items (e.g. when removing the longest line) then you at least don't need to recalculate everything.
  • Since there is no need for these calculations when there are columns and m_expandLastColumn is false, I don't think the max width should be calculated at all in such case.
  • The maxWidth calculations add things such as textPadding and iconWidth on each item. It would probably be better to calculate the maximum line width and then only add the padding and icon width after you found the longest line. (This is a micro-optimization that I don't care much about though, I'm just including it because I'm listing all the other stuff anyway)

@Miv99
Copy link
Contributor Author

Miv99 commented Sep 25, 2020

Thanks for the suggestions. I implemented bullet points 1, 2, and 4 in this commit but skipped the 3rd because I was having some trouble figuring out how to cleanly cache line widths in TextView. The caching could be done pretty easily in Text but would require making getLineWidth() no longer const and I'm not sure if that should be avoided.

@texus
Copy link
Owner

texus commented Sep 25, 2020

One last performance thing to look into is maybe only calling updateHorizontalScrollbarMaximum when needed. It probably only needs to be called when the maximum width changes instead of for each item.

There is some duplicate code in changeItem and changeSubItem, maybe the lines starting at updatedLastColumnMaxItemWidth could be placed in a helper function (with oldDesiredWidthInLastColumn as parameter).

The first two lines in addMultipleItems seem to be leftovers from before, so they can probably be removed.

I looked into whether getLineWidth can be cached and I came to the conclusion that it might not be what you need. The function is only used in one place and was added as a helper function during some PR. You can probably just use getSize().x instead, which does cache its value internally.

The Text::getLineWidth without parameters doesn't even seem to be used anywhere in TGUI (and can thus be removed), while the only use case of the other getLineWidth function would be to calculate the width of the text without first having to create a Text object (and for that reason it might be better to move the function to the Font class). But that it something I'll look into later.

Also fixed changeSubItem() not updating horizontal scrollbar max when there are no columns.
@Miv99
Copy link
Contributor Author

Miv99 commented Sep 26, 2020

Implemented all those (except the Text things you're looking at) in this commit and fixed some edge case with my code for changeSubItem() with no columns.

@texus
Copy link
Owner

texus commented Sep 26, 2020

Thanks for adding the requested optimizations. Everything looks good now.

@texus texus merged commit 88b13f1 into texus:0.9-dev Sep 26, 2020
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