Skip to content

Commit

Permalink
Add Drag and Drop GestureRecognizers (#168)
Browse files Browse the repository at this point in the history
  • Loading branch information
Dreamescaper authored Nov 13, 2024
1 parent 259a16b commit 32d2632
Show file tree
Hide file tree
Showing 6 changed files with 246 additions and 1 deletion.
1 change: 1 addition & 0 deletions samples/ControlGallery/AppShell.razor
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@
<ShadowsPage />
<GestureEvents />
<GestureRecognizersPage />
<DragAndDropPage />
<ErrorBoundariesPage />

@if (DeviceInfo.Platform == DevicePlatform.WinUI || DeviceInfo.Platform == DevicePlatform.MacCatalyst)
Expand Down
78 changes: 78 additions & 0 deletions samples/ControlGallery/Views/Gestures/DragAndDropPage.razor
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
@using Microsoft.Maui.Layouts

<ContentPage Title="Drag And Drop Gestures">
<Grid RowDefinitions="*,40">
<ScrollView Grid.Row="0" Margin="16">
<VerticalStackLayout>
<Label Text="Try dragging boxes below" />

<FlexLayout Wrap="FlexWrap.Wrap">
@foreach (var color in _colors)
{
<BoxView Color="color" HeightRequest="50" WidthRequest="50" Margin="5">
<GestureRecognizers>
<DragGestureRecognizer OnDragStarting="a => DragStarted(a, color)"
OnDropCompleted="Dropped" />
</GestureRecognizers>
</BoxView>
}
</FlexLayout>
</VerticalStackLayout>
</ScrollView>

<Border Grid.Row="1" BackgroundColor="Colors.Red">
<GestureRecognizers>
<DropGestureRecognizer OnDrop="Delete" />
</GestureRecognizers>

<ChildContent>
<Label Text="DELETE"
VerticalOptions="LayoutOptions.Center"
HorizontalOptions="LayoutOptions.Center"
IsVisible="_dragInProgress" />
</ChildContent>
</Border>
</Grid>

</ContentPage>

@code {
bool _dragInProgress;

List<Color> _colors =
[
Colors.AliceBlue,
Colors.Aqua,
Colors.Azure,
Colors.Beige,
Colors.Bisque,
Colors.Black,
Colors.Blue,
Colors.Brown,
Colors.BurlyWood,
Colors.Crimson,
Colors.DarkGray,
Colors.CornflowerBlue,
Colors.DarkOliveGreen,
Colors.Fuchsia,
Colors.AliceBlue,
Colors.DarkSeaGreen
];

void DragStarted(DragStartingEventArgs args, Color color)
{
_dragInProgress = true;
args.Data.Properties["Color"] = color;
}

void Dropped()
{
_dragInProgress = false;
}

void Delete(DropEventArgs args)
{
var color = (Color)args.Data.Properties["Color"];
_colors.Remove(color);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// <auto-generated>
// This code was generated by a BlazorBindings.Maui component generator.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>

using BlazorBindings.Core;
using MC = Microsoft.Maui.Controls;
using Microsoft.AspNetCore.Components;
using System.Threading.Tasks;

#pragma warning disable MBB001

namespace BlazorBindings.Maui.Elements
{
/// <summary>
/// Provides drag gesture recognition and defines the associated events for dragging and dropping.
/// </summary>
public partial class DragGestureRecognizer : GestureRecognizer
{
static DragGestureRecognizer()
{
RegisterAdditionalHandlers();
}

/// <summary>
/// Gets or sets the value which indicates whether the element the gesture recognizer is attached to can be a drag source.
/// </summary>
[Parameter] public bool? CanDrag { get; set; }
[Parameter] public EventCallback<MC.DropCompletedEventArgs> OnDropCompleted { get; set; }
[Parameter] public EventCallback<MC.DragStartingEventArgs> OnDragStarting { get; set; }

public new MC.DragGestureRecognizer NativeControl => (MC.DragGestureRecognizer)((BindableObject)this).NativeControl;

protected override MC.DragGestureRecognizer CreateNativeElement() => new();

protected override void HandleParameter(string name, object value)
{
switch (name)
{
case nameof(CanDrag):
if (!Equals(CanDrag, value))
{
CanDrag = (bool?)value;
NativeControl.CanDrag = CanDrag ?? (bool)MC.DragGestureRecognizer.CanDragProperty.DefaultValue;
}
break;
case nameof(OnDropCompleted):
if (!Equals(OnDropCompleted, value))
{
void NativeControlDropCompleted(object sender, MC.DropCompletedEventArgs e) => InvokeEventCallback(OnDropCompleted, e);

OnDropCompleted = (EventCallback<MC.DropCompletedEventArgs>)value;
NativeControl.DropCompleted -= NativeControlDropCompleted;
NativeControl.DropCompleted += NativeControlDropCompleted;
}
break;
case nameof(OnDragStarting):
if (!Equals(OnDragStarting, value))
{
void NativeControlDragStarting(object sender, MC.DragStartingEventArgs e) => InvokeEventCallback(OnDragStarting, e);

OnDragStarting = (EventCallback<MC.DragStartingEventArgs>)value;
NativeControl.DragStarting -= NativeControlDragStarting;
NativeControl.DragStarting += NativeControlDragStarting;
}
break;

default:
base.HandleParameter(name, value);
break;
}
}

static partial void RegisterAdditionalHandlers();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
// <auto-generated>
// This code was generated by a BlazorBindings.Maui component generator.
//
// Changes to this file may cause incorrect behavior and will be lost if
// the code is regenerated.
// </auto-generated>

using BlazorBindings.Core;
using MC = Microsoft.Maui.Controls;
using Microsoft.AspNetCore.Components;
using System.Threading.Tasks;

#pragma warning disable MBB001

namespace BlazorBindings.Maui.Elements
{
public partial class DropGestureRecognizer : GestureRecognizer
{
static DropGestureRecognizer()
{
RegisterAdditionalHandlers();
}

[Parameter] public bool? AllowDrop { get; set; }
[Parameter] public EventCallback<MC.DragEventArgs> OnDragLeave { get; set; }
[Parameter] public EventCallback<MC.DragEventArgs> OnDragOver { get; set; }
[Parameter] public EventCallback<MC.DropEventArgs> OnDrop { get; set; }

public new MC.DropGestureRecognizer NativeControl => (MC.DropGestureRecognizer)((BindableObject)this).NativeControl;

protected override MC.DropGestureRecognizer CreateNativeElement() => new();

protected override void HandleParameter(string name, object value)
{
switch (name)
{
case nameof(AllowDrop):
if (!Equals(AllowDrop, value))
{
AllowDrop = (bool?)value;
NativeControl.AllowDrop = AllowDrop ?? (bool)MC.DropGestureRecognizer.AllowDropProperty.DefaultValue;
}
break;
case nameof(OnDragLeave):
if (!Equals(OnDragLeave, value))
{
void NativeControlDragLeave(object sender, MC.DragEventArgs e) => InvokeEventCallback(OnDragLeave, e);

OnDragLeave = (EventCallback<MC.DragEventArgs>)value;
NativeControl.DragLeave -= NativeControlDragLeave;
NativeControl.DragLeave += NativeControlDragLeave;
}
break;
case nameof(OnDragOver):
if (!Equals(OnDragOver, value))
{
void NativeControlDragOver(object sender, MC.DragEventArgs e) => InvokeEventCallback(OnDragOver, e);

OnDragOver = (EventCallback<MC.DragEventArgs>)value;
NativeControl.DragOver -= NativeControlDragOver;
NativeControl.DragOver += NativeControlDragOver;
}
break;
case nameof(OnDrop):
if (!Equals(OnDrop, value))
{
void NativeControlDrop(object sender, MC.DropEventArgs e) => InvokeEventCallback(OnDrop, e);

OnDrop = (EventCallback<MC.DropEventArgs>)value;
NativeControl.Drop -= NativeControlDrop;
NativeControl.Drop += NativeControlDrop;
}
break;

default:
base.HandleParameter(name, value);
break;
}
}

static partial void RegisterAdditionalHandlers();
}
}
5 changes: 4 additions & 1 deletion src/BlazorBindings.Maui/Elements/StackLayout.generated.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,11 @@ static StackLayout()
}

/// <summary>
/// Gets or sets the value which indicates the direction which child elements are positioned. Default value is <see cref="F:Microsoft.Maui.Controls.StackOrientation.Vertical" />.
/// Gets or sets the value which indicates the direction which child elements are positioned.
/// </summary>
/// <value>
/// A <see cref="T:Microsoft.Maui.Controls.StackOrientation" /> which indicates the direction children layouts flow. The default value is Vertical.
/// </value>
[Parameter] public MC.StackOrientation? Orientation { get; set; }

public new MC.StackLayout NativeControl => (MC.StackLayout)((BindableObject)this).NativeControl;
Expand Down
2 changes: 2 additions & 0 deletions src/BlazorBindings.Maui/Properties/AttributeInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,8 @@
[assembly: GenerateComponent(typeof(SwipeGestureRecognizer))]
[assembly: GenerateComponent(typeof(TapGestureRecognizer))]
[assembly: GenerateComponent(typeof(PointerGestureRecognizer))]
[assembly: GenerateComponent(typeof(DragGestureRecognizer))]
[assembly: GenerateComponent(typeof(DropGestureRecognizer))]


// Compatibility
Expand Down

0 comments on commit 32d2632

Please sign in to comment.