[GTK] Add support for ClickGestureRecognizer (#3971)
* [GTK] Add support for ClickGestureRecognizer * Add ClickGestureGalleryPage to test ClickGestureRecognizer
This commit is contained in:
Родитель
660613a9ba
Коммит
735c81c530
|
@ -0,0 +1,121 @@
|
||||||
|
using System;
|
||||||
|
|
||||||
|
namespace Xamarin.Forms.Controls
|
||||||
|
{
|
||||||
|
public class ClickGestureGalleryPage : ContentPage
|
||||||
|
{
|
||||||
|
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 = 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -287,6 +287,7 @@ namespace Xamarin.Forms.Controls
|
||||||
new GalleryPageFactory(() => new PanGestureGalleryPage(), "Pan gesture Gallery"),
|
new GalleryPageFactory(() => new PanGestureGalleryPage(), "Pan gesture Gallery"),
|
||||||
new GalleryPageFactory(() => new SwipeGestureGalleryPage(), "Swipe gesture Gallery"),
|
new GalleryPageFactory(() => new SwipeGestureGalleryPage(), "Swipe gesture Gallery"),
|
||||||
new GalleryPageFactory(() => new PinchGestureTestPage(), "Pinch 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 AutomationIdGallery(), "AutomationID Gallery"),
|
||||||
new GalleryPageFactory(() => new LayoutPerformanceGallery(), "Layout Perf Gallery"),
|
new GalleryPageFactory(() => new LayoutPerformanceGallery(), "Layout Perf Gallery"),
|
||||||
new GalleryPageFactory(() => new ListViewSelectionColor(), "ListView SelectionColor Gallery"),
|
new GalleryPageFactory(() => new ListViewSelectionColor(), "ListView SelectionColor Gallery"),
|
||||||
|
|
|
@ -244,7 +244,7 @@ namespace Xamarin.Forms.Platform.GTK
|
||||||
|
|
||||||
_container.ButtonPressEvent -= OnContainerButtonPressEvent;
|
_container.ButtonPressEvent -= OnContainerButtonPressEvent;
|
||||||
|
|
||||||
if (gestures.GetGesturesFor<TapGestureRecognizer>(g => g.NumberOfTapsRequired == 1).Any())
|
if (gestures.GetGesturesFor<TapGestureRecognizer>().Any() || gestures.GetGesturesFor<ClickGestureRecognizer>().Any())
|
||||||
{
|
{
|
||||||
_container.ButtonPressEvent += OnContainerButtonPressEvent;
|
_container.ButtonPressEvent += OnContainerButtonPressEvent;
|
||||||
}
|
}
|
||||||
|
@ -339,7 +339,8 @@ namespace Xamarin.Forms.Platform.GTK
|
||||||
|
|
||||||
private void OnContainerButtonPressEvent(object o, ButtonPressEventArgs args)
|
private void OnContainerButtonPressEvent(object o, ButtonPressEventArgs args)
|
||||||
{
|
{
|
||||||
if (args.Event.Button != 1)
|
var button = args.Event.Button;
|
||||||
|
if (button != 1 && button != 3)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
@ -349,21 +350,47 @@ namespace Xamarin.Forms.Platform.GTK
|
||||||
if (view == null)
|
if (view == null)
|
||||||
return;
|
return;
|
||||||
|
|
||||||
if (args.Event.Type == Gdk.EventType.TwoButtonPress)
|
int numClicks = 0;
|
||||||
|
switch (args.Event.Type)
|
||||||
{
|
{
|
||||||
IEnumerable<TapGestureRecognizer> doubleTapGestures = view.GestureRecognizers
|
case Gdk.EventType.ThreeButtonPress:
|
||||||
.GetGesturesFor<TapGestureRecognizer>(g => g.NumberOfTapsRequired == 2);
|
numClicks = 3;
|
||||||
|
break;
|
||||||
foreach (TapGestureRecognizer recognizer in doubleTapGestures)
|
case Gdk.EventType.TwoButtonPress:
|
||||||
recognizer.SendTapped(view);
|
numClicks = 2;
|
||||||
|
break;
|
||||||
|
case Gdk.EventType.ButtonPress:
|
||||||
|
numClicks = 1;
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
else
|
|
||||||
|
// Taps or Clicks
|
||||||
|
if (button == 1)
|
||||||
{
|
{
|
||||||
IEnumerable<TapGestureRecognizer> tapGestures = view.GestureRecognizers
|
IEnumerable<TapGestureRecognizer> tapGestures = view.GestureRecognizers
|
||||||
.GetGesturesFor<TapGestureRecognizer>(g => g.NumberOfTapsRequired == 1);
|
.GetGesturesFor<TapGestureRecognizer>(g => g.NumberOfTapsRequired == numClicks);
|
||||||
|
|
||||||
foreach (TapGestureRecognizer recognizer in tapGestures)
|
foreach (TapGestureRecognizer recognizer in tapGestures)
|
||||||
recognizer.SendTapped(view);
|
recognizer.SendTapped(view);
|
||||||
|
|
||||||
|
IEnumerable<ClickGestureRecognizer> clickGestures = view.GestureRecognizers
|
||||||
|
.GetGesturesFor<ClickGestureRecognizer>(g => g.NumberOfClicksRequired == numClicks &&
|
||||||
|
g.Buttons == ButtonsMask.Primary);
|
||||||
|
|
||||||
|
foreach (ClickGestureRecognizer recognizer in clickGestures)
|
||||||
|
recognizer.SendClicked(view, ButtonsMask.Primary);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
// right click
|
||||||
|
IEnumerable<ClickGestureRecognizer> rightClickGestures = view.GestureRecognizers
|
||||||
|
.GetGesturesFor<ClickGestureRecognizer>(g => g.NumberOfClicksRequired == numClicks &&
|
||||||
|
g.Buttons == ButtonsMask.Secondary);
|
||||||
|
|
||||||
|
foreach (ClickGestureRecognizer recognizer in rightClickGestures)
|
||||||
|
recognizer.SendClicked(view, ButtonsMask.Secondary);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Загрузка…
Ссылка в новой задаче