zng::handler

Macro hn

source
macro_rules! hn {
    ($($tt:tt)+) => { ... };
}
Expand description

Declare a mutable clone-move event handler.

The macro input is a closure with optional clone-move variables, internally it uses clmv! so the input is the same syntax.

ยงExamples

The example declares an event handler for the on_click property.

on_click = hn!(|_| {
    println!("Clicked!");
});

The closure input is &ClickArgs for this property. Note that if you want to use the event args you must annotate the input type, the context type is inferred.

on_click = hn!(|args: &ClickArgs| {
    println!("Clicked {}!", args.click_count);
});

Internally the clmv! macro is used so you can clone-move variables into the handler.

let foo = var(0);

// ..

on_click = hn!(foo, |args: &ClickArgs| {
    foo.set(args.click_count);
});

// can still use after:
let bar = foo.map(|c| formatx!("click_count: {c}"));

In the example above only a clone of foo is moved into the handler. Note that handlers always capture by move, if foo was not listed in the clone-move section it would not be available after the handler is created. See clmv! for details.