Skip to main content

narui_widgets/
rect.rs

1use crate::layout::{positioned, sized, stack};
2use narui_core::{layout::Maximal, *};
3use narui_macros::{rsx, widget};
4
5
6#[widget]
7pub fn rect_leaf(
8    #[default] border_radius: Dimension,
9    #[default] fill: Option<Color>,
10    #[default] stroke: Option<(Color, f32)>,
11    context: &mut WidgetContext,
12) -> FragmentInner {
13    FragmentInner::Leaf {
14        render_object: RenderObject::RoundedRect {
15            stroke_color: stroke.map(|v| v.0),
16            fill_color: fill,
17            stroke_width: stroke.map(|v| v.1).unwrap_or(0.0),
18            border_radius,
19            inverted: false,
20            for_clipping: false,
21        },
22        layout: Box::new(Maximal),
23    }
24}
25
26
27#[widget]
28pub fn inverse_rect_leaf(
29    #[default] border_radius: Dimension,
30    #[default] fill: Option<Color>,
31    #[default(false)] for_clipping: bool,
32    context: &mut WidgetContext,
33) -> FragmentInner {
34    FragmentInner::Leaf {
35        render_object: RenderObject::RoundedRect {
36            inverted: true,
37            stroke_color: None,
38            fill_color: fill,
39            stroke_width: 0.0,
40            for_clipping,
41            border_radius,
42        },
43        layout: Box::new(Maximal),
44    }
45}
46
47#[widget]
48pub fn rect(
49    #[default] border_radius: Dimension,
50    #[default] fill: Option<Color>,
51    #[default] stroke: Option<(Color, f32)>,
52    #[default] do_clipping: bool,
53    #[default] constraint: BoxConstraints,
54    #[default] children: Option<Fragment>,
55    context: &mut WidgetContext,
56) -> Fragment {
57    if !do_clipping {
58        if let Some(frag) = children {
59            rsx! {
60                <stack>
61                    <positioned>
62                        <rect_leaf border_radius=border_radius fill=fill stroke=stroke />
63                    </positioned>
64                    <sized constraint=constraint>{frag.into()}</sized>
65                </stack>
66            }
67        } else {
68            rsx! {
69                <sized constraint=constraint><rect_leaf border_radius=border_radius fill=fill stroke=stroke /></sized>
70            }
71        }
72    } else {
73        rsx! {
74            <stack is_clipper=true>
75                <positioned z_top=true>
76                    <inverse_rect_leaf fill=Some(Color::new(0., 0., 0., 0.)) border_radius=border_radius for_clipping=true />
77                </positioned>
78                <positioned>
79                    <rect_leaf border_radius=border_radius fill=fill stroke=stroke />
80                </positioned>
81                <sized constraint=constraint>{children}</sized>
82            </stack>
83        }
84    }
85}