1use presentar_core::{
4 widget::LayoutResult, Brick, BrickAssertion, BrickBudget, BrickVerification, Canvas, Color,
5 Constraints, CornerRadius, Event, Rect, Size, TypeId, Widget,
6};
7use serde::{Deserialize, Serialize};
8use std::any::Any;
9use std::time::Duration;
10
11#[derive(Serialize, Deserialize)]
13pub struct Container {
14 pub background: Option<Color>,
16 pub corner_radius: CornerRadius,
18 pub padding: f32,
20 pub min_width: Option<f32>,
22 pub min_height: Option<f32>,
24 pub max_width: Option<f32>,
26 pub max_height: Option<f32>,
28 #[serde(skip)]
30 children: Vec<Box<dyn Widget>>,
31 test_id_value: Option<String>,
33 #[serde(skip)]
35 bounds: Rect,
36}
37
38impl Default for Container {
39 fn default() -> Self {
40 Self {
41 background: None,
42 corner_radius: CornerRadius::ZERO,
43 padding: 0.0,
44 min_width: None,
45 min_height: None,
46 max_width: None,
47 max_height: None,
48 children: Vec::new(),
49 test_id_value: None,
50 bounds: Rect::default(),
51 }
52 }
53}
54
55impl Container {
56 #[must_use]
58 pub fn new() -> Self {
59 Self::default()
60 }
61
62 #[must_use]
64 pub const fn background(mut self, color: Color) -> Self {
65 self.background = Some(color);
66 self
67 }
68
69 #[must_use]
71 pub const fn corner_radius(mut self, radius: CornerRadius) -> Self {
72 self.corner_radius = radius;
73 self
74 }
75
76 #[must_use]
78 pub const fn padding(mut self, padding: f32) -> Self {
79 self.padding = padding;
80 self
81 }
82
83 #[must_use]
85 pub const fn min_width(mut self, width: f32) -> Self {
86 self.min_width = Some(width);
87 self
88 }
89
90 #[must_use]
92 pub const fn min_height(mut self, height: f32) -> Self {
93 self.min_height = Some(height);
94 self
95 }
96
97 #[must_use]
99 pub const fn max_width(mut self, width: f32) -> Self {
100 self.max_width = Some(width);
101 self
102 }
103
104 #[must_use]
106 pub const fn max_height(mut self, height: f32) -> Self {
107 self.max_height = Some(height);
108 self
109 }
110
111 pub fn child(mut self, widget: impl Widget + 'static) -> Self {
113 self.children.push(Box::new(widget));
114 self
115 }
116
117 #[must_use]
119 pub fn with_test_id(mut self, id: impl Into<String>) -> Self {
120 self.test_id_value = Some(id.into());
121 self
122 }
123}
124
125impl Widget for Container {
126 fn type_id(&self) -> TypeId {
127 TypeId::of::<Self>()
128 }
129
130 fn measure(&self, constraints: Constraints) -> Size {
131 let padding2 = self.padding * 2.0;
132
133 let child_constraints = Constraints::new(
135 0.0,
136 (constraints.max_width - padding2).max(0.0),
137 0.0,
138 (constraints.max_height - padding2).max(0.0),
139 );
140
141 let mut child_size = Size::ZERO;
142 for child in &self.children {
143 let size = child.measure(child_constraints);
144 child_size.width = child_size.width.max(size.width);
145 child_size.height = child_size.height.max(size.height);
146 }
147
148 let mut size = Size::new(child_size.width + padding2, child_size.height + padding2);
150
151 if let Some(min_w) = self.min_width {
153 size.width = size.width.max(min_w);
154 }
155 if let Some(min_h) = self.min_height {
156 size.height = size.height.max(min_h);
157 }
158 if let Some(max_w) = self.max_width {
159 size.width = size.width.min(max_w);
160 }
161 if let Some(max_h) = self.max_height {
162 size.height = size.height.min(max_h);
163 }
164
165 constraints.constrain(size)
166 }
167
168 fn layout(&mut self, bounds: Rect) -> LayoutResult {
169 self.bounds = bounds;
170
171 let child_bounds = bounds.inset(self.padding);
173 for child in &mut self.children {
174 child.layout(child_bounds);
175 }
176
177 LayoutResult {
178 size: bounds.size(),
179 }
180 }
181
182 fn paint(&self, canvas: &mut dyn Canvas) {
183 if let Some(color) = self.background {
185 canvas.fill_rect(self.bounds, color);
186 }
187
188 for child in &self.children {
190 child.paint(canvas);
191 }
192 }
193
194 fn event(&mut self, event: &Event) -> Option<Box<dyn Any + Send>> {
195 for child in &mut self.children {
197 if let Some(msg) = child.event(event) {
198 return Some(msg);
199 }
200 }
201 None
202 }
203
204 fn children(&self) -> &[Box<dyn Widget>] {
205 &self.children
206 }
207
208 fn children_mut(&mut self) -> &mut [Box<dyn Widget>] {
209 &mut self.children
210 }
211
212 fn test_id(&self) -> Option<&str> {
213 self.test_id_value.as_deref()
214 }
215}
216
217impl Brick for Container {
219 fn brick_name(&self) -> &'static str {
220 "Container"
221 }
222
223 fn assertions(&self) -> &[BrickAssertion] {
224 &[BrickAssertion::MaxLatencyMs(16)]
225 }
226
227 fn budget(&self) -> BrickBudget {
228 BrickBudget::uniform(16)
229 }
230
231 fn verify(&self) -> BrickVerification {
232 BrickVerification {
233 passed: self.assertions().to_vec(),
234 failed: vec![],
235 verification_time: Duration::from_micros(10),
236 }
237 }
238
239 fn to_html(&self) -> String {
240 let test_id = self.test_id_value.as_deref().unwrap_or("container");
241 format!(r#"<div class="brick-container" data-testid="{test_id}"></div>"#)
242 }
243
244 fn to_css(&self) -> String {
245 ".brick-container { display: block; }".into()
246 }
247
248 fn test_id(&self) -> Option<&str> {
249 self.test_id_value.as_deref()
250 }
251}
252
253#[cfg(test)]
254mod tests {
255 use super::*;
256
257 #[test]
258 fn test_container_default() {
259 let c = Container::new();
260 assert!(c.background.is_none());
261 assert_eq!(c.padding, 0.0);
262 assert!(c.children.is_empty());
263 }
264
265 #[test]
266 fn test_container_builder() {
267 let c = Container::new()
268 .background(Color::WHITE)
269 .padding(10.0)
270 .min_width(100.0)
271 .with_test_id("my-container");
272
273 assert_eq!(c.background, Some(Color::WHITE));
274 assert_eq!(c.padding, 10.0);
275 assert_eq!(c.min_width, Some(100.0));
276 assert_eq!(Widget::test_id(&c), Some("my-container"));
277 }
278
279 #[test]
280 fn test_container_measure_empty() {
281 let c = Container::new().padding(10.0);
282 let size = c.measure(Constraints::loose(Size::new(100.0, 100.0)));
283 assert_eq!(size, Size::new(20.0, 20.0)); }
285
286 #[test]
287 fn test_container_measure_with_min_size() {
288 let c = Container::new().min_width(50.0).min_height(50.0);
289 let size = c.measure(Constraints::loose(Size::new(100.0, 100.0)));
290 assert_eq!(size, Size::new(50.0, 50.0));
291 }
292
293 #[test]
294 fn test_container_measure_with_max_size() {
295 let c = Container::new()
296 .max_width(30.0)
297 .max_height(30.0)
298 .min_width(100.0);
299 let size = c.measure(Constraints::loose(Size::new(200.0, 200.0)));
300 assert_eq!(size.width, 30.0); }
302
303 #[test]
304 fn test_container_corner_radius() {
305 let c = Container::new().corner_radius(CornerRadius::uniform(8.0));
306 assert_eq!(c.corner_radius, CornerRadius::uniform(8.0));
307 }
308
309 #[test]
310 fn test_container_type_id() {
311 let c = Container::new();
312 assert_eq!(Widget::type_id(&c), TypeId::of::<Container>());
313 }
314
315 #[test]
316 fn test_container_layout_sets_bounds() {
317 let mut c = Container::new().padding(10.0);
318 let result = c.layout(Rect::new(0.0, 0.0, 100.0, 80.0));
319 assert_eq!(result.size, Size::new(100.0, 80.0));
320 assert_eq!(c.bounds, Rect::new(0.0, 0.0, 100.0, 80.0));
321 }
322
323 #[test]
324 fn test_container_children_empty() {
325 let c = Container::new();
326 assert!(c.children().is_empty());
327 }
328
329 #[test]
330 fn test_container_event_no_children_returns_none() {
331 let mut c = Container::new();
332 c.layout(Rect::new(0.0, 0.0, 100.0, 100.0));
333 let result = c.event(&Event::MouseEnter);
334 assert!(result.is_none());
335 }
336
337 use presentar_core::draw::DrawCommand;
339 use presentar_core::RecordingCanvas;
340
341 #[test]
342 fn test_container_paint_no_background() {
343 let mut c = Container::new();
344 c.layout(Rect::new(0.0, 0.0, 100.0, 100.0));
345 let mut canvas = RecordingCanvas::new();
346 c.paint(&mut canvas);
347 assert_eq!(canvas.command_count(), 0);
348 }
349
350 #[test]
351 fn test_container_paint_with_background() {
352 let mut c = Container::new().background(Color::RED);
353 c.layout(Rect::new(0.0, 0.0, 100.0, 50.0));
354 let mut canvas = RecordingCanvas::new();
355 c.paint(&mut canvas);
356 assert_eq!(canvas.command_count(), 1);
357 match &canvas.commands()[0] {
358 DrawCommand::Rect { bounds, style, .. } => {
359 assert_eq!(bounds.width, 100.0);
360 assert_eq!(bounds.height, 50.0);
361 assert_eq!(style.fill, Some(Color::RED));
362 }
363 _ => panic!("Expected Rect"),
364 }
365 }
366
367 #[test]
372 fn test_container_min_height_builder() {
373 let c = Container::new().min_height(75.0);
374 assert_eq!(c.min_height, Some(75.0));
375 }
376
377 #[test]
378 fn test_container_max_height_builder() {
379 let c = Container::new().max_height(150.0);
380 assert_eq!(c.max_height, Some(150.0));
381 }
382
383 #[test]
384 fn test_container_max_width_builder() {
385 let c = Container::new().max_width(200.0);
386 assert_eq!(c.max_width, Some(200.0));
387 }
388
389 #[test]
390 fn test_container_all_constraints() {
391 let c = Container::new()
392 .min_width(50.0)
393 .max_width(200.0)
394 .min_height(30.0)
395 .max_height(150.0);
396 assert_eq!(c.min_width, Some(50.0));
397 assert_eq!(c.max_width, Some(200.0));
398 assert_eq!(c.min_height, Some(30.0));
399 assert_eq!(c.max_height, Some(150.0));
400 }
401
402 #[test]
403 fn test_container_chained_all_builders() {
404 let c = Container::new()
405 .background(Color::BLUE)
406 .corner_radius(CornerRadius::uniform(10.0))
407 .padding(5.0)
408 .min_width(100.0)
409 .min_height(80.0)
410 .max_width(300.0)
411 .max_height(200.0)
412 .with_test_id("full-container");
413
414 assert_eq!(c.background, Some(Color::BLUE));
415 assert_eq!(c.corner_radius, CornerRadius::uniform(10.0));
416 assert_eq!(c.padding, 5.0);
417 assert_eq!(c.min_width, Some(100.0));
418 assert_eq!(c.min_height, Some(80.0));
419 assert_eq!(c.max_width, Some(300.0));
420 assert_eq!(c.max_height, Some(200.0));
421 assert_eq!(Widget::test_id(&c), Some("full-container"));
422 }
423
424 #[test]
429 fn test_container_measure_tight_constraints() {
430 let c = Container::new().padding(10.0);
431 let size = c.measure(Constraints::tight(Size::new(50.0, 50.0)));
432 assert_eq!(size, Size::new(50.0, 50.0));
434 }
435
436 #[test]
437 fn test_container_measure_unbounded() {
438 let c = Container::new().min_width(100.0).min_height(50.0);
439 let size = c.measure(Constraints::unbounded());
440 assert_eq!(size, Size::new(100.0, 50.0));
441 }
442
443 #[test]
444 fn test_container_measure_min_overrides_content() {
445 let c = Container::new().min_width(200.0).min_height(200.0);
446 let size = c.measure(Constraints::loose(Size::new(500.0, 500.0)));
447 assert!(size.width >= 200.0);
448 assert!(size.height >= 200.0);
449 }
450
451 #[test]
452 fn test_container_measure_max_clamps() {
453 let c = Container::new().min_width(300.0).max_width(150.0); let size = c.measure(Constraints::loose(Size::new(500.0, 500.0)));
455 assert_eq!(size.width, 150.0);
457 }
458
459 #[test]
460 fn test_container_measure_padding_only() {
461 let c = Container::new().padding(25.0);
462 let size = c.measure(Constraints::loose(Size::new(100.0, 100.0)));
463 assert_eq!(size, Size::new(50.0, 50.0)); }
465
466 #[test]
471 fn test_container_layout_with_offset() {
472 let mut c = Container::new();
473 let result = c.layout(Rect::new(20.0, 30.0, 100.0, 80.0));
474 assert_eq!(result.size, Size::new(100.0, 80.0));
475 assert_eq!(c.bounds.x, 20.0);
476 assert_eq!(c.bounds.y, 30.0);
477 }
478
479 #[test]
480 fn test_container_layout_zero_size() {
481 let mut c = Container::new();
482 let result = c.layout(Rect::new(0.0, 0.0, 0.0, 0.0));
483 assert_eq!(result.size, Size::new(0.0, 0.0));
484 }
485
486 #[test]
487 fn test_container_layout_large_bounds() {
488 let mut c = Container::new();
489 let result = c.layout(Rect::new(0.0, 0.0, 10000.0, 10000.0));
490 assert_eq!(result.size, Size::new(10000.0, 10000.0));
491 }
492
493 #[test]
498 fn test_container_children_mut_access() {
499 let mut c = Container::new();
500 assert!(c.children_mut().is_empty());
501 }
502
503 #[test]
508 fn test_container_test_id_none_by_default() {
509 let c = Container::new();
510 assert!(Widget::test_id(&c).is_none());
511 }
512
513 #[test]
514 fn test_container_test_id_with_str() {
515 let c = Container::new().with_test_id("simple-id");
516 assert_eq!(Widget::test_id(&c), Some("simple-id"));
517 }
518
519 #[test]
520 fn test_container_test_id_with_string() {
521 let id = String::from("dynamic-id");
522 let c = Container::new().with_test_id(id);
523 assert_eq!(Widget::test_id(&c), Some("dynamic-id"));
524 }
525
526 #[test]
531 fn test_container_corner_radius_zero() {
532 let c = Container::new().corner_radius(CornerRadius::ZERO);
533 assert_eq!(c.corner_radius, CornerRadius::ZERO);
534 }
535
536 #[test]
537 fn test_container_corner_radius_asymmetric() {
538 let radius = CornerRadius {
539 top_left: 5.0,
540 top_right: 10.0,
541 bottom_left: 15.0,
542 bottom_right: 20.0,
543 };
544 let c = Container::new().corner_radius(radius);
545 assert_eq!(c.corner_radius.top_left, 5.0);
546 assert_eq!(c.corner_radius.bottom_right, 20.0);
547 }
548
549 #[test]
554 fn test_container_default_all_none() {
555 let c = Container::default();
556 assert!(c.background.is_none());
557 assert!(c.min_width.is_none());
558 assert!(c.min_height.is_none());
559 assert!(c.max_width.is_none());
560 assert!(c.max_height.is_none());
561 assert!(c.test_id_value.is_none());
562 }
563
564 #[test]
565 fn test_container_default_corner_radius_zero() {
566 let c = Container::default();
567 assert_eq!(c.corner_radius, CornerRadius::ZERO);
568 }
569
570 #[test]
571 fn test_container_default_bounds_zero() {
572 let c = Container::default();
573 assert_eq!(c.bounds, Rect::default());
574 }
575
576 #[test]
581 fn test_container_serialize() {
582 let c = Container::new()
583 .background(Color::GREEN)
584 .padding(15.0)
585 .min_width(100.0);
586 let json = serde_json::to_string(&c).unwrap();
587 assert!(json.contains("background"));
588 assert!(json.contains("padding"));
589 assert!(json.contains("15"));
590 }
591
592 #[test]
593 fn test_container_deserialize() {
594 let json = r##"{"background":{"r":1.0,"g":0.0,"b":0.0,"a":1.0},"corner_radius":{"top_left":0.0,"top_right":0.0,"bottom_left":0.0,"bottom_right":0.0},"padding":10.0,"min_width":50.0,"min_height":null,"max_width":null,"max_height":null,"test_id_value":null}"##;
595 let c: Container = serde_json::from_str(json).unwrap();
596 assert_eq!(c.padding, 10.0);
597 assert_eq!(c.min_width, Some(50.0));
598 }
599
600 #[test]
601 fn test_container_roundtrip_serialization() {
602 let original = Container::new()
603 .background(Color::BLUE)
604 .padding(20.0)
605 .min_width(75.0)
606 .max_height(300.0);
607 let json = serde_json::to_string(&original).unwrap();
608 let deserialized: Container = serde_json::from_str(&json).unwrap();
609 assert_eq!(original.padding, deserialized.padding);
610 assert_eq!(original.min_width, deserialized.min_width);
611 assert_eq!(original.max_height, deserialized.max_height);
612 assert_eq!(original.background, deserialized.background);
613 }
614
615 #[test]
620 fn test_container_paint_transparent_background() {
621 let mut c = Container::new().background(Color::TRANSPARENT);
622 c.layout(Rect::new(0.0, 0.0, 100.0, 100.0));
623 let mut canvas = RecordingCanvas::new();
624 c.paint(&mut canvas);
625 assert_eq!(canvas.command_count(), 1);
627 }
628
629 #[test]
630 fn test_container_paint_after_layout() {
631 let mut c = Container::new().background(Color::WHITE);
632 c.layout(Rect::new(50.0, 50.0, 80.0, 60.0));
634 let mut canvas = RecordingCanvas::new();
635 c.paint(&mut canvas);
636 match &canvas.commands()[0] {
637 DrawCommand::Rect { bounds, .. } => {
638 assert_eq!(bounds.x, 50.0);
639 assert_eq!(bounds.y, 50.0);
640 assert_eq!(bounds.width, 80.0);
641 assert_eq!(bounds.height, 60.0);
642 }
643 _ => panic!("Expected Rect"),
644 }
645 }
646
647 #[test]
652 fn test_container_zero_padding_measure() {
653 let c = Container::new().padding(0.0);
654 let size = c.measure(Constraints::loose(Size::new(100.0, 100.0)));
655 assert_eq!(size, Size::new(0.0, 0.0)); }
657
658 #[test]
659 fn test_container_negative_constraints_handled() {
660 let c = Container::new().padding(10.0);
662 let size = c.measure(Constraints::new(0.0, 5.0, 0.0, 5.0));
663 assert_eq!(size, Size::new(5.0, 5.0));
665 }
666}