Skip to content
This repository has been archived by the owner on May 1, 2024. It is now read-only.

Commit

Permalink
Drag and drop (#11537)
Browse files Browse the repository at this point in the history
* 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
PureWeen committed Jul 30, 2020
1 parent fa74c73 commit bc51d24
Show file tree
Hide file tree
Showing 29 changed files with 1,805 additions and 32 deletions.
10 changes: 9 additions & 1 deletion Xamarin.Forms.Controls/CoreGallery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
using Xamarin.Forms.Controls.GalleryPages.RadioButtonGalleries;
using Xamarin.Forms.Controls.GalleryPages.ShapesGalleries;
using Xamarin.Forms.Controls.GalleryPages.GradientGalleries;
using Xamarin.Forms.Controls.GalleryPages.DragAndDropGalleries;

namespace Xamarin.Forms.Controls
{
Expand Down Expand Up @@ -344,6 +345,7 @@ public override string ToString()
new GalleryPageFactory(() => new ButtonBorderBackgroundGalleryPage(VisualMarker.Material), "Button Border & Background Gallery (Material)"),
new GalleryPageFactory(() => new CheckBoxCoreGalleryPage(), "CheckBox Gallery"),
new GalleryPageFactory(() => new DatePickerCoreGalleryPage(), "DatePicker Gallery"),
new GalleryPageFactory(() => new DragAndDropGallery(), "Drag and Drop Gallery"),
new GalleryPageFactory(() => new EditorCoreGalleryPage(), "Editor Gallery"),
new GalleryPageFactory(() => new FrameCoreGalleryPage(), "Frame Gallery"),
new GalleryPageFactory(() => new GradientsGallery(), "Brushes Gallery"),
Expand Down Expand Up @@ -499,7 +501,13 @@ public CorePageView(Page rootPage, NavigationBehavior navigationBehavior = Navig
var item = args.SelectedItem;
var page = item as GalleryPageFactory;
if (page != null)
await PushPage(page.Realize());
{
var realize = page.Realize();
if (realize is Shell)
Application.Current.MainPage = realize;
else
await PushPage(realize);
}
SelectedItem = null;
};
Expand Down
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());
}
}
}
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;
}
}
}
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;
}
}
}
Loading

0 comments on commit bc51d24

Please sign in to comment.