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