zng/
data_view.rs

1#![cfg(feature = "data_view")]
2
3//! Data view widget.
4//!
5//! The [`DataView!`](struct@DataView) widget can be used to dynamically presents data from a variable, unlike
6//! the [`widget::node::presenter`](crate::widget::node::presenter) node the generated UI can be retained
7//! across updates of the data variable.
8//!
9//! The example below declares a `DataView!` using the shorthand syntax:
10//!
11//! ```
12//! use zng::prelude::*;
13//!
14//! fn countdown(n: impl IntoVar<usize>) -> impl UiNode {
15//!     DataView!(::<usize>, n, hn!(|a: &DataViewArgs<usize>| {
16//!         // we generate a new view on the first call or when the data has changed to zero.
17//!         if a.view_is_nil() || a.data().get_new() == Some(0) {
18//!             a.set_view(if a.data().get() > 0 {
19//!                 // countdown view
20//!                 Text! {
21//!                     font_size = 28;
22//!                     // bind data, same view will be used for all n > 0 values.
23//!                     txt = a.data().map_to_txt();
24//!                 }
25//!             } else {
26//!                 // finished view
27//!                 Text! {
28//!                     font_color = rgb(0, 128, 0);
29//!                     font_size = 18;
30//!                     txt = "Congratulations!";
31//!                 }
32//!             });
33//!         }
34//!     }))
35//! }
36//! ```
37//!
38//! You can also use the normal widget syntax and set the `view` property.
39//!
40//! ```
41//! # use zng::prelude::*;
42//! # let _scope = APP.defaults(); let n = var(0usize); let _ =
43//! DataView! {
44//!     view::<usize> = {
45//!         data: n,
46//!         update: hn!(|a: &DataViewArgs<usize>| { }),
47//!     };
48//! }
49//! # ;
50//! ```
51//!
52//! # Full API
53//!
54//! See [`zng_wgt_data_view`] for the full view API.
55
56pub use zng_wgt_data_view::{DataView, DataViewArgs};