Skip to content

Commit

Permalink
Merge pull request unoplatform#14616 from TopProgrammer77/TreeView_Dr…
Browse files Browse the repository at this point in the history
…agItemsCompleted

fix(TreeView): raise DragItemsCompleted when dragging items is completed
  • Loading branch information
jeromelaban authored Dec 22, 2023
2 parents 594d371 + 976a67a commit ff69ec6
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using Uno.UITest.Helpers.Queries;
using System.Linq;
using System;
using System.Threading.Tasks;

namespace SamplesApp.UITests.Windows_UI_Xaml.DragAndDropTests
{
Expand All @@ -25,6 +26,10 @@ public void When_Dragging_TreeView_Item()
var bt2 = _app.Marked("bt2");
var bt3 = _app.Marked("bt3");
var focusbt = _app.Marked("focusbt");
var radio_disable = _app.Marked("radio_disable");

_app.WaitForElement(radio_disable);
_app.Tap(radio_disable);

_app.WaitForElement(tv);
_app.Tap(bt1);
Expand Down Expand Up @@ -64,5 +69,34 @@ public void When_Dragging_TreeView_Item()
result = _app.Screenshot("result3");
ImageAssert.AreEqual(case3, result);
}

[Test]
[AutoRetry]
[ActivePlatforms(Platform.Browser)]
public void When_OnDragItemsCompleted()
{
Run("UITests.Windows_UI_Xaml.DragAndDrop.DragDrop_TreeView", skipInitialScreenshot: true);
var tv = _app.Marked("tv");
var bt0 = _app.Marked("bt0");
var radio_enable = _app.Marked("radio_enable");
var tb = _app.Marked("tb");

_app.WaitForElement(tv);

_app.WaitForElement(radio_enable);
_app.Tap(radio_enable);
_app.WaitForElement(bt0);
_app.Tap(bt0);

var tvBounds = _app.Query("tv").Single().Rect;
float fromX = tvBounds.X + 100;
float fromY = tvBounds.Y + _itemHeight * 3 + _offset;
float toX = tvBounds.X + 100;
float toY = tvBounds.Y + _offset;
_app.DragCoordinates(fromX, fromY, toX, toY);

string text = _app.GetText("tb").Trim();
Assert.AreEqual("DragItemsCompleted is triggered", text);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,8 @@
<Button x:Name="bt2" Content="case2"/>
<Button x:Name="bt3" Content="case3"/>
<Button x:Name="focusbt" Content="focus"/>
<RadioButton x:Name="radio_disable" GroupName="radio" Content="Disable TextBox"/>
<RadioButton x:Name="radio_enable" GroupName="radio" Content="Enable TextBox"/>
<TextBlock x:Name="tb" Visibility="Collapsed"/>
</StackPanel>
</Page>
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,22 @@ public DragDrop_TreeView()
});
};

tv.DragItemsCompleted += (s, e) =>
{
tb.Text = "DragItemsCompleted is triggered";
};

radio_disable.Checked += (s, e) =>
{
tb.Visibility = Microsoft.UI.Xaml.Visibility.Collapsed;
};

radio_enable.Checked += (s, e) =>
{
tb.Visibility = Microsoft.UI.Xaml.Visibility.Visible;
};

radio_disable.IsChecked = true;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,9 @@ private static void PrepareContainerForDragDropCore(UIElement itemContainer)
// Known issue: the ContainerClearedForItem might not be invoked properly for all items on some platforms.
// This patch is acceptable as event handlers are static (so they won't leak).
itemContainer.DragStarting -= OnItemContainerDragStarting;
itemContainer.DropCompleted -= OnItemContainerDragCompleted;

itemContainer.CanDrag = true;
itemContainer.DragStarting += OnItemContainerDragStarting;
itemContainer.DropCompleted += OnItemContainerDragCompleted;
}

private static void ClearContainerForDragDrop(UIElement itemContainer)
Expand All @@ -112,7 +110,6 @@ private static void ClearContainerForDragDrop(UIElement itemContainer)

itemContainer.CanDrag = false;
itemContainer.DragStarting -= OnItemContainerDragStarting;
itemContainer.DropCompleted -= OnItemContainerDragCompleted;

itemContainer.DragEnter -= OnReorderDragUpdated;
itemContainer.DragOver -= OnReorderDragUpdated;
Expand All @@ -124,6 +121,9 @@ private static void OnItemContainerDragStarting(UIElement sender, DragStartingEv
{
if (ItemsControlFromItemContainer(sender) is ListViewBase that && that.CanDragItems)
{
// only raise DragItemsCompleted if DragItemsStarting was raised
sender.DropCompleted -= OnItemContainerDragCompleted;
sender.DropCompleted += OnItemContainerDragCompleted;
// The items contains all selected items ONLY if the draggedItem is selected.
var draggedItem = that.ItemFromContainer(sender);
var items =
Expand Down Expand Up @@ -166,6 +166,7 @@ private static void OnItemContainerDragStarting(UIElement sender, DragStartingEv
that.ChangeSelectorItemsVisualState(true);
}
}

}

private static void OnItemContainerDragCompleted(UIElement sender, DropCompletedEventArgs innerArgs)
Expand All @@ -191,6 +192,7 @@ private static void OnItemContainerDragCompleted(UIElement sender, DropCompleted
// (eg if drag was released outside bounds of list)
that.CleanupReordering();
}
sender.DropCompleted -= OnItemContainerDragCompleted;
}

private static void OnReorderDragUpdated(object sender, _DragEventArgs dragEventArgs)
Expand Down

0 comments on commit ff69ec6

Please sign in to comment.