zng_wgt/
panel_props.rs

1use zng_app::widget::node::Z_INDEX;
2
3use crate::prelude::*;
4
5/// Defines the render order of a widget in a layout panel.
6///
7/// When set the widget will still update and layout according to their *logical* position in the list but
8/// they will render according to the order defined by the [`ZIndex`] value.
9///
10/// An error is logged on init if the widget is not a direct child of a Z-sorting panel.
11///
12/// [`ZIndex`]: zng_app::widget::node::ZIndex
13#[property(CONTEXT, default(ZIndex::DEFAULT))]
14pub fn z_index(child: impl UiNode, index: impl IntoVar<ZIndex>) -> impl UiNode {
15    let index = index.into_var();
16    let mut valid = false;
17
18    match_node(child, move |_, op| match op {
19        UiNodeOp::Init => {
20            valid = Z_INDEX.set(index.get());
21
22            if valid {
23                WIDGET.sub_var(&index);
24            } else {
25                tracing::error!(
26                    "property `z_index` set for `{}` but it is not the direct child of a Z-sorting panel",
27                    WIDGET.trace_id()
28                );
29            }
30        }
31        UiNodeOp::Update { .. } => {
32            if valid {
33                if let Some(i) = index.get_new() {
34                    assert!(Z_INDEX.set(i));
35                }
36            }
37        }
38        _ => {}
39    })
40}