1use std::{any::Any, f32::INFINITY};
2
3use crate::{
4 json::{self, ToJson, Value},
5 Axis, DebugContext, Element, ElementBox, Event, EventContext, LayoutContext, PaintContext,
6 SizeConstraint, Vector2FExt,
7};
8use pathfinder_geometry::{
9 rect::RectF,
10 vector::{vec2f, Vector2F},
11};
12use serde_json::json;
13
14pub struct Flex {
15 axis: Axis,
16 children: Vec<ElementBox>,
17}
18
19impl Flex {
20 pub fn new(axis: Axis) -> Self {
21 Self {
22 axis,
23 children: Default::default(),
24 }
25 }
26
27 pub fn row() -> Self {
28 Self::new(Axis::Horizontal)
29 }
30
31 pub fn column() -> Self {
32 Self::new(Axis::Vertical)
33 }
34
35 fn child_flex<'b>(child: &ElementBox) -> Option<f32> {
36 child
37 .metadata()
38 .and_then(|d| d.downcast_ref::<FlexParentData>())
39 .map(|data| data.flex)
40 }
41}
42
43impl Extend<ElementBox> for Flex {
44 fn extend<T: IntoIterator<Item = ElementBox>>(&mut self, children: T) {
45 self.children.extend(children);
46 }
47}
48
49impl Element for Flex {
50 type LayoutState = ();
51 type PaintState = ();
52
53 fn layout(
54 &mut self,
55 constraint: SizeConstraint,
56 cx: &mut LayoutContext,
57 ) -> (Vector2F, Self::LayoutState) {
58 let mut total_flex = 0.0;
59 let mut fixed_space = 0.0;
60
61 let cross_axis = self.axis.invert();
62 let mut cross_axis_max: f32 = 0.0;
63 for child in &mut self.children {
64 if let Some(flex) = Self::child_flex(&child) {
65 total_flex += flex;
66 } else {
67 let child_constraint = match self.axis {
68 Axis::Horizontal => SizeConstraint::new(
69 vec2f(0.0, constraint.min.y()),
70 vec2f(INFINITY, constraint.max.y()),
71 ),
72 Axis::Vertical => SizeConstraint::new(
73 vec2f(constraint.min.x(), 0.0),
74 vec2f(constraint.max.x(), INFINITY),
75 ),
76 };
77 let size = child.layout(child_constraint, cx);
78 fixed_space += size.along(self.axis);
79 cross_axis_max = cross_axis_max.max(size.along(cross_axis));
80 }
81 }
82
83 let mut size = if total_flex > 0.0 {
84 if constraint.max_along(self.axis).is_infinite() {
85 panic!("flex contains flexible children but has an infinite constraint along the flex axis");
86 }
87
88 let mut remaining_space = constraint.max_along(self.axis) - fixed_space;
89 let mut remaining_flex = total_flex;
90 for child in &mut self.children {
91 if let Some(flex) = Self::child_flex(&child) {
92 let child_max = if remaining_flex == 0.0 {
93 remaining_space
94 } else {
95 let space_per_flex = remaining_space / remaining_flex;
96 space_per_flex * flex
97 };
98 let child_constraint = match self.axis {
99 Axis::Horizontal => SizeConstraint::new(
100 vec2f(0.0, constraint.min.y()),
101 vec2f(child_max, constraint.max.y()),
102 ),
103 Axis::Vertical => SizeConstraint::new(
104 vec2f(constraint.min.x(), 0.0),
105 vec2f(constraint.max.x(), child_max),
106 ),
107 };
108 let child_size = child.layout(child_constraint, cx);
109 remaining_space -= child_size.along(self.axis);
110 remaining_flex -= flex;
111 cross_axis_max = cross_axis_max.max(child_size.along(cross_axis));
112 }
113 }
114
115 match self.axis {
116 Axis::Horizontal => vec2f(constraint.max.x() - remaining_space, cross_axis_max),
117 Axis::Vertical => vec2f(cross_axis_max, constraint.max.y() - remaining_space),
118 }
119 } else {
120 match self.axis {
121 Axis::Horizontal => vec2f(fixed_space, cross_axis_max),
122 Axis::Vertical => vec2f(cross_axis_max, fixed_space),
123 }
124 };
125
126 if constraint.min.x().is_finite() {
127 size.set_x(size.x().max(constraint.min.x()));
128 }
129
130 if constraint.min.y().is_finite() {
131 size.set_y(size.y().max(constraint.min.y()));
132 }
133
134 (size, ())
135 }
136
137 fn paint(
138 &mut self,
139 bounds: RectF,
140 _: &mut Self::LayoutState,
141 cx: &mut PaintContext,
142 ) -> Self::PaintState {
143 let mut child_origin = bounds.origin();
144 for child in &mut self.children {
145 child.paint(child_origin, cx);
146 match self.axis {
147 Axis::Horizontal => child_origin += vec2f(child.size().x(), 0.0),
148 Axis::Vertical => child_origin += vec2f(0.0, child.size().y()),
149 }
150 }
151 }
152
153 fn dispatch_event(
154 &mut self,
155 event: &Event,
156 _: RectF,
157 _: &mut Self::LayoutState,
158 _: &mut Self::PaintState,
159 cx: &mut EventContext,
160 ) -> bool {
161 let mut handled = false;
162 for child in &mut self.children {
163 handled = child.dispatch_event(event, cx) || handled;
164 }
165 handled
166 }
167
168 fn debug(
169 &self,
170 bounds: RectF,
171 _: &Self::LayoutState,
172 _: &Self::PaintState,
173 cx: &DebugContext,
174 ) -> json::Value {
175 json!({
176 "type": "Flex",
177 "bounds": bounds.to_json(),
178 "axis": self.axis.to_json(),
179 "children": self.children.iter().map(|child| child.debug(cx)).collect::<Vec<json::Value>>()
180 })
181 }
182}
183
184struct FlexParentData {
185 flex: f32,
186}
187
188pub struct Expanded {
189 metadata: FlexParentData,
190 child: ElementBox,
191}
192
193impl Expanded {
194 pub fn new(flex: f32, child: ElementBox) -> Self {
195 Expanded {
196 metadata: FlexParentData { flex },
197 child,
198 }
199 }
200}
201
202impl Element for Expanded {
203 type LayoutState = ();
204 type PaintState = ();
205
206 fn layout(
207 &mut self,
208 constraint: SizeConstraint,
209 cx: &mut LayoutContext,
210 ) -> (Vector2F, Self::LayoutState) {
211 let size = self.child.layout(constraint, cx);
212 (size, ())
213 }
214
215 fn paint(
216 &mut self,
217 bounds: RectF,
218 _: &mut Self::LayoutState,
219 cx: &mut PaintContext,
220 ) -> Self::PaintState {
221 self.child.paint(bounds.origin(), cx)
222 }
223
224 fn dispatch_event(
225 &mut self,
226 event: &Event,
227 _: RectF,
228 _: &mut Self::LayoutState,
229 _: &mut Self::PaintState,
230 cx: &mut EventContext,
231 ) -> bool {
232 self.child.dispatch_event(event, cx)
233 }
234
235 fn metadata(&self) -> Option<&dyn Any> {
236 Some(&self.metadata)
237 }
238
239 fn debug(
240 &self,
241 _: RectF,
242 _: &Self::LayoutState,
243 _: &Self::PaintState,
244 cx: &DebugContext,
245 ) -> Value {
246 json!({
247 "type": "Expanded",
248 "flex": self.metadata.flex,
249 "child": self.child.debug(cx)
250 })
251 }
252}