Skip to content

Commit

Permalink
fix: Properly implemented TreeView events and ItemsSourceView
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinZikmund committed Sep 11, 2020
1 parent f9cf8f8 commit ecb8b09
Show file tree
Hide file tree
Showing 6 changed files with 81 additions and 126 deletions.
124 changes: 53 additions & 71 deletions src/Uno.UI/Microsoft/UI/Xaml/Controls/Repeater/ItemsSourceView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,13 @@ namespace Microsoft.UI.Xaml.Controls
public class ItemsSourceView : INotifyCollectionChanged
{
private int m_cachedSize = -1;
private IList m_vector = null;

private IList m_vector;

private IKeyIndexMapping m_uniqueIdMaping;
private INotifyCollectionChanged m_notifyCollectionChanged;
private IBindableObservableVector m_bindableObservableVector;
private IObservableVector<object> m_observableVector;

public ItemsSourceView(object source)
{
Expand All @@ -28,42 +33,20 @@ public ItemsSourceView(object source)
throw new ArgumentNullException("Argument 'source' is null.");
}

var vector = source as IList<object>;
if (vector != null)
var list = source as IList;
if (list != null)
{
m_vector = vector;
m_vector = list;
ListenToCollectionChanges();
}
else
{
// The bindable interop interface are abi compatible with the corresponding
// WinRT interfaces.
var bindableVector = source as IList;
if (bindableVector != null)
var enumerable = source as IEnumerable;
if (enumerable != null)
{
m_vector.set(reinterpret_cast <const IVector<object>&> (bindableVector));
ListenToCollectionChanges();
}
else
{
var iterable = source.try_as<IIterable<object>>();
if (iterable)
{
m_vector.set(WrapIterable(iterable));
}
else
{
var bindableIterable = source.try_as<IBindableIterable>();
if (bindableIterable)
{
m_vector.set(WrapIterable(reinterpret_cast <const IIterable<object> &> (bindableIterable)));
}
else
{
throw hresult_invalid_argument(L"Argument 'source' is not a supported vector.");
}
}
m_vector = WrapIterable(enumerable);
}
throw new ArgumentException("Argument 'source' is not a supported vector.");
}

m_uniqueIdMaping = source as IKeyIndexMapping;
Expand Down Expand Up @@ -118,7 +101,7 @@ internal void OnItemsSourceChanged(NotifyCollectionChangedEventArgs args)
}


~InspectingDataSource()
~ItemsSourceView()
{
UnListenToCollectionChanges();
}
Expand Down Expand Up @@ -179,37 +162,31 @@ private int IndexOf(object value)
return index;
}

//IVector<object> WrapIterable(const IIterable<object>& iterable)
//{
// var vector = make < Vector < object, MakeVectorParam< VectorFlag.DependencyObjectBase > () >> ();
// var iterator = iterable.First();
// while (iterator.HasCurrent())
// {
// vector.Append(iterator.Current());
// iterator.MoveNext();
// }

// return vector;
//}

//private void UnListenToCollectionChanges()
//{
// var notifyCollection = m_notifyCollectionChanged.safe_get()
// if ()
// {
// notifyCollection.CollectionChanged(m_eventToken);
// }

// else if (var bindableObservableCollection = m_bindableObservableVector.safe_get())
// {
// bindableObservableCollection.VectorChanged(m_eventToken);
// }

// else if (var observableCollection = m_observableVector.safe_get())
// {
// observableCollection.VectorChanged(m_eventToken);
// }
//}
private IList WrapIterable(IEnumerable enumerable)
{
var vector = new List<object>();
foreach (var obj in enumerable)
{
vector.Add(obj);
}
return vector;
}

private void UnListenToCollectionChanges()
{
if (m_notifyCollectionChanged != null)
{
m_notifyCollectionChanged.CollectionChanged -= OnCollectionChanged;
}
else if (m_bindableObservableVector != null)
{
m_bindableObservableVector.VectorChanged -= OnBindableVectorChanged;
}
else if (m_observableVector != null)
{
m_observableVector.VectorChanged -= OnVectorChanged;
}
}

void ListenToCollectionChanges()
{
Expand All @@ -220,24 +197,24 @@ void ListenToCollectionChanges()
var incc = m_vector as INotifyCollectionChanged;
if (incc != null)
{
m_eventToken = incc.CollectionChanged({ this, &OnCollectionChanged });
incc.CollectionChanged += OnCollectionChanged;
m_notifyCollectionChanged = incc;
}
else
{
var bindableObservableVector = m_vector.try_as<IBindableObservableVector>();
if (bindableObservableVector)
var bindableObservableVector = m_vector as IBindableObservableVector;
if (bindableObservableVector != null)
{
m_eventToken = bindableObservableVector.VectorChanged({ this, &OnBindableVectorChanged });
m_bindableObservableVector.set(bindableObservableVector);
bindableObservableVector.VectorChanged += OnBindableVectorChanged;
m_bindableObservableVector = bindableObservableVector;
}
else
{
var observableVector = m_vector.try_as<IObservableVector<object>>();
if (observableVector)
var observableVector = m_vector as IObservableVector<object>;
if (observableVector != null)
{
m_eventToken = observableVector.VectorChanged({ this, &OnVectorChanged });
m_observableVector.set(observableVector);
observableVector.VectorChanged += OnVectorChanged;
m_observableVector = observableVector;
}
}
}
Expand Down Expand Up @@ -280,26 +257,31 @@ void OnVectorChanged(
action = NotifyCollectionChangedAction.Add;
newStartingIndex = (int)e.Index;
newItems.Append(null);
OnItemsSourceChanged(new NotifyCollectionChangedEventArgs(action, newItems, newStartingIndex));
break;
case CollectionChange.ItemRemoved:
action = NotifyCollectionChangedAction.Remove;
oldStartingIndex = (int)e.Index;
oldItems.Append(null);
OnItemsSourceChanged(new NotifyCollectionChangedEventArgs(action, oldItems, oldStartingIndex));
break;
case CollectionChange.ItemChanged:
action = NotifyCollectionChangedAction.Replace;
oldStartingIndex = (int)e.Index;
newStartingIndex = oldStartingIndex;
newItems.Append(null);
oldItems.Append(null);
OnItemsSourceChanged(new NotifyCollectionChangedEventArgs(action, newItems, oldItems, newStartingIndex));
break;
case CollectionChange.Reset:
action = NotifyCollectionChangedAction.Reset;
OnItemsSourceChanged(new NotifyCollectionChangedEventArgs(action));
break;
default:
throw new InvalidOperationException("Unsupported collection change");
}

//WinUI uses NotifyCollectionChangedEventArgs with 5 args
//OnItemsSourceChanged(
// new NotifyCollectionChangedEventArgs(
// action,
Expand Down
4 changes: 3 additions & 1 deletion src/Uno.UI/Microsoft/UI/Xaml/Controls/TreeView/TreeView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ public TreeView()

m_rootNode = new TreeViewNode();
m_pendingSelectedNodes = new List<TreeViewNode>();

this.RegisterDisposablePropertyChangedCallback((s, p, e) => OnPropertyChanged(e));
}

internal TreeViewList ListControl { get; private set; }
Expand Down Expand Up @@ -271,7 +273,7 @@ private void UpdateItemsSelectionMode(bool isMultiSelect)
listControl.EnableMultiselect(isMultiSelect);

var viewModel = listControl.ListViewModel;
int size = viewModel.Size;
int size = viewModel.Count;

for (int i = 0; i < size; i++)
{
Expand Down
16 changes: 7 additions & 9 deletions src/Uno.UI/Microsoft/UI/Xaml/Controls/TreeView/TreeViewItem.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ public TreeViewItem()
{
DefaultStyleKey = typeof(TreeViewItem);
SetValue(TreeViewItemTemplateSettingsProperty, new TreeViewItemTemplateSettings());

this.RegisterDisposablePropertyChangedCallback((s, p, e) => OnPropertyChanged(e));
}

protected override void OnKeyDown(KeyRoutedEventArgs e)
Expand Down Expand Up @@ -721,15 +723,11 @@ private void OnExpandCollapseChevronPointerPressed(object sender, PointerRoutedE

private void RecycleEvents(bool useSafeGet = false)
{
//var chevron = m_expandCollapseChevron;
//if (chevron != null)
//{
// if (m_expandCollapseChevronPointerPressedToken.value)
// {
// chevron.PointerCanceled(m_expandCollapseChevronPointerPressedToken);
// m_expandCollapseChevronPointerPressedToken.value = 0;
// }
//}
var chevron = m_expandCollapseChevron;
if (chevron != null)
{
chevron.PointerPressed -= OnExpandCollapseChevronPointerPressed;
}
}

///* static */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ protected override void OnDragOver(Windows.UI.Xaml.DragEventArgs args)
int aboveIndex = -1;
int belowIndex = -1;
var itemsSource = ListViewModel;
int size = itemsSource.Size;
int size = itemsSource.Count;
var point = args.GetPosition((UIElement)insertionPanel);
insertionPanel.GetInsertionIndexes(point, out aboveIndex, out belowIndex);

Expand Down Expand Up @@ -549,7 +549,7 @@ internal int FlatIndex(TreeViewNode node)

internal bool IsFlatIndexValid(int index)
{
return index >= 0 && index < ListViewModel.Size;
return index >= 0 && index < ListViewModel.Count;
}

internal int RemoveNodeFromParent(TreeViewNode node)
Expand Down Expand Up @@ -659,7 +659,7 @@ private TreeViewNode GetRootOfSelection(TreeViewNode node)
internal TreeViewNode NodeFromContainer(DependencyObject container)
{
int index = container != null ? IndexFromContainer(container) : -1;
if (index >= 0 && index < ListViewModel.Size)
if (index >= 0 && index < ListViewModel.Count)
{
return NodeAtFlatIndex(index);
}
Expand Down
55 changes: 14 additions & 41 deletions src/Uno.UI/Microsoft/UI/Xaml/Controls/TreeView/TreeViewNode.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Collections.Generic;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using Windows.Foundation;
using Windows.Foundation.Collections;
Expand All @@ -7,7 +8,7 @@

namespace Microsoft.UI.Xaml.Controls
{
public partial class TreeViewNode : DependencyObject
public partial class TreeViewNode : DependencyObject, ICustomPropertyProvider, IStringable
{
private TreeViewNode _parent;
private bool _hasUnrealizedChildren;
Expand All @@ -20,6 +21,8 @@ public TreeViewNode()
collection.SetParent(this);
Children = collection;
collection.VectorChanged += ChildVectorChanged;

this.RegisterDisposablePropertyChangedCallback((s, p, e) => OnPropertyChanged(e));
}

public TreeViewNode Parent
Expand Down Expand Up @@ -77,8 +80,7 @@ private void ChildVectorChanged(IObservableVector<TreeViewNode> sender, IVectorC
private void OnPropertyChanged(DependencyPropertyChangedEventArgs args)
{
DependencyProperty property = args.Property;
//TODO:
//m_propertyChangedEventSource(this, args);
ExpandedChanged?.Invoke(this, args);
}

private void RaiseChildrenChanged(CollectionChange collectionChange, uint index)
Expand All @@ -92,8 +94,7 @@ internal object ItemsSource
get => m_itemsSource;
set
{
//TODO:
if( m_itemsDataSource != null)
if (m_itemsDataSource != null)
{
m_itemsDataSource.CollectionChanged -= OnItemsSourceChanged;
}
Expand Down Expand Up @@ -238,45 +239,17 @@ private string GetContentAsString()
return GetType().Name;
}

//#pragma region ICustomPropertyProvider

// TypeName TreeViewNode.Type()
// {
// var outer = get_strong().as< IInspectable > ();
// TypeName typeName;
// typeName.Kind = TypeKind.Metadata;
// typeName.Name = get_class_name(outer);
// return typeName;
// }

// ICustomProperty TreeViewNode.GetCustomProperty(hstring const& name)
// {
// return nullptr;
// }

// ICustomProperty TreeViewNode.GetIndexedProperty(hstring const& name, TypeName const& type)
// {
// return nullptr;
// }

// hstring TreeViewNode.GetStringRepresentation()
// {
// return GetContentAsString();
// }

//#pragma endregion

//#pragma region IStringable
// hstring TreeViewNode.ToString()
// {
// return GetContentAsString();
// }
//#pragma endregion
Type ICustomPropertyProvider.Type => typeof(TreeViewNode);

ICustomProperty ICustomPropertyProvider.GetCustomProperty(string name) => null;

ICustomProperty ICustomPropertyProvider.GetIndexedProperty(string name, Type type) => null;

string ICustomPropertyProvider.GetStringRepresentation() => GetContentAsString();

public override string ToString()
{
return GetContentAsString();
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -426,7 +426,7 @@ private void UpdateNodeSelection(TreeViewNode selectNode, TreeNodeSelectionState
if (index > -1)
{
selectedNodes.RemoveAtCore(index);
selectNode.ChildrenChanged -= SelectedNodeChildrenChanged;
selectNode.ChildrenChanged -= SelectedNodeChildrenChanged;
}
break;
}
Expand Down

0 comments on commit ecb8b09

Please sign in to comment.