1use crate::{
2 geometry::{rect::RectF, vector::Vector2F},
3 json, DebugContext, Element, ElementBox, Event, EventContext, LayoutContext, PaintContext,
4 SizeConstraint,
5};
6use serde_json::json;
7
8pub struct Expanded {
9 child: ElementBox,
10 full_width: bool,
11 full_height: bool,
12}
13
14impl Expanded {
15 pub fn new(child: ElementBox) -> Self {
16 Self {
17 child,
18 full_width: true,
19 full_height: true,
20 }
21 }
22
23 pub fn to_full_width(mut self) -> Self {
24 self.full_width = true;
25 self.full_height = false;
26 self
27 }
28
29 pub fn to_full_height(mut self) -> Self {
30 self.full_width = false;
31 self.full_height = true;
32 self
33 }
34}
35
36impl Element for Expanded {
37 type LayoutState = ();
38 type PaintState = ();
39
40 fn layout(
41 &mut self,
42 mut constraint: SizeConstraint,
43 cx: &mut LayoutContext,
44 ) -> (Vector2F, Self::LayoutState) {
45 if self.full_width {
46 constraint.min.set_x(constraint.max.x());
47 }
48 if self.full_height {
49 constraint.min.set_y(constraint.max.y());
50 }
51 let size = self.child.layout(constraint, cx);
52 (size, ())
53 }
54
55 fn paint(
56 &mut self,
57 bounds: RectF,
58 visible_bounds: RectF,
59 _: &mut Self::LayoutState,
60 cx: &mut PaintContext,
61 ) -> Self::PaintState {
62 self.child.paint(bounds.origin(), visible_bounds, cx);
63 }
64
65 fn dispatch_event(
66 &mut self,
67 event: &Event,
68 _: RectF,
69 _: RectF,
70 _: &mut Self::LayoutState,
71 _: &mut Self::PaintState,
72 cx: &mut EventContext,
73 ) -> bool {
74 self.child.dispatch_event(event, cx)
75 }
76
77 fn debug(
78 &self,
79 _: RectF,
80 _: &Self::LayoutState,
81 _: &Self::PaintState,
82 cx: &DebugContext,
83 ) -> json::Value {
84 json!({
85 "type": "Expanded",
86 "full_width": self.full_width,
87 "full_height": self.full_height,
88 "child": self.child.debug(cx)
89 })
90 }
91}