Expand description
Anchored info cards — measured, placed, and clamped by the library.
A recurring overlay shape: a small rounded card with a title and a few detail lines, floated next to an anchor point (a tapped body, a hovered element, a reticle). Every app that hand-rolls one repeats the same three mistakes:
- Guessed text width (
chars × size × k) — wrong for anything non-monospace or non-ASCII, so the card is too tight or too wide. - Clamping against its own widget bounds only — the card lands underneath sibling edge chrome (button rails, trays, the on-screen keyboard) and looks cut off.
- No side-flipping — when the anchor nears an edge the card gets pushed over the anchor or off-screen instead of flipping sides.
This module owns all three: measure uses the draw context’s real
text metrics, and anchored_rect places the card against the
viewport-wide safe area (crate::overlay_insets) with symmetric
margins, flipping below/above the anchor as space demands. The
convenience wrapper paint_anchored does measure → place → paint in
one call — the pit of success for tap-info overlays.
All coordinates are logical, Y-up, relative to the widget doing the
painting — pass that widget’s size as container. When the widget
fills the viewport (the common overlay-host case) the safe-area
insets line up 1:1; a non-fullscreen host can still use
anchored_rect_with_insets with its own insets.
Structs§
- Card
Style - Visual + spacing parameters for an anchored card. The defaults give a dark tooltip-style card; override the colors to match app chrome.
Functions§
- anchored_
rect - Place a card of
sizenearanchorinsidecontainer, avoiding the frame’scrate::overlay_insets. Seeanchored_rect_with_insets. - anchored_
rect_ with_ insets - Pure placement: prefer sitting fully below the anchor (Y-up: spanning
downward from
anchor.y - clearance), flip fully above when the bottom lacks room, and otherwise clamp into the safe area on the side with more space. Horizontally the card centres on the anchor and clamps; if it is wider than the safe area it centres so any overflow is symmetric. The result never leavescontainer − insets − marginon any side the card fits. - measure
- Measure the card needed for
title+detailsusing the context’s real text metrics (falls back to a monospace estimate only if the backend can’t measure). - paint
- Paint the card chrome + text into
rect(as produced byanchored_rect). - paint_
anchored - Measure → wrap → place → paint in one call; returns the rect painted so the caller can hit-test or decorate it. This is the intended entry point: detail lines wrap to the safe width, so even a narrow phone viewport with a wide reserved rail produces a taller card instead of one that pokes underneath the chrome. A clipped or edge-hugging card now requires deliberate effort rather than being the accidental default.