From adac0fcc38295263d08f589b7d564c9b12d9935c Mon Sep 17 00:00:00 2001 From: "E.Z. Hart" Date: Thu, 4 May 2017 03:51:01 -0600 Subject: [PATCH] Give layouts a chance to handle touch events after passing though immediate children (#902) * Repro for Bugzilla 55912 * Automate test * Give the layout a chance to actually handle gestures before bubbling up --- .../Bugzilla55912.cs | 95 +++++++++++++++++++ ...rin.Forms.Controls.Issues.Shared.projitems | 1 + Xamarin.Forms.Platform.Android/Platform.cs | 9 +- 3 files changed, 102 insertions(+), 3 deletions(-) create mode 100644 Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla55912.cs diff --git a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla55912.cs b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla55912.cs new file mode 100644 index 000000000..834244bb6 --- /dev/null +++ b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Bugzilla55912.cs @@ -0,0 +1,95 @@ +using System; +using System.Diagnostics; +using Xamarin.Forms.CustomAttributes; +using Xamarin.Forms.Internals; +#if UITEST +using NUnit.Framework; + +#endif + +namespace Xamarin.Forms.Controls.Issues +{ + [Preserve(AllMembers = true)] + [Issue(IssueTracker.Bugzilla, 955912, "Tap event not always propagated to containing Grid/StackLayout", + PlatformAffected.Android)] + public class Bugzilla55912 : TestContentPage + { + const string Success = "Success"; + const string GridLabelId = "GridLabel"; + const string StackLabelId = "StackLabel"; + + protected override void Init() + { + var layout = new Grid(); + + layout.RowDefinitions.Add(new RowDefinition { Height = GridLength.Star }); + layout.RowDefinitions.Add(new RowDefinition { Height = GridLength.Star }); + layout.RowDefinitions.Add(new RowDefinition { Height = GridLength.Star }); + + var testGrid = new Grid { BackgroundColor = Color.Red }; + var gridLabel = new Label + { + AutomationId = GridLabelId, + Text = "This is a Grid with a TapGesture", + FontSize = 24, + BackgroundColor = Color.Green + }; + Grid.SetRow(testGrid, 1); + testGrid.Children.Add(gridLabel); + + var testStack = new StackLayout { BackgroundColor = Color.Default }; + var stackLabel = new Label + { + AutomationId = StackLabelId, + Text = "This StackLayout also has a TapGesture", + FontSize = 24, + BackgroundColor = Color.Green + }; + Grid.SetRow(testStack, 2); + testStack.Children.Add(stackLabel); + + layout.Children.Add(testGrid); + layout.Children.Add(testStack); + + Content = layout; + + testGrid.GestureRecognizers.Add(new TapGestureRecognizer + { + NumberOfTapsRequired = 1, + Command = new Command(() => + { + Debug.WriteLine($"***** TestGrid Tapped: {DateTime.Now} *****"); + layout.Children.Add(new Label { AutomationId = Success, Text = Success }); + }) + }); + + testStack.GestureRecognizers.Add(new TapGestureRecognizer + { + NumberOfTapsRequired = 1, + Command = new Command(() => + { + Debug.WriteLine($"***** TestStack Tapped: {DateTime.Now} *****"); + layout.Children.Add(new Label { AutomationId = Success, Text = Success }); + }) + }); + } + +#if UITEST + [Test] + public void GestureBubblingInStackLayout() + { + RunningApp.WaitForElement(StackLabelId); + RunningApp.Tap(StackLabelId); + RunningApp.WaitForElement(Success); + } + + [Test] + public void GestureBubblingInGrid() + { + RunningApp.WaitForElement(GridLabelId); + RunningApp.Tap(GridLabelId); + RunningApp.WaitForElement(Success); + } +#endif + } +} \ No newline at end of file diff --git a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems index 70069d9d8..f602ef657 100644 --- a/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems +++ b/Xamarin.Forms.Controls.Issues/Xamarin.Forms.Controls.Issues.Shared/Xamarin.Forms.Controls.Issues.Shared.projitems @@ -196,6 +196,7 @@ + diff --git a/Xamarin.Forms.Platform.Android/Platform.cs b/Xamarin.Forms.Platform.Android/Platform.cs index e2326d485..add84eb64 100644 --- a/Xamarin.Forms.Platform.Android/Platform.cs +++ b/Xamarin.Forms.Platform.Android/Platform.cs @@ -1070,9 +1070,12 @@ namespace Xamarin.Forms.Platform.Android if (result && _notReallyHandled) { - // If the child control returned true from its touch event handler but signalled that it was a fake "true", leave the event unhandled - // so parent controls have the opportunity - return false; + // If the child control returned true from its touch event handler but signalled that it was a fake "true", then we + // don't consider the event truly "handled" yet. + // Since a child control short-circuited the normal dispatchTouchEvent stuff, this layout never got the chance for + // IOnTouchListener.OnTouch and the OnTouchEvent override to try handling the touches; we'll do that now + + return OnTouchEvent(e); } return result;