-
-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add Drag and Drop GestureRecognizers (#168)
- Loading branch information
1 parent
259a16b
commit 32d2632
Showing
6 changed files
with
246 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
78 changes: 78 additions & 0 deletions
78
samples/ControlGallery/Views/Gestures/DragAndDropPage.razor
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
78 changes: 78 additions & 0 deletions
78
src/BlazorBindings.Maui/Elements/DragGestureRecognizer.generated.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
83 changes: 83 additions & 0 deletions
83
src/BlazorBindings.Maui/Elements/DropGestureRecognizer.generated.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters