zng/state_map.rs
1//! Hash-map of type erased values, useful for storing assorted dynamic state.
2//!
3//! A new map can be instantiated using [`OwnedStateMap`], but in a typical app you use maps provided by
4//! the API. The most common widget maps are [`WIDGET.with_state_mut`] that is associated
5//! with the widget instance and [`WidgetInfoBuilder::with_meta`] that is associated with the widget info.
6//!
7//! ```
8//! # fn main() { }
9//! use zng::{prelude::*, prelude_wgt::*};
10//!
11//! static_id! {
12//! static ref STATE_ID: StateId<bool>;
13//! }
14//!
15//! /// Extends [`WidgetInfo`] with state.
16//! pub trait StateWidgetInfoExt {
17//! /// Gets the state.
18//! fn state(&self) -> Option<bool>;
19//! }
20//! impl StateWidgetInfoExt for WidgetInfo {
21//! fn state(&self) -> Option<bool> {
22//! self.meta().get_clone(*STATE_ID)
23//! }
24//! }
25//!
26//! /// State the state info.
27//! #[property(CONTEXT)]
28//! pub fn state(child: impl UiNode, state: impl IntoVar<bool>) -> impl UiNode {
29//! let state = state.into_var();
30//! match_node(child, move |_, op| match op {
31//! UiNodeOp::Init => {
32//! WIDGET.sub_var_info(&state);
33//! }
34//! UiNodeOp::Info { info } => {
35//! info.set_meta(*STATE_ID, state.get());
36//! }
37//! _ => {}
38//! })
39//! }
40//! ```
41//!
42//! # Full API
43//!
44//! See [`zng_state_map`] for the full API.
45//!
46//! [`WIDGET.with_state_mut`]: crate::widget::WIDGET::with_state_mut
47//! [`WidgetInfoBuilder::with_meta`]: crate::widget::info::WidgetInfoBuilder::with_meta
48
49pub use zng_state_map::{
50 OwnedStateMap, StateId, StateMapMut, StateMapRef, StateValue,
51 state_map::{OccupiedStateMapEntry, StateMapEntry, VacantStateMapEntry},
52};