Skip to content

Conversation

SyedAbdulAzeemSF4852
Copy link
Contributor

Note

Are you waiting for the changes in this PR to be merged?
It would be very helpful if you could test the resulting artifacts from this PR and let us know in a comment if this change resolves your issue. Thank you!

Issue Details

  • CurrentItem does not update when ItemSpacing is set.

Root Cause

  • When ItemSpacing is set on a CarouselView using CV2 (CarouselViewHandler2), the page calculation logic was only considering the container size but ignoring the additional space consumed by ItemSpacing.

Description of Change

  • Updated the page index calculation in LayoutFactory2.cs to include ItemSpacing when determining the current page in the carousel layout. This ensures that CurrentItem updates correctly when spacing is present.

Issues Fixed

Fixes #32048

Validated the behaviour in the following platforms

  • Windows
  • Android
  • iOS
  • Mac

Output

Before After
Before.mov
After.mov

@dotnet-policy-service dotnet-policy-service bot added the community ✨ Community Contribution label Oct 22, 2025
@dotnet-policy-service
Copy link
Contributor

Hey there @@SyedAbdulAzeemSF4852! Thank you so much for your PR! Someone from the team will get assigned to your PR shortly and we'll get it reviewed.

@dotnet-policy-service dotnet-policy-service bot added the partner/syncfusion Issues / PR's with Syncfusion collaboration label Oct 22, 2025
@jsuarezruiz
Copy link
Contributor

jsuarezruiz commented Oct 22, 2025

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 3 pipeline(s).

@jsuarezruiz jsuarezruiz added the area-controls-collectionview CollectionView, CarouselView, IndicatorView label Oct 22, 2025

var page = (offset.X + sectionMargin) / (env.Container.ContentSize.Width - sectionMargin * 2);
// Calculate page index accounting for ItemSpacing
var itemSpacing = itemsView.ItemsLayout is LinearItemsLayout linearLayout ? linearLayout.ItemSpacing : 0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you scroll to the very last item and verify CurrentItem updates correctly?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jsuarezruiz , I have verified this by scrolling to the last item, and the CurrentItem is updating correctly. For your reference, I’m attaching the video below.

ScrollingToLastItem.mov


var page = (offset.X + sectionMargin) / (env.Container.ContentSize.Width - sectionMargin * 2);
// Calculate page index accounting for ItemSpacing
var itemSpacing = itemsView.ItemsLayout is LinearItemsLayout linearLayout ? linearLayout.ItemSpacing : 0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you verify also GridItemsLayout?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jsuarezruiz , CarouselView.ItemsLayout is explicitly typed as LinearItemsLayout (not the base ItemsLayout type), so it cannot be a GridItemsLayout.

Looking at the CarouselView class definition :

public LinearItemsLayout ItemsLayout
{
get => (LinearItemsLayout)GetValue(ItemsLayoutProperty);
set => SetValue(ItemsLayoutProperty, value);
}

The property type system enforces that only LinearItemsLayout can be assigned.

var itemSpacing = itemsView.ItemsLayout is LinearItemsLayout linearLayout ? linearLayout.ItemSpacing : 0;

var effectiveItemWidth = env.Container.ContentSize.Width - sectionMargin * 2 + itemSpacing;
double page = (offset.X + sectionMargin) / effectiveItemWidth;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

double page = (offset.X + sectionMargin) / effectiveItemWidth;

Could effectiveItemWidth be zero? Potential Divide-by-Zero Exception?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@jsuarezruiz , Yes, there is a potential divide-by-zero exception that could occur when the container has no width or when the effective item width calculation results in zero or a negative value. To prevent this, I've added a guard to skip the scroll calculation in such cases, avoiding invalid position updates.

@SyedAbdulAzeemSF4852 SyedAbdulAzeemSF4852 marked this pull request as ready for review October 22, 2025 13:39
@Copilot Copilot AI review requested due to automatic review settings October 22, 2025 13:39
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR fixes a bug in CarouselView2 where the CurrentItem property fails to update correctly when ItemSpacing is configured. The root cause was that the page index calculation only considered container size without accounting for the additional space consumed by ItemSpacing.

Key Changes:

  • Updated page index calculation logic in iOS LayoutFactory2 to include ItemSpacing in the effective item width
  • Added validation to prevent division by zero when effectiveItemWidth is non-positive
  • Added comprehensive UI tests to validate the fix across platforms

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
src/Controls/src/Core/Handlers/Items2/iOS/LayoutFactory2.cs Updated page index calculation to account for ItemSpacing in CarouselView layout
src/Controls/tests/TestCases.HostApp/Issues/Issue32048.cs Added HostApp UI test page demonstrating the CurrentItem update with ItemSpacing
src/Controls/tests/TestCases.Shared.Tests/Tests/Issues/Issue32048.cs Added NUnit test to verify CurrentItem updates correctly when ItemSpacing is set

}

var page = (offset.X + sectionMargin) / (env.Container.ContentSize.Width - sectionMargin * 2);
// Calculate page index accounting for ItemSpacing
Copy link

Copilot AI Oct 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[nitpick] The comment on line 346 should clarify why itemSpacing is added to the effective width calculation. The current comment states 'accounting for ItemSpacing' but doesn't explain that the spacing is added because each item's scroll distance includes both its width and the spacing after it.

Suggested change
// Calculate page index accounting for ItemSpacing
// Calculate page index, including ItemSpacing because each item's scroll distance
// consists of its width plus the spacing after it. This ensures paging accounts for
// both the item width and the space between items.

Copilot uses AI. Check for mistakes.

@azure-pipelines
Copy link

Azure Pipelines successfully started running 3 pipeline(s).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-controls-collectionview CollectionView, CarouselView, IndicatorView community ✨ Community Contribution partner/syncfusion Issues / PR's with Syncfusion collaboration platform/ios

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[CarouselViewHandler2] CurrentItem does not work when ItemSpacing is set

3 participants