diff --git a/Xamarin.Forms.Controls/ControlGalleryPages/ClickGestureGalleryPage.cs b/Xamarin.Forms.Controls/ControlGalleryPages/ClickGestureGalleryPage.cs new file mode 100644 index 000000000..a1565d4bf --- /dev/null +++ b/Xamarin.Forms.Controls/ControlGalleryPages/ClickGestureGalleryPage.cs @@ -0,0 +1,121 @@ +using System; + +namespace Xamarin.Forms.Controls +{ + public class ClickGestureGalleryPage : ContentPage + { + Command clickCommand; + BoxView changeColorBoxView; + + public ClickGestureGalleryPage() + { + clickCommand = new Command(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 = Color.PaleGreen + }; + var singleClickGesture = new ClickGestureRecognizer + { + Command = clickCommand, + CommandParameter = Color.PaleGreen, + NumberOfClicksRequired = 1, + Buttons = ButtonsMask.Primary + }; + singleClickLabel.GestureRecognizers.Add(singleClickGesture); + horizontal.Children.Add(singleClickLabel); + + var doubleClickLabel = new Label + { + Text = "Double click me!!", + BackgroundColor = Color.Aqua + }; + var doubleClickGesture = new ClickGestureRecognizer + { + Command = clickCommand, + CommandParameter = Color.Aqua, + NumberOfClicksRequired = 2, + Buttons = ButtonsMask.Primary + }; + doubleClickLabel.GestureRecognizers.Add(doubleClickGesture); + horizontal.Children.Add(doubleClickLabel); + + var tripleClicklabel = new Label + { + Text = "Triple click me!!!", + BackgroundColor = Color.Olive + }; + var tripleClickGesture = new ClickGestureRecognizer + { + Command = clickCommand, + CommandParameter = Color.Olive, + NumberOfClicksRequired = 3, + Buttons = ButtonsMask.Primary + }; + tripleClicklabel.GestureRecognizers.Add(tripleClickGesture); + horizontal.Children.Add(tripleClicklabel); + + var rightClickLabel = new Label + { + Text = "Right click me¡", + BackgroundColor = Color.Coral + }; + var rigthClickGesture = new ClickGestureRecognizer + { + Command = clickCommand, + CommandParameter = Color.Coral, + NumberOfClicksRequired = 1, + Buttons = ButtonsMask.Secondary + }; + rightClickLabel.GestureRecognizers.Add(rigthClickGesture); + horizontal.Children.Add(rightClickLabel); + + var doubleRightClickLabel = new Label + { + Text = "Double right click me¡¡", + BackgroundColor = Color.Gold + }; + var doubleRigthClickGesture = new ClickGestureRecognizer + { + Command = clickCommand, + CommandParameter = Color.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; + } + } +} diff --git a/Xamarin.Forms.Controls/CoreGallery.cs b/Xamarin.Forms.Controls/CoreGallery.cs index df0452889..417339b8e 100644 --- a/Xamarin.Forms.Controls/CoreGallery.cs +++ b/Xamarin.Forms.Controls/CoreGallery.cs @@ -287,6 +287,7 @@ namespace Xamarin.Forms.Controls new GalleryPageFactory(() => new PanGestureGalleryPage(), "Pan gesture Gallery"), new GalleryPageFactory(() => new SwipeGestureGalleryPage(), "Swipe gesture Gallery"), new GalleryPageFactory(() => new PinchGestureTestPage(), "Pinch gesture Gallery"), + new GalleryPageFactory(() => new ClickGestureGalleryPage(), "Click gesture Gallery"), new GalleryPageFactory(() => new AutomationIdGallery(), "AutomationID Gallery"), new GalleryPageFactory(() => new LayoutPerformanceGallery(), "Layout Perf Gallery"), new GalleryPageFactory(() => new ListViewSelectionColor(), "ListView SelectionColor Gallery"), diff --git a/Xamarin.Forms.Platform.GTK/VisualElementTracker.cs b/Xamarin.Forms.Platform.GTK/VisualElementTracker.cs index 6c42efc02..6bd96672c 100644 --- a/Xamarin.Forms.Platform.GTK/VisualElementTracker.cs +++ b/Xamarin.Forms.Platform.GTK/VisualElementTracker.cs @@ -244,7 +244,7 @@ namespace Xamarin.Forms.Platform.GTK _container.ButtonPressEvent -= OnContainerButtonPressEvent; - if (gestures.GetGesturesFor(g => g.NumberOfTapsRequired == 1).Any()) + if (gestures.GetGesturesFor().Any() || gestures.GetGesturesFor().Any()) { _container.ButtonPressEvent += OnContainerButtonPressEvent; } @@ -339,7 +339,8 @@ namespace Xamarin.Forms.Platform.GTK private void OnContainerButtonPressEvent(object o, ButtonPressEventArgs args) { - if (args.Event.Button != 1) + var button = args.Event.Button; + if (button != 1 && button != 3) { return; } @@ -349,21 +350,47 @@ namespace Xamarin.Forms.Platform.GTK if (view == null) return; - if (args.Event.Type == Gdk.EventType.TwoButtonPress) + int numClicks = 0; + switch (args.Event.Type) { - IEnumerable doubleTapGestures = view.GestureRecognizers - .GetGesturesFor(g => g.NumberOfTapsRequired == 2); - - foreach (TapGestureRecognizer recognizer in doubleTapGestures) - recognizer.SendTapped(view); + case Gdk.EventType.ThreeButtonPress: + numClicks = 3; + break; + case Gdk.EventType.TwoButtonPress: + numClicks = 2; + break; + case Gdk.EventType.ButtonPress: + numClicks = 1; + break; + default: + return; } - else + + // Taps or Clicks + if (button == 1) { IEnumerable tapGestures = view.GestureRecognizers - .GetGesturesFor(g => g.NumberOfTapsRequired == 1); + .GetGesturesFor(g => g.NumberOfTapsRequired == numClicks); foreach (TapGestureRecognizer recognizer in tapGestures) recognizer.SendTapped(view); + + IEnumerable clickGestures = view.GestureRecognizers + .GetGesturesFor(g => g.NumberOfClicksRequired == numClicks && + g.Buttons == ButtonsMask.Primary); + + foreach (ClickGestureRecognizer recognizer in clickGestures) + recognizer.SendClicked(view, ButtonsMask.Primary); + } + else + { + // right click + IEnumerable rightClickGestures = view.GestureRecognizers + .GetGesturesFor(g => g.NumberOfClicksRequired == numClicks && + g.Buttons == ButtonsMask.Secondary); + + foreach (ClickGestureRecognizer recognizer in rightClickGestures) + recognizer.SendClicked(view, ButtonsMask.Secondary); } }