Function zng_wgt::node::with_widget_state

source ·
pub fn with_widget_state<U, I, T>(
    child: U,
    id: impl Into<StateId<T>>,
    default: I,
    value: impl IntoVar<T>
) -> impl UiNode
where U: UiNode, I: Fn() -> T + Send + 'static, T: StateValue + VarValue,
Expand description

Helper for declaring properties that set the widget state.

The state ID is set in WIDGET on init and is kept updated. On deinit it is set to the default value.

§Examples

static_id! {
    pub static ref FOO_ID: StateId<u32>;
}

#[property(CONTEXT)]
pub fn foo(child: impl UiNode, value: impl IntoVar<u32>) -> impl UiNode {
    with_widget_state(child, *FOO_ID, || 0, value)
}

// after the property is used and the widget initializes:

/// Get the value from outside the widget.
fn get_foo_outer(widget: &mut impl UiNode) -> u32 {
    widget.with_context(WidgetUpdateMode::Ignore, || WIDGET.get_state(*FOO_ID)).flatten().unwrap_or_default()
}

/// Get the value from inside the widget.
fn get_foo_inner() -> u32 {
    WIDGET.get_state(*FOO_ID).unwrap_or_default()
}