Browse Source

Changed IsSelected semantics.

Move old `IsSelected` semantics to `IsSelectedWithPartial` and add a new `IsSelected` method which checks for direct selection.
fixes/tree-selectionmodel
Steven Kirk 6 years ago
parent
commit
f103df6a9a
  1. 9
      src/Avalonia.Controls/ISelectionModel.cs
  2. 34
      src/Avalonia.Controls/SelectionModel.cs
  3. 4
      tests/Avalonia.Controls.UnitTests/SelectionModelTests.cs

9
src/Avalonia.Controls/ISelectionModel.cs

@ -32,9 +32,12 @@ namespace Avalonia.Controls
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();

34
src/Avalonia.Controls/SelectionModel.cs

@ -174,7 +174,7 @@ namespace Avalonia.Controls
}
set
{
var isSelected = IsSelectedAt(value);
var isSelected = IsSelectedWithPartialAt(value);
if (!isSelected.HasValue || !isSelected.Value)
{
@ -393,7 +393,33 @@ namespace Avalonia.Controls
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)
{
@ -404,7 +430,7 @@ namespace Avalonia.Controls
return isSelected;
}
public bool? IsSelected(int groupIndex, int itemIndex)
public bool? IsSelectedWithPartial(int groupIndex, int itemIndex)
{
if (groupIndex < 0)
{
@ -427,7 +453,7 @@ namespace Avalonia.Controls
return isSelected;
}
public bool? IsSelectedAt(IndexPath index)
public bool? IsSelectedWithPartialAt(IndexPath index)
{
var path = index;
var isRealized = true;

4
tests/Avalonia.Controls.UnitTests/SelectionModelTests.cs

@ -2041,7 +2041,7 @@ namespace Avalonia.Controls.UnitTests
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");
@ -2068,7 +2068,7 @@ namespace Avalonia.Controls.UnitTests
{
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)

Loading…
Cancel
Save