zng_wgt_size_offset/
offset_impl.rs

1use zng_wgt::prelude::*;
2
3/// Widget layout offset.
4///
5/// Relative values are computed from the constraints maximum bounded size.
6///
7/// # `x` and `y`
8///
9/// You can use the [`x`](fn@crate::x) and [`y`](fn@crate::y) properties to only set the position in one dimension.
10#[property(LAYOUT, default((0, 0)))]
11pub fn offset(child: impl IntoUiNode, offset: impl IntoVar<Vector>) -> UiNode {
12    let offset = offset.into_var();
13    match_node(child, move |child, op| match op {
14        UiNodeOp::Init => {
15            WIDGET.sub_var_layout(&offset);
16        }
17        UiNodeOp::Layout { wl, final_size } => {
18            let size = child.layout(wl);
19            let offset = LAYOUT.with_constraints(PxConstraints2d::new_exact_size(LAYOUT.constraints().fill_size().max(size)), || {
20                offset.layout()
21            });
22            wl.translate(offset);
23            *final_size = size;
24        }
25        _ => {}
26    })
27}
28
29/// Offset on the ***x*** axis.
30///
31/// Relative values are computed from the constraints maximum bounded width.
32///
33/// # `offset`
34///
35/// You can set both `x` and `y` at the same time using the [`offset`](fn@crate::offset) property.
36#[property(LAYOUT, default(0))]
37pub fn x(child: impl IntoUiNode, x: impl IntoVar<Length>) -> UiNode {
38    let x = x.into_var();
39    match_node(child, move |child, op| match op {
40        UiNodeOp::Init => {
41            WIDGET.sub_var_layout(&x);
42        }
43        UiNodeOp::Layout { wl, final_size } => {
44            let size = child.layout(wl);
45
46            let x = LAYOUT.with_constraints(PxConstraints2d::new_exact_size(LAYOUT.constraints().fill_size().max(size)), || {
47                x.layout_x()
48            });
49            wl.translate(PxVector::new(x, Px(0)));
50            *final_size = size;
51        }
52        _ => {}
53    })
54}
55
56/// Offset on the ***y*** axis.
57///
58/// Relative values are computed from the constraints maximum bounded height.
59///
60/// # `offset`
61///
62/// You can set both `x` and `y` at the same time using the [`offset`](fn@crate::offset) property.
63#[property(LAYOUT, default(0))]
64pub fn y(child: impl IntoUiNode, y: impl IntoVar<Length>) -> UiNode {
65    let y = y.into_var();
66    match_node(child, move |child, op| match op {
67        UiNodeOp::Init => {
68            WIDGET.sub_var_layout(&y);
69        }
70        UiNodeOp::Layout { wl, final_size } => {
71            let size = child.layout(wl);
72            let y = LAYOUT.with_constraints(PxConstraints2d::new_exact_size(LAYOUT.constraints().fill_size().max(size)), || {
73                y.layout_y()
74            });
75            wl.translate(PxVector::new(Px(0), y));
76            *final_size = size;
77        }
78        _ => {}
79    })
80}