From b651cd3a1bee4c0da033c76ea3624036d27c3aa1 Mon Sep 17 00:00:00 2001 From: Dan Walmsley Date: Thu, 19 Jul 2018 12:05:10 +0100 Subject: [PATCH 1/7] fix carousel-presenter when items are removed. --- src/Avalonia.Controls/Presenters/CarouselPresenter.cs | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/Avalonia.Controls/Presenters/CarouselPresenter.cs b/src/Avalonia.Controls/Presenters/CarouselPresenter.cs index 1d5a187a73..f0f86b7fb4 100644 --- a/src/Avalonia.Controls/Presenters/CarouselPresenter.cs +++ b/src/Avalonia.Controls/Presenters/CarouselPresenter.cs @@ -115,6 +115,11 @@ namespace Avalonia.Controls.Presenters var containers = generator.RemoveRange(e.OldStartingIndex, e.OldItems.Count); Panel.Children.RemoveAll(containers.Select(x => x.ContainerControl)); + if(SelectedIndex > containers.Count()) + { + SelectedIndex = containers.Count(); + } + #pragma warning disable 4014 MoveToPage(-1, SelectedIndex); #pragma warning restore 4014 @@ -130,7 +135,7 @@ namespace Avalonia.Controls.Presenters #pragma warning disable 4014 var newIndex = SelectedIndex; - + if(SelectedIndex < 0) { if(Items != null && Items.Count() > 0) @@ -251,4 +256,4 @@ namespace Avalonia.Controls.Presenters } } } -} \ No newline at end of file +} From 67d60827304ca9700a46dda0f5fc37ab0e54a3fe Mon Sep 17 00:00:00 2001 From: Dan Walmsley Date: Thu, 19 Jul 2018 12:13:43 +0100 Subject: [PATCH 2/7] sync selected index with items count when removing items from carousel. --- src/Avalonia.Controls/Presenters/CarouselPresenter.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Avalonia.Controls/Presenters/CarouselPresenter.cs b/src/Avalonia.Controls/Presenters/CarouselPresenter.cs index f0f86b7fb4..7591117dca 100644 --- a/src/Avalonia.Controls/Presenters/CarouselPresenter.cs +++ b/src/Avalonia.Controls/Presenters/CarouselPresenter.cs @@ -115,9 +115,9 @@ namespace Avalonia.Controls.Presenters var containers = generator.RemoveRange(e.OldStartingIndex, e.OldItems.Count); Panel.Children.RemoveAll(containers.Select(x => x.ContainerControl)); - if(SelectedIndex > containers.Count()) + if(SelectedIndex >= Items.Count()) { - SelectedIndex = containers.Count(); + SelectedIndex = Items.Count() - 1; } #pragma warning disable 4014 From ee55b4fa3dab06c4e8dcae4f46866d99d4ecb466 Mon Sep 17 00:00:00 2001 From: Dan Walmsley Date: Thu, 19 Jul 2018 12:51:48 +0100 Subject: [PATCH 3/7] bug is in selecting items control. add failing unit test. --- .../Presenters/CarouselPresenter.cs | 5 ---- .../Primitives/SelectingItemsControlTests.cs | 28 +++++++++++++++++++ 2 files changed, 28 insertions(+), 5 deletions(-) diff --git a/src/Avalonia.Controls/Presenters/CarouselPresenter.cs b/src/Avalonia.Controls/Presenters/CarouselPresenter.cs index 7591117dca..f77f249c46 100644 --- a/src/Avalonia.Controls/Presenters/CarouselPresenter.cs +++ b/src/Avalonia.Controls/Presenters/CarouselPresenter.cs @@ -115,11 +115,6 @@ namespace Avalonia.Controls.Presenters var containers = generator.RemoveRange(e.OldStartingIndex, e.OldItems.Count); Panel.Children.RemoveAll(containers.Select(x => x.ContainerControl)); - if(SelectedIndex >= Items.Count()) - { - SelectedIndex = Items.Count() - 1; - } - #pragma warning disable 4014 MoveToPage(-1, SelectedIndex); #pragma warning restore 4014 diff --git a/tests/Avalonia.Controls.UnitTests/Primitives/SelectingItemsControlTests.cs b/tests/Avalonia.Controls.UnitTests/Primitives/SelectingItemsControlTests.cs index c052b81309..c7a3465ac4 100644 --- a/tests/Avalonia.Controls.UnitTests/Primitives/SelectingItemsControlTests.cs +++ b/tests/Avalonia.Controls.UnitTests/Primitives/SelectingItemsControlTests.cs @@ -149,6 +149,34 @@ namespace Avalonia.Controls.UnitTests.Primitives Assert.Equal(1, target.SelectedIndex); } + [Fact] + public void SelectedIndex_Item_Is_Updated_As_Items_Removed_When_Last_Item_Is_Selected() + { + var items = new ObservableCollection + { + "Foo", + "Bar", + "FooBar" + }; + + var target = new SelectingItemsControl + { + Items = items, + Template = Template(), + }; + + target.ApplyTemplate(); + target.SelectedItem = items[2]; + + Assert.Equal(items[2], target.SelectedItem); + Assert.Equal(2, target.SelectedIndex); + + items.RemoveAt(0); + + Assert.Equal(items[1], target.SelectedItem); + Assert.Equal(1, target.SelectedIndex); + } + [Fact] public void Setting_SelectedItem_To_Not_Present_Item_Should_Clear_Selection() { From ae4356ef343aa3ebfaae8460296f3b1e412b5b41 Mon Sep 17 00:00:00 2001 From: Dan Walmsley Date: Thu, 19 Jul 2018 12:52:45 +0100 Subject: [PATCH 4/7] add fix for selecting items control. --- src/Avalonia.Controls/Primitives/SelectingItemsControl.cs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/Avalonia.Controls/Primitives/SelectingItemsControl.cs b/src/Avalonia.Controls/Primitives/SelectingItemsControl.cs index a7b8981583..354c740871 100644 --- a/src/Avalonia.Controls/Primitives/SelectingItemsControl.cs +++ b/src/Avalonia.Controls/Primitives/SelectingItemsControl.cs @@ -355,6 +355,12 @@ namespace Avalonia.Controls.Primitives case NotifyCollectionChangedAction.Replace: var selectedIndex = SelectedIndex; + var items = Items?.Cast(); + if (selectedIndex >= items.Count()) + { + selectedIndex = SelectedIndex = items.Count() - 1; + } + if (selectedIndex >= e.OldStartingIndex && selectedIndex < e.OldStartingIndex + e.OldItems.Count) { From fe1620a25dd4788cb00f780a2d4e966b004c455e Mon Sep 17 00:00:00 2001 From: Dan Walmsley Date: Thu, 19 Jul 2018 12:54:14 +0100 Subject: [PATCH 5/7] whitespace. --- .../Presenters/CarouselPresenter.cs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Avalonia.Controls/Presenters/CarouselPresenter.cs b/src/Avalonia.Controls/Presenters/CarouselPresenter.cs index f77f249c46..42910cdc48 100644 --- a/src/Avalonia.Controls/Presenters/CarouselPresenter.cs +++ b/src/Avalonia.Controls/Presenters/CarouselPresenter.cs @@ -39,7 +39,7 @@ namespace Avalonia.Controls.Presenters Carousel.PageTransitionProperty.AddOwner(); private int _selectedIndex = -1; - // private Task _current; + // private Task _current; private Task _currentTransition; private int _queuedTransitionIndex = -1; @@ -130,10 +130,10 @@ namespace Avalonia.Controls.Presenters #pragma warning disable 4014 var newIndex = SelectedIndex; - - if(SelectedIndex < 0) + + if (SelectedIndex < 0) { - if(Items != null && Items.Count() > 0) + if (Items != null && Items.Count() > 0) { newIndex = 0; } @@ -142,11 +142,11 @@ namespace Avalonia.Controls.Presenters newIndex = -1; } } - + MoveToPage(-1, newIndex); #pragma warning restore 4014 } - break; + break; } } @@ -226,7 +226,7 @@ namespace Avalonia.Controls.Presenters int fromIndex = (int)e.OldValue; int toIndex = (int)e.NewValue; - for (;;) + for (; ; ) { _currentTransition = MoveToPage(fromIndex, toIndex); await _currentTransition; From 684c65c2606fd0ecc0eb6dd30adf620c92301bf8 Mon Sep 17 00:00:00 2001 From: Dan Walmsley Date: Thu, 19 Jul 2018 12:56:12 +0100 Subject: [PATCH 6/7] revert file. --- .../Presenters/CarouselPresenter.cs | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/Avalonia.Controls/Presenters/CarouselPresenter.cs b/src/Avalonia.Controls/Presenters/CarouselPresenter.cs index 42910cdc48..1d5a187a73 100644 --- a/src/Avalonia.Controls/Presenters/CarouselPresenter.cs +++ b/src/Avalonia.Controls/Presenters/CarouselPresenter.cs @@ -39,7 +39,7 @@ namespace Avalonia.Controls.Presenters Carousel.PageTransitionProperty.AddOwner(); private int _selectedIndex = -1; - // private Task _current; + // private Task _current; private Task _currentTransition; private int _queuedTransitionIndex = -1; @@ -131,9 +131,9 @@ namespace Avalonia.Controls.Presenters #pragma warning disable 4014 var newIndex = SelectedIndex; - if (SelectedIndex < 0) + if(SelectedIndex < 0) { - if (Items != null && Items.Count() > 0) + if(Items != null && Items.Count() > 0) { newIndex = 0; } @@ -142,11 +142,11 @@ namespace Avalonia.Controls.Presenters newIndex = -1; } } - + MoveToPage(-1, newIndex); #pragma warning restore 4014 } - break; + break; } } @@ -226,7 +226,7 @@ namespace Avalonia.Controls.Presenters int fromIndex = (int)e.OldValue; int toIndex = (int)e.NewValue; - for (; ; ) + for (;;) { _currentTransition = MoveToPage(fromIndex, toIndex); await _currentTransition; @@ -251,4 +251,4 @@ namespace Avalonia.Controls.Presenters } } } -} +} \ No newline at end of file From 6eb80bd55c856cd22653f4c6b28c7c05ea0e470c Mon Sep 17 00:00:00 2001 From: Dan Walmsley Date: Thu, 19 Jul 2018 13:04:18 +0100 Subject: [PATCH 7/7] fix broken logic. --- .../Primitives/SelectingItemsControl.cs | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/src/Avalonia.Controls/Primitives/SelectingItemsControl.cs b/src/Avalonia.Controls/Primitives/SelectingItemsControl.cs index 354c740871..eb3fbde8f2 100644 --- a/src/Avalonia.Controls/Primitives/SelectingItemsControl.cs +++ b/src/Avalonia.Controls/Primitives/SelectingItemsControl.cs @@ -355,18 +355,12 @@ namespace Avalonia.Controls.Primitives case NotifyCollectionChangedAction.Replace: var selectedIndex = SelectedIndex; - var items = Items?.Cast(); - if (selectedIndex >= items.Count()) - { - selectedIndex = SelectedIndex = items.Count() - 1; - } - if (selectedIndex >= e.OldStartingIndex && selectedIndex < e.OldStartingIndex + e.OldItems.Count) { if (!AlwaysSelected) { - SelectedIndex = -1; + selectedIndex = SelectedIndex = -1; } else { @@ -374,6 +368,11 @@ namespace Avalonia.Controls.Primitives } } + var items = Items?.Cast(); + if (selectedIndex >= items.Count()) + { + selectedIndex = SelectedIndex = items.Count() - 1; + } break; case NotifyCollectionChangedAction.Reset: