TL;DR: Given an old list that the user has scrolled to index i and a new list that replaces the old list by adding/removing filters, what should the new scroll index be?
Details: I have a list of strings visible to user. The user has scrolled to some point in that list. There's also some filter controls. The user enabling or disabling filter controls causes the list to grow or shrink to only the items that match the filter.
Is there some standard algorithm for figuring out where to position the scrollbar when the list is modified? It seems obvious that if the user was scrolled all the way to the top of the list, when I reduce the list, I should keep the scroll at the top. Likewise, if the user was at the end, when I reduce the list, I should keep the scroll pinned at the end of the list, wherever that ends up. But for all the points in the middle, I'm not sure. It seems like I somehow want to maximize the number of previously visible items on screen still on screen so that the user doesn't lose their place in a large list.
I have been designing a few different variations of how to pick the new scroll position that keeps the user in as close to the same place as they were as the list gets modified. But I am thinking (hoping!) that this is probably a solved problem.
Is there a standard algorithm for computing the new scroll position in a modified list?
I am not looking for code in any particular language. I am looking for pseudocode that describes how the new scroll position should be calculated for best user experience. An ideal answer will include citation of UI studies that looked at different ways to update the scroll position and identified the best experience for most users. But in the absence of that, I'll take answers that describe good experiences you know about.