Skip to content

core: Close an open menu on a secondary click outside it without opening another - #4221

Merged
huacnlee merged 2 commits into
mainfrom
agent/right-click-closes-menu
Dec 3, 2026
Merged

huacnlee merged 2 commits into
mainfrom
agent/right-click-closes-menu

Conversation

@huacnlee

@huacnlee huacnlee commented Dec 3, 2026

Copy link
Copy Markdown
Member

Description

While a menu, popover or any overlay listening to onPressOutside is open, a primary click outside it closes it, but a right click did not behave the same way:

  • On a view with no onContextMenu listener, or on selectable text or a text field, the platform got no press at all: the open menu stayed up and the platform's own edit menu could open on top of it, leaving two menus.
  • On a view listening to onContextMenu, the menu closed and a new one opened at once (behavior added in menu: Reopen ContextMenu where its trigger is right-clicked again #1610).

A secondary click outside an open menu now only closes it. It opens no other menu, neither the app's nor the platform's. The next right click works as usual. A right click inside the overlay is not affected.

How

  • Core (nsh-core): while an overlay that listens to onPressOutside is shown, the root view it is shown in also gets onPressOutside among its mounted events. This is an internal marker that lets the platform know a press-outside overlay is open in that window.
  • nsh_core::input::context_menu_target: if the root hop has that marker and no inner hop listens to onPressOutside (the click is outside every such overlay), it returns the new ContextMenuTarget::Dismiss(root).
  • Platforms (macOS, Linux, Windows): on Dismiss, they skip the app, field, edit-menu and text-link menus and report the click as a context menu on the root view.
  • Engine: if a secondary click raised onPressOutside events, they are handed out and onContextMenu is not dispatched. This reverses the "close and reopen at the pointer" part of menu: Reopen ContextMenu where its trigger is right-clicked again #1610 (the controlled-open fix in popup.rs from that PR is kept).
  • Headless tester: rightClick picks targets with the same rule, so app tests behave like the desktops.

Public API

No new app-facing API. ContextMenuHop gains a press_outside field and ContextMenuTarget gains a Dismiss variant. Both live in nsh_core::input and are used by the platform crates only.

Behavior change: ContextMenu's docs now say that a secondary click outside an open menu, whether on its trigger or anywhere else, only closes it.

How to Test

  • cargo test -p nsh-core --lib: all 2160 pass. New tests:
    • input::tests::a_secondary_click_outside_an_open_menu_only_closes_it
    • core::tests::the_root_view_of_an_overlay_watching_presses_tells_its_platform_view
  • cargo test -p nsh-base --test base: all 1345 pass. a_secondary_click_on_the_trigger_of_an_open_context_menu_only_closes_it replaces the reopen test.
  • cargo test -p nsh-testing --features bundle --test testing -- a_press_tells_an_overlay: the new TSX case checks that a right click on a row with onContextMenu while an overlay is open tells the overlay onPressOutside and calls no onContextMenu.
  • cargo test -p nsh-engine --test engine -- --skip websocket: all 493 pass. The websocket tests fail in this sandbox because they need loopback networking.
  • examples/gallery/tests/context-menu-reopen.test.tsx is updated to the new behavior. I did not run it locally.

Not verified: I built and tested this on Linux only. The macOS and Windows platform changes were not compiled locally and rely on CI. I have not checked this in a real window on any platform yet, including a native text field's own menu on macOS, which AppKit may show before NSH sees the click.

🤖 Generated with Claude Code

🤖 Noop (lead) · Opus 5.5 acting for @huacnlee

huacnlee and others added 2 commits December 3, 2026 21:42
…ing another

A secondary click outside an open menu (any overlay listening to
onPressOutside) now only closes it, on macOS, Linux and Windows: it opens
neither the app's onContextMenu menu nor a platform menu (a text field's,
selectable text's edit menu).

Agent: lead
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…s menu

A Popover with closeOnPressOutside={false} still hears onPressOutside (it
reports a cancel), but a press outside leaves it open: a secondary click
outside it now goes on to the menu it asks for instead of only closing
overlays that do not close. Fixes the compat case base-popups-desktop.

Agent: lead
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@huacnlee

huacnlee commented Dec 3, 2026

Copy link
Copy Markdown
Member Author

Pushed b393966 to fix the compat failure (case-browser step 62 expect DeskCopy).

Cause: the case keeps a Popover with closeOnPressOutside={false} open. It listens to onPressOutside (to report a cancel), so the first commit treated it as an open menu and swallowed every secondary click while it was shown.

Fix: such a popover now marks its overlay as staying open (an internal, #[doc(hidden)] overlay prop, no app-facing API). It still hears onPressOutside, but neither the engine nor the platforms' root-view check treat it as something a secondary click should only close; the click goes on to onContextMenu / the platform menu. Covered by a new nsh-core test.

Local (Windows): nsh-core overlay/context-menu tests and nsh-base popover/menu tests (159) pass; nsh-base and nsh-engine check clean.

🤖 Noop (lead) · AI agent acting for @huacnlee

@huacnlee

huacnlee commented Dec 3, 2026

Copy link
Copy Markdown
Member Author

💡 Consistent implementation, but it reverses #1610: a secondary click outside an open menu now only closes it instead of also opening the new context menu, unlike macOS native behaviour.

  • nsh-engine/src/lib.rs: closing click no longer dispatches onContextMenu.
  • New public items: ContextMenuHop::press_outside, ContextMenuTarget::Dismiss(usize) (breaks exhaustive matches), OverlayProp::StaysOpen, hidden Overlay::stays_open_on_press_outside.
  • Not checked in a real window on any platform; macOS native text fields may show their own menu first.
  • Tests cover core, nsh-base and nsh-testing. CI pending.

Reviewed at b393966.

🤖 Noop (reviewer) · AI agent acting for @huacnlee

@huacnlee
huacnlee merged commit 423be19 into main Dec 3, 2026
21 of 29 checks passed
@huacnlee
huacnlee deleted the agent/right-click-closes-menu branch December 3, 2026 17:24
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant