Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[RC1] Implement Platform Args for Drag and Drop EventArgs #16962

Merged
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
a6d34e1
Introduce the Platform Args for Drag and Drop Event Args
tj-devel709 Aug 17, 2023
6bfa640
First pass of review - remove handled and others
tj-devel709 Aug 24, 2023
fd08b8a
change prop names
tj-devel709 Aug 24, 2023
2b1cf5c
windows uitest changes
Aug 24, 2023
1faffc8
UITest does not drag and release at starting position
tj-devel709 Aug 24, 2023
58a1959
APIPublic docs
tj-devel709 Aug 24, 2023
1fb38b0
Merge branch 'main' into GestureRecognizer-DragNDrop-Extensions-Final
tj-devel709 Aug 24, 2023
9bc61ce
null reference for ui tests
tj-devel709 Aug 25, 2023
9173081
Merge remote-tracking branch 'TJ/GestureRecognizer-DragNDrop-Extensio…
tj-devel709 Aug 25, 2023
c0b84fc
fix windows public api txt
tj-devel709 Aug 25, 2023
bc05e0d
Suppress error in compat that is not being used
tj-devel709 Aug 25, 2023
4d841a6
Merge branch 'main' into GestureRecognizer-DragNDrop-Extensions-Final
tj-devel709 Aug 25, 2023
8b302f4
Set PlatformArgs in constructors
tj-devel709 Aug 28, 2023
2a6d88d
Add additional docs and throw exception if localObject is set
tj-devel709 Aug 28, 2023
4209d8f
Fix merge conflicts
tj-devel709 Aug 28, 2023
d4ea01c
Make the constructors with PlatformArgs public
tj-devel709 Aug 28, 2023
b59d400
Add the eventargs.Handled property back in and small syntax changes
tj-devel709 Aug 29, 2023
795f86b
various small changes
tj-devel709 Aug 29, 2023
b514eab
Merge branch 'main' into GestureRecognizer-DragNDrop-Extensions-Final
tj-devel709 Aug 29, 2023
60c4b64
Windows null dereference fix
tj-devel709 Aug 29, 2023
93ff03b
Merge remote-tracking branch 'TJ/GestureRecognizer-DragNDrop-Extensio…
tj-devel709 Aug 29, 2023
779b484
A few different discussed changes
tj-devel709 Aug 29, 2023
bea4365
Merge conflict - missing bracket in test
tj-devel709 Aug 29, 2023
22c52f0
Nullable datapackage in constructor
tj-devel709 Aug 29, 2023
8dd3ca4
Make the public ctors internal and cast the winUI UIElement
tj-devel709 Aug 30, 2023
dd8cfc2
change type in public api
Aug 30, 2023
29afeff
Merge branch 'main' into GestureRecognizer-DragNDrop-Extensions-Final
tj-devel709 Aug 30, 2023
7d5c5c4
Merge branch 'main' into pr/16962
mattleibow Aug 30, 2023
82c9ad6
Fix the PublicAPI
mattleibow Aug 30, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/Compatibility/Core/src/iOS/DragAndDropDelegate.cs
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,9 @@ public UIDragItem[] HandleDragStarting(View element, IVisualElementRenderer rend

if (args.Cancel)
return;

#pragma warning disable CS0618 // Type or member is obsolete
if (!args.Handled)
#pragma warning restore CS0618 // Type or member is obsolete
{
UIImage uIImage = null;
string clipDescription = String.Empty;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentView xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
AutomationId="DragAndDropEventArgs"
x:Class="Maui.Controls.Sample.DragAndDropEventArgs">
<StackLayout>
<StackLayout Orientation="Horizontal">
<Label HeightRequest="200" WidthRequest="200" AutomationId="LabelDragElement"
Text="Drag me Over the Purple Box, off the purple box, drop me on the purple box, and then verify all the correct events fired">
<Label.GestureRecognizers>
<DragGestureRecognizer DragStarting="DragStarting" DropCompleted="DropCompleted"></DragGestureRecognizer>
</Label.GestureRecognizers>
</Label>
<Label HeightRequest="200" HorizontalOptions="FillAndExpand" Background="Purple" AutomationId="DragTarget">
<Label.GestureRecognizers>
<DropGestureRecognizer DragLeave="DragLeave" DragOver="DragOver" Drop="Drop"></DropGestureRecognizer>
</Label.GestureRecognizers>
</Label>
</StackLayout>
<Label x:Name="events" AutomationId="DragEventsLabel" Text="EventsLabel">
<Label.GestureRecognizers>
<DropGestureRecognizer></DropGestureRecognizer>
</Label.GestureRecognizers>
</Label>
</StackLayout>
</ContentView>
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
using Microsoft.Maui.Controls;
using Microsoft.Maui.Controls.Xaml;

namespace Maui.Controls.Sample;

[XamlCompilation(XamlCompilationOptions.Compile)]
public partial class DragAndDropEventArgs : ContentView
{
bool _emittedDragOver = false;
public DragAndDropEventArgs()
{
InitializeComponent();
}

void AddEvent(string name)
{
events.Text += $"{name},";
}

void DragStarting(object sender, DragStartingEventArgs e)
{
_emittedDragOver = false;
if (e.PlatformArgs is PlatformDragStartingEventArgs platformArgs)
{
#if IOS || MACCATALYST
if (platformArgs.Sender is not null)
AddEvent("DragStarting:" + nameof(platformArgs.Sender));
if (platformArgs.DragInteraction is not null)
AddEvent("DragStarting:" + nameof(platformArgs.DragInteraction));
if (platformArgs.DragSession is not null)
AddEvent("DragStarting:" + nameof(platformArgs.DragSession));
#elif ANDROID
if (platformArgs.Sender is not null)
AddEvent("DragStarting:" + nameof(platformArgs.Sender));
if (platformArgs.MotionEvent is not null)
AddEvent("DragStarting:" + nameof(platformArgs.MotionEvent));
#elif WINDOWS
if (platformArgs.Sender is not null)
AddEvent("DragStarting:" + nameof(platformArgs.Sender));
if (platformArgs.DragStartingEventArgs is not null)
AddEvent("DragStarting:" + nameof(platformArgs.DragStartingEventArgs));
AddEvent("DragStarting:" + nameof(platformArgs.Handled));
#endif
}
}

void DropCompleted(object sender, DropCompletedEventArgs e)
{
if (e.PlatformArgs is PlatformDropCompletedEventArgs platformArgs)
{
#if IOS || MACCATALYST
if (platformArgs.Sender is not null)
AddEvent("DropCompleted:" + nameof(platformArgs.Sender));
if (platformArgs.DropInteraction is not null)
AddEvent("DropCompleted:" + nameof(platformArgs.DropInteraction));
if (platformArgs.DropSession is not null)
AddEvent("DropCompleted:" + nameof(platformArgs.DropSession));
#elif ANDROID
if (platformArgs.Sender is not null)
AddEvent("DropCompleted:" + nameof(platformArgs.Sender));
if (platformArgs.DragEvent is not null)
AddEvent("DropCompleted:" + nameof(platformArgs.DragEvent));
#elif WINDOWS
if (platformArgs.Sender is not null)
AddEvent("DropCompleted:" + nameof(platformArgs.Sender));
if (platformArgs.DropCompletedEventArgs is not null)
AddEvent("DropCompleted:" + nameof(platformArgs.DropCompletedEventArgs));
#endif
}
}

void DragLeave(object sender, DragEventArgs e)
{
if (e.PlatformArgs is PlatformDragEventArgs platformArgs)
{
#if IOS || MACCATALYST
if (platformArgs.Sender is not null)
AddEvent("DragLeave:" + nameof(platformArgs.Sender));
if (platformArgs.DropInteraction is not null)
AddEvent("DragLeave:" + nameof(platformArgs.DropInteraction));
if (platformArgs.DropSession is not null)
AddEvent("DragLeave:" + nameof(platformArgs.DropSession));
#elif ANDROID
if (platformArgs.Sender is not null)
AddEvent("DragLeave:" + nameof(platformArgs.Sender));
if (platformArgs.DragEvent is not null)
AddEvent("DragLeave:" + nameof(platformArgs.DragEvent));
#elif WINDOWS
if (platformArgs.Sender is not null)
AddEvent("DragLeave:" + nameof(platformArgs.Sender));
if (platformArgs.DragEventArgs is not null)
AddEvent("DragLeave:" + nameof(platformArgs.DragEventArgs));
AddEvent("DragLeave:" + nameof(platformArgs.Handled));
#endif
}
}

void DragOver(object sender, DragEventArgs e)
{
if (!_emittedDragOver) // This can generate a lot of noise, only add it once
{
if (e.PlatformArgs is PlatformDragEventArgs platformArgs)
{
#if IOS || MACCATALYST
if (platformArgs.Sender is not null)
AddEvent("DragOver:" + nameof(platformArgs.Sender));
if (platformArgs.DropInteraction is not null)
AddEvent("DragOver:" + nameof(platformArgs.DropInteraction));
if (platformArgs.DropSession is not null)
AddEvent("DragOver:" + nameof(platformArgs.DropSession));
#elif ANDROID
if (platformArgs.Sender is not null)
AddEvent("DragOver:" + nameof(platformArgs.Sender));
if (platformArgs.DragEvent is not null)
AddEvent("DragOver:" + nameof(platformArgs.DragEvent));
#elif WINDOWS
if (platformArgs.Sender is not null)
AddEvent("DragOver:" + nameof(platformArgs.Sender));
if (platformArgs.DragEventArgs is not null)
AddEvent("DragOver:" + nameof(platformArgs.DragEventArgs));
#endif
}
_emittedDragOver = true;
}
}

void Drop(object sender, DropEventArgs e)
{
if (e.PlatformArgs is PlatformDropEventArgs platformArgs)
{
#if IOS || MACCATALYST
if (platformArgs.Sender is not null)
AddEvent("Drop:" + nameof(platformArgs.Sender));
if (platformArgs.DropInteraction is not null)
AddEvent("Drop:" + nameof(platformArgs.DropInteraction));
if (platformArgs.DropSession is not null)
AddEvent("Drop:" + nameof(platformArgs.DropSession));
#elif ANDROID
if (platformArgs.Sender is not null)
AddEvent("Drop:" + nameof(platformArgs.Sender));
if (platformArgs.DragEvent is not null)
AddEvent("Drop:" + nameof(platformArgs.DragEvent));
#elif WINDOWS
if (platformArgs.Sender is not null)
AddEvent("Drop:" + nameof(platformArgs.Sender));
if (platformArgs.DragEventArgs is not null)
AddEvent("Drop:" + nameof(platformArgs.DragEventArgs));
#endif
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ public DragAndDropGallery()
{
Add(new DragAndDropEvents());
Add(new DragAndDropBetweenLayouts());
Add(new DragAndDropEventArgs());
}
}
}
10 changes: 10 additions & 0 deletions src/Controls/src/Core/DragAndDrop/DragEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,19 @@ public DragEventArgs(DataPackage dataPackage)
AcceptedOperation = DataPackageOperation.Copy;
}

internal DragEventArgs(DataPackage dataPackage, PlatformDragEventArgs platformArgs) : this(dataPackage)
{
PlatformArgs = platformArgs;
}

/// <include file="../../../docs/Microsoft.Maui.Controls/DragEventArgs.xml" path="//Member[@MemberName='Data']/Docs/*" />
public DataPackage Data { get; }
/// <include file="../../../docs/Microsoft.Maui.Controls/DragEventArgs.xml" path="//Member[@MemberName='AcceptedOperation']/Docs/*" />
public DataPackageOperation AcceptedOperation { get; set; }

/// <summary>
/// Gets the platform-specific arguments associated with the <see cref="DragEventArgs"/>.
/// </summary>
public PlatformDragEventArgs PlatformArgs { get; }
}
}
11 changes: 4 additions & 7 deletions src/Controls/src/Core/DragAndDrop/DragGestureRecognizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -90,19 +90,16 @@ internal void SendDropCompleted(DropCompletedEventArgs args)
DropCompleted?.Invoke(this, args);
}

internal DragStartingEventArgs SendDragStarting(IView element)
internal DragStartingEventArgs SendDragStarting(IView element, PlatformDragStartingEventArgs platformArgs = null)
{
var args = new DragStartingEventArgs();
var args = new DragStartingEventArgs(platformArgs);

DragStartingCommand?.Execute(DragStartingCommandParameter);
DragStarting?.Invoke(this, args);

if (!args.Handled)
{
args.Data.PropertiesInternal.Add("DragSource", element);
}
args.Data.PropertiesInternal.Add("DragSource", element);

if (args.Cancel || args.Handled)
if (args.Cancel)
return args;

_isDragActive = true;
Expand Down
11 changes: 11 additions & 0 deletions src/Controls/src/Core/DragAndDrop/DragStartingEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,22 @@ public DragStartingEventArgs()
Data = new DataPackage();
}

internal DragStartingEventArgs(PlatformDragStartingEventArgs platformArgs) : this()
{
PlatformArgs = platformArgs;
}

/// <include file="../../../docs/Microsoft.Maui.Controls/DragStartingEventArgs.xml" path="//Member[@MemberName='Handled']/Docs/*" />
[Obsolete("Use PlatformArgs to handle customization. On Windows, set the PlatformArgs.Handled property to true if changing DragStartingEventArgs.")]
public bool Handled { get; set; }
/// <include file="../../../docs/Microsoft.Maui.Controls/DragStartingEventArgs.xml" path="//Member[@MemberName='Cancel']/Docs/*" />
public bool Cancel { get; set; }
/// <include file="../../../docs/Microsoft.Maui.Controls/DragStartingEventArgs.xml" path="//Member[@MemberName='Data']/Docs/*" />
public DataPackage Data { get; private set; }

/// <summary>
/// Gets the platform-specific arguments associated with the <see cref="DragStartingEventArgs"/>.
/// </summary>
public PlatformDragStartingEventArgs PlatformArgs { get; }
}
}
18 changes: 17 additions & 1 deletion src/Controls/src/Core/DragAndDrop/DropCompletedEventArgs.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
#nullable disable
using System;

namespace Microsoft.Maui.Controls
Expand All @@ -7,5 +6,22 @@ namespace Microsoft.Maui.Controls
public class DropCompletedEventArgs : EventArgs
{
DataPackageOperation DropResult { get; }

/// <summary>
/// Initializes a new instance of the <see cref="DropCompletedEventArgs"/> class.
/// </summary>
public DropCompletedEventArgs()
{
}
Comment on lines +13 to +15
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we add all the non-platform args ctors to the banned apis? Then we don't accidentally use the wrong one?


internal DropCompletedEventArgs(PlatformDropCompletedEventArgs platformArgs) : this()
{
PlatformArgs = platformArgs;
}

/// <summary>
/// Gets the platform-specific arguments associated with the <see cref="DropCompletedEventArgs"/>.
/// </summary>
public PlatformDropCompletedEventArgs? PlatformArgs { get; }
}
}
11 changes: 11 additions & 0 deletions src/Controls/src/Core/DragAndDrop/DropEventArgs.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,20 @@ public DropEventArgs(DataPackageView view)
Data = view;
}

internal DropEventArgs(DataPackageView view, PlatformDropEventArgs platformArgs) : this(view)
{
PlatformArgs = platformArgs;
}

/// <include file="../../../docs/Microsoft.Maui.Controls/DropEventArgs.xml" path="//Member[@MemberName='Data']/Docs/*" />
public DataPackageView Data { get; }

/// <include file="../../../docs/Microsoft.Maui.Controls/DropEventArgs.xml" path="//Member[@MemberName='Handled']/Docs/*" />
public bool Handled { get; set; }

/// <summary>
/// Gets the platform-specific arguments associated with the <see cref="DropEventArgs"/>.
/// </summary>
public PlatformDropEventArgs PlatformArgs { get; }
}
}
Loading