Repository navigation
Tags
Tags
context_menu: Show the system's context menu on iOS for `items` (#2202) ## Description On iOS a finger held on a message, a photo or a row is expected to bring up the system's context menu: the view lifts over the blurred screen with the system's haptics and motion, and the menu shows beside it. NSH's `ContextMenu` draws its own sheet or popup there, and `MenuButton` (the system's `UIMenu`) only opens from a tap. This lets a `ContextMenu` take `MenuButton`'s item data. Given `items`, iOS (and Mac Catalyst) shows the system's context menu instead of the drawn one: ```tsx <ContextMenu title={photo.name} items={[ { id: "share", label: "Share", icon: "square.and.arrow.up" }, { id: "", label: "", inline: true, items: [{ id: "delete", label: "Delete", icon: "trash", destructive: true }] }, ]} id }) => act(photo, id)} trigger={<PhotoTile photo={photo} />} > {/* the drawn menu of the other platforms */} </ContextMenu> ``` - **iOS / Mac Catalyst**: a new native component, `nsh/context-menu` (`nsh-platform-ios`'s `system_context_menu.rs`), puts a `UIContextMenuInteraction` on the view around the trigger. A held finger, or an iPad pointer's / a Mac's secondary click, is answered natively without waiting for JS; the trigger itself is the preview the system lifts, clipped to its own outline (each corner as drawn); the menu is built from the mounted items with the same code as `MenuButton`'s (SF Symbols, checks, disabled and destructive items, submenus, inline sections, `title`). The component reports `open`, `select` and `close`, which `ContextMenu` turns into the events the drawn menu already has: `onOpenChange` (`longPress` for the lifted menu, `contextMenu` for the secondary click's), `onOpen`, `onSelect({ id })`, `onCancel`, `onClose`, in the drawn menu's order (a dismissal by the user: `onCancel`, `onOpenChange(false)`, `onClose`; the app closing it: `onClose` only). The backend holds a chosen action until it tells the dismissal and tells the choice first, so `ContextMenu` always hears a choice before the closing it caused, with no timer; a choice the backend can only tell after the dismissal still reaches `onSelect`. While the system's menu is used, the drawn menu's openers (the held-finger timer, `onContextMenu`) stand aside, so one menu opens. The system only opens it for the user: `open` set to `true` shows nothing; `open` set to `false` closes it. - **Android, the desktops, the web**: unchanged. `items`, `title` and `onSelect` are not used there; the children are the menu, drawn as without `items` (the website and the prop docs say so). - Without `items`, nothing changes anywhere: off iOS the menu is made by the same code as before (no extra memo, region, listener or element), and a test holds iOS without items to the same mount items as Android, as the menu renders and as a held finger opens it. `Menu` is untouched. The item model is `MenuButtonItem` as it is, so an app describes a system menu one way for both components. A custom preview view (another view than the trigger) and preview styling props are left out: the trigger's own corners and the system's platter cover the common rows, cards and bubbles. Also: - `examples/gallery`'s Menus page: a note in "The system's menu" card with both menus (UIKit's on iOS, the gallery's drawn menu elsewhere). - `website/elements/menu.md` (English and zh-CN): "The system's context menu on iOS", the new props and event; `docs/dev/components.md` and `docs/dev/api-naming.md`: where it lives and why it is an addition. - `tests/compat/cases/base`: a `ContextMenu` with `items` on the headless runtime (iOS) draws no menu on a held finger (`covers [props] ContextMenu.items`). ## Public API - `nsh/base` `ContextMenu`: prop `items?: MenuButtonItem[]` (default `[]`) — iOS only (and Mac Catalyst): the system's context menu (`UIContextMenuInteraction`) in place of the children; not used on the other platforms. - `nsh/base` `ContextMenu`: prop `title?: string` — iOS only: the system menu's title above its items. - `nsh/base` `ContextMenu`: event `onSelect({ id }: { id: string })` — iOS only, with `items`: the chosen item's id. - Rust `nsh_base::ContextMenuElement::items(..)`, `::title(..)`, `::on_select(|id: &SharedString| ..)`. - Rust `nsh_base::ContextMenuEvents` — `ContextMenu`'s own events: `MenuEvent`'s variants plus `Select(SharedString)`, so `Menu` keeps its events and declares no `onSelect` it would never emit. The name is plural because the prelude already exports `ContextMenuEvent` (the core's `onContextMenu` payload). `From<MenuEvent>` is implemented. - Rust `nsh_core::platform::CONTEXT_MENU_COMPONENT: &str = "nsh/context-menu"` — the native component's name, registered by the iOS backend. These are additions for a maintainer's decision; the shape follows `MenuButton` (items as data, `onSelect({ id })`). ## Breaking Changes Rust only (no release is recorded yet; the JS face only gains `onSelect`): `ContextMenu`'s listeners take `ContextMenuEvents` instead of `MenuEvent`. ```diff - context_menu().on_open_change(|e: &MenuEvent| if let MenuEvent::OpenChange(open, _) = e { .. }) + context_menu().on_open_change(|e: &ContextMenuEvents| if let ContextMenuEvents::OpenChange(open, _) = e { .. }) ``` ## How to Test Builds and tests were not run locally; please let CI run, plus: ```sh cargo test -p nsh-base --test base menu:: # incl. without_items_ios_mounts_what_the_other_platforms_do, # a_system_menu_the_app_closes_reports_only_its_closing, # a_system_menus_choice_told_after_its_dismissal_still_reaches_the_app cargo test -p nsh-engine --test engine context_menu cargo test -p nsh-platform-ios --target aarch64-apple-ios-macabi # unit tests compile there; run where supported cargo clippy -p nsh-platform-ios -p nsh-ios-host -p nsh-ios-app --target aarch64-apple-ios-macabi --features nsh-platform-ios/embed,nsh-platform-ios/dev cargo clippy --workspace --all-targets cargo xtask compat cargo test --workspace ``` On a simulator or device: `./target/debug/nsh run examples/gallery --target ios`, Menus → "The system's menu", hold the note (the lift, the blur, the menu; a choice updates the star / folder and the Events card with no cancel), tap outside to dismiss; a trigger with different corners (`rounded-t-xl rounded-b-sm`) lifts with them; check light and dark. Mac Catalyst: right click the note. ## Checklist - [x] I have read the [CONTRIBUTING](../CONTRIBUTING.md) document and followed the guidelines. - [x] Reviewed the changes in this PR and confirmed AI generated code (if any) is accurate. - [ ] Passed `cargo test --workspace`, `cargo +nightly fmt --all` and clippy (including the Catalyst and Android runs for platform code). - [x] Changes to what apps use keep what released versions shipped and come with a case in `tests/compat/` (`cargo xtask compat`). - [ ] Ran the related `examples/gallery` demos on iOS and Android (if the change is visible or platform-specific).
docs: Lead the README and home page with native UI, plugins and TSX (#45 ) ## Summary The README read as a list of internals and commands, so a visitor could not tell quickly what Native Shell does or how to start. This rewrites it to put the main points first, and reorders the home page's feature cards the same way. - **README:** opens with one paragraph on what nsh does and a short counter written with Tailwind classes. Then come, in order: - the features: real native UI; plugins loaded at run time, or standalone apps; AI-friendly TSX + Tailwind; sandboxed and signed; fast, with no Node.js; - the platform table; - a quick start (install, `nsh init`, `nsh run`); - embedding snippets for iOS and Android; - shipping signed updates; - the examples (now listing `examples/desktop` and `examples/gallery-rs`); - building from source. The details link to native-shell.com. - **Home page (en and zh-CN):** the feature cards now go native UI → plugins or standalone apps (new card, with a plug icon and an illustration) → AI-friendly: TSX + Tailwind (merges the "format models write well" and "generated contract" cards). The section lead text is updated to match. ## Testing - Ran the README's example with `nsh trace`: it renders, and two taps update the count. - Ran `astro build` and the website's `bun test`. - Checked screenshots of the built home page in headless Chromium, English at 1400 px and 400 px wide and zh-CN at 1400 px. The cards lay out correctly in both languages. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>