Browse Source

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

pull/3498/head
Steven Kirk 6 years ago
parent
commit
f416454b5a
  1. 47
      src/Avalonia.Controls/ISelectionModel.cs
  2. 16
      src/Avalonia.Controls/SelectionModel.cs

47
src/Avalonia.Controls/ISelectionModel.cs

@ -0,0 +1,47 @@
// This source file is adapted from the WinUI project.
// (https://github.com/microsoft/microsoft-ui-xaml)
//
// Licensed to The Avalonia Project under MIT License, courtesy of The .NET Foundation.
using System;
using System.Collections.Generic;
namespace Avalonia.Controls
{
public interface ISelectionModel
{
IndexPath AnchorIndex { get; set; }
IndexPath SelectedIndex { get; set; }
IReadOnlyList<IndexPath> SelectedIndices { get; }
object SelectedItem { get; }
IReadOnlyList<object> SelectedItems { get; }
bool SingleSelect { get; set; }
object Source { get; set; }
event EventHandler<SelectionModelChildrenRequestedEventArgs> ChildrenRequested;
event EventHandler<SelectionModelSelectionChangedEventArgs> SelectionChanged;
void ClearSelection();
void Deselect(int index);
void Deselect(int groupIndex, int itemIndex);
void DeselectAt(IndexPath index);
void DeselectRange(IndexPath start, IndexPath end);
void DeselectRangeFromAnchor(int index);
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);
void Select(int index);
void Select(int groupIndex, int itemIndex);
void SelectAll();
void SelectAt(IndexPath index);
void SelectRange(IndexPath start, IndexPath end);
void SelectRangeFromAnchor(int index);
void SelectRangeFromAnchor(int endGroupIndex, int endItemIndex);
void SelectRangeFromAnchorTo(IndexPath index);
void SetAnchorIndex(int index);
void SetAnchorIndex(int groupIndex, int index);
}
}

16
src/Avalonia.Controls/SelectionModel.cs

@ -13,7 +13,7 @@ using Avalonia.Controls.Utils;
namespace Avalonia.Controls namespace Avalonia.Controls
{ {
public class SelectionModel : INotifyPropertyChanged, IDisposable public class SelectionModel : ISelectionModel, INotifyPropertyChanged, IDisposable
{ {
private readonly SelectionNode _rootNode; private readonly SelectionNode _rootNode;
private bool _singleSelect; private bool _singleSelect;
@ -317,7 +317,7 @@ namespace Avalonia.Controls
_selectedIndicesCached = indices; _selectedIndicesCached = indices;
} }
return _selectedIndicesCached; return _selectedIndicesCached;
} }
} }
@ -595,7 +595,7 @@ namespace Avalonia.Controls
RaisePropertyChanged(nameof(SelectedIndex)); RaisePropertyChanged(nameof(SelectedIndex));
RaisePropertyChanged(nameof(SelectedIndices)); RaisePropertyChanged(nameof(SelectedIndices));
if (_rootNode.Source != null) if (_rootNode.Source != null)
{ {
RaisePropertyChanged(nameof(SelectedItem)); RaisePropertyChanged(nameof(SelectedItem));
@ -611,7 +611,7 @@ namespace Avalonia.Controls
} }
var selected = _rootNode.Select(index, select); var selected = _rootNode.Select(index, select);
if (selected) if (selected)
{ {
AnchorIndex = new IndexPath(index); AnchorIndex = new IndexPath(index);
@ -627,7 +627,7 @@ namespace Avalonia.Controls
var childNode = _rootNode.GetAt(groupIndex, realizeChild: true); var childNode = _rootNode.GetAt(groupIndex, realizeChild: true);
var selected = childNode!.Select(itemIndex, select); var selected = childNode!.Select(itemIndex, select);
if (selected) if (selected)
{ {
AnchorIndex = new IndexPath(groupIndex, itemIndex); AnchorIndex = new IndexPath(groupIndex, itemIndex);
@ -637,7 +637,7 @@ namespace Avalonia.Controls
private void SelectWithPathImpl(IndexPath index, bool select) private void SelectWithPathImpl(IndexPath index, bool select)
{ {
bool selected = false; bool selected = false;
if (_singleSelect) if (_singleSelect)
{ {
ClearSelection(resetAnchor: true); ClearSelection(resetAnchor: true);
@ -666,7 +666,7 @@ namespace Avalonia.Controls
{ {
int anchorIndex = 0; int anchorIndex = 0;
var anchor = AnchorIndex; var anchor = AnchorIndex;
if (anchor != null) if (anchor != null)
{ {
anchorIndex = anchor.GetAt(0); anchorIndex = anchor.GetAt(0);
@ -680,7 +680,7 @@ namespace Avalonia.Controls
var startGroupIndex = 0; var startGroupIndex = 0;
var startItemIndex = 0; var startItemIndex = 0;
var anchorIndex = AnchorIndex; var anchorIndex = AnchorIndex;
if (anchorIndex != null) if (anchorIndex != null)
{ {
startGroupIndex = anchorIndex.GetAt(0); startGroupIndex = anchorIndex.GetAt(0);

Loading…
Cancel
Save