fix: Scroll to hidden layouts using scrollIndex (#12598) 679b808585 Fixes 2 issues: 1. When setting scrollIndex to an index that has a layout that is hidden, it would always get set to index 0 because its bounds were 0,0,0,0. Now, if this happens, we: 1) look forward in the list to find the bounds of the next layout with valid bounds, if we are using carousel, we wrap around to the start of the list and keep looking 2) for non carousel, if we reach the end of the list, walk backwards from the index until we find a visible item. 2. Always just set the index value when setting it on a keyframe. Otherwise when there are hidden items, this could result in the scrollIndex not being the same as the value the user intended to key. This also fixes crashes related to setting scrollIndex to hidden items due to NaN values. Co-authored-by: Philip Chung <philterdesign@gmail.com>
diff --git a/.rive_head b/.rive_head index 9ef8b10..ca85904 100644 --- a/.rive_head +++ b/.rive_head
@@ -1 +1 @@ -623d5fe7a3454f66c88ec19ff0830f7cbb52bc98 +679b808585a2c0dbb5da885c243e3c7f46769f0c
diff --git a/include/rive/constraints/scrolling/scroll_constraint.hpp b/include/rive/constraints/scrolling/scroll_constraint.hpp index 2f24a3e..967ae02 100644 --- a/include/rive/constraints/scrolling/scroll_constraint.hpp +++ b/include/rive/constraints/scrolling/scroll_constraint.hpp
@@ -29,7 +29,9 @@ ScrollVirtualizer* m_virtualizer = nullptr; std::vector<LayoutNodeProvider*> m_layoutChildren; int m_childConstraintAppliedCount = 0; + bool m_hasListChildren = false; + AABB boundsForFlatIndex(size_t index); Vec2D positionAtIndex(float index); float indexAtPosition(Vec2D pos); float maxOffsetXForPercent();
diff --git a/src/constraints/scrolling/scroll_constraint.cpp b/src/constraints/scrolling/scroll_constraint.cpp index 16e38ed..32c6ed2 100644 --- a/src/constraints/scrolling/scroll_constraint.cpp +++ b/src/constraints/scrolling/scroll_constraint.cpp
@@ -1,4 +1,5 @@ #include "rive/constraints/scrolling/scroll_constraint.hpp" +#include "rive/artboard_component_list.hpp" #include "rive/constraints/scrolling/scroll_constraint_proxy.hpp" #include "rive/constraints/scrolling/scroll_virtualizer.hpp" #include "rive/constraints/transform_constraint.hpp" @@ -256,21 +257,21 @@ { return snappingPoints; } - for (auto child : content()->children()) + for (auto child : scrollChildren()) { - auto c = LayoutNodeProvider::from(child); - if (c != nullptr) + if (child == nullptr) { - size_t count = c->numLayoutNodes(); - for (int j = 0; j < count; j++) + continue; + } + int nodeCount = (int)child->numLayoutNodes(); + for (int j = 0; j < nodeCount; j++) + { + auto bounds = child->layoutBoundsForNode(j); + if (isBoundsCollapsed(bounds)) { - auto bounds = c->layoutBoundsForNode(j); - if (isBoundsCollapsed(bounds)) - { - continue; - } - snappingPoints.push_back(Vec2D(bounds.left(), bounds.top())); + continue; } + snappingPoints.push_back(Vec2D(bounds.left(), bounds.top())); } } return snappingPoints; @@ -324,6 +325,7 @@ void ScrollConstraint::buildDependencies() { Super::buildDependencies(); + m_hasListChildren = false; for (auto child : content()->children()) { auto layout = LayoutNodeProvider::from(child); @@ -332,6 +334,10 @@ addDependent(child); layout->addLayoutConstraint(static_cast<LayoutConstraint*>(this)); } + if (child->is<ArtboardComponentList>()) + { + m_hasListChildren = true; + } } } @@ -468,42 +474,158 @@ Vec2D ScrollConstraint::positionAtIndex(float index) { + if (!std::isfinite(index)) + { + return Vec2D(); + } auto count = scrollItemCount(); if (content() == nullptr || count == 0) { return Vec2D(); } - uint32_t i = 0; Vec2D contentGap = gap(); float normalizedIndex = infinite() ? std::fmod(index, (float)count) : index; float floorIndex = std::floor(normalizedIndex); - LayoutNodeProvider* lastChild = nullptr; - for (auto child : scrollChildren()) + float mod = normalizedIndex - floorIndex; + uint32_t targetIndex = (uint32_t)floorIndex; + if (targetIndex >= (uint32_t)count) + { + return Vec2D(); + } + + // No list children: O(1) direct index into scrollChildren. + if (!m_hasListChildren) + { + // Target is visible — return its position with fractional offset. + auto bounds = boundsForFlatIndex(targetIndex); + if (!isBoundsCollapsed(bounds)) + { + return Vec2D(-bounds.left() - (bounds.width() + contentGap.x) * mod, + -bounds.top() - + (bounds.height() + contentGap.y) * mod); + } + // Target is collapsed — walk forward to next visible item. + for (uint32_t k = targetIndex + 1; k < (uint32_t)count; k++) + { + auto b = boundsForFlatIndex(k); + if (!isBoundsCollapsed(b)) + { + return Vec2D(-b.left(), -b.top()); + } + } + if (infinite()) + { + // Carousel: wrap around from the beginning. + for (uint32_t k = 0; k < targetIndex; k++) + { + auto b = boundsForFlatIndex(k); + if (!isBoundsCollapsed(b)) + { + return Vec2D(-b.left(), -b.top()); + } + } + } + else + { + // End of list: walk backward to last visible item. + for (int k = (int)targetIndex - 1; k >= 0; k--) + { + auto b = boundsForFlatIndex(k); + if (!isBoundsCollapsed(b)) + { + return Vec2D(-b.left(), -b.top()); + } + } + } + return Vec2D(); + } + + // Has list children: single-pass through nested child→node structure. + auto& children = scrollChildren(); + uint32_t flatIndex = 0; + Vec2D lastVisibleBeforeTarget; + bool hasVisibleBeforeTarget = false; + bool reachedTarget = false; + + for (auto child : children) { if (child == nullptr) { continue; } - size_t count = child->numLayoutNodes(); - if ((uint32_t)floorIndex < i + count) + int nodeCount = (int)child->numLayoutNodes(); + for (int j = 0; j < nodeCount; j++, flatIndex++) { - float mod = normalizedIndex - floorIndex; - auto bounds = child->layoutBoundsForNode(floorIndex - i); - return Vec2D(-bounds.left() - (bounds.width() + contentGap.x) * mod, - -bounds.top() - - (bounds.height() + contentGap.y) * mod); + auto bounds = child->layoutBoundsForNode(j); + // Before target: track last visible for backward fallback. + if (flatIndex < targetIndex) + { + if (!isBoundsCollapsed(bounds)) + { + lastVisibleBeforeTarget = + Vec2D(-bounds.left(), -bounds.top()); + hasVisibleBeforeTarget = true; + } + continue; + } + // At target: return position if visible, otherwise keep walking. + if (flatIndex == targetIndex) + { + reachedTarget = true; + if (!isBoundsCollapsed(bounds)) + { + return Vec2D( + -bounds.left() - (bounds.width() + contentGap.x) * mod, + -bounds.top() - (bounds.height() + contentGap.y) * mod); + } + continue; + } + // Past target: return first visible item found. + if (!isBoundsCollapsed(bounds)) + { + return Vec2D(-bounds.left(), -bounds.top()); + } } - lastChild = child; - i += count; } - if (lastChild == nullptr) + + if (!reachedTarget) { return Vec2D(); } - auto bounds = - lastChild->layoutBoundsForNode((int)lastChild->numLayoutNodes() - 1); - return Vec2D(-bounds.left(), -bounds.top()); + // No visible item after target. + if (infinite()) + { + // Carousel: wrap around from the beginning. + flatIndex = 0; + for (auto child : children) + { + if (child == nullptr) + { + continue; + } + int nodeCount = (int)child->numLayoutNodes(); + for (int j = 0; j < nodeCount; j++, flatIndex++) + { + if (flatIndex >= targetIndex) + { + return Vec2D(); + } + auto bounds = child->layoutBoundsForNode(j); + if (!isBoundsCollapsed(bounds)) + { + return Vec2D(-bounds.left(), -bounds.top()); + } + } + } + } + else if (hasVisibleBeforeTarget) + { + // End of list: fall back to last visible item before target. + return lastVisibleBeforeTarget; + } + + return Vec2D(); } float ScrollConstraint::indexAtPosition(Vec2D pos) @@ -512,8 +634,43 @@ { return 0; } - float i = 0.0f; Vec2D contentGap = gap(); + if (!m_hasListChildren) + { + size_t count = scrollChildren().size(); + if (constrainsHorizontal()) + { + for (size_t i = 0; i < count; i++) + { + auto bounds = scrollChildren()[i]->layoutBoundsForNode(0); + float step = bounds.width() + contentGap.x; + if (pos.x > -bounds.left() - step) + { + return step != 0 + ? (float)i + (-pos.x - bounds.left()) / step + : (float)i; + } + } + return (float)count; + } + else if (constrainsVertical()) + { + for (size_t i = 0; i < count; i++) + { + auto bounds = scrollChildren()[i]->layoutBoundsForNode(0); + float step = bounds.height() + contentGap.y; + if (pos.y > -bounds.top() - step) + { + return step != 0 ? (float)i + (-pos.y - bounds.top()) / step + : (float)i; + } + } + return (float)count; + } + return 0; + } + // Has list children: nested iteration visiting each node once. + float flatIndex = 0.0f; if (constrainsHorizontal()) { for (auto child : scrollChildren()) @@ -522,19 +679,21 @@ { continue; } - size_t count = child->numLayoutNodes(); - for (int j = 0; j < count; j++) + int nodeCount = (int)child->numLayoutNodes(); + for (int j = 0; j < nodeCount; j++) { auto bounds = child->layoutBoundsForNode(j); - if (pos.x > -bounds.left() - (bounds.width() + contentGap.x)) + float step = bounds.width() + contentGap.x; + if (pos.x > -bounds.left() - step) { - return (i + j) + (-pos.x - bounds.left()) / - (bounds.width() + contentGap.x); + return step != 0 ? (flatIndex + j) + + (-pos.x - bounds.left()) / step + : (flatIndex + j); } } - i += count; + flatIndex += nodeCount; } - return i; + return flatIndex; } else if (constrainsVertical()) { @@ -544,19 +703,22 @@ { continue; } - size_t count = child->numLayoutNodes(); - for (int j = 0; j < count; j++) + int nodeCount = (int)child->numLayoutNodes(); + for (int j = 0; j < nodeCount; j++) { auto bounds = child->layoutBoundsForNode(j); - if (pos.y > -bounds.top() - (bounds.height() + contentGap.y)) + float step = bounds.height() + contentGap.y; + if (pos.y > -bounds.top() - step) { - return (i + j) + (-pos.y - bounds.top()) / - (bounds.height() + contentGap.y); + return step != 0 ? (flatIndex + j) + + (-pos.y - bounds.top()) / step + : (flatIndex + j); + (bounds.height() + contentGap.y); } } - i += count; + flatIndex += nodeCount; } - return i; + return flatIndex; } return 0; } @@ -569,6 +731,10 @@ size_t ScrollConstraint::scrollItemCount() { + if (!m_hasListChildren) + { + return scrollChildren().size(); + } size_t count = 0; for (auto child : scrollChildren()) { @@ -581,6 +747,34 @@ return count; } +AABB ScrollConstraint::boundsForFlatIndex(size_t index) +{ + auto& children = scrollChildren(); + if (!m_hasListChildren) + { + if (index < children.size() && children[index] != nullptr) + { + return children[index]->layoutBoundsForNode(0); + } + return AABB(); + } + size_t flatIndex = 0; + for (auto child : children) + { + if (child == nullptr) + { + continue; + } + size_t nodeCount = child->numLayoutNodes(); + if (index < flatIndex + nodeCount) + { + return child->layoutBoundsForNode((int)(index - flatIndex)); + } + flatIndex += nodeCount; + } + return AABB(); +} + Vec2D ScrollConstraint::gap() { if (content() == nullptr)
diff --git a/tests/unit_tests/assets/layout/layout_scroll_visibility.riv b/tests/unit_tests/assets/layout/layout_scroll_visibility.riv new file mode 100644 index 0000000..67d3caf --- /dev/null +++ b/tests/unit_tests/assets/layout/layout_scroll_visibility.riv Binary files differ
diff --git a/tests/unit_tests/runtime/layout_scroll_test.cpp b/tests/unit_tests/runtime/layout_scroll_test.cpp index c5268e6..e77d44b 100644 --- a/tests/unit_tests/runtime/layout_scroll_test.cpp +++ b/tests/unit_tests/runtime/layout_scroll_test.cpp
@@ -4,6 +4,7 @@ #include "rive/math/transform_components.hpp" #include "rive/shapes/rectangle.hpp" #include "rive/text/text.hpp" +#include "rive/viewmodel/viewmodel_instance_enum.hpp" #include "utils/no_op_factory.hpp" #include "utils/serializing_factory.hpp" #include "rive_file_reader.hpp" @@ -290,4 +291,74 @@ scroll->nearestSnapOffsetInDirection(rive::Vec2D(0.0f, 0.0f), rive::Vec2D(0.0f, -220.0f)); REQUIRE(onSnap.y == -220.0f); +} + +TEST_CASE("ScrollConstraint scrollIndex with hidden items", "[silver]") +{ + rive::File::deterministicMode = true; + + rive::SerializingFactory silver; + auto file = + ReadRiveFile("assets/layout/layout_scroll_visibility.riv", &silver); + + auto artboard = file->artboard()->instance(); + REQUIRE(artboard != nullptr); + silver.frameSize(artboard->width(), artboard->height()); + + auto vmi = file->createDefaultViewModelInstance(artboard.get()); + REQUIRE(vmi != nullptr); + artboard->bindViewModelInstance(vmi); + + auto smi = artboard->defaultStateMachine(); + REQUIRE(smi != nullptr); + + auto vis2 = vmi->propertyValue("vis2")->as<rive::ViewModelInstanceEnum>(); + auto vis3 = vmi->propertyValue("vis3")->as<rive::ViewModelInstanceEnum>(); + auto vis4 = vmi->propertyValue("vis4")->as<rive::ViewModelInstanceEnum>(); + + auto renderer = silver.makeRenderer(); + float dt = 1.0f / 60.0f; + + smi->advanceAndApply(0.0f); + artboard->draw(renderer.get()); + + // Run 300 frames, toggling visibility at key moments. + for (int frame = 0; frame < 300; frame++) + { + // Hide item 2 at frame 30. + if (frame == 30) + { + vis2->value(1); + } + // Hide item 3 at frame 90. + if (frame == 90) + { + vis3->value(1); + } + // Show item 2 again at frame 150. + if (frame == 150) + { + vis2->value(0); + } + // Hide item 4 at frame 210. + if (frame == 210) + { + vis4->value(1); + } + // Show all at frame 270. + if (frame == 270) + { + vis2->value(0); + vis3->value(0); + vis4->value(0); + } + + silver.addFrame(); + smi->advanceAndApply(dt); + artboard->draw(renderer.get()); + } + + CHECK(silver.matches("layout_scroll_visibility")); + + rive::File::deterministicMode = false; } \ No newline at end of file
diff --git a/tests/unit_tests/silvers/layout_scroll_visibility.sriv b/tests/unit_tests/silvers/layout_scroll_visibility.sriv new file mode 100644 index 0000000..9b04ccb --- /dev/null +++ b/tests/unit_tests/silvers/layout_scroll_visibility.sriv Binary files differ