fix DivideByZeroExceptions and add minimum handle size

This commit is contained in:
Sinai 2021-04-16 18:24:45 +10:00
parent 9bdcccaaa1
commit 480a8cb31c

View File

@ -110,9 +110,7 @@ namespace UnityExplorer.UI.Widgets.InfiniteScroll
internal void SetSliderFromScrollValue(bool forceValue = true) internal void SetSliderFromScrollValue(bool forceValue = true)
{ {
int total = DataSource.ItemCount; int total = DataSource.ItemCount;
// avoid DivideByZeroException, this is harmless if count was <= 0. total = Math.Max(total, 1);
if (total <= 0)
total = 1;
var spread = _cellPool.Count - (ExtraCellPoolSize * 2); var spread = _cellPool.Count - (ExtraCellPoolSize * 2);
@ -123,7 +121,7 @@ namespace UnityExplorer.UI.Widgets.InfiniteScroll
_slider.value = 0f; _slider.value = 0f;
else else
// top-most displayed index divided by (totalCount - displayedRange) // top-most displayed index divided by (totalCount - displayedRange)
_slider.value = (float)((decimal)range.x / (total - _cellPool.Count)); _slider.value = (float)((decimal)range.x / Math.Max(1, (total - _cellPool.Count)));
} }
// resize the handle rect to reflect the size of the displayed content vs. the total content height. // resize the handle rect to reflect the size of the displayed content vs. the total content height.
@ -134,6 +132,9 @@ namespace UnityExplorer.UI.Widgets.InfiniteScroll
var handleRatio = (decimal)spread / total; var handleRatio = (decimal)spread / total;
var handleHeight = viewportHeight * (float)Math.Min(1, handleRatio); var handleHeight = viewportHeight * (float)Math.Min(1, handleRatio);
// minimum handle size
handleHeight = Math.Max(handleHeight, 15f);
// need to resize the handle container area for the size of the handle (bigger handle = smaller area) // need to resize the handle container area for the size of the handle (bigger handle = smaller area)
var container = _slider.m_HandleContainerRect; var container = _slider.m_HandleContainerRect;
container.offsetMax = new Vector2(container.offsetMax.x, -(handleHeight * 0.5f)); container.offsetMax = new Vector2(container.offsetMax.x, -(handleHeight * 0.5f));