This repository has been archived by the owner on May 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Drag and Drop Gesture Recognizers * - wire up gallery * - fix android completed * - wire up completed ios * - fix macos build * - add exclusion * - add commands, tests, and additional galleryies * - create image for ios drag * - fix macos build issue and add flag * - set flag on unit tests * Update Xamarin.Forms.Controls/GalleryPages/DragAndDropGalleries/EnablingAndDisablingGestureTests.cs Co-authored-by: Samantha Houts <samhouts@users.noreply.github.com> * - fix null check * - ios null check * - fix uwp check * - fix sample Co-authored-by: Samantha Houts <samhouts@users.noreply.github.com> fixes #10778
- Loading branch information
Showing
29 changed files
with
1,805 additions
and
32 deletions.
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
16 changes: 16 additions & 0 deletions
16
Xamarin.Forms.Controls/GalleryPages/DragAndDropGalleries/DragAndDropGallery.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,16 @@ | ||
using System.Collections.Generic; | ||
using Xamarin.Forms.Internals; | ||
|
||
namespace Xamarin.Forms.Controls.GalleryPages.DragAndDropGalleries | ||
{ | ||
[Preserve(AllMembers = true)] | ||
public class DragAndDropGallery : Shell | ||
{ | ||
public DragAndDropGallery() | ||
{ | ||
Device.SetFlags(new List<string> { ExperimentalFlags.DragAndDropExperimental, ExperimentalFlags.ShellUWPExperimental }); | ||
Items.Add(new EnablingAndDisablingGestureTests()); | ||
Items.Add(new VariousDragAndDropPermutations()); | ||
} | ||
} | ||
} |
141 changes: 141 additions & 0 deletions
141
Xamarin.Forms.Controls/GalleryPages/DragAndDropGalleries/EnablingAndDisablingGestureTests.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,141 @@ | ||
using System.Collections.Generic; | ||
using System.Collections.ObjectModel; | ||
using System.Linq; | ||
using System.Text; | ||
using Xamarin.Forms.Internals; | ||
|
||
namespace Xamarin.Forms.Controls.GalleryPages.DragAndDropGalleries | ||
{ | ||
|
||
|
||
[Preserve(AllMembers = true)] | ||
public class EnablingAndDisablingGestureTests : ContentPage | ||
{ | ||
public EnablingAndDisablingGestureTests() | ||
{ | ||
Title = "Enabling and Disabling Gestures"; | ||
StackLayout stackLayout = new StackLayout(); | ||
CollectionView collectionView = new CollectionView(); | ||
collectionView.ItemsUpdatingScrollMode = ItemsUpdatingScrollMode.KeepScrollOffset; | ||
ObservableCollection<string> observableCollection = new ObservableCollection<string>(); | ||
collectionView.ItemsSource = observableCollection; | ||
|
||
Image imageSource = new Image() | ||
{ | ||
Source = "coffee.png", | ||
BackgroundColor = Color.Green | ||
}; | ||
|
||
Image imageDestination = new Image() | ||
{ | ||
BackgroundColor = Color.Purple, | ||
HeightRequest = 50, | ||
WidthRequest = 50 | ||
}; | ||
|
||
Button addRemoveDragGesture = new Button() | ||
{ | ||
Text = "Add/Remove Drag Gesture", | ||
Command = new Command(() => | ||
{ | ||
var dragGestureRecognizer = imageSource.GestureRecognizers.OfType<DragGestureRecognizer>() | ||
.FirstOrDefault(); | ||
if (dragGestureRecognizer != null) | ||
imageSource.GestureRecognizers.Remove(dragGestureRecognizer); | ||
else | ||
{ | ||
var dragGesture = new DragGestureRecognizer() | ||
{ | ||
CanDrag = true | ||
}; | ||
dragGesture.DragStarting += (_, args) => | ||
{ | ||
observableCollection.Insert(0, $"DragStarting"); | ||
}; | ||
dragGesture.DropCompleted += (_, args) => | ||
{ | ||
observableCollection.Insert(0, $"DropCompleted"); | ||
}; | ||
imageSource.GestureRecognizers.Add(dragGesture); | ||
} | ||
}) | ||
}; | ||
|
||
Button toggleCanDrag = new Button() | ||
{ | ||
Text = "Toggle Can Drag", | ||
Command = new Command(() => | ||
{ | ||
var dragGestureRecognizer = imageSource.GestureRecognizers.OfType<DragGestureRecognizer>() | ||
.FirstOrDefault(); | ||
if (dragGestureRecognizer != null) | ||
dragGestureRecognizer.CanDrag = !dragGestureRecognizer.CanDrag; | ||
}) | ||
}; | ||
|
||
|
||
|
||
Button addRemoveDropGesture = new Button() | ||
{ | ||
Text = "Add/Remove Drop Gesture", | ||
Command = new Command(() => | ||
{ | ||
var dropGestureRecognizer = imageDestination.GestureRecognizers.OfType<DropGestureRecognizer>() | ||
.FirstOrDefault(); | ||
if (dropGestureRecognizer != null) | ||
imageDestination.GestureRecognizers.Remove(dropGestureRecognizer); | ||
else | ||
{ | ||
var dropGesture = new DropGestureRecognizer() | ||
{ | ||
AllowDrop = true | ||
}; | ||
dropGesture.Drop += (_, args) => | ||
{ | ||
observableCollection.Insert(0, $"Drop"); | ||
}; | ||
dropGesture.DragOver += (_, args) => | ||
{ | ||
observableCollection.Insert(0, $"DragOver"); | ||
args.AcceptedOperation = DataPackageOperation.Copy; | ||
}; | ||
imageDestination.GestureRecognizers.Add(dropGesture); | ||
} | ||
}) | ||
}; | ||
|
||
Button toggleCanDrop = new Button() | ||
{ | ||
Text = "Toggle Can Drop", | ||
Command = new Command(() => | ||
{ | ||
var dropGestureRecognizer = imageDestination.GestureRecognizers.OfType<DropGestureRecognizer>() | ||
.FirstOrDefault(); | ||
if (dropGestureRecognizer != null) | ||
dropGestureRecognizer.AllowDrop = !dropGestureRecognizer.AllowDrop; | ||
}) | ||
}; | ||
|
||
stackLayout.Children.Add(imageSource); | ||
stackLayout.Children.Add(addRemoveDragGesture); | ||
stackLayout.Children.Add(toggleCanDrag); | ||
|
||
stackLayout.Children.Add(imageDestination); | ||
stackLayout.Children.Add(addRemoveDropGesture); | ||
stackLayout.Children.Add(toggleCanDrop); | ||
|
||
stackLayout.Children.Add(collectionView); | ||
Content = stackLayout; | ||
} | ||
} | ||
} |
127 changes: 127 additions & 0 deletions
127
Xamarin.Forms.Controls/GalleryPages/DragAndDropGalleries/VariousDragAndDropPermutations.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,127 @@ | ||
using System; | ||
using Xamarin.Forms.Internals; | ||
using Xamarin.Forms.Markup; | ||
|
||
namespace Xamarin.Forms.Controls.GalleryPages.DragAndDropGalleries | ||
{ | ||
[Preserve(AllMembers = true)] | ||
public class VariousDragAndDropPermutations : ContentPage | ||
{ | ||
public Color DraggingColor { get; set; } | ||
|
||
public VariousDragAndDropPermutations() | ||
{ | ||
Title = "Various Drag And Drop Permutations"; | ||
|
||
StackLayout stackLayout = new StackLayout(); | ||
|
||
stackLayout.Children.Add(CreateControls<Label>((drag, drop) => | ||
{ | ||
drag.Text = "Drag"; | ||
drag.FontSize = 18; | ||
drop.Text = "Drop"; | ||
drop.FontSize = 18; | ||
})); | ||
|
||
stackLayout.Children.Add(CreateControls<Image>((drag, drop) => | ||
{ | ||
drag.HeightRequest = 50; | ||
drag.BackgroundColor = Color.Green; | ||
})); | ||
|
||
stackLayout.Children.Add(CreateControls<Image>((drag, drop) => | ||
{ | ||
drag.Source = "coffee.png"; | ||
drag.BackgroundColor = Color.Green; | ||
})); | ||
|
||
stackLayout.Children.Add(CreateControls<Entry>(dragElementText: "Some text")); | ||
stackLayout.Children.Add(CreateControls<Editor>(dragElementText: "True")); | ||
stackLayout.Children.Add(CreateControls<DatePicker>(dragElementText: "False")); | ||
stackLayout.Children.Add(CreateControls<TimePicker>(dragElementText: $"{DateTime.Now}")); | ||
stackLayout.Children.Add(CreateControls<CheckBox>(dragElementText: $"{DateTime.Now.TimeOfDay}")); | ||
stackLayout.Children.Add(CreateControls<Entry>(dragElementText: "https://github.com/xamarin/Xamarin.Forms/blob/f27f5a3650f37894d4a1ac925d6fab4dc7350087/Xamarin.Forms.ControlGallery.Android/Resources/drawable/oasis.jpg?raw=true")); | ||
stackLayout.Children.Add(CreateControls<StackDrag>()); | ||
|
||
Content = stackLayout; | ||
} | ||
|
||
[Preserve(AllMembers = true)] | ||
class StackDrag : StackLayout | ||
{ | ||
public StackDrag() | ||
{ | ||
Children.Add(new Image() { Source = "coffee.png" }); | ||
Children.Add(new Label { Text = "COFFEE" }); | ||
} | ||
} | ||
|
||
View CreateControls<TView>(Action<TView, TView> action = null, string dragElementText = null) | ||
where TView : View | ||
{ | ||
Grid layout = new Grid(); | ||
|
||
layout.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Star }); | ||
layout.ColumnDefinitions.Add(new ColumnDefinition() { Width = GridLength.Star }); | ||
|
||
View drag = null; | ||
|
||
if (!String.IsNullOrWhiteSpace(dragElementText)) | ||
drag = AddDragGesture((View)new Label() { Text = dragElementText }); | ||
else | ||
drag = AddDragGesture(Activator.CreateInstance<TView>()); | ||
|
||
var drop = AddDropGesture(Activator.CreateInstance<TView>()); | ||
|
||
drop.SetBinding(VisualElement.BackgroundColorProperty, "DraggingColor"); | ||
drop.BindingContext = this; | ||
layout.AddChild(drag, 0, 0); | ||
layout.AddChild(drop, 1, 0); | ||
action?.Invoke((TView)layout.Children[0], (TView)layout.Children[1]); | ||
return layout; | ||
} | ||
|
||
TView AddDragGesture<TView>(TView view) | ||
where TView : View | ||
{ | ||
var dragRecognizer = new DragGestureRecognizer() | ||
{ | ||
CanDrag = true | ||
}; | ||
|
||
dragRecognizer.DragStarting += (_, args) => | ||
{ | ||
DraggingColor = Color.Purple; | ||
OnPropertyChanged(nameof(DraggingColor)); | ||
if(view is StackDrag sd) | ||
{ | ||
args.Data.Image = "coffee.png"; | ||
} | ||
}; | ||
|
||
dragRecognizer.DropCompleted += (_, __) => | ||
{ | ||
DraggingColor = Color.Default; | ||
OnPropertyChanged(nameof(DraggingColor)); | ||
}; | ||
|
||
view.GestureRecognizers.Add(dragRecognizer); | ||
|
||
return view; | ||
} | ||
|
||
TView AddDropGesture<TView>(TView view) | ||
where TView : View | ||
{ | ||
var dropRecognizer = new DropGestureRecognizer() | ||
{ | ||
AllowDrop = true | ||
}; | ||
|
||
view.GestureRecognizers.Add(dropRecognizer); | ||
|
||
return view; | ||
} | ||
} | ||
} |
Oops, something went wrong.