zng/
handler.rs

1//! Event handler API.
2//!
3//! A handler is a closure that takes a *context* and *arguments*, the context can be [`WIDGET`] or the app,
4//! handler types implement [`WidgetHandler`] or [`AppHandler`] respectively. These traits allow a single caller
5//! to support multiple different flavors of handlers, both synchronous and asynchronous, and both `FnMut` and `FnOnce` all
6//! by implementing a single entry point.
7//!
8//! Macros are provided for declaring the various flavors of handlers, [`hn!`], [`hn_once!`], [`async_hn!`], [`async_hn_once!`]
9//! for widget contexts and [`app_hn!`], [`app_hn_once!`], [`async_app_hn!`], [`async_app_hn_once!`] for the app context. These
10//! macros also build on top of the primitive macros [`clmv!`], [`async_clmv_fn!`] and [`async_clmv_fn_once!`] to
11//! provide a very easy way to *clone-move* captured variables into the handler.
12//!
13//! ```
14//! use zng::prelude::*;
15//! # let _scope = APP.defaults();
16//!
17//! let last_clicked = var(Txt::from(""));
18//! # let _ =
19//! Stack!(top_to_bottom, 5, ui_vec![
20//!     Button! {
21//!         child = Text!("hn!");
22//!         on_click = hn!(last_clicked, |_| {
23//!             last_clicked.set("hn!");
24//!         });
25//!     },
26//!     Button! {
27//!         child = Text!("hn_once!");
28//!         on_click = hn_once!(last_clicked, |_| {
29//!             last_clicked.set("hn_once!");
30//!         });
31//!     },
32//!     {
33//!         let enabled = var(true);
34//!         Button! {
35//!             child = Text!("async_hn!");
36//!             on_click = async_hn!(last_clicked, enabled, |_| {
37//!                 last_clicked.set("async_hn!");
38//!                 enabled.set(false);
39//!                 task::deadline(1.secs()).await;
40//!                 enabled.set(true);
41//!             });
42//!             widget::enabled;
43//!         }
44//!     },
45//!     Text!(last_clicked),
46//! ])
47//! # ;
48//! ```
49//!
50//! [`WIDGET`]: crate::widget::WIDGET
51//! [`clmv!`]: crate::clmv
52//! [`async_clmv_fn!`]: crate::async_clmv_fn
53//! [`async_clmv_fn_once!`]: crate::async_clmv_fn_once
54//!
55//! # Full API
56//!
57//! See [`zng_app::handler`] for the full handler API.
58
59pub use zng_app::handler::{
60    AppHandler, AppHandlerArgs, AppWeakHandle, FilterAppHandler, FilterWidgetHandler, WidgetHandler, app_hn, app_hn_once, async_app_hn,
61    async_app_hn_once, async_hn, async_hn_once, hn, hn_once,
62};