Skip to content

macos: Draw plain texts as layers with no view in the hierarchy - #2997

Open
huacnlee wants to merge 3 commits into
mainfrom
agent/T-J0015-leaf-layers
Open

huacnlee wants to merge 3 commits into
mainfrom
agent/T-J0015-leaf-layers

Conversation

@huacnlee

Copy link
Copy Markdown
Member

Summary

Part of #2066. On macOS, a plain text node (no listener, no props, no tracked states, no children) no longer joins its parent's subviews: only its CALayer goes into the parent's container layer. AppKit's per-subview bookkeeping on insert (geometry, display and accessibility tracking, layer-tree attach) is skipped for these nodes.

Rules:

  • Leaves are only used under a plain View whose children are all leaves, so layer order is child order. Scroll views, stacks and views that host native AppKit views keep real subviews.
  • A leaf turns back into a view (together with its sibling leaves, in order) when it gets a listener (Events), props (ViewProps), tracked states, or a child, or when a non-leaf child is inserted into its parent.
  • Picking (input::pick_in / pick_children), child_views (selection walk, sticky cells, etc.), accessibilityChildren / accessibilityParent, a button's name gathered from its content, access::frame_of, and the agent's bounds / text_bounds all reach leaves through their parent.
  • A leaf holds its parent weakly; the parent holds its leaves, so deleting a subtree frees them.
  • Leaf layers live in one plain CALayer per parent (inside its container layer). Put straight into a view-backed layer, AppKit removes a view's layer whose view is not a subview on the next update, and the texts vanished.
  • isDescendantOf: checks that may see a leaf (window::root_containing, shader marks, hover clean-up on removal) ask the leaf's parent; leaves are rescaled when their parent moves window or backing scale, and sharpened on insert.

The leaf still keeps its ViewObj (its state lives in the NSView ivars). Not creating the ViewObj at all would also take the Create cost down, but that means moving ViewState off the NSView and is left for a follow-up.

Performance

From an earlier measurement prototype (gallery, release build without LTO, 3 launches per group). The prototype put leaf layers straight into the parent's layer and had none of the fallbacks above, so this commit still needs its own A/B run:

Batch Group apply total Create Insert
First screen (Stocks, 1418 creates, 690 text leaves) before median ~129 ms (95–142) 21–38 median ~37
after median ~110 ms (107–112) 29–31 27–29
Sidebar (803 creates, 311 text leaves) before 164–176 ms 17–19 26–27
after 140–147 ms 16–18 16–18

These numbers are from the prototype, not re-measured on this commit; the extra host layer per parent and the fallbacks may take some of the gain back.

Tests

  • New examples/gallery/tests/leaf-layers.test.tsx (macOS): a press on a leaf label reaches its button; the button's accessible name is read from the leaf; a leaf that gets a listener takes the press; leaf rows in a scroll view.
  • Existing copy-selection, accordion, button, data-table-interactions run on macOS against this branch.

Results on a locked Mac mini (debug host): leaf-layers 4/4, accordion 4/4, button 7/7, data-table-interactions 20/22. The two data-table failures (the Tab stop test times out; keyboard.down is unsupported) and all 7 copy-selection failures (no pressable view matches id:composer) fail the same way on main on that machine, so cross-text selection is not covered by this run.

Screenshots (gallery, main vs this branch; only the live quote values differ):

main this branch
before after

Not verified

  • A release A/B of this commit and first-frame time (the Mac was locked).
  • VoiceOver itself was not run; accessibility was checked through the test driver's role/name queries.

🤖 Noop (rust-maintainer) · AI agent acting for @huacnlee

Jason Lee added 3 commits November 24, 2026 02:08
A text with no listener, props, tracked states or children no longer
joins its parent's subviews: only its layer goes into the parent's
container layer (#2066). AppKit's per-subview bookkeeping on insert is
skipped, which took about 25-35% off Insert and about 15% off a batch's
apply in the gallery's first screen.

Leaves are only used under a plain view whose children are all leaves,
so the layer order stays the child order. A leaf turns back into a view
(with its siblings) once it gets a listener, props, tracked states or a
child, or when a view child joins its parent. Picking, the accessibility
children and names, selection, and the agent's bounds reach leaves
through their parent.

Agent: rust-maintainer
A leaf is in no view hierarchy, so `isDescendantOf:` and AppKit's
window and backing-scale callbacks never reach it. Ask its parent in
`root_containing`, the shader marks and the hover clean-up on removal,
rescale leaves when their parent moves to a window or changes backing
scale, and sharpen and tell `sight` on leaf insert and removal.

Agent: rust-maintainer
AppKit takes a view's layer out of a view-backed layer whose view does
not have it as a subview, so leaves put straight into their parent's
layer disappeared after the next update. They now go into one plain
layer per parent, which AppKit leaves alone, and that layer moves into
the container when the parent gets one (it used to re-add the leaves'
views as subviews there).

Agent: rust-maintainer

This branch has not been deployed

No deployments
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