pub fn contextual_var<T: VarValue>(
context_init: impl FnMut() -> Var<T> + Send + 'static,
) -> Var<T>Expand description
Create a contextualized variable.
This is useful for declaring variables that depend on the contextual state on first usage to actually determinate the value.
§Examples
Basic usage:
widget_set! {
self;
padding = contextual_var(|| WINDOW.vars().safe_padding().map(|p| SideOffsets::from(*p)));
};The example above shows the declaration of a default widget property padding that depends on the contextual WINDOW.vars value.
When the padding property reads the variable for the first time (on UiNode::init) the contextual may be different from the
declaration, so closure will eval to produce a contextualized inner variable. If the widget is moved to another window the closure
will be called again to get a new contextualized inner variable.
This variable is for advanced usage like this, where you need a contextual value and there is no CONTEXT_VAR that provides the value. Note that you do not need this to contextualize context vars, they already are context aware.
§Capabilities
When the returned variable is used in a new context for the first time the context_init closure is called
to produce the actual variable in that context.
If a clone of the returned variable is moved to another context the context_init closure is called again
to init that clone.
If the return variable is mapped the mapping var is also context aware and will also delay init until first usage.
If AnyVar::capabilities is called in a new context the context_init is not called, the capabilities for an unloaded
contextual var is CONTEXT | MODIFY_CHANGES, if the context is loaded the inner variable capabilities is included.