maycoon_widgets/
ext.rs

1use maycoon_core::state::{State, Val};
2use maycoon_core::widget::Widget;
3
4/// An extension trait for widgets with a single child widget.
5pub trait WidgetChildExt<S: State, W: Widget<S> + 'static> {
6    /// Sets the child widget of the widget.
7    fn set_child(&mut self, child: impl Into<Val<S, W>>);
8
9    /// Sets the child widget of the widget and returns self.
10    fn with_child(mut self, child: impl Into<Val<S, W>>) -> Self
11    where
12        Self: Sized,
13    {
14        self.set_child(child);
15        self
16    }
17}
18
19/// An extension trait for widgets with multiple child widgets.
20pub trait WidgetChildrenExt<S: State> {
21    /// Sets the child widgets of the widget.
22    fn set_children(&mut self, children: Vec<Val<S, impl Widget<S> + 'static>>);
23
24    /// Sets the child widgets of the widget and returns self.
25    fn with_children(mut self, children: Vec<Val<S, impl Widget<S> + 'static>>) -> Self
26    where
27        Self: Sized,
28    {
29        self.set_children(children);
30        self
31    }
32
33    /// Adds a child widget to the widget.
34    fn add_child<W: Widget<S> + 'static>(&mut self, child: impl Into<Val<S, W>>);
35
36    /// Adds a child widget to the widget and returns self.
37    fn with_child<W: Widget<S> + 'static>(mut self, child: impl Into<Val<S, W>>) -> Self
38    where
39        Self: Sized,
40    {
41        self.add_child(child);
42        self
43    }
44}
45
46/// An extension trait for widgets with a layout style.
47pub trait WidgetLayoutExt<S: State> {
48    /// Sets the layout style of the widget.
49    fn set_layout_style(
50        &mut self,
51        layout_style: impl Into<Val<S, maycoon_core::layout::LayoutStyle>>,
52    );
53
54    /// Sets the layout style of the widget and returns self.
55    fn with_layout_style(
56        mut self,
57        layout_style: impl Into<Val<S, maycoon_core::layout::LayoutStyle>>,
58    ) -> Self
59    where
60        Self: Sized,
61    {
62        self.set_layout_style(layout_style);
63        self
64    }
65}