zen_rs/components/
container.rs

1//! Container component
2
3use crate::aspects::{
4    Align, BackgroundColor, BorderPart, BorderStyle, Color, Gap, Height, Order, Padding, Size,
5    Width,
6};
7
8use super::Components;
9
10#[inline]
11/// Returns a default [Container] instance.
12pub fn container() -> Container {
13    Container::default()
14}
15
16#[derive(Debug, Default, Clone, PartialEq, PartialOrd)]
17/// Represents a container with customizable aspects like size, color, border, and alignment.
18pub struct Container {
19    /// Components contained within the container.
20    components: Vec<Components>,
21    /// Background color of the container.
22    background_color: BackgroundColor,
23    /// Width of the container.
24    width: Width,
25    /// Whether the container should occupy the full width.
26    is_width_full: bool,
27    /// Height of the container.
28    height: Height,
29    /// Whether the container should occupy the full height.
30    is_height_full: bool,
31    /// Border properties of the container.
32    border: BorderStyle,
33    /// Layout direction of components within the container.
34    direction: Order,
35    /// Gap between components in the container.
36    gap: Gap,
37    /// Padding inside the container.
38    padding: Padding,
39    /// Whether the container uses flexible layout.
40    is_flex: bool,
41    /// Alignment of content within the container.
42    align_content: Align,
43    /// Alignment of individual items within the container.
44    align_items: Align,
45}
46
47impl Container {
48    /// add component to end of list
49    #[inline]
50    pub fn push(&mut self, component: impl Into<Components>) {
51        self.components.push(component.into());
52    }
53
54    /// Adds a single component to the container.
55    #[inline]
56    pub fn component(mut self, component: impl Into<Components>) -> Self {
57        self.components.push(component.into());
58        self
59    }
60
61    /// Adds multiple components to the container.
62    /// **note** at once can be added only 1 type of object
63    #[inline]
64    pub fn components(
65        mut self,
66        components: impl IntoIterator<Item = impl Into<Components>>,
67    ) -> Self {
68        self.components
69            .append(&mut components.into_iter().map(|x| x.into()).collect());
70        self
71    }
72
73    /// Retrieves all components in the container.
74    #[inline]
75    pub fn get_components(&self) -> &[Components] {
76        &self.components
77    }
78
79    /// Sets the gap between components in the container.
80    #[inline]
81    pub fn gap(mut self, gap: Gap) -> Self {
82        self.gap = gap;
83        self
84    }
85
86    /// Sets the padding inside the container.
87    #[inline]
88    pub fn padding(mut self, padding: Padding) -> Self {
89        self.padding = padding;
90        self
91    }
92
93    /// Retrieves the gap setting of the container.
94    #[inline]
95    pub fn get_gap(&self) -> &Gap {
96        &self.gap
97    }
98
99    /// Retrieves the padding setting of the container.
100    #[inline]
101    pub fn get_padding(&self) -> &Padding {
102        &self.padding
103    }
104
105    /// Sets the background color of the container.
106    #[inline]
107    pub fn background_color(mut self, background_color: BackgroundColor) -> Self {
108        self.background_color = background_color;
109        self
110    }
111
112    /// Sets the width of the container.
113    #[inline]
114    pub fn width(mut self, width: Width) -> Self {
115        self.width = width;
116        self
117    }
118
119    /// Sets the height of the container.
120    #[inline]
121    pub fn height(mut self, height: Height) -> Self {
122        self.height = height;
123        self
124    }
125
126    /// Toggles whether the container occupies the full width.
127    #[inline]
128    pub fn width_full(mut self) -> Self {
129        self.is_width_full = !self.is_width_full;
130        self
131    }
132
133    /// Toggles whether the container occupies the full height.
134    #[inline]
135    pub fn height_full(mut self) -> Self {
136        self.is_height_full = !self.is_height_full;
137        self
138    }
139
140    /// Sets the border properties of the container.
141    #[inline]
142    pub fn border(mut self, border: BorderStyle) -> Self {
143        self.border = border;
144        self
145    }
146
147    /// Sets the border color properties of the container.
148    #[inline]
149    pub fn border_color(mut self, border_color: Color) -> Self {
150        self.border.1 = border_color;
151        self
152    }
153
154    /// Set the border radius propertie of the container.
155    #[inline]
156    pub fn border_radius(mut self, border_size: Size) -> Self {
157        self.border.2 = border_size;
158        self
159    }
160
161    /// Sets the border properties of the container.
162    #[inline]
163    pub fn border_size(mut self, border_size: BorderPart) -> Self {
164        self.border.0 = border_size;
165        self
166    }
167
168    /// Sets the border properties size of the container.
169    #[inline]
170    pub fn border_size_full(mut self, border_size_full: Size) -> Self {
171        self.border.0 = (
172            border_size_full,
173            border_size_full,
174            border_size_full,
175            border_size_full,
176        );
177        self
178    }
179
180    /// Set the border size propertie
181    #[inline]
182    pub fn border_size_l(mut self, border_size_l: Size) -> Self {
183        let (_, t, b, r) = self.border.0;
184        self.border.0 = (border_size_l, t, b, r);
185        self
186    }
187
188    /// Set the border size propertie
189    #[inline]
190    pub fn border_size_t(mut self, border_size_t: Size) -> Self {
191        let (l, _, b, r) = self.border.0;
192        self.border.0 = (l, border_size_t, b, r);
193        self
194    }
195
196    /// Set the border size propertie
197    #[inline]
198    pub fn border_size_b(mut self, border_size_b: Size) -> Self {
199        let (l, t, _, r) = self.border.0;
200        self.border.0 = (l, t, border_size_b, r);
201        self
202    }
203
204    /// Set the border size propertie
205    #[inline]
206    pub fn border_size_r(mut self, border_size_r: Size) -> Self {
207        let (l, t, b, _) = self.border.0;
208        self.border.0 = (l, t, b, border_size_r);
209        self
210    }
211
212    /// Sets the layout direction of components in the container.
213    #[inline]
214    pub fn direction(mut self, direction: Order) -> Self {
215        self.direction = direction;
216        self
217    }
218
219    /// Toggles whether the container uses flexible layout.
220    #[inline]
221    pub fn flex(mut self) -> Self {
222        self.is_flex = !self.is_flex;
223        self
224    }
225
226    /// Sets the alignment of content within the container.
227    #[inline]
228    pub fn align_content(mut self, align_content: Align) -> Self {
229        self.align_content = align_content;
230        self
231    }
232
233    /// Sets the alignment of individual items within the container.
234    #[inline]
235    pub fn align_items(mut self, align_items: Align) -> Self {
236        self.align_items = align_items;
237        self
238    }
239
240    /// Retrieves the flexible layout setting of the container.
241    #[inline]
242    pub fn get_flex(&self) -> &bool {
243        &self.is_flex
244    }
245
246    /// Retrieves the full-width setting of the container.
247    #[inline]
248    pub fn get_width_full(&self) -> &bool {
249        &self.is_width_full
250    }
251
252    /// Retrieves the full-height setting of the container.
253    #[inline]
254    pub fn get_height_full(&self) -> &bool {
255        &self.is_height_full
256    }
257
258    /// Retrieves the alignment of content within the container.
259    #[inline]
260    pub fn get_align_content(&self) -> &Align {
261        &self.align_content
262    }
263
264    /// Retrieves the alignment of items within the container.
265    #[inline]
266    pub fn get_align_items(&self) -> &Align {
267        &self.align_items
268    }
269
270    /// Retrieves the background color of the container.
271    #[inline]
272    pub fn get_background_color(&self) -> &(u8, u8, u8, u8) {
273        &self.background_color
274    }
275
276    /// Retrieves the width of the container.
277    #[inline]
278    pub fn get_width(&self) -> &u64 {
279        &self.width
280    }
281
282    /// Retrieves the height of the container.
283    #[inline]
284    pub fn get_height(&self) -> &u64 {
285        &self.height
286    }
287
288    /// Retrieves the border properties of the container.
289    #[inline]
290    pub fn get_border(&self) -> &BorderStyle {
291        &self.border
292    }
293
294    /// Retrieves the layout direction of the container.
295    #[inline]
296    pub fn get_direction(&self) -> &Order {
297        &self.direction
298    }
299}