Skip to content

Commit

Permalink
Android Gestures (#1489)
Browse files Browse the repository at this point in the history
* Android Gestures

* - fix drag and drop pages

* - fix setting virtual view

* - cleanup

* - cleanup

* - fix content

* - cleanup

* - cleanup

* - only show click on catalyst and windows

* - fix unit tests

* - remove editorBrowsable where I could

* - fix iOS

* - fix winui

* - change to interfaces

* - fix validation service check

* - fix ios gallery

* Update GestureManager.cs

* Update GestureManager.cs

* Update GestureManager.cs

* - organize
  • Loading branch information
PureWeen authored Jun 30, 2021
1 parent 0ae9b74 commit f245183
Show file tree
Hide file tree
Showing 46 changed files with 1,490 additions and 193 deletions.
17 changes: 17 additions & 0 deletions Microsoft.Maui-net6.sln
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,12 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Microsoft.Maui.Extensions",
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Maui.Controls.Sample.Profiling", "src\Controls\samples\Controls.Sample.Profiling\Maui.Controls.Sample.Profiling.csproj", "{75DB635C-0035-46E7-9A33-857D38082503}"
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "test", "test", "{8F075D72-B023-4B5A-AB5E-43CA38B6CE92}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Controls.Core.UnitTests-net6", "src\Controls\tests\Core.UnitTests\Controls.Core.UnitTests-net6.csproj", "{4FA0DFAE-1C4A-46BC-BC56-ABB12A0DAFBF}"
EndProject
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Controls.Maps-net6", "src\Controls\Maps\src\Controls.Maps-net6.csproj", "{09D4CF7F-7B0D-45FE-B017-561B2A981912}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -281,6 +287,14 @@ Global
{75DB635C-0035-46E7-9A33-857D38082503}.Release|Any CPU.ActiveCfg = Release|Any CPU
{75DB635C-0035-46E7-9A33-857D38082503}.Release|Any CPU.Build.0 = Release|Any CPU
{75DB635C-0035-46E7-9A33-857D38082503}.Release|Any CPU.Deploy.0 = Release|Any CPU
{4FA0DFAE-1C4A-46BC-BC56-ABB12A0DAFBF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{4FA0DFAE-1C4A-46BC-BC56-ABB12A0DAFBF}.Debug|Any CPU.Build.0 = Debug|Any CPU
{4FA0DFAE-1C4A-46BC-BC56-ABB12A0DAFBF}.Release|Any CPU.ActiveCfg = Release|Any CPU
{4FA0DFAE-1C4A-46BC-BC56-ABB12A0DAFBF}.Release|Any CPU.Build.0 = Release|Any CPU
{09D4CF7F-7B0D-45FE-B017-561B2A981912}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{09D4CF7F-7B0D-45FE-B017-561B2A981912}.Debug|Any CPU.Build.0 = Debug|Any CPU
{09D4CF7F-7B0D-45FE-B017-561B2A981912}.Release|Any CPU.ActiveCfg = Release|Any CPU
{09D4CF7F-7B0D-45FE-B017-561B2A981912}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down Expand Up @@ -331,6 +345,9 @@ Global
{081EE5E5-69D4-493C-9EB4-47423C4728AB} = {EC63FD88-5E12-46D7-B440-68F70241D987}
{6EDE0C1B-B718-4331-99BA-423042CDBE24} = {EC63FD88-5E12-46D7-B440-68F70241D987}
{75DB635C-0035-46E7-9A33-857D38082503} = {E1082E26-D700-4127-9329-66D673FD2D55}
{8F075D72-B023-4B5A-AB5E-43CA38B6CE92} = {459BF674-83CB-46F6-881F-A2D2117DBF4D}
{4FA0DFAE-1C4A-46BC-BC56-ABB12A0DAFBF} = {8F075D72-B023-4B5A-AB5E-43CA38B6CE92}
{09D4CF7F-7B0D-45FE-B017-561B2A981912} = {50C758FE-4E10-409A-94F5-A75480960864}
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {0B8ABEAD-D2B5-4370-A187-62B5ABE4EE50}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using Microsoft.Maui.Controls;
using Microsoft.Maui.Controls.Compatibility.ControlGallery.Android;
using Microsoft.Maui.Controls.Compatibility.ControlGallery;
using Microsoft.Extensions.DependencyInjection;

[assembly: Dependency(typeof(RegistrarValidationService))]
namespace Microsoft.Maui.Controls.Compatibility.ControlGallery.Android
Expand All @@ -21,7 +22,15 @@ public bool Validate(VisualElement element, out string message)
if (element == null)
return true;

var renderer = Platform.Android.Platform.CreateRendererWithContext(element, _context);
object renderer = Platform.Android.Platform.CreateRendererWithContext(element, _context);

if (renderer == null
|| renderer.GetType().Name == "DefaultRenderer"
)
{
var sp = MauiApplication.Current.Services.GetRequiredService<IMauiHandlersServiceProvider>();
renderer = sp.GetHandler(element.GetType());
}

if (renderer == null
|| renderer.GetType().Name == "DefaultRenderer"
Expand Down
13 changes: 11 additions & 2 deletions src/Compatibility/ControlGallery/src/Core/CoreGallery.cs
Original file line number Diff line number Diff line change
Expand Up @@ -552,12 +552,21 @@ public void FilterPages(string filter)
[Preserve(AllMembers = true)]
internal class CoreRootPage : ContentPage
{
bool registrarValidated;
CoreRootView CoreRootView { get; }

public CoreRootPage(Page rootPage, NavigationBehavior navigationBehavior = NavigationBehavior.PushAsync)
protected override void OnAttachedHandler()
{
ValidateRegistrar();
base.OnAttachedHandler();

if(!registrarValidated)
ValidateRegistrar();

registrarValidated = true;
}

public CoreRootPage(Page rootPage, NavigationBehavior navigationBehavior = NavigationBehavior.PushAsync)
{
var galleryFactory = DependencyService.Get<IPlatformSpecificCoreGalleryFactory>();

Title = galleryFactory?.Title ?? "Core Gallery";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

namespace Microsoft.Maui.Controls.Compatibility.Platform.Android
{
internal class DragAndDropGestureHandler : Java.Lang.Object, AView.IOnDragListener
class DragAndDropGestureHandler : Java.Lang.Object, AView.IOnDragListener
{
bool _isDisposed;
CustomLocalStateData _currentCustomLocalStateData;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ public override void Draw(Canvas canvas)

public override bool OnTouchEvent(MotionEvent e)
{
if (_visualElementRenderer.OnTouchEvent(e) || base.OnTouchEvent(e))
if (base.OnTouchEvent(e))
{
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ protected virtual void OnElementChanged(ElementChangedEventArgs<Image> e)

public override bool OnTouchEvent(MotionEvent e)
{
if (_visualElementRenderer.OnTouchEvent(e) || base.OnTouchEvent(e))
if (base.OnTouchEvent(e))
{
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ protected override void Dispose(bool disposing)

public override bool OnTouchEvent(MotionEvent e)
{
if (_visualElementRenderer.OnTouchEvent(e) || base.OnTouchEvent(e))
if (base.OnTouchEvent(e))
{
return true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ public sealed class VisualElementRenderer : IDisposable, IEffectControlProvider,
bool _disposed;

IVisualElementRenderer _renderer;
readonly GestureManager _gestureManager;
readonly AutomationPropertiesProvider _automationPropertiesProvider;
readonly EffectControlProvider _effectControlProvider;

Expand All @@ -21,7 +20,6 @@ public VisualElementRenderer(IVisualElementRenderer renderer)
_renderer = renderer;
_renderer.ElementPropertyChanged += OnElementPropertyChanged;
_renderer.ElementChanged += OnElementChanged;
_gestureManager = new GestureManager(_renderer);
_automationPropertiesProvider = new AutomationPropertiesProvider(_renderer);

_effectControlProvider = new EffectControlProvider(_renderer?.View);
Expand All @@ -46,11 +44,6 @@ void UpdateFlowDirection()
Control.UpdateFlowDirection(Element);
}

public bool OnTouchEvent(MotionEvent e)
{
return _gestureManager.OnTouchEvent(e);
}

public void Dispose()
{
Dispose(true);
Expand Down Expand Up @@ -80,7 +73,6 @@ void Dispose(bool disposing)
_renderer = null;
}

_gestureManager?.Dispose();
_automationPropertiesProvider?.Dispose();
}
}
Expand All @@ -101,6 +93,7 @@ void OnElementChanged(object sender, VisualElementChangedEventArgs e)
}

EffectUtilities.RegisterEffectControlProvider(this, e.OldElement, e.NewElement);

Performance.Stop(reference);
}

Expand Down
14 changes: 0 additions & 14 deletions src/Compatibility/Core/src/Android/VisualElementRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,8 @@ public abstract class VisualElementRenderer<TElement> : FormsViewGroup, IVisualE
VisualElementPackager _packager;
PropertyChangedEventHandler _propertyChangeHandler;

GestureManager _gestureManager;

protected VisualElementRenderer(Context context) : base(context)
{
_gestureManager = new GestureManager(this);
}

public override bool OnTouchEvent(MotionEvent e)
{
return _gestureManager.OnTouchEvent(e) || base.OnTouchEvent(e);
}

public override bool OnInterceptTouchEvent(MotionEvent ev)
Expand Down Expand Up @@ -302,12 +294,6 @@ protected override void Dispose(bool disposing)
_packager = null;
}

if (_gestureManager != null)
{
_gestureManager.Dispose();
_gestureManager = null;
}

if (ManageNativeControlLifetime)
{
while (ChildCount > 0)
Expand Down
5 changes: 2 additions & 3 deletions src/Compatibility/Core/src/RendererToHandlerShim.cs
Original file line number Diff line number Diff line change
Expand Up @@ -144,10 +144,9 @@ public override void SetVirtualView(IView view)
{
VisualElementRenderer.SetElement((VisualElement)view);
}
else if(view != VirtualView)
{

if (view != VirtualView)
base.SetVirtualView(view);
}
}
#else
protected override NativeView CreateNativeView()
Expand Down
8 changes: 4 additions & 4 deletions src/Compatibility/Core/src/WinUI/VisualElementTracker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -433,7 +433,7 @@ void HandlePan(ManipulationDeltaRoutedEventArgs e, View view)

_isPanning = true;

foreach (PanGestureRecognizer recognizer in view.GestureRecognizers.GetGesturesFor<PanGestureRecognizer>().Where(g => g.TouchPoints == _fingers.Count))
foreach (IPanGestureController recognizer in view.GestureRecognizers.GetGesturesFor<PanGestureRecognizer>().Where(g => g.TouchPoints == _fingers.Count))
{
if (!_wasPanGestureStartedSent)
{
Expand All @@ -455,7 +455,7 @@ void HandlePinch(ManipulationDeltaRoutedEventArgs e, View view)

var scaleOriginPoint = new Point(translationPoint.X / view.Width, translationPoint.Y / view.Height);
IEnumerable<PinchGestureRecognizer> pinchGestures = view.GestureRecognizers.GetGesturesFor<PinchGestureRecognizer>();
foreach (PinchGestureRecognizer recognizer in pinchGestures)
foreach (IPinchGestureController recognizer in pinchGestures)
{
if (!_wasPinchGestureStartedSent)
{
Expand Down Expand Up @@ -632,7 +632,7 @@ void PanComplete(bool success)
if (view == null || !_isPanning)
return;

foreach (PanGestureRecognizer recognizer in view.GestureRecognizers.GetGesturesFor<PanGestureRecognizer>().Where(g => g.TouchPoints == _fingers.Count))
foreach (IPanGestureController recognizer in view.GestureRecognizers.GetGesturesFor<PanGestureRecognizer>().Where(g => g.TouchPoints == _fingers.Count))
{
if (success)
{
Expand All @@ -655,7 +655,7 @@ void PinchComplete(bool success)
return;

IEnumerable<PinchGestureRecognizer> pinchGestures = view.GestureRecognizers.GetGesturesFor<PinchGestureRecognizer>();
foreach (PinchGestureRecognizer recognizer in pinchGestures)
foreach (IPinchGestureController recognizer in pinchGestures)
{
if (success)
{
Expand Down
6 changes: 3 additions & 3 deletions src/Compatibility/Core/src/iOS/EventTracker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -310,13 +310,13 @@ protected virtual NativeGestureRecognizer GetNativeRecognizer(IGestureRecognizer
#endif
}

var pinchRecognizer = recognizer as PinchGestureRecognizer;
var pinchRecognizer = recognizer as IPinchGestureController;
if (pinchRecognizer != null)
{
double startingScale = 1;
var uiRecognizer = CreatePinchRecognizer(r =>
{
var pinchGestureRecognizer = weakRecognizer.Target as PinchGestureRecognizer;
var pinchGestureRecognizer = weakRecognizer.Target as IPinchGestureController;
var eventTracker = weakEventTracker.Target as EventTracker;
var view = eventTracker?._renderer?.Element as View;
Expand Down Expand Up @@ -387,7 +387,7 @@ protected virtual NativeGestureRecognizer GetNativeRecognizer(IGestureRecognizer
var eventTracker = weakEventTracker.Target as EventTracker;
var view = eventTracker?._renderer?.Element as View;
var panGestureRecognizer = weakRecognizer.Target as PanGestureRecognizer;
var panGestureRecognizer = weakRecognizer.Target as IPanGestureController;
if (panGestureRecognizer != null && view != null)
{
switch (r.State)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
using System;
using Maui.Controls.Sample.Pages.Base;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Graphics;

namespace Maui.Controls.Sample.Pages.Gestures
{
public class ClickGestureGalleryPage : BasePage
{
Command clickCommand;
BoxView changeColorBoxView;

public ClickGestureGalleryPage()
{
clickCommand = new Command<Color>(HandleClickCommand);
var vertical = new StackLayout
{
VerticalOptions = LayoutOptions.Center,
HorizontalOptions = LayoutOptions.Center,
Spacing = 40
};

var horizontal = new StackLayout
{
Orientation = StackOrientation.Horizontal,
Spacing = 20,
HorizontalOptions = LayoutOptions.Center,
VerticalOptions = LayoutOptions.Center
};
vertical.Children.Add(horizontal);

var singleClickLabel = new Label
{
Text = "Click me!",
BackgroundColor = Colors.PaleGreen
};
var singleClickGesture = new ClickGestureRecognizer
{
Command = clickCommand,
CommandParameter = Colors.PaleGreen,
NumberOfClicksRequired = 1,
Buttons = ButtonsMask.Primary
};
singleClickLabel.GestureRecognizers.Add(singleClickGesture);
horizontal.Children.Add(singleClickLabel);

var doubleClickLabel = new Label
{
Text = "Double click me!!",
BackgroundColor = Colors.Aqua
};
var doubleClickGesture = new ClickGestureRecognizer
{
Command = clickCommand,
CommandParameter = Colors.Aqua,
NumberOfClicksRequired = 2,
Buttons = ButtonsMask.Primary
};
doubleClickLabel.GestureRecognizers.Add(doubleClickGesture);
horizontal.Children.Add(doubleClickLabel);

var tripleClicklabel = new Label
{
Text = "Triple click me!!!",
BackgroundColor = Colors.Olive
};
var tripleClickGesture = new ClickGestureRecognizer
{
Command = clickCommand,
CommandParameter = Colors.Olive,
NumberOfClicksRequired = 3,
Buttons = ButtonsMask.Primary
};
tripleClicklabel.GestureRecognizers.Add(tripleClickGesture);
horizontal.Children.Add(tripleClicklabel);

var rightClickLabel = new Label
{
Text = "Right click me¡",
BackgroundColor = Colors.Coral
};
var rigthClickGesture = new ClickGestureRecognizer
{
Command = clickCommand,
CommandParameter = Colors.Coral,
NumberOfClicksRequired = 1,
Buttons = ButtonsMask.Secondary
};
rightClickLabel.GestureRecognizers.Add(rigthClickGesture);
horizontal.Children.Add(rightClickLabel);

var doubleRightClickLabel = new Label
{
Text = "Double right click me¡¡",
BackgroundColor = Colors.Gold
};
var doubleRigthClickGesture = new ClickGestureRecognizer
{
Command = clickCommand,
CommandParameter = Colors.Gold,
NumberOfClicksRequired = 2,
Buttons = ButtonsMask.Secondary
};
doubleRightClickLabel.GestureRecognizers.Add(doubleRigthClickGesture);
horizontal.Children.Add(doubleRightClickLabel);


changeColorBoxView = new BoxView
{
VerticalOptions = LayoutOptions.CenterAndExpand,
HorizontalOptions = LayoutOptions.CenterAndExpand,
WidthRequest = 200,
HeightRequest = 50
};
vertical.Children.Add(changeColorBoxView);
Content = vertical;
}

void HandleClickCommand(Color backgroundColor)
{
changeColorBoxView.BackgroundColor = backgroundColor;
}
}
}
Loading

0 comments on commit f245183

Please sign in to comment.