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

Add Drag and Drop GestureRecognizers #168

Merged
merged 1 commit into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
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 @@ -50,7 +50,7 @@
[assembly: GenerateComponent(typeof(FlexLayout))]
[assembly: GenerateComponent(typeof(FlyoutItem))]
[assembly: GenerateComponent(typeof(FlyoutPage), Exclude = [nameof(FlyoutPage.Detail)])]
[assembly: GenerateComponent(typeof(Frame))]

Check warning on line 53 in src/BlazorBindings.Maui/Properties/AttributeInfo.cs

View workflow job for this annotation

GitHub Actions / build

'Frame' is obsolete: 'Frame is obsolete as of .NET 9. Please use Border instead.'
[assembly: GenerateComponent(typeof(GestureElement))]
[assembly: GenerateComponent(typeof(GradientBrush))]
[assembly: GenerateComponent(typeof(GradientStop))]
Expand Down Expand Up @@ -185,10 +185,12 @@
[assembly: GenerateComponent(typeof(SwipeGestureRecognizer))]
[assembly: GenerateComponent(typeof(TapGestureRecognizer))]
[assembly: GenerateComponent(typeof(PointerGestureRecognizer))]
[assembly: GenerateComponent(typeof(DragGestureRecognizer))]
[assembly: GenerateComponent(typeof(DropGestureRecognizer))]


// Compatibility
[assembly: GenerateComponent(typeof(Microsoft.Maui.Controls.Compatibility.Layout))]

Check warning on line 193 in src/BlazorBindings.Maui/Properties/AttributeInfo.cs

View workflow job for this annotation

GitHub Actions / build

'Layout' is obsolete: 'Use Microsoft.Maui.Controls.Layout instead. For more information, see https://learn.microsoft.com/dotnet/maui/user-interface/layouts/custom'

// Shapes
[assembly: GenerateComponent(typeof(Ellipse))]
Expand Down
Loading