Browse Source

Merge branch 'feature/selectionmodel' into feature/selectionmodel-changed-notifications

pull/3470/head
Steven Kirk 7 years ago
parent
commit
3f13eb4e24
  1. 8
      src/Avalonia.Controls/SelectionModel.cs
  2. 35
      src/Avalonia.Controls/SelectionModelChildrenRequestedEventArgs.cs
  3. 3
      src/Avalonia.Controls/SelectionNode.cs
  4. 20
      tests/Avalonia.Controls.UnitTests/SelectionModelTests.cs

8
src/Avalonia.Controls/SelectionModel.cs

@ -529,7 +529,7 @@ namespace Avalonia.Controls
OnSelectionChanged(e); OnSelectionChanged(e);
} }
internal object? ResolvePath(object data, SelectionNode sourceNode) internal object? ResolvePath(object data, IndexPath dataIndexPath)
{ {
object? resolved = null; object? resolved = null;
@ -538,18 +538,18 @@ namespace Avalonia.Controls
{ {
if (_childrenRequestedEventArgs == null) if (_childrenRequestedEventArgs == null)
{ {
_childrenRequestedEventArgs = new SelectionModelChildrenRequestedEventArgs(data, sourceNode); _childrenRequestedEventArgs = new SelectionModelChildrenRequestedEventArgs(data, dataIndexPath, false);
} }
else else
{ {
_childrenRequestedEventArgs.Initialize(data, sourceNode); _childrenRequestedEventArgs.Initialize(data, dataIndexPath, false);
} }
ChildrenRequested(this, _childrenRequestedEventArgs); ChildrenRequested(this, _childrenRequestedEventArgs);
resolved = _childrenRequestedEventArgs.Children; resolved = _childrenRequestedEventArgs.Children;
// Clear out the values in the args so that it cannot be used after the event handler call. // Clear out the values in the args so that it cannot be used after the event handler call.
_childrenRequestedEventArgs.Initialize(null, null); _childrenRequestedEventArgs.Initialize(null, default, true);
} }
else else
{ {

35
src/Avalonia.Controls/SelectionModelChildrenRequestedEventArgs.cs

@ -12,12 +12,16 @@ namespace Avalonia.Controls
public class SelectionModelChildrenRequestedEventArgs : EventArgs public class SelectionModelChildrenRequestedEventArgs : EventArgs
{ {
private object? _source; private object? _source;
private SelectionNode? _sourceNode; private IndexPath _sourceIndexPath;
private bool _throwOnAccess;
internal SelectionModelChildrenRequestedEventArgs(object source, SelectionNode sourceNode)
internal SelectionModelChildrenRequestedEventArgs(
object source,
IndexPath sourceIndexPath,
bool throwOnAccess)
{ {
_source = source; source = source ?? throw new ArgumentNullException(nameof(source));
_sourceNode = sourceNode; Initialize(source, sourceIndexPath, throwOnAccess);
} }
public object? Children { get; set; } public object? Children { get; set; }
@ -26,12 +30,12 @@ namespace Avalonia.Controls
{ {
get get
{ {
if (_source == null) if (_throwOnAccess)
{ {
throw new ObjectDisposedException(nameof(SelectionModelChildrenRequestedEventArgs)); throw new ObjectDisposedException(nameof(SelectionModelChildrenRequestedEventArgs));
} }
return _source; return _source!;
} }
} }
@ -39,19 +43,28 @@ namespace Avalonia.Controls
{ {
get get
{ {
if (_sourceNode == null) if (_throwOnAccess)
{ {
throw new ObjectDisposedException(nameof(SelectionModelChildrenRequestedEventArgs)); throw new ObjectDisposedException(nameof(SelectionModelChildrenRequestedEventArgs));
} }
return _sourceNode.IndexPath; return _sourceIndexPath;
} }
} }
internal void Initialize(object? source, SelectionNode? sourceNode) internal void Initialize(
object? source,
IndexPath sourceIndexPath,
bool throwOnAccess)
{ {
if (!throwOnAccess && source == null)
{
throw new ArgumentNullException(nameof(source));
}
_source = source; _source = source;
_sourceNode = sourceNode; _sourceIndexPath = sourceIndexPath;
_throwOnAccess = throwOnAccess;
} }
} }
} }

3
src/Avalonia.Controls/SelectionNode.cs

@ -133,7 +133,8 @@ namespace Avalonia.Controls
if (childData != null) if (childData != null)
{ {
var resolvedChild = _manager.ResolvePath(childData, this); var childDataIndexPath = IndexPath.CloneWithChildIndex(index);
var resolvedChild = _manager.ResolvePath(childData, childDataIndexPath);
if (resolvedChild != null) if (resolvedChild != null)
{ {

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

@ -277,21 +277,21 @@ namespace Avalonia.Controls.UnitTests
// Validate SourceIndices. // Validate SourceIndices.
var expectedSourceIndices = new List<IndexPath>() var expectedSourceIndices = new List<IndexPath>()
{ {
Path(),
Path(1), Path(1),
Path(1, 0), Path(1, 0),
Path(1),
Path(1, 0, 1),
Path(1, 0, 1),
Path(1, 0, 1),
Path(1, 0, 1), Path(1, 0, 1),
Path(1, 1), Path(1, 1),
Path(1, 1), Path(1, 0, 1, 3),
Path(1, 1, 0), Path(1, 0, 1, 2),
Path(1, 1, 0), Path(1, 0, 1, 1),
Path(1, 1, 0), Path(1, 0, 1, 0),
Path(1, 1, 1),
Path(1, 1, 0), Path(1, 1, 0),
Path(1, 1, 1) Path(1, 1, 0, 3),
Path(1, 1, 0, 2),
Path(1, 1, 0, 1),
Path(1, 1, 0, 0),
Path(1, 1, 1, 0)
}; };
Assert.Equal(expectedSourceIndices.Count, sourcePaths.Count); Assert.Equal(expectedSourceIndices.Count, sourcePaths.Count);

Loading…
Cancel
Save