Skip to main content

zng_wgt/
node.rs

1//! Helper nodes.
2//!
3//! This module defines some foundational nodes that can be used for declaring properties and widgets.
4
5use std::{any::Any, sync::Arc};
6
7use crate::WidgetFn;
8use zng_app::{
9    event::{Command, CommandHandle, CommandScope, Event, EventArgs},
10    handler::{Handler, HandlerExt as _},
11    render::{FrameBuilder, FrameValueKey},
12    update::WidgetUpdates,
13    widget::{
14        VarLayout, WIDGET,
15        border::{BORDER, BORDER_ALIGN_VAR, BORDER_OVER_VAR},
16        info::Interactivity,
17        node::*,
18    },
19    window::WINDOW,
20};
21use zng_app_context::{ContextLocal, LocalContext};
22use zng_layout::{
23    context::LAYOUT,
24    unit::{PxConstraints2d, PxCornerRadius, PxPoint, PxRect, PxSideOffsets, PxSize, PxVector, SideOffsets},
25};
26use zng_state_map::{StateId, StateMapRef, StateValue};
27use zng_var::*;
28
29#[doc(hidden)]
30pub use pastey::paste;
31
32#[doc(hidden)]
33pub mod __macro_util {
34    pub use zng_app::{
35        event::CommandArgs,
36        handler::{Handler, hn},
37        widget::{
38            node::{IntoUiNode, UiNode},
39            property,
40        },
41    };
42    pub use zng_var::{IntoVar, context_var};
43}
44
45/// Helper for declaring properties that sets a context var.
46///
47/// The generated [`UiNode`] delegates each method to `child` inside a call to [`ContextVar::with_context`].
48///
49/// # Examples
50///
51/// A simple context property declaration:
52///
53/// ```
54/// # fn main() -> () { }
55/// # use zng_app::{*, widget::{node::*, *}};
56/// # use zng_var::*;
57/// # use zng_wgt::node::*;
58/// #
59/// context_var! {
60///     pub static FOO_VAR: u32 = 0u32;
61/// }
62///
63/// /// Sets the [`FOO_VAR`] in the widgets and its content.
64/// #[property(CONTEXT, default(FOO_VAR))]
65/// pub fn foo(child: impl IntoUiNode, value: impl IntoVar<u32>) -> UiNode {
66///     with_context_var(child, FOO_VAR, value)
67/// }
68/// ```
69///
70/// When set in a widget, the `value` is accessible in all inner nodes of the widget, using `FOO_VAR.get`, and if `value` is set to a
71/// variable the `FOO_VAR` will also reflect its [`is_new`] and [`read_only`]. If the `value` var is not read-only inner nodes
72/// can modify it using `FOO_VAR.set` or `FOO_VAR.modify`.
73///
74/// Also note that the property [`default`] is set to the same `FOO_VAR`, this causes the property to *pass-through* the outer context
75/// value, as if it was not set.
76///
77/// **Tip:** You can use a [`merge_var!`] to merge a new value to the previous context value:
78///
79/// ```
80/// # fn main() -> () { }
81/// # use zng_app::{*, widget::{node::*, *}};
82/// # use zng_var::*;
83/// # use zng_wgt::node::*;
84/// #
85/// #[derive(Debug, Clone, Default, PartialEq)]
86/// pub struct Config {
87///     pub foo: bool,
88///     pub bar: bool,
89/// }
90///
91/// context_var! {
92///     pub static CONFIG_VAR: Config = Config::default();
93/// }
94///
95/// /// Sets the *foo* config.
96/// #[property(CONTEXT, default(false))]
97/// pub fn foo(child: impl IntoUiNode, value: impl IntoVar<bool>) -> UiNode {
98///     with_context_var(
99///         child,
100///         CONFIG_VAR,
101///         merge_var!(CONFIG_VAR, value.into_var(), |c, &v| {
102///             let mut c = c.clone();
103///             c.foo = v;
104///             c
105///         }),
106///     )
107/// }
108///
109/// /// Sets the *bar* config.
110/// #[property(CONTEXT, default(false))]
111/// pub fn bar(child: impl IntoUiNode, value: impl IntoVar<bool>) -> UiNode {
112///     with_context_var(
113///         child,
114///         CONFIG_VAR,
115///         merge_var!(CONFIG_VAR, value.into_var(), |c, &v| {
116///             let mut c = c.clone();
117///             c.bar = v;
118///             c
119///         }),
120///     )
121/// }
122/// ```
123///
124/// When set in a widget, the [`merge_var!`] will read the context value of the parent properties, modify a clone of the value and
125/// the result will be accessible to the inner properties, the widget user can then set with the composed value in steps and
126/// the final consumer of the composed value only need to monitor to a single context variable.
127///
128/// [`is_new`]: zng_var::AnyVar::is_new
129/// [`read_only`]: zng_var::Var::read_only
130/// [`default`]: zng_app::widget::property#default
131/// [`merge_var!`]: zng_var::merge_var
132/// [`UiNode`]: zng_app::widget::node::UiNode
133/// [`ContextVar::with_context`]: zng_var::ContextVar::with_context
134pub fn with_context_var<T: VarValue>(child: impl IntoUiNode, context_var: ContextVar<T>, value: impl IntoVar<T>) -> UiNode {
135    let value = value.into_var();
136    let mut actual_value = None;
137    let mut id = None;
138
139    match_node(child, move |child, op| {
140        let mut is_deinit = false;
141        match &op {
142            UiNodeOp::Init => {
143                id = Some(ContextInitHandle::new());
144                actual_value = Some(Arc::new(value.current_context().into()));
145            }
146            UiNodeOp::Deinit => {
147                is_deinit = true;
148            }
149            _ => {}
150        }
151
152        context_var.with_context(id.clone().expect("node not inited"), &mut actual_value, || child.op(op));
153
154        if is_deinit {
155            id = None;
156            actual_value = None;
157        }
158    })
159}
160
161/// Helper for declaring properties that sets a context var to a value generated on init.
162///
163/// The method calls the `init_value` closure on init to produce a *value* var that is presented as the [`ContextVar<T>`]
164/// in the widget and widget descendants. The closure can be called more than once if the returned node is reinited.
165///
166/// Apart from the value initialization this behaves just like [`with_context_var`].
167///
168/// [`ContextVar<T>`]: zng_var::ContextVar
169pub fn with_context_var_init<T: VarValue>(
170    child: impl IntoUiNode,
171    var: ContextVar<T>,
172    mut init_value: impl FnMut() -> Var<T> + Send + 'static,
173) -> UiNode {
174    let mut id = None;
175    let mut value = None;
176    match_node(child, move |child, op| {
177        let mut is_deinit = false;
178        match &op {
179            UiNodeOp::Init => {
180                id = Some(ContextInitHandle::new());
181                value = Some(Arc::new(init_value().current_context().into()));
182            }
183            UiNodeOp::Deinit => {
184                is_deinit = true;
185            }
186            _ => {}
187        }
188
189        var.with_context(id.clone().expect("node not inited"), &mut value, || child.op(op));
190
191        if is_deinit {
192            id = None;
193            value = None;
194        }
195    })
196}
197
198/// Helper for declaring event properties.
199pub struct EventNodeBuilder<A: EventArgs, F, M> {
200    event: Event<A>,
201    filter_builder: F,
202    map_args: M,
203}
204/// Helper for declaring event properties from variables.
205pub struct VarEventNodeBuilder<I, F, M> {
206    init_var: I,
207    filter_builder: F,
208    map_args: M,
209}
210
211impl<A: EventArgs> EventNodeBuilder<A, (), ()> {
212    /// Node that calls the handler for all args that target the widget and has not stopped propagation.
213    pub fn new(event: Event<A>) -> EventNodeBuilder<A, (), ()> {
214        EventNodeBuilder {
215            event,
216            filter_builder: (),
217            map_args: (),
218        }
219    }
220}
221impl<I, T> VarEventNodeBuilder<I, (), ()>
222where
223    T: VarValue,
224    I: FnMut() -> Var<T> + Send + 'static,
225{
226    /// Node that calls the handler for var updates.
227    ///
228    /// The `init_var` is called on init to
229    pub fn new(init_var: I) -> VarEventNodeBuilder<I, (), ()> {
230        VarEventNodeBuilder {
231            init_var,
232            filter_builder: (),
233            map_args: (),
234        }
235    }
236}
237
238impl<A: EventArgs, M> EventNodeBuilder<A, (), M> {
239    /// Filter event.
240    ///
241    /// The `filter_builder` is called on init and on event, it must produce another closure, the filter predicate. The `filter_builder`
242    /// runs in the widget context, the filter predicate does not always.
243    ///
244    /// In the event hook the filter predicate runs in the app context, it is called if the args target the widget, the predicate must
245    /// use any captured contextual info to filter the args, this is an optimization, it can save a visit to the widget node.
246    ///
247    /// If the event is received the second filter predicate is called again to confirm the event.
248    /// The second instance is called if [`propagation`] was not stopped, if it returns `true` the `handler` closure is called.
249    ///
250    /// Note that events that represent an *interaction* with the widget are send for both [`ENABLED`] and [`DISABLED`] targets,
251    /// event properties should probably distinguish if they fire on normal interactions vs on *disabled* interactions.
252    ///
253    /// [`propagation`]: zng_app::event::AnyEventArgs::propagation
254    /// [`ENABLED`]: Interactivity::ENABLED
255    /// [`DISABLED`]: Interactivity::DISABLED
256    pub fn filter<FB, F>(self, filter_builder: FB) -> EventNodeBuilder<A, FB, M>
257    where
258        FB: FnMut() -> F + Send + 'static,
259        F: Fn(&A) -> bool + Send + Sync + 'static,
260    {
261        EventNodeBuilder {
262            event: self.event,
263            filter_builder,
264            map_args: self.map_args,
265        }
266    }
267}
268impl<T, I, M> VarEventNodeBuilder<I, (), M>
269where
270    T: VarValue,
271    I: FnMut() -> Var<T> + Send + 'static,
272{
273    /// Filter event.
274    ///
275    /// The `filter_builder` is called on init and on new, it must produce another closure, the filter predicate. The `filter_builder`
276    /// runs in the widget context, the filter predicate does not always.
277    ///
278    /// In the variable hook the filter predicate runs in the app context, it is called if the args target the widget, the predicate must
279    /// use any captured contextual info to filter the args, this is an optimization, it can save a visit to the widget node.
280    ///
281    /// If the update is received the second filter predicate is called again to confirm the update.
282    /// If it returns `true` the `handler` closure is called.
283    pub fn filter<FB, F>(self, filter_builder: FB) -> VarEventNodeBuilder<I, FB, M>
284    where
285        FB: FnMut() -> F + Send + 'static,
286        F: Fn(&T) -> bool + Send + Sync + 'static,
287    {
288        VarEventNodeBuilder {
289            init_var: self.init_var,
290            filter_builder,
291            map_args: self.map_args,
292        }
293    }
294}
295
296impl<A: EventArgs, F> EventNodeBuilder<A, F, ()> {
297    /// Convert args.
298    ///
299    /// The `map_args` closure is called in context, just before the handler is called.
300    pub fn map_args<M, MA>(self, map_args: M) -> EventNodeBuilder<A, F, M>
301    where
302        M: FnMut(&A) -> MA + Send + 'static,
303        MA: Clone + 'static,
304    {
305        EventNodeBuilder {
306            event: self.event,
307            filter_builder: self.filter_builder,
308            map_args,
309        }
310    }
311}
312impl<T, I, F> VarEventNodeBuilder<I, F, ()>
313where
314    T: VarValue,
315    I: FnMut() -> Var<T> + Send + 'static,
316{
317    /// Convert args.
318    ///
319    /// The `map_args` closure is called in context, just before the handler is called.
320    ///
321    /// Note that if the args is a full [`EventArgs`] type it must share the same propagation handle in the preview and normal route
322    /// properties, if the source type is also a full args just clone the propagation handle, otherwise you must use [`WIDGET::set_state`]
323    /// to communicate between the properties.
324    pub fn map_args<M, MA>(self, map_args: M) -> VarEventNodeBuilder<I, F, M>
325    where
326        M: FnMut(&T) -> MA + Send + 'static,
327        MA: Clone + 'static,
328    {
329        VarEventNodeBuilder {
330            init_var: self.init_var,
331            filter_builder: self.filter_builder,
332            map_args,
333        }
334    }
335}
336
337/// Build with filter and args mapping.
338impl<A, F, FB, MA, M> EventNodeBuilder<A, FB, M>
339where
340    A: EventArgs,
341    F: Fn(&A) -> bool + Send + Sync + 'static,
342    FB: FnMut() -> F + Send + 'static,
343    MA: Clone + 'static,
344    M: FnMut(&A) -> MA + Send + 'static,
345{
346    /// Build node.
347    ///
348    /// If `PRE` is `true` the handler is called before the children, *preview* route.
349    pub fn build<const PRE: bool>(self, child: impl IntoUiNode, handler: Handler<MA>) -> UiNode {
350        let Self {
351            event,
352            mut filter_builder,
353            mut map_args,
354        } = self;
355        let mut handler = handler.into_wgt_runner();
356        match_node(child, move |child, op| match op {
357            UiNodeOp::Init => {
358                WIDGET.sub_event_when(&event, filter_builder());
359            }
360            UiNodeOp::Deinit => {
361                handler.deinit();
362            }
363            UiNodeOp::Update { updates } => {
364                if !PRE {
365                    child.update(updates);
366                }
367
368                handler.update();
369
370                let mut f = None;
371                event.each_update(false, |args| {
372                    if f.get_or_insert_with(&mut filter_builder)(args) {
373                        handler.event(&map_args(args));
374                    }
375                });
376            }
377            _ => {}
378        })
379    }
380}
381
382/// Build with filter and args mapping.
383impl<T, I, F, FB, MA, M> VarEventNodeBuilder<I, FB, M>
384where
385    T: VarValue,
386    I: FnMut() -> Var<T> + Send + 'static,
387    F: Fn(&T) -> bool + Send + Sync + 'static,
388    FB: FnMut() -> F + Send + 'static,
389    MA: Clone + 'static,
390    M: FnMut(&T) -> MA + Send + 'static,
391{
392    /// Build node.
393    ///
394    /// If `PRE` is `true` the handler is called before the children, *preview* route.
395    pub fn build<const PRE: bool>(self, child: impl IntoUiNode, handler: Handler<MA>) -> UiNode {
396        let Self {
397            mut init_var,
398            mut filter_builder,
399            mut map_args,
400        } = self;
401        let mut handler = handler.into_wgt_runner();
402        let mut var = None;
403        match_node(child, move |child, op| match op {
404            UiNodeOp::Init => {
405                let v = init_var();
406                let f = filter_builder();
407                WIDGET.sub_var_when(&v, move |a| f(a.value()));
408                var = Some(v);
409            }
410            UiNodeOp::Deinit => {
411                handler.deinit();
412                var = None;
413            }
414            UiNodeOp::Update { updates } => {
415                if PRE {
416                    child.update(updates);
417                }
418
419                handler.update();
420
421                var.as_ref().unwrap().with_new(|t| {
422                    if filter_builder()(t) {
423                        handler.event(&map_args(t));
424                    }
425                });
426            }
427            _ => {}
428        })
429    }
430}
431
432/// Build with filter and without args mapping.
433impl<A, F, FB> EventNodeBuilder<A, FB, ()>
434where
435    A: EventArgs,
436    F: Fn(&A) -> bool + Send + Sync + 'static,
437    FB: FnMut() -> F + Send + 'static,
438{
439    /// Build node.
440    ///
441    /// If `PRE` is `true` the handler is called before the children, *preview* route.
442    pub fn build<const PRE: bool>(self, child: impl IntoUiNode, handler: Handler<A>) -> UiNode {
443        let Self {
444            event, mut filter_builder, ..
445        } = self;
446        let mut handler = handler.into_wgt_runner();
447        match_node(child, move |child, op| match op {
448            UiNodeOp::Init => {
449                WIDGET.sub_event_when(&event, filter_builder());
450            }
451            UiNodeOp::Deinit => {
452                handler.deinit();
453            }
454            UiNodeOp::Update { updates } => {
455                if !PRE {
456                    child.update(updates);
457                }
458
459                handler.update();
460
461                let mut f = None;
462                event.each_update(false, |args| {
463                    if f.get_or_insert_with(&mut filter_builder)(args) {
464                        handler.event(args);
465                    }
466                });
467            }
468            _ => {}
469        })
470    }
471}
472/// Build with filter and without args mapping.
473impl<T, I, F, FB> VarEventNodeBuilder<I, FB, ()>
474where
475    T: VarValue,
476    I: FnMut() -> Var<T> + Send + 'static,
477    F: Fn(&T) -> bool + Send + Sync + 'static,
478    FB: FnMut() -> F + Send + 'static,
479{
480    /// Build node.
481    ///
482    /// If `PRE` is `true` the handler is called before the children, *preview* route.
483    pub fn build<const PRE: bool>(self, child: impl IntoUiNode, handler: Handler<T>) -> UiNode {
484        let Self {
485            mut init_var,
486            mut filter_builder,
487            ..
488        } = self;
489        let mut handler = handler.into_wgt_runner();
490        let mut var = None;
491        match_node(child, move |child, op| match op {
492            UiNodeOp::Init => {
493                let v = init_var();
494                let f = filter_builder();
495                WIDGET.sub_var_when(&v, move |a| f(a.value()));
496                var = Some(v);
497            }
498            UiNodeOp::Deinit => {
499                handler.deinit();
500                var = None;
501            }
502            UiNodeOp::Update { updates } => {
503                if !PRE {
504                    child.update(updates);
505                }
506
507                handler.update();
508
509                var.as_ref().unwrap().with_new(|t| {
510                    if filter_builder()(t) {
511                        handler.event(t);
512                    }
513                });
514            }
515            _ => {}
516        })
517    }
518}
519
520/// Build without filter and without args mapping.
521impl<A> EventNodeBuilder<A, (), ()>
522where
523    A: EventArgs,
524{
525    /// Build node.
526    ///
527    /// If `PRE` is `true` the handler is called before the children, *preview* route.
528    pub fn build<const PRE: bool>(self, child: impl IntoUiNode, handler: Handler<A>) -> UiNode {
529        let Self { event, .. } = self;
530        let mut handler = handler.into_wgt_runner();
531        match_node(child, move |child, op| match op {
532            UiNodeOp::Init => {
533                WIDGET.sub_event(&event);
534            }
535            UiNodeOp::Deinit => {
536                handler.deinit();
537            }
538            UiNodeOp::Update { updates } => {
539                if !PRE {
540                    child.update(updates);
541                }
542
543                handler.update();
544
545                event.each_update(false, |args| {
546                    handler.event(args);
547                });
548            }
549            _ => {}
550        })
551    }
552}
553/// Build without filter and without args mapping.
554impl<T, I> VarEventNodeBuilder<I, (), ()>
555where
556    T: VarValue,
557    I: FnMut() -> Var<T> + Send + 'static,
558{
559    /// Build node.
560    ///
561    /// If `PRE` is `true` the handler is called before the children, *preview* route.
562    pub fn build<const PRE: bool>(self, child: impl IntoUiNode, handler: Handler<T>) -> UiNode {
563        let Self { mut init_var, .. } = self;
564        let mut handler = handler.into_wgt_runner();
565        let mut var = None;
566        match_node(child, move |child, op| match op {
567            UiNodeOp::Init => {
568                let v = init_var();
569                WIDGET.sub_var(&v);
570                var = Some(v);
571            }
572            UiNodeOp::Deinit => {
573                handler.deinit();
574                var = None;
575            }
576            UiNodeOp::Update { updates } => {
577                if !PRE {
578                    child.update(updates);
579                }
580
581                handler.update();
582
583                var.as_ref().unwrap().with_new(|t| {
584                    handler.event(t);
585                });
586            }
587            _ => {}
588        })
589    }
590}
591
592/// Build with no filter and args mapping.
593impl<A, MA, M> EventNodeBuilder<A, (), M>
594where
595    A: EventArgs,
596    MA: Clone + 'static,
597    M: FnMut(&A) -> MA + Send + 'static,
598{
599    /// Build node.
600    ///
601    /// If `PRE` is `true` the handler is called before the children, *preview* route.
602    pub fn build<const PRE: bool>(self, child: impl IntoUiNode, handler: Handler<MA>) -> UiNode {
603        self.filter(|| |_| true).build::<PRE>(child, handler)
604    }
605}
606/// Build with no filter and args mapping.
607impl<T, I, MA, M> VarEventNodeBuilder<I, (), M>
608where
609    T: VarValue,
610    I: FnMut() -> Var<T> + Send + 'static,
611    MA: Clone + 'static,
612    M: FnMut(&T) -> MA + Send + 'static,
613{
614    /// Build node.
615    ///
616    /// If `PRE` is `true` the handler is called before the children, *preview* route.
617    pub fn build<const PRE: bool>(self, child: impl IntoUiNode, handler: Handler<MA>) -> UiNode {
618        self.filter(|| |_| true).build::<PRE>(child, handler)
619    }
620}
621
622///<span data-del-macro-root></span> Declare event properties.
623///
624/// Each declaration can expand to an `on_event` and optionally an `on_pre_event`. The body can be declared using [`EventNodeBuilder`] or
625/// [`VarEventNodeBuilder`].
626///
627/// # Examples
628///
629/// ```
630/// # fn main() { }
631/// # use zng_app::{event::*, widget::{node::*, *}, handler::*};
632/// # use zng_wgt::node::*;
633/// # #[derive(Clone, Debug, PartialEq)] pub enum KeyState { Pressed }
634/// # event_args! { pub struct KeyInputArgs { pub state: KeyState, .. fn is_in_target(&self, _id: WidgetId) -> bool { true } } }
635/// # event! { pub static KEY_INPUT_EVENT: KeyInputArgs; }
636/// # struct CONTEXT;
637/// # impl CONTEXT { pub fn state(&self) -> zng_var::Var<bool> { zng_var::var(true) } }
638/// event_property! {
639///     /// Docs copied for `on_key_input` and `on_pre_key_input`.
640///     ///
641///     /// The macro also generates docs linking between the two properties.
642///     #[property(EVENT)]
643///     pub fn on_key_input<on_pre_key_input>(child: impl IntoUiNode, handler: Handler<KeyInputArgs>) -> UiNode {
644///         // Preview flag, only if the signature contains the `<on_pre...>` part,
645///         // the macro matches `const $IDENT: bool;` and expands to `const IDENT: bool = true/false;`.
646///         const PRE: bool;
647///
648///         // rest of the body can be anything the builds a node.
649///         EventNodeBuilder::new(KEY_INPUT_EVENT).build::<PRE>(child, handler)
650///     }
651///
652///     /// Another property.
653///     #[property(EVENT)]
654///     pub fn on_key_down<on_pre_key_down>(child: impl IntoUiNode, handler: Handler<KeyInputArgs>) -> UiNode {
655///         const PRE: bool;
656///         EventNodeBuilder::new(KEY_INPUT_EVENT)
657///             .filter(|| |a| a.state == KeyState::Pressed)
658///             .build::<PRE>(child, handler)
659///     }
660///
661///     /// Another, this time derived from a var source, and without the optional preview property.
662///     #[property(EVENT)]
663///     pub fn on_state(child: impl IntoUiNode, handler: Handler<bool>) -> UiNode {
664///         VarEventNodeBuilder::new(|| CONTEXT.state())
665///             .map_args(|b| !*b)
666///             .build::<false>(child, handler)
667///     }
668/// }
669/// ```
670///
671/// The example above generates five event properties.
672///
673/// # Route
674///
675/// Note that is an event property has an `on_pre_*` pair it is expected to be representing a fully routing event, with args that
676/// implement [`EventArgs`]. If the property does not have a preview pair it is expected to be a *direct* event. This is the event
677/// property pattern and is explained in the generated documentation, don't declare a non-standard pair using this macro.
678///
679/// # Commands
680///
681/// You can use [`command_property`] to declare command event properties, it also generates enabled control properties.
682#[macro_export]
683macro_rules! event_property {
684    ($(
685        $(#[$meta:meta])+
686        $vis:vis fn $on_ident:ident $(< $on_pre_ident:ident $(,)?>)? (
687            $child:ident: impl $IntoUiNode:path,
688            $handler:ident: $Handler:ty $(,)?
689        ) -> $UiNode:path {
690            $($body:tt)+
691        }
692    )+) => {$(
693       $crate::event_property_impl! {
694            $(#[$meta])+
695            $vis fn $on_ident $(< $on_pre_ident >)? ($child: impl $IntoUiNode, $handler: $Handler) -> $UiNode {
696                $($body)+
697            }
698       }
699    )+};
700}
701#[doc(inline)]
702pub use event_property;
703
704#[doc(hidden)]
705#[macro_export]
706macro_rules! event_property_impl {
707    (
708        $(#[$meta:meta])+
709        $vis:vis fn $on_ident:ident < $on_pre_ident:ident > ($child:ident : impl $IntoUiNode:path, $handler:ident : $Handler:ty) -> $UiNode:path {
710            const $PRE:ident : bool;
711            $($body:tt)+
712        }
713    ) => {
714        $(#[$meta])+
715        ///
716        /// # Route
717        ///
718        /// This event property uses the normal route, that is, the `handler` is called after the children widget handlers and after the
719        #[doc = concat!("[`", stringify!($pn_pre_ident), "`](fn@", stringify!($pn_pre_ident), ")")]
720        /// handlers.
721        $vis fn $on_ident($child: impl $IntoUiNode, $handler: $Handler) -> $UiNode {
722            const $PRE: bool = false;
723            $($body)+
724        }
725
726        $(#[$meta])+
727        ///
728        /// # Route
729        ///
730        /// This event property uses the preview route, that is, the `handler` is called before the children widget handlers and before the
731        #[doc = concat!("[`", stringify!($pn_ident), "`](fn@", stringify!($pn_ident), ")")]
732        /// handlers.
733        $vis fn $on_pre_ident($child: impl $IntoUiNode, $handler: $Handler) -> $UiNode {
734            const $PRE: bool = true;
735            $($body)+
736        }
737    };
738
739    (
740        $(#[$meta:meta])+
741        $vis:vis fn $on_ident:ident ($child:ident : impl $IntoUiNode:path, $handler:ident : $Handler:path) -> $UiNode:path {
742            $($body:tt)+
743        }
744    ) => {
745        $(#[$meta])+
746        ///
747        /// # Route
748        ///
749        /// This event property uses a *direct* route, that is, it cannot be intercepted in parent widgets.
750        $vis fn $on_ident($child: impl $IntoUiNode, $handler: $Handler) -> $UiNode {
751            $($body)+
752        }
753    };
754}
755
756///<span data-del-macro-root></span> Declare command event properties.
757///
758/// Each declaration can expand to an `on_cmd`, `on_pre_cmd` and  and optionally an `can_cmd` and `CAN_CMD_VAR`.
759///
760/// # Examples
761///
762/// ```
763/// # fn main() { }
764/// # use zng_app::{event::*, widget::{*, node::*}, handler::*};
765/// # use zng_app::var::*;
766/// # use zng_wgt::node::*;
767/// # command! {
768/// # pub static COPY_CMD;
769/// # pub static PASTE_CMD;
770/// # }
771/// command_property! {
772///     /// Property docs.
773///     #[property(EVENT)]
774///     pub fn on_paste<on_pre_paste>(child: impl IntoUiNode, handler: Handler<CommandArgs>) -> UiNode {
775///         PASTE_CMD
776///     }
777///
778///     /// Another property, with optional `can_*` contextual property.
779///     #[property(EVENT)]
780///     pub fn on_copy<on_pre_copy, can_copy>(child: impl IntoUiNode, handler: Handler<CommandArgs>) -> UiNode {
781///         COPY_CMD
782///     }
783/// }
784/// ```
785///
786/// The example above declares five properties and a context var. Note that unlike [`event_property!`] the body only defines the command,
787/// a standard node is generated.
788///
789/// # Enabled
790///
791/// An optional contextual property (`can_*`) and context var (`CAN_*_VAR`) can be generated. When defined the command handle enabled status
792/// is controlled by the contextual property. When not defined the command handle is always enabled.
793#[macro_export]
794macro_rules! command_property {
795    ($(
796        $(#[$meta:meta])+
797        $vis:vis fn $on_ident:ident $(< $on_pre_ident:ident $(, $can_ident:ident)? $(,)?>)? (
798            $child:ident: impl $IntoUiNode:path,
799            $handler:ident: $Handler:ty $(,)?
800        ) -> $UiNode:path {
801            $COMMAND:path
802        }
803    )+) => {$(
804       $crate::command_property_impl! {
805            $(#[$meta])+
806            $vis fn $on_ident$(<$on_pre_ident $(, $can_ident)?>)?($child: impl $IntoUiNode, $handler: $Handler) -> $UiNode {
807                $COMMAND
808            }
809       }
810    )+};
811}
812#[doc(inline)]
813pub use command_property;
814#[doc(hidden)]
815#[macro_export]
816macro_rules! command_property_impl {
817    (
818        $(#[$meta:meta])+
819        $vis:vis fn $on_ident:ident < $on_pre_ident:ident, $can_ident:ident> (
820            $child:ident: impl $IntoUiNode:path,
821            $handler:ident: $Handler:ty
822        ) -> $UiNode:path {
823            $COMMAND:path
824        }
825    ) => {
826        $crate::node::paste! {
827            $crate::node::__macro_util::context_var! {
828                /// Defines if
829                #[doc = concat!("[`", stringify!($on_ident), "`](fn@", stringify!($on_ident), ")")]
830                /// and
831                #[doc = concat!("[`", stringify!($on_pre_ident), "`](fn@", stringify!($on_pre_ident), ")")]
832                /// command handlers are enabled in a widget and descendants.
833                ///
834                /// Use
835                #[doc = concat!("[`", stringify!($can_ident), "`](fn@", stringify!($can_ident), ")")]
836                /// to set. Is enabled by default.
837                $vis static [<$can_ident:upper _VAR>]: bool = true;
838            }
839
840            /// Defines if
841            #[doc = concat!("[`", stringify!($on_ident), "`](fn@", stringify!($on_ident), ")")]
842            /// and
843            #[doc = concat!("[`", stringify!($on_pre_ident), "`](fn@", stringify!($on_pre_ident), ")")]
844            /// command handlers are enabled in the widget and descendants.
845            ///
846            #[doc = "Sets the [`"$can_ident:upper "_VAR`]."]
847            #[$crate::node::__macro_util::property(CONTEXT, default([<$can_ident:upper _VAR>]))]
848            $vis fn $can_ident(
849                child: impl $crate::node::__macro_util::IntoUiNode,
850                enabled: impl $crate::node::__macro_util::IntoVar<bool>,
851            ) -> $crate::node::__macro_util::UiNode {
852                $crate::node::with_context_var(child, self::[<$can_ident:upper _VAR>], enabled)
853            }
854
855            $crate::event_property! {
856                $(#[$meta])+
857                ///
858                /// # Command
859                ///
860                /// This property will subscribe to the
861                #[doc = concat!("[`", stringify!($COMMAND), "`]")]
862                /// command scoped on the widget. If set on the `Window!` root widget it will also subscribe to
863                /// the command scoped on the window.
864                ///
865                /// The command handle is enabled by default and can be disabled using the contextual property
866                #[doc = concat!("[`", stringify!($can_ident), "`](fn@", stringify!($can_ident), ")")]
867                /// .
868                $vis fn $on_ident<$on_pre_ident>($child: impl $IntoUiNode, $handler: $Handler) -> $UiNode {
869                    const PRE: bool;
870                    let child = $crate::node::EventNodeBuilder::new(*$COMMAND)
871                        .filter(|| {
872                            let enabled = self::[<$can_ident:upper _VAR>].current_context();
873                            move |_| enabled.get()
874                        })
875                        .build::<PRE>($child, $handler);
876                    $crate::node::command_contextual_enabled(child, $COMMAND, [<$can_ident:upper _VAR>])
877                }
878            }
879        }
880    };
881    (
882        $(#[$meta:meta])+
883        $vis:vis fn $on_ident:ident< $on_pre_ident:ident> (
884            $child:ident: impl $IntoUiNode:path,
885            $handler:ident: $Handler:ty
886        ) -> $UiNode:path {
887            $COMMAND:path
888        }
889    ) => {
890        $crate::event_property! {
891            $(#[$meta])+
892            ///
893            /// # Command
894            ///
895            /// This property will subscribe to the
896            #[doc = concat!("[`", stringify!($COMMAND), "`]")]
897            /// command scoped on the widget. If set on the `Window!` root widget it will also subscribe to
898            /// the command scoped on the window.
899            ///
900            /// The command handle is always enabled.
901            $vis fn $on_ident<$on_pre_ident>($child: impl $IntoUiNode, $handler: $Handler) -> $UiNode {
902                const PRE: bool;
903                let child = $crate::node::EventNodeBuilder::new(*$COMMAND).build::<PRE>($child, $handler);
904                $crate::node::command_always_enabled(child, $COMMAND)
905            }
906        }
907    };
908    (
909        $(#[$meta:meta])+
910        $vis:vis fn $on_ident:ident (
911            $child:ident: impl $IntoUiNode:path,
912            $handler:ident: $Handler:ty
913        ) -> $UiNode:path {
914            $COMMAND:path
915        }
916    ) => {
917        $crate::event_property! {
918            $(#[$meta])+
919            ///
920            /// # Command
921            ///
922            /// This property will subscribe to the
923            #[doc = concat!("[`", stringify!($COMMAND), "`]")]
924            /// command scoped on the widget. If set on the `Window!` root widget it will also subscribe to
925            /// the command scoped on the window.
926            ///
927            /// The command handle is always enabled.
928            $vis fn $on_ident($child: impl $IntoUiNode, $handler: $Handler) -> $UiNode {
929                let child = $crate::node::EventNodeBuilder::new(*$COMMAND).build::<false>($child, $handler);
930                $crate::node::command_always_enabled(child, $COMMAND)
931            }
932        }
933    };
934}
935
936fn validate_cmd(cmd: Command) {
937    if !matches!(cmd.scope(), CommandScope::App) {
938        tracing::error!("command for command property cannot be scoped, {cmd:?} scope will be ignored");
939    }
940}
941
942#[doc(hidden)]
943pub fn command_always_enabled(child: UiNode, cmd: Command) -> UiNode {
944    let mut _wgt_handle = CommandHandle::dummy();
945    let mut _win_handle = CommandHandle::dummy();
946    match_node(child, move |_, op| match op {
947        UiNodeOp::Init => {
948            validate_cmd(cmd);
949            _wgt_handle = cmd.scoped(WIDGET.id()).subscribe(true);
950            if WIDGET.parent_id().is_none() {
951                _win_handle = cmd.scoped(WINDOW.id()).subscribe(true);
952            }
953        }
954        UiNodeOp::Deinit => {
955            _wgt_handle = CommandHandle::dummy();
956            _win_handle = CommandHandle::dummy();
957        }
958        _ => {}
959    })
960}
961
962#[doc(hidden)]
963pub fn command_contextual_enabled(child: UiNode, cmd: Command, ctx: ContextVar<bool>) -> UiNode {
964    let mut _handle = VarHandle::dummy();
965    let mut _wgt_handle = CommandHandle::dummy();
966    let mut _win_handle = CommandHandle::dummy();
967    match_node(child, move |_, op| match op {
968        UiNodeOp::Init => {
969            let ctx = ctx.current_context();
970            let handle = cmd.scoped(WIDGET.id()).subscribe(ctx.get());
971            let win_handle = if WIDGET.parent_id().is_none() {
972                cmd.scoped(WINDOW.id()).subscribe(ctx.get())
973            } else {
974                CommandHandle::dummy()
975            };
976            if !ctx.capabilities().is_const() {
977                let handle = handle.enabled().clone();
978                let win_handle = win_handle.enabled().clone();
979                _handle = ctx.hook(move |a| {
980                    handle.set(*a.value());
981                    win_handle.set(*a.value());
982                    true
983                });
984            }
985            _wgt_handle = handle;
986            _win_handle = win_handle;
987        }
988        UiNodeOp::Deinit => {
989            _handle = VarHandle::dummy();
990            _wgt_handle = CommandHandle::dummy();
991            _win_handle = CommandHandle::dummy();
992        }
993        _ => {}
994    })
995}
996
997/// Logs an error if the `_var` is always read-only.
998pub fn validate_getter_var<T: VarValue>(_var: &Var<T>) {
999    #[cfg(debug_assertions)]
1000    if _var.capabilities().is_always_read_only() {
1001        tracing::error!(
1002            "`is_`, `has_` or `get_` property inited with read-only var in `{}`",
1003            WIDGET.trace_id()
1004        );
1005    }
1006}
1007
1008/// Helper for declaring state properties that are controlled by a variable.
1009///
1010/// On init the `state` variable is set to `source` and bound to it, you can use this to create state properties
1011/// that map from a context variable or to create composite properties that merge other state properties.
1012pub fn bind_state<T: VarValue>(child: impl IntoUiNode, source: impl IntoVar<T>, state: impl IntoVar<T>) -> UiNode {
1013    let source = source.into_var();
1014    bind_state_init(child, state, move |state| {
1015        state.set_from(&source);
1016        source.bind(state)
1017    })
1018}
1019
1020/// Helper for declaring state properties that are controlled by a variable.
1021///
1022/// On init the `bind` closure is called with the `state` variable, it must set and bind it.
1023pub fn bind_state_init<T>(
1024    child: impl IntoUiNode,
1025    state: impl IntoVar<T>,
1026    mut bind: impl FnMut(&Var<T>) -> VarHandle + Send + 'static,
1027) -> UiNode
1028where
1029    T: VarValue,
1030{
1031    let state = state.into_var();
1032    let mut _binding = VarHandle::dummy();
1033
1034    match_node(child, move |_, op| match op {
1035        UiNodeOp::Init => {
1036            validate_getter_var(&state);
1037            _binding = bind(&state);
1038        }
1039        UiNodeOp::Deinit => {
1040            _binding = VarHandle::dummy();
1041        }
1042        _ => {}
1043    })
1044}
1045
1046/// Helper for declaring state properties that are controlled by values in the widget state map.
1047///
1048/// The `predicate` closure is called with the widget state on init and every update, if the returned value changes the `state`
1049/// updates. The `deinit` closure is called on deinit to get the *reset* value.
1050pub fn widget_state_is_state(
1051    child: impl IntoUiNode,
1052    predicate: impl Fn(StateMapRef<WIDGET>) -> bool + Send + 'static,
1053    deinit: impl Fn(StateMapRef<WIDGET>) -> bool + Send + 'static,
1054    state: impl IntoVar<bool>,
1055) -> UiNode {
1056    let state = state.into_var();
1057
1058    match_node(child, move |child, op| match op {
1059        UiNodeOp::Init => {
1060            validate_getter_var(&state);
1061            child.init();
1062            let s = WIDGET.with_state(&predicate);
1063            if s != state.get() {
1064                state.set(s);
1065            }
1066        }
1067        UiNodeOp::Deinit => {
1068            child.deinit();
1069            let s = WIDGET.with_state(&deinit);
1070            if s != state.get() {
1071                state.set(s);
1072            }
1073        }
1074        UiNodeOp::Update { updates } => {
1075            child.update(updates);
1076            let s = WIDGET.with_state(&predicate);
1077            if s != state.get() {
1078                state.set(s);
1079            }
1080        }
1081        _ => {}
1082    })
1083}
1084
1085/// Helper for declaring state getter properties that are controlled by values in the widget state map.
1086///
1087/// The `get_new` closure is called with the widget state and current `state` every init and update, if it returns some value
1088/// the `state` updates. The `get_deinit` closure is called on deinit to get the *reset* value.
1089pub fn widget_state_get_state<T: VarValue>(
1090    child: impl IntoUiNode,
1091    get_new: impl Fn(StateMapRef<WIDGET>, &T) -> Option<T> + Send + 'static,
1092    get_deinit: impl Fn(StateMapRef<WIDGET>, &T) -> Option<T> + Send + 'static,
1093    state: impl IntoVar<T>,
1094) -> UiNode {
1095    let state = state.into_var();
1096    match_node(child, move |child, op| match op {
1097        UiNodeOp::Init => {
1098            validate_getter_var(&state);
1099            child.init();
1100            let new = state.with(|s| WIDGET.with_state(|w| get_new(w, s)));
1101            if let Some(new) = new {
1102                state.set(new);
1103            }
1104        }
1105        UiNodeOp::Deinit => {
1106            child.deinit();
1107
1108            let new = state.with(|s| WIDGET.with_state(|w| get_deinit(w, s)));
1109            if let Some(new) = new {
1110                state.set(new);
1111            }
1112        }
1113        UiNodeOp::Update { updates } => {
1114            child.update(updates);
1115            let new = state.with(|s| WIDGET.with_state(|w| get_new(w, s)));
1116            if let Some(new) = new {
1117                state.set(new);
1118            }
1119        }
1120        _ => {}
1121    })
1122}
1123
1124/// Transforms and clips the `content` node according with the default widget border align behavior.
1125///
1126/// Properties that *fill* the widget can wrap their fill content in this node to automatically implement
1127/// the expected interaction with the widget borders, the content will be positioned, sized and clipped according to the
1128/// widget borders, corner radius and border align.
1129pub fn fill_node(content: impl IntoUiNode) -> UiNode {
1130    let mut clip_bounds = PxSize::zero();
1131    let mut clip_corners = PxCornerRadius::zero();
1132
1133    let mut offset = PxVector::zero();
1134    let offset_key = FrameValueKey::new_unique();
1135    let mut define_frame = false;
1136
1137    match_node(content, move |child, op| match op {
1138        UiNodeOp::Init => {
1139            WIDGET.sub_var_layout(&BORDER_ALIGN_VAR);
1140            define_frame = false;
1141            offset = PxVector::zero();
1142        }
1143        UiNodeOp::Measure { desired_size, .. } => {
1144            let offsets = BORDER.inner_offsets();
1145            let align = BORDER_ALIGN_VAR.get();
1146
1147            let our_offsets = offsets * align;
1148            let size_offset = offsets - our_offsets;
1149
1150            let size_increase = PxSize::new(size_offset.horizontal(), size_offset.vertical());
1151
1152            *desired_size = LAYOUT.constraints().fill_size() + size_increase;
1153        }
1154        UiNodeOp::Layout { wl, final_size } => {
1155            // We are inside the *inner* bounds AND inside border_nodes:
1156            //
1157            // .. ( layout ( new_border/inner ( border_nodes ( FILL_NODES ( new_child_context ( new_child_layout ( ..
1158
1159            let (bounds, corners) = BORDER.fill_bounds();
1160
1161            let mut new_offset = bounds.origin.to_vector();
1162
1163            if clip_bounds != bounds.size || clip_corners != corners {
1164                clip_bounds = bounds.size;
1165                clip_corners = corners;
1166                WIDGET.render();
1167            }
1168
1169            let (_, branch_offset) = LAYOUT.with_constraints(PxConstraints2d::new_exact_size(bounds.size), || {
1170                wl.with_branch_child(|wl| child.layout(wl))
1171            });
1172            new_offset += branch_offset;
1173
1174            if offset != new_offset {
1175                offset = new_offset;
1176
1177                if define_frame {
1178                    WIDGET.render_update();
1179                } else {
1180                    define_frame = true;
1181                    WIDGET.render();
1182                }
1183            }
1184
1185            *final_size = bounds.size;
1186        }
1187        UiNodeOp::Render { frame } => {
1188            let mut render = |frame: &mut FrameBuilder| {
1189                let bounds = PxRect::from_size(clip_bounds);
1190                frame.push_clips(
1191                    |c| {
1192                        if clip_corners != PxCornerRadius::zero() {
1193                            c.push_clip_rounded_rect(bounds, clip_corners, false, false);
1194                        } else {
1195                            c.push_clip_rect(bounds, false, false);
1196                        }
1197
1198                        if let Some(inline) = WIDGET.bounds().inline() {
1199                            for r in inline.negative_space().iter() {
1200                                c.push_clip_rect(*r, true, false);
1201                            }
1202                        }
1203                    },
1204                    |f| child.render(f),
1205                );
1206            };
1207
1208            if define_frame {
1209                frame.push_reference_frame(offset_key.into(), offset_key.bind(offset.into(), false), true, false, |frame| {
1210                    render(frame);
1211                });
1212            } else {
1213                render(frame);
1214            }
1215        }
1216        UiNodeOp::RenderUpdate { update } => {
1217            if define_frame {
1218                update.with_transform(offset_key.update(offset.into(), false), false, |update| {
1219                    child.render_update(update);
1220                });
1221            } else {
1222                child.render_update(update);
1223            }
1224        }
1225        _ => {}
1226    })
1227}
1228
1229/// Creates a border node that delegates rendering to a `border_visual` and manages the `border_offsets` coordinating
1230/// with the other borders of the widget.
1231///
1232/// This node disables inline layout for the widget.
1233pub fn border_node(child: impl IntoUiNode, border_offsets: impl IntoVar<SideOffsets>, border_visual: impl IntoUiNode) -> UiNode {
1234    let offsets = border_offsets.into_var();
1235    let mut render_offsets = PxSideOffsets::zero();
1236    let mut border_rect = PxRect::zero();
1237
1238    match_node(ui_vec![child, border_visual], move |children, op| match op {
1239        UiNodeOp::Init => {
1240            WIDGET.sub_var_layout(&offsets).sub_var_render(&BORDER_OVER_VAR);
1241        }
1242        UiNodeOp::Measure { wm, desired_size } => {
1243            let offsets = offsets.layout();
1244            *desired_size = BORDER.measure_border(offsets, || {
1245                LAYOUT.with_sub_size(PxSize::new(offsets.horizontal(), offsets.vertical()), || {
1246                    children.node().with_child(0, |n| wm.measure_block(n))
1247                })
1248            });
1249            children.delegated();
1250        }
1251        UiNodeOp::Layout { wl, final_size } => {
1252            // We are inside the *inner* bounds or inside a parent border_node:
1253            //
1254            // .. ( layout ( new_border/inner ( BORDER_NODES ( fill_nodes ( new_child_context ( new_child_layout ( ..
1255            //
1256            // `wl` is targeting the child transform, child nodes are naturally inside borders, so we
1257            // need to add to the offset and take the size, fill_nodes optionally cancel this transform.
1258
1259            let offsets = offsets.layout();
1260            if render_offsets != offsets {
1261                render_offsets = offsets;
1262                WIDGET.render();
1263            }
1264
1265            let parent_offsets = BORDER.inner_offsets();
1266            let origin = PxPoint::new(parent_offsets.left, parent_offsets.top);
1267            if border_rect.origin != origin {
1268                border_rect.origin = origin;
1269                WIDGET.render();
1270            }
1271
1272            // layout child and border visual
1273            BORDER.layout_border(offsets, || {
1274                wl.translate(PxVector::new(offsets.left, offsets.top));
1275
1276                let taken_size = PxSize::new(offsets.horizontal(), offsets.vertical());
1277                border_rect.size = LAYOUT.with_sub_size(taken_size, || children.node().with_child(0, |n| n.layout(wl)));
1278
1279                // layout border visual
1280                LAYOUT.with_constraints(PxConstraints2d::new_exact_size(border_rect.size), || {
1281                    BORDER.with_border_layout(border_rect, offsets, || {
1282                        children.node().with_child(1, |n| n.layout(wl));
1283                    });
1284                });
1285            });
1286            children.delegated();
1287
1288            *final_size = border_rect.size;
1289        }
1290        UiNodeOp::Render { frame } => {
1291            if BORDER_OVER_VAR.get() {
1292                children.node().with_child(0, |c| c.render(frame));
1293                BORDER.with_border_layout(border_rect, render_offsets, || {
1294                    children.node().with_child(1, |c| c.render(frame));
1295                });
1296            } else {
1297                BORDER.with_border_layout(border_rect, render_offsets, || {
1298                    children.node().with_child(1, |c| c.render(frame));
1299                });
1300                children.node().with_child(0, |c| c.render(frame));
1301            }
1302            children.delegated();
1303        }
1304        UiNodeOp::RenderUpdate { update } => {
1305            children.node().with_child(0, |c| c.render_update(update));
1306            BORDER.with_border_layout(border_rect, render_offsets, || {
1307                children.node().with_child(1, |c| c.render_update(update));
1308            });
1309            children.delegated();
1310        }
1311        _ => {}
1312    })
1313}
1314
1315/// Helper for declaring nodes that sets a context local value.
1316///
1317/// See [`context_local!`] for more details about contextual values.
1318///
1319/// [`context_local!`]: crate::prelude::context_local
1320pub fn with_context_local<T: Any + Send + Sync + 'static>(
1321    child: impl IntoUiNode,
1322    context: &'static ContextLocal<T>,
1323    value: impl Into<T>,
1324) -> UiNode {
1325    let mut value = Some(Arc::new(value.into()));
1326
1327    match_node(child, move |child, op| {
1328        context.with_context(&mut value, || child.op(op));
1329    })
1330}
1331
1332/// Helper for declaring nodes that sets a context local value generated on init.
1333///
1334/// The method calls the `init_value` closure on init to produce a *value* var that is presented as the [`ContextLocal<T>`]
1335/// in the widget and widget descendants. The closure can be called more than once if the returned node is reinited.
1336///
1337/// Apart from the value initialization this behaves just like [`with_context_local`].
1338///
1339/// [`ContextLocal<T>`]: zng_app_context::ContextLocal
1340pub fn with_context_local_init<T: Any + Send + Sync + 'static>(
1341    child: impl IntoUiNode,
1342    context: &'static ContextLocal<T>,
1343    init_value: impl FnMut() -> T + Send + 'static,
1344) -> UiNode {
1345    with_context_local_init_impl(child.into_node(), context, init_value)
1346}
1347fn with_context_local_init_impl<T: Any + Send + Sync + 'static>(
1348    child: UiNode,
1349    context: &'static ContextLocal<T>,
1350    mut init_value: impl FnMut() -> T + Send + 'static,
1351) -> UiNode {
1352    let mut value = None;
1353
1354    match_node(child, move |child, op| {
1355        let mut is_deinit = false;
1356        match &op {
1357            UiNodeOp::Init => {
1358                value = Some(Arc::new(init_value()));
1359            }
1360            UiNodeOp::Deinit => {
1361                is_deinit = true;
1362            }
1363            _ => {}
1364        }
1365
1366        context.with_context(&mut value, || child.op(op));
1367
1368        if is_deinit {
1369            value = None;
1370        }
1371    })
1372}
1373
1374/// Helper for declaring widgets that are recontextualized to take in some of the context
1375/// of an *original* parent.
1376///
1377/// See [`LocalContext::with_context_blend`] for more details about `over`. The returned
1378/// node will delegate all node operations to inside the blend. The [`WidgetUiNode::with_context`]
1379/// will delegate to the `child` widget context, but the `ctx` is not blended for this method, only
1380/// for [`UiNodeOp`] methods.
1381///
1382/// # Warning
1383///
1384/// Properties, context vars and context locals are implemented with the assumption that all consumers have
1385/// released the context on return, that is even if the context was shared with worker threads all work was block-waited.
1386/// This node breaks this assumption, specially with `over: true` you may cause unexpected behavior if you don't consider
1387/// carefully what context is being captured and what context is being replaced.
1388///
1389/// As a general rule, only capture during init or update in [`NestGroup::CHILD`], only wrap full widgets and only place the wrapped
1390/// widget in a parent's [`NestGroup::CHILD`] for a parent that has no special expectations about the child.
1391///
1392/// As an example of things that can go wrong, if you capture during layout, the `LAYOUT` context is captured
1393/// and replaces `over` the actual layout context during all subsequent layouts in the actual parent.
1394///
1395/// # Panics
1396///
1397/// Panics during init if `ctx` is not from the same app as the init context.
1398///
1399/// [`NestGroup::CHILD`]: zng_app::widget::builder::NestGroup::CHILD
1400/// [`UiNodeOp`]: zng_app::widget::node::UiNodeOp
1401/// [`LocalContext::with_context_blend`]: zng_app_context::LocalContext::with_context_blend
1402pub fn with_context_blend(mut ctx: LocalContext, over: bool, child: impl IntoUiNode) -> UiNode {
1403    match_widget(child, move |c, op| {
1404        if let UiNodeOp::Init = op {
1405            let init_app = LocalContext::current_app();
1406            ctx.with_context_blend(over, || {
1407                let ctx_app = LocalContext::current_app();
1408                assert_eq!(init_app, ctx_app);
1409                c.op(op)
1410            });
1411        } else {
1412            ctx.with_context_blend(over, || c.op(op));
1413        }
1414    })
1415}
1416
1417/// Helper for declaring properties that set the widget state.
1418///
1419/// The state ID is set in [`WIDGET`] on init and is kept updated. On deinit it is set to the `default` value.
1420///
1421/// # Examples
1422///
1423/// ```
1424/// # fn main() -> () { }
1425/// # use zng_app::{widget::{property, node::{UiNode, IntoUiNode}, WIDGET, WidgetUpdateMode}};
1426/// # use zng_var::IntoVar;
1427/// # use zng_wgt::node::with_widget_state;
1428/// # use zng_state_map::{StateId, static_id};
1429/// #
1430/// static_id! {
1431///     pub static ref FOO_ID: StateId<u32>;
1432/// }
1433///
1434/// #[property(CONTEXT)]
1435/// pub fn foo(child: impl IntoUiNode, value: impl IntoVar<u32>) -> UiNode {
1436///     with_widget_state(child, *FOO_ID, || 0, value)
1437/// }
1438///
1439/// // after the property is used and the widget initializes:
1440///
1441/// /// Get the value from outside the widget.
1442/// fn get_foo_outer(widget: &mut UiNode) -> u32 {
1443///     if let Some(mut wgt) = widget.as_widget() {
1444///         wgt.with_context(WidgetUpdateMode::Ignore, || WIDGET.get_state(*FOO_ID))
1445///             .unwrap_or(0)
1446///     } else {
1447///         0
1448///     }
1449/// }
1450///
1451/// /// Get the value from inside the widget.
1452/// fn get_foo_inner() -> u32 {
1453///     WIDGET.get_state(*FOO_ID).unwrap_or_default()
1454/// }
1455/// ```
1456///
1457/// [`WIDGET`]: zng_app::widget::WIDGET
1458pub fn with_widget_state<U, I, T>(child: U, id: impl Into<StateId<T>>, default: I, value: impl IntoVar<T>) -> UiNode
1459where
1460    U: IntoUiNode,
1461    I: Fn() -> T + Send + 'static,
1462    T: StateValue + VarValue,
1463{
1464    with_widget_state_impl(child.into_node(), id.into(), default, value.into_var())
1465}
1466fn with_widget_state_impl<I, T>(child: UiNode, id: impl Into<StateId<T>>, default: I, value: impl IntoVar<T>) -> UiNode
1467where
1468    I: Fn() -> T + Send + 'static,
1469    T: StateValue + VarValue,
1470{
1471    let id = id.into();
1472    let value = value.into_var();
1473
1474    match_node(child, move |child, op| match op {
1475        UiNodeOp::Init => {
1476            child.init();
1477            WIDGET.sub_var(&value);
1478            WIDGET.set_state(id, value.get());
1479        }
1480        UiNodeOp::Deinit => {
1481            child.deinit();
1482            WIDGET.set_state(id, default());
1483        }
1484        UiNodeOp::Update { updates } => {
1485            child.update(updates);
1486            if let Some(v) = value.get_new() {
1487                WIDGET.set_state(id, v);
1488            }
1489        }
1490        _ => {}
1491    })
1492}
1493
1494/// Helper for declaring properties that set the widget state with a custom closure.
1495///
1496/// The `default` closure is used to init the state value, then the `modify` closure is used to modify the state using the variable value.
1497///
1498/// On deinit the `default` value is set on the state again.
1499///
1500/// See [`with_widget_state`] for more details.
1501pub fn with_widget_state_modify<U, S, V, I, M>(child: U, id: impl Into<StateId<S>>, value: impl IntoVar<V>, default: I, modify: M) -> UiNode
1502where
1503    U: IntoUiNode,
1504    S: StateValue,
1505    V: VarValue,
1506    I: Fn() -> S + Send + 'static,
1507    M: FnMut(&mut S, &V) + Send + 'static,
1508{
1509    with_widget_state_modify_impl(child.into_node(), id.into(), value.into_var(), default, modify)
1510}
1511fn with_widget_state_modify_impl<S, V, I, M>(
1512    child: UiNode,
1513    id: impl Into<StateId<S>>,
1514    value: impl IntoVar<V>,
1515    default: I,
1516    mut modify: M,
1517) -> UiNode
1518where
1519    S: StateValue,
1520    V: VarValue,
1521    I: Fn() -> S + Send + 'static,
1522    M: FnMut(&mut S, &V) + Send + 'static,
1523{
1524    let id = id.into();
1525    let value = value.into_var();
1526
1527    match_node(child, move |child, op| match op {
1528        UiNodeOp::Init => {
1529            child.init();
1530
1531            WIDGET.sub_var(&value);
1532
1533            value.with(|v| {
1534                WIDGET.with_state_mut(|mut s| {
1535                    modify(s.entry(id).or_insert_with(&default), v);
1536                })
1537            })
1538        }
1539        UiNodeOp::Deinit => {
1540            child.deinit();
1541
1542            WIDGET.set_state(id, default());
1543        }
1544        UiNodeOp::Update { updates } => {
1545            child.update(updates);
1546            value.with_new(|v| {
1547                WIDGET.with_state_mut(|mut s| {
1548                    modify(s.req_mut(id), v);
1549                })
1550            });
1551        }
1552        _ => {}
1553    })
1554}
1555
1556/// Create a node that controls interaction for all widgets inside `node`.
1557///
1558/// When the `interactive` var is `false` all descendant widgets are [`BLOCKED`].
1559///
1560/// Unlike the [`interactive`] property this does not apply to the contextual widget, only `child` and descendants.
1561///
1562/// The node works for either if the `child` is a widget or if it only contains widgets, the performance
1563/// is slightly better if the `child` is a widget.
1564///
1565/// [`interactive`]: fn@crate::interactive
1566/// [`BLOCKED`]: Interactivity::BLOCKED
1567pub fn interactive_node(child: impl IntoUiNode, interactive: impl IntoVar<bool>) -> UiNode {
1568    let interactive = interactive.into_var();
1569
1570    match_node(child, move |child, op| match op {
1571        UiNodeOp::Init => {
1572            WIDGET.sub_var_info(&interactive);
1573        }
1574        UiNodeOp::Info { info } => {
1575            if interactive.get() {
1576                child.info(info);
1577            } else if let Some(mut wgt) = child.node().as_widget() {
1578                let id = wgt.id();
1579                // child is a widget.
1580                info.push_interactivity_filter(move |args| {
1581                    if args.info.id() == id {
1582                        Interactivity::BLOCKED
1583                    } else {
1584                        Interactivity::ENABLED
1585                    }
1586                });
1587                child.info(info);
1588            } else {
1589                let block_range = info.with_children_range(|info| child.info(info));
1590                if !block_range.is_empty() {
1591                    // has child widgets.
1592
1593                    let id = WIDGET.id();
1594                    info.push_interactivity_filter(move |args| {
1595                        if let Some(parent) = args.info.parent()
1596                            && parent.id() == id
1597                        {
1598                            // check child range
1599                            for (i, item) in parent.children().enumerate() {
1600                                if item == args.info {
1601                                    return if !block_range.contains(&i) {
1602                                        Interactivity::ENABLED
1603                                    } else {
1604                                        Interactivity::BLOCKED
1605                                    };
1606                                } else if i >= block_range.end {
1607                                    break;
1608                                }
1609                            }
1610                        }
1611                        Interactivity::ENABLED
1612                    });
1613                }
1614            }
1615        }
1616        _ => {}
1617    })
1618}
1619
1620/// Helper for a property that gets the index of the widget in the parent panel.
1621///
1622/// See [`with_index_len_node`] for more details.
1623pub fn with_index_node(
1624    child: impl IntoUiNode,
1625    panel_list_id: impl Into<StateId<PanelListRange>>,
1626    mut update: impl FnMut(Option<usize>) + Send + 'static,
1627) -> UiNode {
1628    let panel_list_id = panel_list_id.into();
1629    let mut version = None;
1630    match_node(child, move |_, op| match op {
1631        UiNodeOp::Deinit => {
1632            update(None);
1633            version = None;
1634        }
1635        UiNodeOp::Update { .. } => {
1636            // parent PanelList requests updates for this widget every time there is an update.
1637            let info = WIDGET.info();
1638            if let Some(parent) = info.parent()
1639                && let Some(mut c) = PanelListRange::update(&parent, panel_list_id, &mut version)
1640            {
1641                let id = info.id();
1642                let p = c.position(|w| w.id() == id);
1643                update(p);
1644            }
1645        }
1646        _ => {}
1647    })
1648}
1649
1650/// Helper for a property that gets the reverse index of the widget in the parent panel.
1651///
1652/// See [`with_index_len_node`] for more details.
1653pub fn with_rev_index_node(
1654    child: impl IntoUiNode,
1655    panel_list_id: impl Into<StateId<PanelListRange>>,
1656    mut update: impl FnMut(Option<usize>) + Send + 'static,
1657) -> UiNode {
1658    let panel_list_id = panel_list_id.into();
1659    let mut version = None;
1660    match_node(child, move |_, op| match op {
1661        UiNodeOp::Deinit => {
1662            update(None);
1663            version = None;
1664        }
1665        UiNodeOp::Update { .. } => {
1666            let info = WIDGET.info();
1667            if let Some(parent) = info.parent()
1668                && let Some(c) = PanelListRange::update(&parent, panel_list_id, &mut version)
1669            {
1670                let id = info.id();
1671                let p = c.rev().position(|w| w.id() == id);
1672                update(p);
1673            }
1674        }
1675        _ => {}
1676    })
1677}
1678
1679/// Helper for a property that gets the index of the widget in the parent panel and the number of children.
1680///  
1681/// Panels must use [`PanelList::track_info_range`] to collect the `panel_list_id`, then implement getter properties
1682/// using the methods in this module. See the `stack!` getter properties for examples.
1683///
1684/// [`PanelList::track_info_range`]: zng_app::widget::node::PanelList::track_info_range
1685pub fn with_index_len_node(
1686    child: impl IntoUiNode,
1687    panel_list_id: impl Into<StateId<PanelListRange>>,
1688    mut update: impl FnMut(Option<(usize, usize)>) + Send + 'static,
1689) -> UiNode {
1690    let panel_list_id = panel_list_id.into();
1691    let mut version = None;
1692    match_node(child, move |_, op| match op {
1693        UiNodeOp::Deinit => {
1694            update(None);
1695            version = None;
1696        }
1697        UiNodeOp::Update { .. } => {
1698            let info = WIDGET.info();
1699            if let Some(parent) = info.parent()
1700                && let Some(mut iter) = PanelListRange::update(&parent, panel_list_id, &mut version)
1701            {
1702                let id = info.id();
1703                let mut p = 0;
1704                let mut count = 0;
1705                for c in &mut iter {
1706                    if c.id() == id {
1707                        p = count;
1708                        count += 1 + iter.count();
1709                        break;
1710                    } else {
1711                        count += 1;
1712                    }
1713                }
1714                update(Some((p, count)));
1715            }
1716        }
1717        _ => {}
1718    })
1719}
1720
1721/// Node that presents `data` using `wgt_fn`.
1722///
1723/// The node's child is always the result of `wgt_fn` called for the `data` value, it is reinited every time
1724/// either variable changes. If the child is an widget the node becomes it.
1725///
1726/// See also [`presenter_opt`] for a presenter that is nil with the data is `None`.
1727///
1728/// See also the [`present`](VarPresent::present) method that can be called on the `data`` variable and [`present_data`](VarPresentData::present_data)
1729/// that can be called on the `wgt_fn` variable.
1730pub fn presenter<D: VarValue>(data: impl IntoVar<D>, wgt_fn: impl IntoVar<WidgetFn<D>>) -> UiNode {
1731    let data = data.into_var();
1732    let wgt_fn = wgt_fn.into_var();
1733
1734    match_widget(UiNode::nil(), move |c, op| match op {
1735        UiNodeOp::Init => {
1736            WIDGET.sub_var(&data).sub_var(&wgt_fn);
1737            *c.node() = wgt_fn.get()(data.get());
1738        }
1739        UiNodeOp::Deinit => {
1740            c.deinit();
1741            *c.node() = UiNode::nil();
1742        }
1743        UiNodeOp::Update { .. } => {
1744            if data.is_new() || wgt_fn.is_new() {
1745                c.node().deinit();
1746                *c.node() = wgt_fn.get()(data.get());
1747                c.node().init();
1748                c.delegated();
1749                WIDGET.update_info().layout().render();
1750            }
1751        }
1752        _ => {}
1753    })
1754}
1755
1756/// Node that presents `data` using `wgt_fn` if data is available, otherwise presents nil.
1757///
1758/// This behaves like [`presenter`], but `wgt_fn` is not called if `data` is `None`.
1759///
1760/// See also the [`present_opt`](VarPresentOpt::present_opt) method that can be called on the data variable.
1761pub fn presenter_opt<D: VarValue>(data: impl IntoVar<Option<D>>, wgt_fn: impl IntoVar<WidgetFn<D>>) -> UiNode {
1762    let data = data.into_var();
1763    let wgt_fn = wgt_fn.into_var();
1764
1765    match_widget(UiNode::nil(), move |c, op| match op {
1766        UiNodeOp::Init => {
1767            WIDGET.sub_var(&data).sub_var(&wgt_fn);
1768            if let Some(data) = data.get() {
1769                *c.node() = wgt_fn.get()(data);
1770            }
1771        }
1772        UiNodeOp::Deinit => {
1773            c.deinit();
1774            *c.node() = UiNode::nil();
1775        }
1776        UiNodeOp::Update { .. } => {
1777            if data.is_new() || wgt_fn.is_new() {
1778                if let Some(data) = data.get() {
1779                    c.node().deinit();
1780                    *c.node() = wgt_fn.get()(data);
1781                    c.node().init();
1782                    c.delegated();
1783                    WIDGET.update_info().layout().render();
1784                } else if !c.node().is_nil() {
1785                    c.node().deinit();
1786                    *c.node() = UiNode::nil();
1787                    c.delegated();
1788                    WIDGET.update_info().layout().render();
1789                }
1790            }
1791        }
1792        _ => {}
1793    })
1794}
1795
1796/// Node list that presents `list` using `item_fn` for each new list item.
1797///
1798/// The node's children is the list mapped to node items, it is kept in sync, any list update is propagated to the node list.
1799///
1800/// See also the [`present_list`](VarPresentList::present_list) method that can be called on the list variable.
1801pub fn list_presenter<D: VarValue>(list: impl IntoVar<ObservableVec<D>>, item_fn: impl IntoVar<WidgetFn<D>>) -> UiNode {
1802    ListPresenter {
1803        list: list.into_var(),
1804        item_fn: item_fn.into_var(),
1805        view: ui_vec![],
1806        _e: std::marker::PhantomData,
1807    }
1808    .into_node()
1809}
1810
1811/// Node list that presents `list` using `item_fn` for each list item.
1812///
1813/// The node's children are **regenerated** for each change in `list`, if possible prefer using [`ObservableVec`] with [`list_presenter`].
1814///
1815/// See also the [`present_list_from_iter`](VarPresentListFromIter::present_list_from_iter) method that can be called on the list variable.
1816pub fn list_presenter_from_iter<D, L>(list: impl IntoVar<L>, item_fn: impl IntoVar<WidgetFn<D>>) -> UiNode
1817where
1818    D: VarValue,
1819    L: IntoIterator<Item = D> + VarValue,
1820{
1821    ListPresenterFromIter {
1822        list: list.into_var(),
1823        item_fn: item_fn.into_var(),
1824        view: ui_vec![],
1825        _e: std::marker::PhantomData,
1826    }
1827    .into_node()
1828}
1829
1830struct ListPresenter<D>
1831where
1832    D: VarValue,
1833{
1834    list: Var<ObservableVec<D>>,
1835    item_fn: Var<WidgetFn<D>>,
1836    view: UiVec,
1837    _e: std::marker::PhantomData<D>,
1838}
1839
1840impl<D> UiNodeImpl for ListPresenter<D>
1841where
1842    D: VarValue,
1843{
1844    fn children_len(&self) -> usize {
1845        self.view.len()
1846    }
1847
1848    fn with_child(&mut self, index: usize, visitor: &mut dyn FnMut(&mut UiNode)) {
1849        self.view.with_child(index, visitor)
1850    }
1851
1852    fn is_list(&self) -> bool {
1853        true
1854    }
1855
1856    fn for_each_child(&mut self, visitor: &mut dyn FnMut(usize, &mut UiNode)) {
1857        self.view.for_each_child(visitor);
1858    }
1859
1860    fn try_for_each_child(
1861        &mut self,
1862        visitor: &mut dyn FnMut(usize, &mut UiNode) -> std::ops::ControlFlow<BoxAnyVarValue>,
1863    ) -> std::ops::ControlFlow<BoxAnyVarValue> {
1864        self.view.try_for_each_child(visitor)
1865    }
1866
1867    fn par_each_child(&mut self, visitor: &(dyn Fn(usize, &mut UiNode) + Sync)) {
1868        self.view.par_each_child(visitor);
1869    }
1870
1871    fn par_fold_reduce(
1872        &mut self,
1873        identity: BoxAnyVarValue,
1874        fold: &(dyn Fn(BoxAnyVarValue, usize, &mut UiNode) -> BoxAnyVarValue + Sync),
1875        reduce: &(dyn Fn(BoxAnyVarValue, BoxAnyVarValue) -> BoxAnyVarValue + Sync),
1876    ) -> BoxAnyVarValue {
1877        self.view.par_fold_reduce(identity, fold, reduce)
1878    }
1879
1880    fn init(&mut self) {
1881        debug_assert!(self.view.is_empty());
1882        self.view.clear();
1883
1884        WIDGET.sub_var(&self.list).sub_var(&self.item_fn);
1885
1886        let e_fn = self.item_fn.get();
1887        self.list.with(|l| {
1888            for el in l.iter() {
1889                let child = e_fn(el.clone());
1890                self.view.push(child);
1891            }
1892        });
1893
1894        self.view.init();
1895    }
1896
1897    fn deinit(&mut self) {
1898        self.view.deinit();
1899        self.view.clear();
1900    }
1901
1902    fn update(&mut self, updates: &WidgetUpdates) {
1903        self.update_list(updates, &mut ());
1904    }
1905
1906    fn update_list(&mut self, updates: &WidgetUpdates, observer: &mut dyn UiNodeListObserver) {
1907        let mut need_reset = self.item_fn.is_new();
1908
1909        let is_new = self
1910            .list
1911            .with_new(|l| {
1912                need_reset |= l.changes().is_empty() || l.changes() == [VecChange::Clear];
1913
1914                if need_reset {
1915                    return;
1916                }
1917
1918                // update before new items to avoid update before init.
1919                self.view.update_list(updates, observer);
1920
1921                let e_fn = self.item_fn.get();
1922
1923                for change in l.changes() {
1924                    match change {
1925                        VecChange::Insert { index, count } => {
1926                            for i in *index..(*index + count) {
1927                                let mut el = e_fn(l[i].clone());
1928                                el.init();
1929                                self.view.insert(i, el);
1930                                observer.inserted(i);
1931                            }
1932                        }
1933                        VecChange::Remove { index, count } => {
1934                            let mut count = *count;
1935                            let index = *index;
1936                            while count > 0 {
1937                                count -= 1;
1938
1939                                let mut el = self.view.remove(index);
1940                                el.deinit();
1941                                observer.removed(index);
1942                            }
1943                        }
1944                        VecChange::Move { from_index, to_index } => {
1945                            let el = self.view.remove(*from_index);
1946                            self.view.insert(*to_index, el);
1947                            observer.moved(*from_index, *to_index);
1948                        }
1949                        VecChange::Clear => unreachable!(),
1950                    }
1951                }
1952            })
1953            .is_some();
1954
1955        if !need_reset && !is_new && self.list.with(|l| l.len() != self.view.len()) {
1956            need_reset = true;
1957        }
1958
1959        if need_reset {
1960            self.view.deinit();
1961            self.view.clear();
1962
1963            let e_fn = self.item_fn.get();
1964            self.list.with(|l| {
1965                for el in l.iter() {
1966                    let child = e_fn(el.clone());
1967                    self.view.push(child);
1968                }
1969            });
1970
1971            self.view.init();
1972        } else if !is_new {
1973            self.view.update_list(updates, observer);
1974        }
1975    }
1976
1977    fn info(&mut self, info: &mut zng_app::widget::info::WidgetInfoBuilder) {
1978        self.view.info(info);
1979    }
1980
1981    fn measure(&mut self, wm: &mut zng_app::widget::info::WidgetMeasure) -> PxSize {
1982        self.view.measure(wm)
1983    }
1984
1985    fn measure_list(
1986        &mut self,
1987        wm: &mut zng_app::widget::info::WidgetMeasure,
1988        measure: &(dyn Fn(usize, &mut UiNode, &mut zng_app::widget::info::WidgetMeasure) -> PxSize + Sync),
1989        fold_size: &(dyn Fn(PxSize, PxSize) -> PxSize + Sync),
1990    ) -> PxSize {
1991        self.view.measure_list(wm, measure, fold_size)
1992    }
1993
1994    fn layout(&mut self, wl: &mut zng_app::widget::info::WidgetLayout) -> PxSize {
1995        self.view.layout(wl)
1996    }
1997
1998    fn layout_list(
1999        &mut self,
2000        wl: &mut zng_app::widget::info::WidgetLayout,
2001        layout: &(dyn Fn(usize, &mut UiNode, &mut zng_app::widget::info::WidgetLayout) -> PxSize + Sync),
2002        fold_size: &(dyn Fn(PxSize, PxSize) -> PxSize + Sync),
2003    ) -> PxSize {
2004        self.view.layout_list(wl, layout, fold_size)
2005    }
2006
2007    fn render(&mut self, frame: &mut FrameBuilder) {
2008        self.view.render(frame);
2009    }
2010
2011    fn render_list(&mut self, frame: &mut FrameBuilder, render: &(dyn Fn(usize, &mut UiNode, &mut FrameBuilder) + Sync)) {
2012        self.view.render_list(frame, render);
2013    }
2014
2015    fn render_update(&mut self, update: &mut zng_app::render::FrameUpdate) {
2016        self.view.render_update(update);
2017    }
2018
2019    fn render_update_list(
2020        &mut self,
2021        update: &mut zng_app::render::FrameUpdate,
2022        render_update: &(dyn Fn(usize, &mut UiNode, &mut zng_app::render::FrameUpdate) + Sync),
2023    ) {
2024        self.view.render_update_list(update, render_update);
2025    }
2026
2027    fn as_widget(&mut self) -> Option<&mut dyn WidgetUiNodeImpl> {
2028        None
2029    }
2030}
2031
2032struct ListPresenterFromIter<D, L>
2033where
2034    D: VarValue,
2035    L: IntoIterator<Item = D> + VarValue,
2036{
2037    list: Var<L>,
2038    item_fn: Var<WidgetFn<D>>,
2039    view: UiVec,
2040    _e: std::marker::PhantomData<(D, L)>,
2041}
2042
2043impl<D, L> UiNodeImpl for ListPresenterFromIter<D, L>
2044where
2045    D: VarValue,
2046    L: IntoIterator<Item = D> + VarValue,
2047{
2048    fn children_len(&self) -> usize {
2049        self.view.len()
2050    }
2051
2052    fn with_child(&mut self, index: usize, visitor: &mut dyn FnMut(&mut UiNode)) {
2053        self.view.with_child(index, visitor)
2054    }
2055
2056    fn for_each_child(&mut self, visitor: &mut dyn FnMut(usize, &mut UiNode)) {
2057        self.view.for_each_child(visitor)
2058    }
2059
2060    fn try_for_each_child(
2061        &mut self,
2062        visitor: &mut dyn FnMut(usize, &mut UiNode) -> std::ops::ControlFlow<BoxAnyVarValue>,
2063    ) -> std::ops::ControlFlow<BoxAnyVarValue> {
2064        self.view.try_for_each_child(visitor)
2065    }
2066
2067    fn par_each_child(&mut self, visitor: &(dyn Fn(usize, &mut UiNode) + Sync)) {
2068        self.view.par_each_child(visitor);
2069    }
2070
2071    fn par_fold_reduce(
2072        &mut self,
2073        identity: BoxAnyVarValue,
2074        fold: &(dyn Fn(BoxAnyVarValue, usize, &mut UiNode) -> BoxAnyVarValue + Sync),
2075        reduce: &(dyn Fn(BoxAnyVarValue, BoxAnyVarValue) -> BoxAnyVarValue + Sync),
2076    ) -> BoxAnyVarValue {
2077        self.view.par_fold_reduce(identity, fold, reduce)
2078    }
2079
2080    fn is_list(&self) -> bool {
2081        true
2082    }
2083
2084    fn init(&mut self) {
2085        debug_assert!(self.view.is_empty());
2086        self.view.clear();
2087
2088        WIDGET.sub_var(&self.list).sub_var(&self.item_fn);
2089
2090        let e_fn = self.item_fn.get();
2091
2092        self.view.extend(self.list.get().into_iter().map(&*e_fn));
2093        self.view.init();
2094    }
2095
2096    fn deinit(&mut self) {
2097        self.view.deinit();
2098        self.view.clear();
2099    }
2100
2101    fn update(&mut self, updates: &WidgetUpdates) {
2102        self.update_list(updates, &mut ())
2103    }
2104    fn update_list(&mut self, updates: &WidgetUpdates, observer: &mut dyn UiNodeListObserver) {
2105        if self.list.is_new() || self.item_fn.is_new() {
2106            self.view.deinit();
2107            self.view.clear();
2108            let e_fn = self.item_fn.get();
2109            self.view.extend(self.list.get().into_iter().map(&*e_fn));
2110            self.view.init();
2111            observer.reset();
2112        } else {
2113            self.view.update_list(updates, observer);
2114        }
2115    }
2116
2117    fn info(&mut self, info: &mut zng_app::widget::info::WidgetInfoBuilder) {
2118        self.view.info(info)
2119    }
2120
2121    fn measure(&mut self, wm: &mut zng_app::widget::info::WidgetMeasure) -> PxSize {
2122        self.view.measure(wm)
2123    }
2124
2125    fn measure_list(
2126        &mut self,
2127        wm: &mut zng_app::widget::info::WidgetMeasure,
2128        measure: &(dyn Fn(usize, &mut UiNode, &mut zng_app::widget::info::WidgetMeasure) -> PxSize + Sync),
2129        fold_size: &(dyn Fn(PxSize, PxSize) -> PxSize + Sync),
2130    ) -> PxSize {
2131        self.view.measure_list(wm, measure, fold_size)
2132    }
2133
2134    fn layout(&mut self, wl: &mut zng_app::widget::info::WidgetLayout) -> PxSize {
2135        self.view.layout(wl)
2136    }
2137
2138    fn layout_list(
2139        &mut self,
2140        wl: &mut zng_app::widget::info::WidgetLayout,
2141        layout: &(dyn Fn(usize, &mut UiNode, &mut zng_app::widget::info::WidgetLayout) -> PxSize + Sync),
2142        fold_size: &(dyn Fn(PxSize, PxSize) -> PxSize + Sync),
2143    ) -> PxSize {
2144        self.view.layout_list(wl, layout, fold_size)
2145    }
2146
2147    fn render(&mut self, frame: &mut FrameBuilder) {
2148        self.view.render(frame);
2149    }
2150
2151    fn render_list(&mut self, frame: &mut FrameBuilder, render: &(dyn Fn(usize, &mut UiNode, &mut FrameBuilder) + Sync)) {
2152        self.view.render_list(frame, render);
2153    }
2154
2155    fn render_update(&mut self, update: &mut zng_app::render::FrameUpdate) {
2156        self.view.render_update(update);
2157    }
2158
2159    fn render_update_list(
2160        &mut self,
2161        update: &mut zng_app::render::FrameUpdate,
2162        render_update: &(dyn Fn(usize, &mut UiNode, &mut zng_app::render::FrameUpdate) + Sync),
2163    ) {
2164        self.view.render_update_list(update, render_update);
2165    }
2166
2167    fn as_widget(&mut self) -> Option<&mut dyn WidgetUiNodeImpl> {
2168        None
2169    }
2170}
2171
2172/// Extension method to *convert* a variable to a node.
2173pub trait VarPresent<D: VarValue> {
2174    /// Present the variable data using a [`presenter`] node.
2175    fn present(&self, wgt_fn: impl IntoVar<WidgetFn<D>>) -> UiNode;
2176}
2177impl<D: VarValue> VarPresent<D> for Var<D> {
2178    fn present(&self, wgt_fn: impl IntoVar<WidgetFn<D>>) -> UiNode {
2179        presenter(self.clone(), wgt_fn)
2180    }
2181}
2182
2183/// Extension method to *convert* a variable to a node.
2184pub trait VarPresentOpt<D: VarValue> {
2185    /// Present the variable data using a [`presenter_opt`] node.
2186    fn present_opt(&self, wgt_fn: impl IntoVar<WidgetFn<D>>) -> UiNode;
2187}
2188impl<D: VarValue> VarPresentOpt<D> for Var<Option<D>> {
2189    fn present_opt(&self, wgt_fn: impl IntoVar<WidgetFn<D>>) -> UiNode {
2190        presenter_opt(self.clone(), wgt_fn)
2191    }
2192}
2193
2194/// Extension method fo *convert* a variable to a node list.
2195pub trait VarPresentList<D: VarValue> {
2196    /// Present the variable data using a [`list_presenter`] node list.
2197    fn present_list(&self, wgt_fn: impl IntoVar<WidgetFn<D>>) -> UiNode;
2198}
2199impl<D: VarValue> VarPresentList<D> for Var<ObservableVec<D>> {
2200    fn present_list(&self, wgt_fn: impl IntoVar<WidgetFn<D>>) -> UiNode {
2201        list_presenter(self.clone(), wgt_fn)
2202    }
2203}
2204
2205/// Extension method fo *convert* a variable to a node list.
2206pub trait VarPresentListFromIter<D: VarValue, L: IntoIterator<Item = D> + VarValue> {
2207    /// Present the variable data using a [`list_presenter_from_iter`] node list.
2208    fn present_list_from_iter(&self, wgt_fn: impl IntoVar<WidgetFn<D>>) -> UiNode;
2209}
2210impl<D: VarValue, L: IntoIterator<Item = D> + VarValue> VarPresentListFromIter<D, L> for Var<L> {
2211    fn present_list_from_iter(&self, wgt_fn: impl IntoVar<WidgetFn<D>>) -> UiNode {
2212        list_presenter_from_iter(self.clone(), wgt_fn)
2213    }
2214}
2215
2216/// Extension method to *convert* a variable to a node.
2217pub trait VarPresentData<D: VarValue> {
2218    /// Present the `data` variable using a [`presenter`] node.
2219    fn present_data(&self, data: impl IntoVar<D>) -> UiNode;
2220}
2221impl<D: VarValue> VarPresentData<D> for Var<WidgetFn<D>> {
2222    fn present_data(&self, data: impl IntoVar<D>) -> UiNode {
2223        presenter(data, self.clone())
2224    }
2225}