pub struct Cx<'a, S> { /* private fields */ }Expand description
Build context threaded through a view closure.
Carries the active Theme and accumulates event handlers. Registering a
handler returns an id the caller stamps onto an element (see
Element::on_tap /
Element::on_key).
Implementations§
Source§impl<'a, S> Cx<'a, S>
impl<'a, S> Cx<'a, S>
Sourcepub fn register(&mut self, handler: impl FnMut(&mut S) + 'static) -> ActionId
pub fn register(&mut self, handler: impl FnMut(&mut S) + 'static) -> ActionId
Register a pointer-tap handler, returning its ActionId.
Sourcepub fn register_key(
&mut self,
handler: impl FnMut(&mut S, &KeyInput) + 'static,
) -> FocusId
pub fn register_key( &mut self, handler: impl FnMut(&mut S, &KeyInput) + 'static, ) -> FocusId
Register a keyboard handler for a focusable element, returning its
FocusId.
Sourcepub fn register_drag(
&mut self,
handler: impl FnMut(&mut S, f64) + 'static,
) -> DragId
pub fn register_drag( &mut self, handler: impl FnMut(&mut S, f64) + 'static, ) -> DragId
Register a drag handler, returning its DragId. The handler receives
the pointer’s fractional x position (0..=1) across the element.
Sourcepub fn register_text_pos(
&mut self,
handler: impl FnMut(&mut S, usize, bool) + 'static,
) -> TextPosId
pub fn register_text_pos( &mut self, handler: impl FnMut(&mut S, usize, bool) + 'static, ) -> TextPosId
Register a text-pointer handler, returning its TextPosId. The handler
receives a resolved byte index into the element’s text and an extend
flag (false = place caret, true = extend selection).
Sourcepub fn register_context(
&mut self,
handler: impl FnMut(&mut S, Point) + 'static,
) -> ContextId
pub fn register_context( &mut self, handler: impl FnMut(&mut S, Point) + 'static, ) -> ContextId
Register a secondary-click (context) handler, returning its
ContextId. The handler receives the right-click position in logical
pixels — typically used to open a context menu there via Cx::overlay.
Sourcepub fn register_scroll(&mut self) -> ScrollId
pub fn register_scroll(&mut self) -> ScrollId
Register a scroll container, returning a stable ScrollId. The app
keeps the scroll offset for this id and re-applies it each frame; there is
no handler closure (scrolling adjusts the offset directly).
Sourcepub fn overlay(&mut self, spec: OverlaySpec)
pub fn overlay(&mut self, spec: OverlaySpec)
Declare a floating overlay layer (menu/popover/tooltip/dialog) drawn above
the main tree this frame. Build spec.content with this same Cx first
so its handlers register normally.
Sourcepub fn take_overlays(&mut self) -> Vec<OverlaySpec>
pub fn take_overlays(&mut self) -> Vec<OverlaySpec>
Take the overlays declared this frame (the app lays them out + paints them
on top). Call before into_handlers.
Sourcepub fn memo(&mut self, key: u64, build: impl FnOnce() -> Element) -> Element
pub fn memo(&mut self, key: u64, build: impl FnOnce() -> Element) -> Element
Return a cached, static subtree for key, building it with build
only when the key is new (or after the cache was seeded from a prior
frame). On an unchanged key the build closure is skipped entirely — the
previous frame’s Element is cloned — so unchanged branches aren’t
rebuilt. build receives no Cx, so a memoized subtree can’t register
event handlers (their ids would desync); use it for display-only content
like icons, labels, or decorative panels whose look depends on key.
Sourcepub fn set_memo_cache(&mut self, cache: HashMap<u64, Element>)
pub fn set_memo_cache(&mut self, cache: HashMap<u64, Element>)
Seed the memo cache from the previous frame (see Cx::memo).
Sourcepub fn take_memo_cache(&mut self) -> HashMap<u64, Element>
pub fn take_memo_cache(&mut self) -> HashMap<u64, Element>
Take the memo cache back, dropping entries not touched this frame so it doesn’t grow without bound.
Sourcepub fn into_handlers(self) -> Handlers<S>
pub fn into_handlers(self) -> Handlers<S>
Consume the context, yielding the accumulated Handlers table.