Skip to content

macOS: onTapGesture does not fire in an offset NSHostingView #1083

Description

@Kyle-Ye

Clicks can pass AppKit hit testing but fail to trigger onTapGesture when an NSHostingView has a nonzero offset within its window. This occurs in the Example macOS preview, where the hosting view is centered inside the preview window.

Reproduction

  1. Use the following content in the Example app:

    struct ContentView: View {
        var body: some View {
            Text("Hello, world!")
                .onTapGesture {
                    print("Tapped")
                }
                .padding()
        }
    }
  2. Run the macOS preview with OpenSwiftUI and click the text. The same condition can be reproduced by placing an NSHostingView at a nonzero origin inside an AppKit container.

Expected: The tap handler runs once for each click. Hit testing stays correct when the hosting view or an ancestor moves.

Actual: The handler does not run. Event diagnostics show an empty BINDINGS section and root-phase ... failed, even though native hit testing finds the rendered text view.

Cause

MouseEvent.globalLocation uses window coordinates, but responder geometry remains relative to the hosting view. For example, the reported click is near (301.82, 199.96), while the text responder reports its global bounds at (16, 35) with size (76, 16).

NSHostingView does not conform to RootTransformProvider, and its as(...) method does not return that interface. RootTransform.value therefore falls back to an empty transform and omits the host-to-window mapping. Event binding fails before the gesture phase is evaluated.

Proposed fix

Add the root transform provider and delegate lookup, using the same window coordinate convention as mouse events. Observe changes to the hosting view's geometry in the window so that the transform is invalidated after host or ancestor movement.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    area: event-gestureEvents, gestures, tracing, or interaction plumbing.area: hosting-bridgeSwiftUI bridge, UIHosting/NSHosting, representables, and platform host views.platform: macOSmacOS-specific behavior or support.type: bugSomething is not working correctly.

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions