Skip to main content

Cx

Struct Cx 

Source
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>

Source

pub fn new(theme: &'a Theme) -> Self

Create a context borrowing theme.

Source

pub fn theme(&self) -> &Theme

The active theme.

Source

pub fn register(&mut self, handler: impl FnMut(&mut S) + 'static) -> ActionId

Register a pointer-tap handler, returning its ActionId.

Source

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.

Source

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.

Source

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).

Source

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.

Source

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).

Source

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.

Source

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.

Source

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.

Source

pub fn set_memo_cache(&mut self, cache: HashMap<u64, Element>)

Seed the memo cache from the previous frame (see Cx::memo).

Source

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.

Source

pub fn into_handlers(self) -> Handlers<S>

Consume the context, yielding the accumulated Handlers table.

Trait Implementations§

Source§

impl<S> Debug for Cx<'_, S>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a, S> !RefUnwindSafe for Cx<'a, S>

§

impl<'a, S> !Send for Cx<'a, S>

§

impl<'a, S> !Sync for Cx<'a, S>

§

impl<'a, S> !UnwindSafe for Cx<'a, S>

§

impl<'a, S> Freeze for Cx<'a, S>

§

impl<'a, S> Unpin for Cx<'a, S>

§

impl<'a, S> UnsafeUnpin for Cx<'a, S>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.