Skip to content

Fix touch Click not raised after a tap opens a modal dialog - #11999

Open
amarinov-msft wants to merge 9 commits into
dotnet:mainfrom
amarinov-msft:fix/194-touch-click-stuck-devices
Open

amarinov-msft wants to merge 9 commits into
dotnet:mainfrom
amarinov-msft:fix/194-touch-click-stuck-devices

Conversation

@amarinov-msft

@amarinov-msft amarinov-msft commented Oct 6, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #194

Main PR: Not applicable; this is the main-branch fix, not a backport.

Description

A touch contact can promote a mouse-down handler that opens a modal dialog while the contact is still active. The original contact can then prevent touches in the dialog from becoming the primary mouse-promoting contact, suppressing Click events.

When a window is disabled, mark its active WISP touch contacts as orphaned. They remain active until their Up is processed, but no longer block primary-contact selection in an enabled window. Abandon their promoted mouse press internally and give up promotion ownership. This relies on native window disabling already triggering mouse-capture cancellation; it does not silently cancel interactions in windows that remain enabled.

The trigger is native window disabling (WM_ENABLE(false)), not ShowDialog() specifically. Other callers of EnableWindow, including setting ElementHost.Enabled = false, can reach this path. Nested Dispatcher loops in a still-enabled window are outside this fix.

Re-enabling a window must not reconstruct a mouse press from a still-down orphaned contact's InAir, GestureWasFired, and active-button values. The re-enable path now skips that reconstruction for an active orphaned contact, preserving the shared state rather than unconditionally setting it to Released. The contact's late Up cannot release a newer contact's press. Re-enable behavior for non-orphaned stylus devices is unchanged.

The separate completed-touch/manipulation-replay fix in #12034 does not cover this held-contact re-enable sequence; its ownership receipts apply only to completed-touch replay.

Customer Impact

Touch-operated applications, including LOB applications on tablets and industrial panels, can stop producing clicks after validation or confirmation dialogs open from data binding or input handlers.

Closing the dialog by keyboard or programmatically before the original finger lifts can also restore a shared Pressed state after the device-local press was abandoned, swallowing the next promoted mouse-down.

Regression

No known recent-release regression. The underlying issue is longstanding.

Testing

Added 16 deterministic WispOrphanedTouchTests cases. They model held-contact and gesture bookkeeping, then call the production orphaning, re-enable, and mouse-up bookkeeping paths. No native input is injected. The re-enable call uses a nonzero unregistered HWND to isolate the state-reconstruction branch from native tablet enumeration.

Before the re-enable correction, the unchanged PR product at 5f54e6051b42fa8f27ca1c1b5a67cdcc183570da produced 6 failures and 10 passes:

  • Both left/right held-contact cases resurrected the abandoned shared press.
  • Four other-button cases overwrote the newer contact's press.

Coverage after the correction includes same- and different-button newer contacts, both normal and disabled-window Up cleanup, promotion-owner preservation, reuse of the old device for a new contact, and unchanged non-orphaned stylus re-enable rules.

Validated on Windows, x64 Debug, targeting net11.0-windows, using .NET 12.0.0-alpha.1.26480.102, in an isolated standalone clone of this PR. No #12034 product changes were included.

$env:LC_ALL = 'C'; $env:LANG = 'C'; $env:NUGET_PACKAGES = 'Q:\.tools\.nuget\packages'; .\build.cmd -projects 'src\Microsoft.DotNet.Wpf\tests\UnitTests\PresentationCore.Tests\PresentationCore.Tests.csproj' -platform x64 -msbuildEngine vs -verbosity quiet -binaryLogName Issue194ReviewFixed.binlog

Result: passed, 0 warnings, 0 errors; exit 0. PresentationBuildTasks was explicitly restored/built and required hand-crafted reference assemblies were built with Platform=AnyCPU first. Installed SDK/native tools were reused. Native tool bootstrap still logs missing optional/cache prerequisites, so this is not clean-machine bootstrap validation. The final build used the normal source-control metadata path; no product/build-script changes or assertion suppression were needed.

$env:DOTNET_ROOT = (Resolve-Path .dotnet).Path; & .\artifacts\bin\PresentationCore.Tests\x64\Debug\net11.0-windows\PresentationCore.Tests.exe --filter-class 'System.Windows.Input.*' --no-progress --report-xunit-xml --report-xunit-xml-filename Issue194ReenableInput.xml --timeout 2m

Result: 219 passed, 0 failed, 0 skipped; exit 0. Includes all 16 new regression cases.

git diff --check
git diff --cached --check

Result: passed.

Not run for this correction: physical touchscreen/pen/mouse testing, native touch injection, the full repository suite, or DRTs. These managed tests confirm the state mismatch and its correction, not end-to-end reproduction of a Drag-opened dialog on hardware.

This does not claim to resolve every modal input failure: the earlier controlled comparison with #12034's original native regressions found that this PR's previous product version improved initial responsiveness but still missed a later dialog click. That broader completed-touch replay scenario has not been rerun with this re-enable correction.

Risk

Medium. The product changes affect WISP touch primary-selection and shared mouse-promotion state. The pointer implementation is unchanged.

Orphaning is limited to active touch contacts in a natively disabled window. The disabled-window Up call to AbandonPromotedMouseButton() is not touch-only: it also affects pen/stylus devices that promoted a mouse press. Accordingly, pen input paths are not claimed to be untouched. The new re-enable guard itself only excludes active orphaned touch contacts and leaves the existing non-orphaned stylus rules in place.

An abandoned press is not routed as a synthetic MouseUp. Its later cleanup must not release a newer contact's press or replace its promotion owner. The deterministic tests cover those state transitions but do not replace physical-device validation.

Microsoft Reviewers: Open in CodeFlow

When a touch tap promotes a mouse down that opens a modal dialog (e.g. via a binding setter on LostFocus), the tapping touch device stays active while its up is processed inside the nested pump. New touches in the dialog then activate as non-primary and are never promoted to mouse, so Click is not raised until WISP reuses the cursor id.

Track touch devices whose up is being processed (up-pending), exclude them when choosing the primary/mouse-promotion device, and drop the stale promoted mouse press of the up-pending device when a new device takes over promotion.

Fixes dotnet#194

Co-authored-by: Copilot <[email protected]>
Copilot-Session: 1fea3d60-9fff-49a1-8eb6-84f04d7d0fa7
@dotnet-policy-service dotnet-policy-service Bot added PR metadata: Label to tag PRs, to facilitate with triage draft labels Oct 6, 2026
…ndow (dotnet#194)

If a touch promotes a mouse down before its up (e.g. a slower tap) and that mouse down opens a modal dialog, the up is delivered to the now-disabled window. That path reset the device's promoted mouse state but left WispLogic's mouse button state Pressed, so the first tap in the dialog had its mouse down dropped and Click needed a second tap.

Co-authored-by: Copilot <[email protected]>
Copilot-Session: 1fea3d60-9fff-49a1-8eb6-84f04d7d0fa7
@amarinov-msft
amarinov-msft marked this pull request as ready for review October 7, 2026 09:38
@amarinov-msft
amarinov-msft requested review from a team and a balanced review from Copilot October 7, 2026 09:38
@amarinov-msft
amarinov-msft requested a review from a team as a code owner October 7, 2026 09:38

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot review overview

🟡 Changes recommended

Promotion handoff can still suppress clicks or leave enabled controls captured and pressed.

Review effort: Balanced
Findings: 2 Medium severity

Open (2)
What changed in this PR

Addresses #194 by allowing new WISP touch contacts to promote mouse input while an earlier touch-up is paused in a modal dialog.

Changes:

  • Tracks active devices with pending touch-up processing.
  • Excludes pending devices when selecting the mouse-promotion owner.
  • Clears stale promoted mouse-button state during handoff and disabled-window cleanup.
File Description
src/​Microsoft.DotNet.Wpf/​src/​PresentationCore/​System/​Windows/​Input/​Stylus/​Wisp/​WispStylusTouchDevice.cs Updates promotion ownership and handoff cleanup.
src/​Microsoft.DotNet.Wpf/​src/​PresentationCore/​System/​Windows/​Input/​Stylus/​Wisp/​WispStylusDevice.cs Adds internal promoted-button release.
src/​Microsoft.DotNet.Wpf/​src/​PresentationCore/​System/​Windows/​Input/​Stylus/​Wisp/​WispLogic.cs Marks pending touch-ups and clears disabled-window button state.
src/​Microsoft.DotNet.Wpf/​src/​PresentationCore/​System/​Windows/​Input/​Stylus/​Common/​StylusTouchDeviceBase.cs Tracks pending devices and adjusts primary selection.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

…otnet#194 review)

Address review feedback:
- Excluding a device from primary selection as soon as its up arrives could transfer mouse promotion away from a device whose window is still enabled (e.g. a TouchUp handler pumping a DispatcherFrame), silently abandoning its mouse press and leaving the element captured and pressed. Now a device is only excluded (orphaned) when its window is disabled. Disabling sends WM_CANCELMODE, which already releases mouse capture, so dropping the promoted press is safe.
- Restore clearing the promotion owner only when no device at all is active, so a secondary contact lifting does not clear the owner prematurely.

Co-authored-by: Copilot <[email protected]>
Copilot-Session: 1fea3d60-9fff-49a1-8eb6-84f04d7d0fa7
@wnvko-msft

Copy link
Copy Markdown
Contributor

LGTM as whole. However IMO there is one potential state mismatch: re-enabling the window can restore the shared mouse state to Pressed after AbandonPromotedMouseButton() has cleared the device’s state. Closing the dialog before the original finger lifts, e.g. by keyboard, could therefore suppress the next click. This needs confirmation.

Skip legacy mouse-state reconstruction for active orphaned WISP contacts. Cover held-contact re-enable, newer-contact ownership, late-up cleanup and unchanged non-orphaned stylus behavior.

Co-authored-by: Copilot <[email protected]>

Copilot-Session: 1fe51848-46fe-4323-8746-5267d21e7788
@amarinov-msft

amarinov-msft commented Oct 20, 2026 •

Copy link
Copy Markdown
Contributor Author

Addressed the re-enable concern from #11999 (comment) in a128349. The existing active-touch IsOrphaned state now prevents OnWindowEnableChanged from reconstructing an abandoned press or overwriting a newer contact's mouse state. It does not unconditionally release buttons or change non-orphaned stylus re-enable behavior. No additional cancellation flag was needed.

Added 16 deterministic bookkeeping cases. On the previous PR product, 6 failed: both left/right abandoned-press resurrection cases and four other-button newer-contact cases. After the correction, all 219 Core input cases pass, including all 16 regressions. Coverage includes late normal/disabled-window Up cleanup, preservation of the newer promotion owner, device reuse, and the existing non-orphaned stylus rules. These tests confirm the state mismatch through the production bookkeeping paths; they are not a physical Drag/dialog reproduction.

Updated the description to identify native window disabling (including EnableWindow callers such as ElementHost.Enabled=false) as the trigger, keep enabled-window nested Dispatcher loops outside scope, and acknowledge that disabled-window Up abandonment also affects pen/stylus input. Exact validation commands and limitations are in the Testing section. The completed-touch replay receipts in #12034 do not cover this still-down contact sequence.

wnvko-msft
wnvko-msft previously approved these changes Oct 21, 2026

@wnvko-msft wnvko-msft left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The update addresses my previous concerns about restoring an abandoned mouse press on re-enable and preserving a newer contact’s press and promotion ownership. The added regression coverage looks appropriate, and I found no remaining blocking code issues for the #194 fix. Looks good to me.

subhajitm
subhajitm previously approved these changes Nov 4, 2026

@subhajitm subhajitm left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

lgtm. make sure we run the WPF regression test using the app.


if (_stylusLogic.CurrentMousePromotionStylusDevice == StylusDevice)
{
_stylusLogic.CurrentMousePromotionStylusDevice =

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Important: an orphaned contact isn't fully demoted, so it can still promote to mouse.

When it's the last active contact, CurrentMousePromotionStylusDevice becomes null, and ShouldPromoteToMouse treats null as "anyone can promote", including this orphan. Its StoredStagingAreaItems and PromotingToOther are also left as they were. Two ways this can bite:

  1. Window still disabled. The disabled-window up path calls OnDeactivate(). Releasing capture cancels the manipulation, and OnManipulationEnded(cancel) then calls PromoteStoredItemsToMouse synchronously. A stored drag could replay a mouse down into the disabled owner and leave the left button Pressed, which is Click event is rarely raised when using touch #194 again. I haven't reproduced this; it needs a stored drag while the manipulation is still pending.
  2. Owner re-enabled before the finger lifts (e.g. the dialog closes itself). With Fix completed WISP touch promotion across modal dialogs #12034, the orphan's up goes through PromoteMainUpToTouch, which builds a completed promotion and replays down+up onto the opener. That's a ghost Click that could reopen the dialog.

Suggested fix: here, clear _storedStagingAreaItems (bumping its version) and set PromotingToOther = false. You could also make ShouldPromoteToMouse return false when TouchDevice.IsOrphaned.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Addressed in b61ef3f
OnOrphanedImpl now clears StoredStagingAreaItems and bumps its version, so an in-flight PromoteStoredItemsToMouse loop aborts even if it already detached the queue. It also sets PromotingToOther = false.
• OnManipulationEnded no longer takes the cancel/replay branch for an orphaned contact.
• ShouldPromoteToMouse returns false for orphaned contacts, so a null CurrentMousePromotionStylusDevice no longer lets the orphan promote. With #12034, this also means it's up never builds a completed promotion, so there's no ghost Click.
• Because the orphan's up is no longer promoted, ResetStateForStylusUp wouldn't run on the re-enabled path. Tap state such as _fBlockMouseMoveChanges could then leak into the next contact that reuses the device. PromoteMainUpToTouch now resets it for orphaned contacts. The check runs after OnUp, since a TouchUp handler can orphan the contact too.
Also added regression tests.

// An orphaned contact has abandoned its press. Re-enabling must not restore it
// or overwrite a newer contact's mouse state.
if (!disabled && _currentStylusDevice != null &&
_currentStylusDevice.ActiveTouchDevice?.IsOrphaned != true)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Important (when combined with #12034): this guard is fine on its own. But after merging with #12034 it wraps the whole re-enable block, which then includes RetireCompletedTouchMousePresses and the UpdateMouseState() resync for buttons held by the physical mouse. If the current device is orphaned, that reconciliation is skipped too, and _mouseButtonsOwnedByMouse and the button state can stay stale (e.g. a physical button changed during a MessageBox).

Whichever PR merges second should apply the orphan check only to the last two stylus-derived assignments, not the whole block.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in 45e2f65
Now the orphan check wraps only the stylus-derived _mouseLeftButtonState/_mouseRightButtonState assignments, inside an outer !disabled && _currentStylusDevice != null block that matches #12034's. A trial merge now conflicts only on those assignments. The resolution keeps RetireCompletedTouchMousePresses and the UpdateMouseState()/InvalidateMouseButtonOwnership step unconditional and applies the orphan check only to the per-button assignments.


if (disabled)
{
OrphanTouchDevices(sourceHit);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Minor: orphaning only happens on WM_ENABLE.

  • Child HwndSources (HwndHost / interop-hosted content) never get WM_ENABLE when the top-level window is disabled, so contacts on them are never orphaned.
  • UnRegisterHwndForInput doesn't orphan contacts that are active on the source being torn down.

Both gaps existed before this PR, but they're worth a comment or a follow-up issue.

Also, with #12034, the order here matters: CancelCompletedTouchPromotionsForSource has to run before OrphanTouchDevices. Otherwise AbandonPromotedMouseButton bumps the button revision first, and RetireCompletedTouchMousePress skips ResetStateForStylusUp. A comment and a test that cover both in the same WM_ENABLE would protect that.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Agreed, both gaps are pre-existing. Child sources don't get IsWindowDisabled either, so fixing them properly is more than orphaning and outside this PR. I documented both on OrphanTouchDevices and opened a follow-up issue: #12179 .

For the ordering: there's now a comment at the OrphanTouchDevices call that disable cleanup depending on the prior button state must run before it. A trial merge with #12034 places CancelCompletedTouchPromotionsForSource first. I'll add a test that covers both in the same WM_ENABLE in whichever PR merges second, since it needs #12034's completed-promotion state.

if (_promotedMouseState == MouseButtonState.Pressed)
{
_promotedMouseState = MouseButtonState.Released;
_stylusLogic.UpdateMouseButtonState(_fLeftButtonDownTrigger ? RawMouseActions.Button1Release : RawMouseActions.Button2Release);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Minor: this can release a button press the device doesn't own.

This needs mouse and touch active at the same time, so it's an edge case. Suggest routing the abandon through a WispLogic helper that skips buttons owned by the physical mouse and only bumps the revision when the state actually changes.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Fixed in 67aabe4:
The device now records whether its promoted press actually changed the shared button state (PromoteMouseButtonPress), and AbandonPromotedMouseButton only releases a press it owns. A button already held, e.g. by the physical mouse, is left alone. The release goes through a new WispLogic.AbandonPromotedMouseButton helper. When this is combined with #12034, that helper is where to skip _mouseButtonsOwnedByMouse buttons and bump the revision only on an actual change. I'll do that in whichever PR merges second. Added AbandonedPress_DoesNotReleaseButtonPressedByOthers.

{
if (IsActive && !IsOrphaned)
{
IsOrphaned = true;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Minor / cosmetic: the base TouchDevice still keeps the orphan as _activeDevices[0], so GetPrimaryTouchPoint keeps reporting the orphan's point on the disabled window. Meanwhile the dialog's first contact also gets IsPrimary = true (via ActiveDeviceCountExcludingOrphaned), so two devices are "primary" at the same time.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

In 9996fa2, OnOrphaned now clears the stack's IsPrimary, so only the dialog's contact is primary in the touch stack. I left the base TouchDevice primary-point tracking alone: changing it means touching the shared base class used by every touch stack, and it only affects GetPrimaryTouchPoint on a window that can't take input until the orphan lifts. I documented that on OnOrphaned. Added OrphanedContact_IsNoLongerPrimary.

An orphaned contact kept its stored staging items and PromotingToOther,
and ShouldPromoteToMouse allowed it to promote once it was the last
active contact. A cancelled manipulation or a late up after re-enable
could then replay a mouse down (and with dotnet#12034, a ghost click).

- Clear and version-bump the stored items and reset PromotingToOther
  when a contact is orphaned.
- Skip the cancel replay in OnManipulationEnded for orphaned contacts.
- Never promote orphaned contacts to mouse.
- Reset per-device stylus state on an orphaned contact's up, since its
  up is no longer promoted and would otherwise leak tap state into the
  next contact on the same device.

Co-authored-by: Copilot <[email protected]>
Copilot-Session: e694ab5e-6feb-41c2-b4ee-19f115e0cf54
@amarinov-msft
amarinov-msft dismissed stale reviews from subhajitm and wnvko-msft via b61ef3f December 1, 2026 14:02
Keep the outer re-enable block unconditional and apply the orphaned
contact check only to the stylus-derived button assignments, so other
re-enable reconciliation (e.g. from dotnet#12034) is not skipped when the
current device is orphaned.

Co-authored-by: Copilot <[email protected]>
Copilot-Session: e694ab5e-6feb-41c2-b4ee-19f115e0cf54
amarinov-msft and others added 3 commits December 2, 2026 13:00
Note that orphaning must run after any per-source disable cleanup that
depends on the prior mouse button state, and that child HwndSources and
unregistered sources are not orphaned.

Co-authored-by: Copilot <[email protected]>
Copilot-Session: e694ab5e-6feb-41c2-b4ee-19f115e0cf54
The promoted mouse state was marked pressed even when the shared
button was already pressed (e.g. by the physical mouse), so abandoning
it released a press the device didn't make. Track whether the
promoted press changed the shared state and only release it then,
routing the release through a WispLogic helper.

Co-authored-by: Copilot <[email protected]>
Copilot-Session: e694ab5e-6feb-41c2-b4ee-19f115e0cf54
An orphaned contact stayed primary in the stylus touch stack while the
next contact (e.g. in a modal dialog) also became primary. Document that
the base TouchDevice still reports the orphan as the primary touch point
until its up.

Co-authored-by: Copilot <[email protected]>
Copilot-Session: e694ab5e-6feb-41c2-b4ee-19f115e0cf54
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-Touch Included in test pass PR metadata: Label to tag PRs, to facilitate with triage Status:Completed

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Click event is rarely raised when using touch

5 participants