Skip to content

fix(ui): keep a ContextMenu's rows apart from its target's parts - #37

Closed
xiaoyumuxi wants to merge 1 commit into
mainfrom
fix/context-menu-scope
Closed

xiaoyumuxi wants to merge 1 commit into
mainfrom
fix/context-menu-scope

Conversation

@xiaoyumuxi

Copy link
Copy Markdown

Upstream: vivaui#341 (same branch).

This branch is based on upstream aa89181, which fork main already contains, so it merges cleanly. Lithe needs it so the first row of the editor and gutter context menus can be clicked. The Viva maintainers bump the replace pin in Lithe after this is merged.

What this changes

An open ContextMenu now declares its rows, its backdrop and its scroll bar in a place of its own under the target's scope (~menu), instead of in the target's scope itself. The menu's state is still kept at the target's scope, so nothing changes for a closed menu. ContextMenuAt already had a scope of its own (bound to its *bool) and is unchanged.

Why

contextMenu used tree.Sub(id.Scope(), id.Name()) of the target as the menu's scope. Control hands out the same scope as the control's parts (Handlers.Parts), and a container given Hit has its children there too. So for a ContextMenu on a control with parts:

  • row i was the identity parts.At(3+i), the backdrop parts.At(1) and the bar parts.At(2);
  • Page.controlOf found the control for each of them (h.parts == id.Scope()) and routed the press to its Pointer as a press on that part;
  • a control that handled the press returned Handled, the painter then delivered no click, and the row was never chosen.

We hit it in a text editor that names its scroll bars parts.At(2) and parts.At(3): the first row of its context menu (Find) could never be chosen with the pointer, while the second row worked. The keyboard worked, because keys focus the row directly.

How it was verified

On macOS 27 (arm64):

  • test -z "$(gofmt -l $(git ls-files '*.go'))"
  • go vet ./...
  • go test ./... -count=1: every package passes except cmd/viva, whose packaging tests fail on this machine with "the program was built for macOS 13.0 or later but [mac] min_system says 12.0". They fail the same way on aa89181 without this change, so the cause is the local toolchain, not this change.
  • platform vet: no backend is touched.
  • benchmarks before and after: no existing benchmark declares a ContextMenu, so I ran a temporary one (not committed). It paints a page of 200 targets, each with a closed ContextMenu, -count 6, on aa89181 and on this branch: about 113 µs/op both times, 0 allocs/op. BenchmarkMenuOpen and BenchmarkTree also show no change. A first version that made the ~menu scope on every paint cost about 5% on that page, which is why the scope is now made only while the menu is open.
  • stripped binary size: no new links.
  • a real window: the same fix, applied to the application that found it (Lithe), makes the first row of its editor menu clickable in its headless UI tests.

TestControlContextMenuIsNotItsParts (in ui/control_parts_test.go) puts a ContextMenu on the five-star rating Control and checks that:

  • each of the menu's three rows can be chosen with the pointer;
  • a click outside the menu closes it without pressing a star;
  • a repaint with the menu open allocates nothing.

Without the fix it fails at once with a click on Clear chose [], menu shown true, because the first row's press went to star 4.

Where the boundary sits

Framework capability: it fixes how ContextMenu names what it declares. There is no product behaviour.

Cost

Nothing for a closed menu. An open context menu does one more interned scope lookup per paint.

A ContextMenu declared its rows, backdrop and scroll bar in the scope of its target, tree.Sub(id.Scope(), id.Name()). That is also the scope Control gives the parts of a control (Handlers.Parts), and the scope a container's children live in. So row i of the menu was the identity parts.At(3+i), the backdrop parts.At(1) and the bar parts.At(2): a press on a row was routed to the control's Pointer as a press on its part, and a control that handled it took the click, so the row was never chosen. A text editor that names its scroll bars parts.At(2) and parts.At(3) could never choose the first row of its own context menu by pointer.

The open menu now takes a place of its own under the target's scope (~menu). Its state is still kept at the target's scope, so a closed ContextMenu costs a paint nothing more.
@1lck

1lck commented Nov 18, 2026

Copy link
Copy Markdown

Superseded: upstream merged the same branch as vivaui#341 (b4ee5fd), which fork main now contains through #39.

@1lck 1lck closed this Nov 18, 2026
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.

2 participants