Skip to content

Commit

Permalink
Update TreeView to support blocking drag&drop for specific items (#1646)
Browse files Browse the repository at this point in the history
* Check 'Handled' for drag drop events

* Add NewParent property in TreeViewDraItemsCompletedEventArgs

* CR feedback

* CR feedback2

* Dummy commit to reset status checks
  • Loading branch information
kaiguo authored Nov 23, 2019
1 parent 463842e commit 4dd6b16
Show file tree
Hide file tree
Showing 10 changed files with 165 additions and 115 deletions.
12 changes: 7 additions & 5 deletions dev/TreeView/InteractionTests/TreeViewTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1898,7 +1898,7 @@ private void TreeViewDragItemTest(bool isContentMode = false)
ClickButton("SetupDragDropHandlersForApiTest");
UIObject dragUIObject = LabelFirstItem();
InputHelper.DragDistance(dragUIObject, dragUIObject.BoundingRectangle.Height, Direction.South);
Verify.AreEqual("DragItemsStarting:Root->DragItemsCompleted:Root", ReadResult());
Verify.AreEqual("DragItemsStarting:Root\nDragItemsCompleted:Root", ReadResult());
}
}

Expand All @@ -1918,8 +1918,8 @@ public void TreeViewDragItemTest_ContentMode()

private void TreeViewDragMultipleItemsTest(bool isContentMode = false)
{
if (IsPhoneDevice() || IsLowerThanRS2() ||
(isContentMode && IsLowerThanRS5()))
// InputHelper.DragToTarget() does not work properly on lower versions
if (IsLowerThanRS5())
{
return;
}
Expand Down Expand Up @@ -1951,8 +1951,10 @@ private void TreeViewDragMultipleItemsTest(bool isContentMode = false)

UIObject dragUIObject = FindElement.ByName("Root.0");
Verify.IsNotNull(dragUIObject, "Verifying Root.0 is found");
InputHelper.DragDistance(dragUIObject, dragUIObject.BoundingRectangle.Height / 2, Direction.South);
Verify.AreEqual("DragItemsStarting:Root.0|Root.1->DragItemsCompleted:Root.0|Root.1", ReadResult());
UIObject dropUIObject = FindElement.ByName("Root.2");
Verify.IsNotNull(dropUIObject, "Verifying Root.2 is found");
InputHelper.DragToTarget(dragUIObject, dropUIObject);
Verify.AreEqual("DragItemsStarting:Root.0|Root.1\nDragItemsCompleted:Root.0|Root.1\nNewParent: Root.2", ReadResult());
}
}

Expand Down
9 changes: 8 additions & 1 deletion dev/TreeView/TestUI/TreeViewPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -721,7 +721,14 @@ private void DragItemsStartingForApiTest(TreeView sender, TreeViewDragItemsStart

private void DragItemsCompletedForApiTest(TreeView sender, TreeViewDragItemsCompletedEventArgs args)
{
Results.Text += "->DragItemsCompleted:" + GetDraggedItemsNames(args.Items);
Results.Text += "\nDragItemsCompleted:" + GetDraggedItemsNames(args.Items);

var parent = args.NewParent;
if (parent != null)
{
var parentName = IsInContentMode() ? (parent as TreeViewItemSource).Content : (parent as TreeViewNode).Content.ToString();
Results.Text += "\nNewParent: " + parentName;
}
}

private String GetDraggedItemsNames(IEnumerable<object> items)
Expand Down
22 changes: 18 additions & 4 deletions dev/TreeView/TreeView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,15 +249,29 @@ void TreeView::OnPropertyChanged(const winrt::DependencyPropertyChangedEventArgs

void TreeView::OnListControlDragItemsStarting(const winrt::IInspectable& sender, const winrt::DragItemsStartingEventArgs& args)
{
auto treeViewArgs = winrt::make_self<TreeViewDragItemsStartingEventArgs>();
treeViewArgs->DragItemsStartingEventArgs(args);
const auto treeViewArgs = winrt::make_self<TreeViewDragItemsStartingEventArgs>(args);
m_dragItemsStartingEventSource(*this, *treeViewArgs);
}

void TreeView::OnListControlDragItemsCompleted(const winrt::IInspectable& sender, const winrt::DragItemsCompletedEventArgs& args)
{
auto treeViewArgs = winrt::make_self<TreeViewDragItemsCompletedEventArgs>();
treeViewArgs->DragItemsCompletedEventArgs(args);
const auto newParent = [items = args.Items(), listControl = ListControl(), rootNode = m_rootNode.get()]()
{
if (listControl && items && items.Size() > 0)
{
if (const auto draggedNode = listControl->NodeFromItem(items.GetAt(0)))
{
const auto parentNode = draggedNode.Parent();
if (parentNode && parentNode != rootNode)
{
return listControl->ItemFromNode(parentNode);
}
}
}
return static_cast<winrt::IInspectable>(nullptr);
}();

const auto treeViewArgs = winrt::make_self<TreeViewDragItemsCompletedEventArgs>(args, newParent);
m_dragItemsCompletedEventSource(*this, *treeViewArgs);
}

Expand Down
5 changes: 5 additions & 0 deletions dev/TreeView/TreeView.idl
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,11 @@ runtimeclass TreeViewDragItemsCompletedEventArgs
{
Windows.ApplicationModel.DataTransfer.DataPackageOperation DropResult{ get; };
Windows.Foundation.Collections.IVectorView<Object> Items{ get; };

[WUXC_VERSION_PREVIEW]
{
Object NewParent{ get; };
}
}

[WUXC_VERSION_RS4]
Expand Down
11 changes: 9 additions & 2 deletions dev/TreeView/TreeViewDragItemsCompletedEventArgs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,13 @@
#include "Vector.h"
#include "TreeViewDragItemsCompletedEventArgs.h"

void TreeViewDragItemsCompletedEventArgs::DragItemsCompletedEventArgs(const winrt::DragItemsCompletedEventArgs& args)
TreeViewDragItemsCompletedEventArgs::TreeViewDragItemsCompletedEventArgs(const winrt::DragItemsCompletedEventArgs& args, const winrt::IInspectable& newParent)
{
m_dragItemsCompletedEventArgs = args;
m_newParent = newParent;
}


DataPackageOperation TreeViewDragItemsCompletedEventArgs::DropResult() const
{
return m_dragItemsCompletedEventArgs.DropResult();
Expand All @@ -19,4 +21,9 @@ DataPackageOperation TreeViewDragItemsCompletedEventArgs::DropResult() const
winrt::IVectorView<winrt::IInspectable> TreeViewDragItemsCompletedEventArgs::Items()
{
return m_dragItemsCompletedEventArgs.Items();
}
}

winrt::IInspectable TreeViewDragItemsCompletedEventArgs::NewParent()
{
return m_newParent;
}
6 changes: 4 additions & 2 deletions dev/TreeView/TreeViewDragItemsCompletedEventArgs.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,12 @@ class TreeViewDragItemsCompletedEventArgs :
public ReferenceTracker<TreeViewDragItemsCompletedEventArgs, winrt::implementation::TreeViewDragItemsCompletedEventArgsT, winrt::composing, winrt::composable>
{
public:
void DragItemsCompletedEventArgs(const winrt::DragItemsCompletedEventArgs& args);
TreeViewDragItemsCompletedEventArgs(const winrt::DragItemsCompletedEventArgs& args, const winrt::IInspectable& newParent);
DataPackageOperation DropResult() const;
winrt::IVectorView<winrt::IInspectable> Items();
winrt::IInspectable NewParent();

private:
winrt::DragItemsCompletedEventArgs m_dragItemsCompletedEventArgs{ nullptr };
};
winrt::IInspectable m_newParent;
};
4 changes: 2 additions & 2 deletions dev/TreeView/TreeViewDragItemsStartingEventArgs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#include "Vector.h"
#include "TreeViewDragItemsStartingEventArgs.h"

void TreeViewDragItemsStartingEventArgs::DragItemsStartingEventArgs(const winrt::DragItemsStartingEventArgs& args)
TreeViewDragItemsStartingEventArgs::TreeViewDragItemsStartingEventArgs(const winrt::DragItemsStartingEventArgs& args)
{
m_dragItemsStartingEventArgs = args;
}
Expand All @@ -29,4 +29,4 @@ DataPackage TreeViewDragItemsStartingEventArgs::Data() const
winrt::IVector<winrt::IInspectable> TreeViewDragItemsStartingEventArgs::Items()
{
return m_dragItemsStartingEventArgs.Items();
}
}
2 changes: 1 addition & 1 deletion dev/TreeView/TreeViewDragItemsStartingEventArgs.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class TreeViewDragItemsStartingEventArgs :
public ReferenceTracker<TreeViewDragItemsStartingEventArgs, winrt::implementation::TreeViewDragItemsStartingEventArgsT, winrt::composing, winrt::composable>
{
public:
void DragItemsStartingEventArgs(const winrt::DragItemsStartingEventArgs& args);
TreeViewDragItemsStartingEventArgs(const winrt::DragItemsStartingEventArgs& args);
bool Cancel() const;
void Cancel(const bool value);
DataPackage Data() const;
Expand Down
23 changes: 13 additions & 10 deletions dev/TreeView/TreeViewItem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ void TreeViewItem::OnKeyDown(winrt::KeyRoutedEventArgs const& e)

void TreeViewItem::OnDrop(winrt::DragEventArgs const& args)
{
if (args.AcceptedOperation() == winrt::Windows::ApplicationModel::DataTransfer::DataPackageOperation::Move)
if (!args.Handled() && args.AcceptedOperation() == winrt::Windows::ApplicationModel::DataTransfer::DataPackageOperation::Move)
{
winrt::TreeViewItem droppedOnItem = *this;
auto treeView = AncestorTreeView();
Expand Down Expand Up @@ -127,7 +127,7 @@ void TreeViewItem::OnDrop(winrt::DragEventArgs const& args)
void TreeViewItem::OnDragOver(winrt::DragEventArgs const& args)
{
auto treeView = AncestorTreeView();
if (treeView)
if (treeView && !args.Handled())
{
auto treeViewList = treeView->ListControl();
winrt::TreeViewItem draggedOverItem = *this;
Expand Down Expand Up @@ -179,7 +179,7 @@ void TreeViewItem::OnDragEnter(winrt::DragEventArgs const& args)
args.DragUIOverride().IsGlyphVisible(true);

auto treeView = AncestorTreeView();
if (treeView && treeView->CanReorderItems())
if (treeView && treeView->CanReorderItems() && !args.Handled())
{
auto treeViewList = treeView->ListControl();
winrt::TreeViewNode draggedNode = treeViewList->DraggedTreeViewNode();
Expand Down Expand Up @@ -230,15 +230,18 @@ void TreeViewItem::OnDragEnter(winrt::DragEventArgs const& args)

void TreeViewItem::OnDragLeave(winrt::DragEventArgs const& args)
{
if (auto treeView = AncestorTreeView())
if (!args.Handled())
{
auto treeViewList = treeView->ListControl();
treeViewList->SetDraggedOverItem(nullptr);
}
if (auto treeView = AncestorTreeView())
{
auto treeViewList = treeView->ListControl();
treeViewList->SetDraggedOverItem(nullptr);
}

if (m_expandContentTimer)
{
m_expandContentTimer.get().Stop();
if (m_expandContentTimer)
{
m_expandContentTimer.get().Stop();
}
}

__super::OnDragLeave(args);
Expand Down
Loading

0 comments on commit 4dd6b16

Please sign in to comment.