Skip to content

fix: fire Ruby on_select and on_dismiss on Cupertino ContextMenu - #40

Merged
AdamMusa merged 2 commits into
codex/engine-initializationfrom
fix/cupertino-context-menu-select-dismiss
Oct 29, 2026
Merged

AdamMusa merged 2 commits into
codex/engine-initializationfrom
fix/cupertino-context-menu-select-dismiss

Conversation

@AdamMusa

Copy link
Copy Markdown
Owner

Summary

Ruby's ContextMenu (materials/contextmenu_control.rb, wire type ContextMenu) declares on_select and on_dismiss. The Material renderer fires "select" when an item is chosen and "dismiss" when the menu closes without a choice. Both carry the payload {b, tr, id, idx, ic, g, l}.

On iOS and macOS, CupertinoContextMenuControl renders Flutter's CupertinoContextMenu. Before this fix, only each action's own "click" fired, so context_menu(items: [...], on_select:, on_dismiss:) never reported either event.

Fix

  • on_select: a small CupertinoContextMenuSelection inherited widget wraps each action. CupertinoContextMenuActionControl, which also renders PopupMenuItem, reports the pressed item to it before closing. The menu then fires "select" with the Material payload keys: id is the item id, idx its index among the visible items, and ic the item count. The item's own "click" still fires.
  • on_dismiss: CupertinoContextMenu has no open or close callbacks, but its actions are only built while the menu route is up. Each wrapped action registers with the menu when it mounts and unregisters when it disposes. When the last one goes, the session has ended, and a session without a chosen item fires "dismiss". Because the count is reference-counted, a rebuilt sheet (for example after rotation) does not end the session.
  • The payload uses b: "primary" and tr: "longPress", because the native menu opens on a long press. g and l are null, because the native menu reports no pointer position.

Out of scope: the Cupertino renderer still ignores primary_items, secondary_items and tertiary_items, since iOS has a single long-press trigger, and it does not wrap the menu in LayoutControl.

Tests

New test/widgets/ruby_context_menu_events_test.dart (Cupertino). It long-presses to open the native menu, then:

  • Tapping "Delete" fires exactly select with id 4, idx 1 and ic 2, and no dismiss.
  • Tapping "Open" fires the item's click and the menu's select with idx 0.
  • Tapping outside fires exactly dismiss with ic 2. A second session then reports select independently.

All three tests failed before the fix, with no menu events.

verify_pr.sh: flutter test +698, all passed (695 baseline + 3 new). flutter analyze lib test found no issues. Flutter 3.41.2.

Wave-2 follow-up P2

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