pub fn with_widget_state<U, I, T>(
child: U,
id: impl Into<StateId<T>>,
default: I,
value: impl IntoVar<T>,
) -> UiNodeExpand 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 IntoUiNode, value: impl IntoVar<u32>) -> 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 UiNode) -> u32 {
if let Some(mut wgt) = widget.as_widget() {
wgt.with_context(WidgetUpdateMode::Ignore, || WIDGET.get_state(*FOO_ID))
.unwrap_or(0)
} else {
0
}
}
/// Get the value from inside the widget.
fn get_foo_inner() -> u32 {
WIDGET.get_state(*FOO_ID).unwrap_or_default()
}