Skip to content

Commit

Permalink
Changed IsSelected semantics.
Browse files Browse the repository at this point in the history
Move old `IsSelected` semantics to `IsSelectedWithPartial` and add a new `IsSelected` method which checks for direct selection.
  • Loading branch information
grokys committed Feb 15, 2020
1 parent 96fbd6c commit f103df6
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 9 deletions.
9 changes: 6 additions & 3 deletions src/Avalonia.Controls/ISelectionModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,12 @@ public interface ISelectionModel : INotifyPropertyChanged
void DeselectRangeFromAnchor(int endGroupIndex, int endItemIndex);
void DeselectRangeFromAnchorTo(IndexPath index);
void Dispose();
bool? IsSelected(int index);
bool? IsSelected(int groupIndex, int itemIndex);
bool? IsSelectedAt(IndexPath index);
bool IsSelected(int index);
bool IsSelected(int grouIndex, int itemIndex);
public bool IsSelectedAt(IndexPath index);
bool? IsSelectedWithPartial(int index);
bool? IsSelectedWithPartial(int groupIndex, int itemIndex);
bool? IsSelectedWithPartialAt(IndexPath index);
void Select(int index);
void Select(int groupIndex, int itemIndex);
void SelectAll();
Expand Down
34 changes: 30 additions & 4 deletions src/Avalonia.Controls/SelectionModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ public IndexPath SelectedIndex
}
set
{
var isSelected = IsSelectedAt(value);
var isSelected = IsSelectedWithPartialAt(value);

if (!isSelected.HasValue || !isSelected.Value)
{
Expand Down Expand Up @@ -393,7 +393,33 @@ public void DeselectAt(IndexPath index)
ApplyAutoSelect();
}

public bool? IsSelected(int index)
public bool IsSelected(int index) => _rootNode.IsSelected(index);

public bool IsSelected(int grouIndex, int itemIndex)
{
return IsSelectedAt(new IndexPath(grouIndex, itemIndex));
}

public bool IsSelectedAt(IndexPath index)
{
var path = index;
SelectionNode? node = _rootNode;

for (int i = 0; i < path.GetSize() - 1; i++)
{
var childIndex = path.GetAt(i);
node = node.GetAt(childIndex, realizeChild: false);

if (node == null)
{
return false;
}
}

return node.IsSelected(index.GetAt(index.GetSize() - 1));
}

public bool? IsSelectedWithPartial(int index)
{
if (index < 0)
{
Expand All @@ -404,7 +430,7 @@ public void DeselectAt(IndexPath index)
return isSelected;
}

public bool? IsSelected(int groupIndex, int itemIndex)
public bool? IsSelectedWithPartial(int groupIndex, int itemIndex)
{
if (groupIndex < 0)
{
Expand All @@ -427,7 +453,7 @@ public void DeselectAt(IndexPath index)
return isSelected;
}

public bool? IsSelectedAt(IndexPath index)
public bool? IsSelectedWithPartialAt(IndexPath index)
{
var path = index;
var isRealized = true;
Expand Down
4 changes: 2 additions & 2 deletions tests/Avalonia.Controls.UnitTests/SelectionModelTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2041,7 +2041,7 @@ private void SetAnchorIndex(SelectionModel manager, IndexPath index)
List<IndexPath> allIndices = GetIndexPathsInSource(selectionModel.Source);
foreach (var index in allIndices)
{
bool? isSelected = selectionModel.IsSelectedAt(index);
bool? isSelected = selectionModel.IsSelectedWithPartialAt(index);
if (Contains(expectedSelected, index) && !Contains(expectedPartialSelected, index))
{
Assert.True(isSelected.Value, index + " is Selected");
Expand All @@ -2068,7 +2068,7 @@ private void SetAnchorIndex(SelectionModel manager, IndexPath index)
{
foreach (var index in expectedSelected)
{
Assert.True(selectionModel.IsSelectedAt(index).Value, index + " is Selected");
Assert.True(selectionModel.IsSelectedWithPartialAt(index), index + " is Selected");
}
}
if (expectedSelected.Count > 0)
Expand Down

0 comments on commit f103df6

Please sign in to comment.