Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Windows] Fix pointer released event not being handled on ImageButton #21766

Merged
merged 9 commits into from
May 7, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?xml version="1.0" encoding="utf-8" ?>
<ContentPage xmlns="http://schemas.microsoft.com/dotnet/2021/maui"
xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
x:Class="Maui.Controls.Sample.Issues.Issue21706"
xmlns:ns="clr-namespace:Maui.Controls.Sample.Issues">
<VerticalStackLayout>
<ImageButton
Source="groceries.png"
WidthRequest="200"
HeightRequest="200"
AutomationId="WaitForElement"
x:Name="PointerImageButton">
<VisualStateManager.VisualStateGroups>
<VisualStateGroup
x:Name="CommonStates">
<VisualState
x:Name="Normal">
<VisualState.Setters>
<Setter
Property="BackgroundColor" Value="Blue" />
</VisualState.Setters>
</VisualState>
<VisualState
x:Name="PointerOver">
<VisualState.Setters>
<Setter
Property="BackgroundColor" Value="Red" />
</VisualState.Setters>
</VisualState>
</VisualStateGroup>
</VisualStateManager.VisualStateGroups>
</ImageButton>

<Button
AutomationId="OtherButton"
WidthRequest="200"
HeightRequest="200"
Text="Other"/>
</VerticalStackLayout>
</ContentPage>
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
using System;
using System.Collections.Generic;
using System.ComponentModel;
using Microsoft.Maui.Controls;
using Microsoft.Maui.Controls.Xaml;

namespace Maui.Controls.Sample.Issues
{
[XamlCompilation(XamlCompilationOptions.Compile)]
[Issue(IssueTracker.Github, 21706, "ImageButton stuck in PointerOver state", PlatformAffected.UWP)]
public partial class Issue21706 : ContentPage
{
public Issue21706()
{
InitializeComponent();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@
FontSize="Large"
BackgroundColor="Pink"
HorizontalTextAlignment="Center" />
</FlexLayout>
</FlexLayout>
</views:BasePage.Content>
</views:BasePage>
26 changes: 26 additions & 0 deletions src/Controls/tests/UITests/Tests/Issues/Issue21706.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
using NUnit.Framework;
using UITest.Appium;
using UITest.Core;

namespace Microsoft.Maui.AppiumTests.Issues
{
public class Issue21706 : _IssuesUITest
{
public override string Issue => "ImageButton stuck in PointerOver state";

public Issue21706(TestDevice device) : base(device)
{
}

[Test]
public async Task ImageButtonStuckAfterRightClick()
{
this.IgnoreIfPlatforms(new TestDevice[] { TestDevice.Android, TestDevice.Mac, TestDevice.iOS });
Copy link
Member

@jfversluis jfversluis May 6, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any specific reason we want to not test this on the other platforms?

I get that this is about clicks/hovers so it might not work on some platforms, but if it reasonably can work, why not let them run there as well?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, the ticket mentioned that it already worked fine in macOS, so I wrote the test to only check on Win. The Appium test does specifically use the pointer right click, so we could enable it for macOS as well.

App.WaitForElement("WaitForElement");
App.RightClick("WaitForElement");
App.Click("OtherButton");
await Task.Delay(500);
VerifyScreenshot();
}
}
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
10 changes: 10 additions & 0 deletions src/Core/src/Handlers/ImageButton/ImageButtonHandler.Windows.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ public partial class ImageButtonHandler : ViewHandler<IImageButton, Button>
Image? _image;

PointerEventHandler? _pointerPressedHandler;
PointerEventHandler? _pointerReleasedHandler;

protected override Button CreatePlatformView()
{
Expand All @@ -34,6 +35,7 @@ protected override Button CreatePlatformView()
protected override void ConnectHandler(Button platformView)
{
_pointerPressedHandler = new PointerEventHandler(OnPointerPressed);
_pointerReleasedHandler = new PointerEventHandler(OnPointerReleased);

if (_image != null)
{
Expand All @@ -43,6 +45,7 @@ protected override void ConnectHandler(Button platformView)

platformView.Click += OnClick;
platformView.AddHandler(UIElement.PointerPressedEvent, _pointerPressedHandler, true);
platformView.AddHandler(UIElement.PointerReleasedEvent, _pointerReleasedHandler, true);

base.ConnectHandler(platformView);
}
Expand All @@ -57,8 +60,10 @@ protected override void DisconnectHandler(Button platformView)

platformView.Click -= OnClick;
platformView.RemoveHandler(UIElement.PointerPressedEvent, _pointerPressedHandler);
platformView.RemoveHandler(UIElement.PointerReleasedEvent, _pointerReleasedHandler);

_pointerPressedHandler = null;
_pointerReleasedHandler = null;

base.DisconnectHandler(platformView);

Expand Down Expand Up @@ -101,6 +106,11 @@ void OnPointerPressed(object sender, PointerRoutedEventArgs e)
VirtualView?.Pressed();
}

void OnPointerReleased(object sender, PointerRoutedEventArgs e)
Foda marked this conversation as resolved.
Show resolved Hide resolved
{
VirtualView?.Released();
}

void OnImageOpened(object sender, RoutedEventArgs routedEventArgs)
{
VirtualView?.UpdateIsLoading(false);
Expand Down
14 changes: 14 additions & 0 deletions src/TestUtils/src/UITest.Appium/HelperExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@ public static void Click(this IApp app, string element)
app.FindElement(element).Click();
}

public static void RightClick(this IApp app, string element)
{
app.FindElement(element).RightClick();
}

public static string? GetText(this IUIElement element)
{
var response = element.Command.Execute("getText", new Dictionary<string, object>()
Expand Down Expand Up @@ -119,6 +124,15 @@ public static void Click(this IUIElement element)
});
}

public static void RightClick(this IUIElement element)
Foda marked this conversation as resolved.
Show resolved Hide resolved
{
element.Command.Execute("click", new Dictionary<string, object>()
{
{ "element", element },
{ "button", "right" }
});
}

public static void SendKeys(this IUIElement element, string text)
{
element.Command.Execute("sendKeys", new Dictionary<string, object>()
Expand Down
Loading