Skip to content

Commit

Permalink
Don't only select leaf nodes from SelectRange.
Browse files Browse the repository at this point in the history
  • Loading branch information
grokys committed Feb 15, 2020
1 parent c7a0188 commit 96fbd6c
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 27 deletions.
6 changes: 1 addition & 5 deletions src/Avalonia.Controls/SelectionModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -759,11 +759,7 @@ private void SelectRangeImpl(IndexPath start, IndexPath end, bool select)
winrtEnd,
info =>
{
if (info.Node.DataCount == 0)
{
// Select only leaf nodes
info.ParentNode!.Select(info.Path.GetAt(info.Path.GetSize() - 1), select);
}
info.ParentNode!.Select(info.Path.GetAt(info.Path.GetSize() - 1), select);
});
}

Expand Down
81 changes: 59 additions & 22 deletions tests/Avalonia.Controls.UnitTests/SelectionModelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -305,16 +305,19 @@ public void ValidateNestedMultipleSelection(bool handleChildrenRequested)
ValidateSelection(selectionModel,
new List<IndexPath>()
{
Path(1, 0),
Path(1, 1),
Path(1, 0, 1),
Path(1, 0, 1, 0),
Path(1, 0, 1, 1),
Path(1, 0, 1, 2),
Path(1, 0, 1, 3),
Path(1, 0, 1),
Path(1, 1, 0),
Path(1, 1, 1),
Path(1, 1, 0, 0),
Path(1, 1, 0, 1),
Path(1, 1, 0, 2),
Path(1, 1, 0, 3),
Path(1, 1, 0),
Path(1, 1, 1, 0),
},
new List<IndexPath>()
Expand All @@ -324,8 +327,7 @@ public void ValidateNestedMultipleSelection(bool handleChildrenRequested)
Path(1, 0),
Path(1, 1),
Path(1, 1, 1),
},
2 /* selectedInnerNodes */);
});

ClearSelection(selectionModel);
ValidateSelection(selectionModel, new List<IndexPath>() { });
Expand All @@ -337,16 +339,20 @@ public void ValidateNestedMultipleSelection(bool handleChildrenRequested)
ValidateSelection(selectionModel,
new List<IndexPath>()
{
Path(0, 0, 0, 2),
Path(0, 0, 0, 3),
Path(0, 0, 1, 0),
Path(0, 0, 1, 1),
Path(0, 0, 1, 2),
Path(0, 0, 1, 3),
Path(0, 0, 1),
Path(0, 1, 0, 0),
Path(0, 1, 0, 1),
Path(0, 1, 0, 2),
Path(0, 0),
Path(0, 1),
Path(0, 0, 0),
Path(0, 0, 1),
Path(0, 0, 0, 2),
Path(0, 0, 0, 3),
Path(0, 0, 1, 0),
Path(0, 0, 1, 1),
Path(0, 0, 1, 2),
Path(0, 0, 1, 3),
Path(0, 1, 0),
Path(0, 1, 0, 0),
Path(0, 1, 0, 1),
Path(0, 1, 0, 2),
},
new List<IndexPath>()
{
Expand All @@ -356,8 +362,7 @@ public void ValidateNestedMultipleSelection(bool handleChildrenRequested)
Path(0, 0, 0),
Path(0, 1),
Path(0, 1, 0),
},
1 /* selectedInnerNodes */);
});

startPath = Path(0, 1, 0, 2);
SetAnchorIndex(selectionModel, startPath);
Expand Down Expand Up @@ -853,19 +858,19 @@ public void SelectRangeRegressionTest()
ValidateSelection(selectionModel,
new List<IndexPath>()
{
Path(0),
Path(1),
Path(0, 0),
Path(0, 1),
Path(0, 2),
Path(0),
Path(1, 0),
Path(1, 1)
},
new List<IndexPath>()
{
Path(),
Path(1)
},
1 /* selectedInnerNodes */);
});
}

[Fact]
Expand Down Expand Up @@ -1271,7 +1276,7 @@ public void Removing_Selected_Item_With_Children_Raises_SelectionChanged()
target.SelectionChanged += (s, e) =>
{
Assert.Empty(e.DeselectedIndices);
Assert.Equal(new object[] { 0, 1, 2 }, e.DeselectedItems);
Assert.Equal(new object[] { new AvaloniaList<int> { 0, 1, 2 }, 0, 1, 2 }, e.DeselectedItems);
Assert.Empty(e.SelectedIndices);
Assert.Empty(e.SelectedItems);
++raised;
Expand Down Expand Up @@ -1371,6 +1376,38 @@ public void Removing_Item_Unhooks_CollectionChanged_Handlers()
Assert.Equal(0, GetSubscriberCount(toRemove));
}

[Fact]
public void SelectRange_Behaves_The_Same_As_Multiple_Selects()
{
var data = new[] { 1, 2, 3 };
var target = new SelectionModel { Source = data };

target.Select(1);

Assert.Equal(new[] { IndexPath.CreateFrom(1) }, target.SelectedIndices);

target.ClearSelection();
target.SelectRange(new IndexPath(1), new IndexPath(1));

Assert.Equal(new[] { IndexPath.CreateFrom(1) }, target.SelectedIndices);
}

[Fact]
public void SelectRange_Behaves_The_Same_As_Multiple_Selects_Nested()
{
var data = CreateNestedData(3, 2, 2);
var target = new SelectionModel { Source = data };

target.Select(1);

Assert.Equal(new[] { IndexPath.CreateFrom(1) }, target.SelectedIndices);

target.ClearSelection();
target.SelectRange(new IndexPath(1), new IndexPath(1));

Assert.Equal(new[] { IndexPath.CreateFrom(1) }, target.SelectedIndices);
}

[Fact]
public void Should_Not_Treat_Strings_As_Nested_Selections()
{
Expand Down Expand Up @@ -2005,7 +2042,7 @@ private void ValidateSelection(
foreach (var index in allIndices)
{
bool? isSelected = selectionModel.IsSelectedAt(index);
if (Contains(expectedSelected, index))
if (Contains(expectedSelected, index) && !Contains(expectedPartialSelected, index))
{
Assert.True(isSelected.Value, index + " is Selected");
}
Expand Down Expand Up @@ -2037,7 +2074,7 @@ private void ValidateSelection(
if (expectedSelected.Count > 0)
{
_output.WriteLine("SelectedIndex is " + selectionModel.SelectedIndex);
Assert.Equal(0, selectionModel.SelectedIndex.CompareTo(expectedSelected[0]));
Assert.Equal(expectedSelected[0], selectionModel.SelectedIndex);
if (selectionModel.Source != null)
{
Assert.Equal(selectionModel.SelectedItem, GetData(selectionModel, expectedSelected[0]));
Expand Down

0 comments on commit 96fbd6c

Please sign in to comment.