microsoft / referencesource

Source from the Microsoft .NET Reference Source that represent a subset of the .NET Framework
https://referencesource.microsoft.com/
MIT License
3.16k stars 1.27k forks source link

Touches from dialog window get "stuck" in main window #97

Closed Stannieman closed 5 years ago

Stannieman commented 5 years ago

We are having quite a problem with our WPF app on touch devices. Sometimes after closing a modal window (opened with ShowDialog()) the buttons on our main window respond correctly to triggers on AreAnyTouchesOver, but they never fire a Click event or command when tapped. After quite some trail and error we believe we have found the issue causing this. The following scenario makes the issue appear.

Open a modal window and then make the UI thread busy for a while. On the modal window there's a button that closes the window. Now tap the close button and then any locations in the modal window (the close button or even some other place) WHILE the UI thread is still busy. When the UI thread has done it's work it will see the click on the close button and close the window. All touches that came after the first tap on close appear now on the main window. However ONLY the (Preview)TouchDown events are raised in the main window and NO (Preview)TouchUp is ever occurring. This makes the main window think there are still fingers over itself while in reality they are not, and as a result when you tap a button this is seen as "1 more finger" instead of just a single finger tapping the screen.

So essentially the problem is that even after all threads are idle again our app is seeing fingers over itself while they are already long gone from the physical screen.

https://1drv.ms/u/s!AhqurxiexJWri5EleIA1-J6XZ0aKvA

This sample app is a minimal repro. After the dialog closes you can see that the PreviewTouchDown handler of the main window is fired the same number of times you tapped the dialog after you pressed close for the first time. The PreviewTouchUp handler is never fired. You can see that every time the PreviewTouchDown handler is fired a TouchDevice is added to the TouchesOver collection.

For some reason after a "random" amount of interactions with the app the number of devices decreases and from the moment all "ghost fingers" are gone the buttons work as expected again.

This is reproducible on Windows 10 1609 and up, but not on Windows 8.1.

Stannieman commented 5 years ago

I found something very suspicious: https://referencesource.microsoft.com/#PresentationCore/Core/CSharp/System/Windows/Input/TouchDevice.cs,720

Stannieman commented 5 years ago

We'll continue here: https://github.com/dotnet/wpf/issues/450