-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement PointerGestureRecognizer (#9592)
* Initial PointerGestureRecognizer code * Update PointerGestureRecognizer.cs * Clean up code and add nullability * Fix bad merge * Remove test code * Add sample to GestureGallery * Copy and refactor GetPosition logic used in TapGesture * Clean up code and use GetPosition * Update control gallery sample * Add public API file changes * Clean up code * Clean up more code Co-authored-by: Shane Neuville <shneuvil@microsoft.com>
- Loading branch information
1 parent
abd82aa
commit 41b1c9e
Showing
15 changed files
with
406 additions
and
9 deletions.
There are no files selected for viewing
18 changes: 18 additions & 0 deletions
18
src/Controls/samples/Controls.Sample/Pages/Core/PointerGestureGalleryPage.xaml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
<?xml version="1.0" encoding="utf-8" ?> | ||
<views:BasePage | ||
xmlns="http://schemas.microsoft.com/dotnet/2021/maui" | ||
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml" | ||
x:Class="Maui.Controls.Sample.Pages.PointerGestureGalleryPage" | ||
xmlns:views="clr-namespace:Maui.Controls.Sample.Pages.Base"> | ||
<StackLayout> | ||
<Label | ||
x:Name="hoverLabel" | ||
FontSize="24" | ||
Text="Hover me!"> | ||
<Label.GestureRecognizers> | ||
<PointerGestureRecognizer PointerEntered="HoverBegan" PointerExited="HoverEnded" PointerMoved="HoverMoved" /> | ||
</Label.GestureRecognizers> | ||
</Label> | ||
<Label x:Name="positionLabel" Text="Hover above label to reveal pointer position"/> | ||
</StackLayout> | ||
</views:BasePage> |
28 changes: 28 additions & 0 deletions
28
src/Controls/samples/Controls.Sample/Pages/Core/PointerGestureGalleryPage.xaml.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using Microsoft.Maui.Controls; | ||
|
||
namespace Maui.Controls.Sample.Pages | ||
{ | ||
public partial class PointerGestureGalleryPage | ||
{ | ||
public PointerGestureGalleryPage() | ||
{ | ||
InitializeComponent(); | ||
} | ||
|
||
void HoverBegan(object sender, PointerEventArgs e) | ||
{ | ||
hoverLabel.Text = "Thanks for hovering me!"; | ||
} | ||
|
||
void HoverEnded(object sender, PointerEventArgs e) | ||
{ | ||
hoverLabel.Text = "Hover me again!"; | ||
positionLabel.Text = "Hover above label to reveal pointer position again"; | ||
} | ||
|
||
void HoverMoved(object sender, PointerEventArgs e) | ||
{ | ||
positionLabel.Text = $"Pointer position is at: {e.GetPosition((View)sender)}"; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
#nullable enable | ||
|
||
using Microsoft.Maui.Graphics; | ||
using System; | ||
|
||
namespace Microsoft.Maui.Controls | ||
{ | ||
/// <summary> | ||
/// Arguments for PointerGestureRecognizer events. | ||
/// </summary> | ||
public class PointerEventArgs : EventArgs | ||
{ | ||
|
||
Func<IElement?, Point?>? _getPosition; | ||
|
||
public PointerEventArgs() | ||
{ | ||
} | ||
|
||
internal PointerEventArgs(Func<IElement?, Point?>? getPosition) | ||
{ | ||
_getPosition = getPosition; | ||
} | ||
|
||
public virtual Point? GetPosition(Element? relativeTo) => | ||
_getPosition?.Invoke(relativeTo); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
#nullable enable | ||
using System; | ||
using Microsoft.Maui.Graphics; | ||
using System.Windows.Input; | ||
|
||
namespace Microsoft.Maui.Controls | ||
{ | ||
/// <summary> | ||
/// Provides pointer gesture recognition and events. | ||
/// </summary> | ||
public sealed class PointerGestureRecognizer : GestureRecognizer | ||
{ | ||
public static readonly BindableProperty PointerEnteredCommandProperty = BindableProperty.Create(nameof(PointerEnteredCommand), typeof(ICommand), typeof(PointerGestureRecognizer), null); | ||
|
||
public static readonly BindableProperty PointerEnteredCommandParameterProperty = BindableProperty.Create(nameof(PointerEnteredCommandParameter), typeof(object), typeof(PointerGestureRecognizer), null); | ||
|
||
public static readonly BindableProperty PointerExitedCommandProperty = BindableProperty.Create(nameof(PointerExitedCommand), typeof(ICommand), typeof(PointerGestureRecognizer), null); | ||
|
||
public static readonly BindableProperty PointerExitedCommandParameterProperty = BindableProperty.Create(nameof(PointerExitedCommandParameter), typeof(object), typeof(PointerGestureRecognizer), null); | ||
|
||
public static readonly BindableProperty PointerMovedCommandProperty = BindableProperty.Create(nameof(PointerMovedCommand), typeof(ICommand), typeof(PointerGestureRecognizer), null); | ||
|
||
public static readonly BindableProperty PointerMovedCommandParameterProperty = BindableProperty.Create(nameof(PointerMovedCommandParameter), typeof(object), typeof(PointerGestureRecognizer), null); | ||
|
||
public PointerGestureRecognizer() | ||
{ | ||
} | ||
|
||
public event EventHandler<PointerEventArgs>? PointerEntered; | ||
public event EventHandler<PointerEventArgs>? PointerExited; | ||
public event EventHandler<PointerEventArgs>? PointerMoved; | ||
|
||
public ICommand PointerEnteredCommand | ||
{ | ||
get { return (ICommand)GetValue(PointerEnteredCommandProperty); } | ||
set { SetValue(PointerEnteredCommandProperty, value); } | ||
} | ||
|
||
public ICommand PointerEnteredCommandParameter | ||
{ | ||
get { return (ICommand)GetValue(PointerEnteredCommandParameterProperty); } | ||
set { SetValue(PointerEnteredCommandParameterProperty, value); } | ||
} | ||
public ICommand PointerExitedCommand | ||
{ | ||
get { return (ICommand)GetValue(PointerExitedCommandProperty); } | ||
set { SetValue(PointerExitedCommandProperty, value); } | ||
} | ||
public ICommand PointerExitedCommandParameter | ||
{ | ||
get { return (ICommand)GetValue(PointerExitedCommandParameterProperty); } | ||
set { SetValue(PointerExitedCommandParameterProperty, value); } | ||
} | ||
|
||
public ICommand PointerMovedCommand | ||
{ | ||
get { return (ICommand)GetValue(PointerMovedCommandProperty); } | ||
set { SetValue(PointerMovedCommandProperty, value); } | ||
} | ||
|
||
public ICommand PointerMovedCommandParameter | ||
{ | ||
get { return (ICommand)GetValue(PointerMovedCommandParameterProperty); } | ||
set { SetValue(PointerMovedCommandParameterProperty, value); } | ||
} | ||
|
||
internal void SendPointerEntered(View sender, Func<IElement?, Point?>? getPosition) | ||
{ | ||
ICommand cmd = PointerEnteredCommand; | ||
if (cmd?.CanExecute(PointerEnteredCommandParameter) == true) | ||
cmd.Execute(PointerEnteredCommandParameter); | ||
|
||
EventHandler<PointerEventArgs>? handler = PointerEntered; | ||
handler?.Invoke(sender, new PointerEventArgs(getPosition)); | ||
} | ||
|
||
internal void SendPointerExited(View sender, Func<IElement?, Point?>? getPosition) | ||
{ | ||
ICommand cmd = PointerExitedCommand; | ||
if (cmd?.CanExecute(PointerExitedCommandParameter) == true) | ||
cmd.Execute(PointerExitedCommandParameter); | ||
|
||
EventHandler<PointerEventArgs>? handler = PointerExited; | ||
handler?.Invoke(sender, new PointerEventArgs(getPosition)); | ||
} | ||
|
||
internal void SendPointerMoved(View sender, Func<IElement?, Point?>? getPosition) | ||
{ | ||
ICommand cmd = PointerMovedCommand; | ||
if (cmd?.CanExecute(PointerMovedCommandParameter) == true) | ||
cmd.Execute(PointerMovedCommandParameter); | ||
|
||
EventHandler<PointerEventArgs>? handler = PointerMoved; | ||
handler?.Invoke(sender, new PointerEventArgs(getPosition)); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.