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