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

IsPointerOver does not Work with OnPointerReleased #15293

Open
Elbon-Eastmage opened this issue Apr 9, 2024 · 3 comments · May be fixed by #15357
Open

IsPointerOver does not Work with OnPointerReleased #15293

Elbon-Eastmage opened this issue Apr 9, 2024 · 3 comments · May be fixed by #15357
Labels

Comments

@Elbon-Eastmage
Copy link

Describe the bug

When IsPointerOver is placed inside an OnPointerReleased event handler, it always returns true, even if the pointer isn't actually over the associated control.

To Reproduce

  1. Add the following XAML text to a window.
<StackPanel HorizontalAlignment="Center" VerticalAlignment="Center">
	<Border Name="section" PointerReleased="OnPointerReleased">
		<StackPanel>
			<TextBlock
				Text="Clickable"
				Background="LightGoldenrodYellow"
				FontSize="21"
				HorizontalAlignment="Center" />
			<TextBlock
				Name="treasure"
				Text="Hello World!"
				FontSize="21"
				Background="Gold"
				HorizontalAlignment="Center"
				IsVisible="False" />
		</StackPanel>
	</Border>
</StackPanel>
  1. Add the following code to the window's code behind file.
void OnPointerReleased(object sender, PointerReleasedEventArgs e)
{
    if (section.IsPointerOver)
        treasure.IsVisible = !treasure.IsVisible;
}
  1. Run the program.
  2. Click on the visible box, but drag the mouse away before releasing it.

Expected behavior

Since the pointer wasn't over the visible box when the mouse button was released, the hidden text shouldn't have been shown.

Avalonia version

11.0.10

OS

Windows

Additional context

No response

@stevemonaco
Copy link
Contributor

Check the Button source for how it handles this.

In code-behind, it can be a bit simpler:

void OnPointerReleased(object sender, PointerReleasedEventArgs e)
{
    var point = e.GetCurrentPoint(section).Position;

    if (section.Bounds.Contains(point))
        treasure.IsVisible = !treasure.IsVisible;
}

This may be by-design because there are scenarios where you want IsPointerOver to be true across the entire interaction and it's easy enough to opt-out of with a small amount of code.

@timunie
Copy link
Contributor

timunie commented Apr 12, 2024

@Elbon-Eastmage are you happy with the provided proposal? If so, we can make this a Q&A rather than a bug report.

@Elbon-Eastmage
Copy link
Author

That pull request looks perfect! Thank you!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
3 participants