From 480a8cb31c713dc519ae38f1d13419cd8e220b4d Mon Sep 17 00:00:00 2001 From: Sinai Date: Fri, 16 Apr 2021 18:24:45 +1000 Subject: [PATCH] fix DivideByZeroExceptions and add minimum handle size --- src/UI/Widgets/InfiniteScroll/InfiniteScrollRect.cs | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/UI/Widgets/InfiniteScroll/InfiniteScrollRect.cs b/src/UI/Widgets/InfiniteScroll/InfiniteScrollRect.cs index 3255075..d557cf1 100644 --- a/src/UI/Widgets/InfiniteScroll/InfiniteScrollRect.cs +++ b/src/UI/Widgets/InfiniteScroll/InfiniteScrollRect.cs @@ -110,9 +110,7 @@ namespace UnityExplorer.UI.Widgets.InfiniteScroll internal void SetSliderFromScrollValue(bool forceValue = true) { int total = DataSource.ItemCount; - // avoid DivideByZeroException, this is harmless if count was <= 0. - if (total <= 0) - total = 1; + total = Math.Max(total, 1); var spread = _cellPool.Count - (ExtraCellPoolSize * 2); @@ -123,7 +121,7 @@ namespace UnityExplorer.UI.Widgets.InfiniteScroll _slider.value = 0f; else // 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. @@ -134,6 +132,9 @@ namespace UnityExplorer.UI.Widgets.InfiniteScroll var handleRatio = (decimal)spread / total; 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) var container = _slider.m_HandleContainerRect; container.offsetMax = new Vector2(container.offsetMax.x, -(handleHeight * 0.5f));