1use std::fmt;
4use std::rc::Rc;
5
6use waft_foundation::App;
7use waft_rendering::BoxConstraints;
8
9use crate::framework::{BuildContext, KeyRef, StatelessWidget, WidgetRef};
10use crate::widgets::layout_builder::LayoutBuilder;
11use crate::widgets::media_query::{MediaQuery, Orientation};
12
13pub type OrientationWidgetBuilder = Rc<dyn Fn(&mut App, BuildContext, Orientation) -> WidgetRef>;
16
17pub struct OrientationBuilder {
23 pub key: Option<KeyRef>,
24 pub builder: OrientationWidgetBuilder,
26}
27
28impl OrientationBuilder {
29 pub fn new(
31 builder: impl Fn(&mut App, BuildContext, Orientation) -> WidgetRef + 'static,
32 ) -> OrientationBuilder {
33 OrientationBuilder {
34 key: None,
35 builder: Rc::new(builder),
36 }
37 }
38
39 pub fn key(mut self, key: KeyRef) -> OrientationBuilder {
41 self.key = Some(key);
42 self
43 }
44}
45
46fn orientation_of_constraints(constraints: BoxConstraints) -> Orientation {
49 if constraints.max_width > constraints.max_height {
50 Orientation::Landscape
51 } else {
52 Orientation::Portrait
53 }
54}
55
56impl StatelessWidget for OrientationBuilder {
57 fn key(&self) -> Option<&KeyRef> {
58 self.key.as_ref()
59 }
60
61 fn build(&self, _app: &mut App, _context: BuildContext) -> WidgetRef {
62 let builder = Rc::clone(&self.builder);
63 LayoutBuilder::new(move |app, context, constraints| {
64 builder(
65 app,
66 context,
67 orientation_of_constraints(constraints.constraints()),
68 )
69 })
70 .into_widget()
71 }
72}
73
74impl fmt::Debug for OrientationBuilder {
75 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
76 f.debug_struct("OrientationBuilder")
77 .field("key", &self.key)
78 .finish_non_exhaustive()
79 }
80}
81
82pub struct DeviceOrientationBuilder {
88 pub key: Option<KeyRef>,
89 pub builder: OrientationWidgetBuilder,
91}
92
93impl DeviceOrientationBuilder {
94 pub fn new(
96 builder: impl Fn(&mut App, BuildContext, Orientation) -> WidgetRef + 'static,
97 ) -> DeviceOrientationBuilder {
98 DeviceOrientationBuilder {
99 key: None,
100 builder: Rc::new(builder),
101 }
102 }
103
104 pub fn key(mut self, key: KeyRef) -> DeviceOrientationBuilder {
106 self.key = Some(key);
107 self
108 }
109}
110
111impl StatelessWidget for DeviceOrientationBuilder {
112 fn key(&self) -> Option<&KeyRef> {
113 self.key.as_ref()
114 }
115
116 fn build(&self, app: &mut App, context: BuildContext) -> WidgetRef {
117 let orientation = MediaQuery::orientation_of(app, context);
118 (self.builder)(app, context, orientation)
119 }
120}
121
122impl fmt::Debug for DeviceOrientationBuilder {
123 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
124 f.debug_struct("DeviceOrientationBuilder")
125 .field("key", &self.key)
126 .finish_non_exhaustive()
127 }
128}
129
130#[cfg(test)]
131mod tests {
132 use std::cell::Cell;
133
134 use waft_embedder::{Size, TextDirection};
135
136 use super::*;
137 use crate::framework::IntoWidget;
138 use crate::testing::{WidgetTester, find, test_widgets};
139 use crate::widgets::basic::{Center, Directionality, SizedBox};
140 use crate::widgets::container::Container;
141 use crate::widgets::media_query::MediaQueryData;
142 use crate::widgets::text::Text;
143
144 fn pump_sized(
147 tester: &mut WidgetTester,
148 media_size: Size,
149 size: Size,
150 device: bool,
151 ) -> Option<Orientation> {
152 let seen: Rc<Cell<Option<Orientation>>> = Rc::default();
153 let record = {
154 let seen = Rc::clone(&seen);
155 move |_: &mut App, _: BuildContext, orientation: Orientation| {
156 seen.set(Some(orientation));
157 Container::new().into_widget()
158 }
159 };
160 let builder = if device {
161 DeviceOrientationBuilder::new(record).into_widget()
162 } else {
163 OrientationBuilder::new(record).into_widget()
164 };
165 tester.pump_widget(
166 MediaQuery::new(
167 MediaQueryData::new().size(media_size),
168 Center::new().child(
169 SizedBox::new()
170 .width(size.width())
171 .height(size.height())
172 .child(builder),
173 ),
174 )
175 .into_widget(),
176 );
177 seen.get()
178 }
179
180 #[test]
181 fn orientation_builder_determines_orientation_from_constraints() {
182 test_widgets(|tester| {
183 let orientation = pump_sized(
185 tester,
186 Size::new(800.0, 600.0),
187 Size::new(100.0, 200.0),
188 false,
189 );
190 assert_eq!(orientation, Some(Orientation::Portrait));
191 });
192 }
193
194 #[test]
195 fn orientation_builder_reports_landscape_when_width_is_greater_than_height() {
196 test_widgets(|tester| {
197 let orientation = pump_sized(
198 tester,
199 Size::new(600.0, 800.0),
200 Size::new(200.0, 100.0),
201 false,
202 );
203 assert_eq!(orientation, Some(Orientation::Landscape));
204 });
205 }
206
207 #[test]
208 fn orientation_builder_rebuilds_when_constraints_change() {
209 test_widgets(|tester| {
210 let media_size = Size::new(800.0, 600.0);
211 let portrait = pump_sized(tester, media_size, Size::new(100.0, 200.0), false);
212 assert_eq!(portrait, Some(Orientation::Portrait));
213 let landscape = pump_sized(tester, media_size, Size::new(200.0, 100.0), false);
214 assert_eq!(landscape, Some(Orientation::Landscape));
215 });
216 }
217
218 #[test]
219 fn orientation_builder_does_not_crash_at_zero_area() {
220 test_widgets(|tester| {
221 tester.pump_widget(Directionality::new(
222 TextDirection::Ltr,
223 Center::new().child(SizedBox::shrink().child(OrientationBuilder::new(
224 |_, _, _| Text::new("X").into_widget(),
225 ))),
226 ));
227 assert_eq!(
228 tester.get_size(find::by_type::<OrientationBuilder>()),
229 Size::ZERO
230 );
231 });
232 }
233
234 #[test]
235 fn device_orientation_builder_does_not_crash_at_zero_area() {
236 test_widgets(|tester| {
237 tester.pump_widget(Directionality::new(
238 TextDirection::Ltr,
239 Center::new().child(SizedBox::shrink().child(DeviceOrientationBuilder::new(
240 |_, _, _| Text::new("X").into_widget(),
241 ))),
242 ));
243 assert_eq!(
244 tester.get_size(find::by_type::<DeviceOrientationBuilder>()),
245 Size::ZERO
246 );
247 });
248 }
249
250 #[test]
251 fn device_orientation_builder_uses_media_query_orientation() {
252 test_widgets(|tester| {
253 let orientation = pump_sized(
255 tester,
256 Size::new(800.0, 600.0),
257 Size::new(100.0, 200.0),
258 true,
259 );
260 assert_eq!(orientation, Some(Orientation::Landscape));
261 });
262 }
263
264 #[test]
265 fn device_orientation_builder_reports_portrait_when_device_is_portrait() {
266 test_widgets(|tester| {
267 let orientation = pump_sized(
269 tester,
270 Size::new(600.0, 800.0),
271 Size::new(200.0, 100.0),
272 true,
273 );
274 assert_eq!(orientation, Some(Orientation::Portrait));
275 });
276 }
277
278 #[test]
279 fn device_orientation_builder_rebuilds_when_media_query_orientation_changes() {
280 test_widgets(|tester| {
281 let seen: Rc<Cell<Option<Orientation>>> = Rc::default();
282 let pump = |tester: &mut WidgetTester, size: Size| {
283 let seen = Rc::clone(&seen);
284 tester.pump_widget(
285 MediaQuery::new(
286 MediaQueryData::new().size(size),
287 DeviceOrientationBuilder::new(move |_, _, orientation| {
288 seen.set(Some(orientation));
289 Container::new().into_widget()
290 }),
291 )
292 .into_widget(),
293 );
294 };
295 pump(tester, Size::new(600.0, 800.0));
296 assert_eq!(seen.get(), Some(Orientation::Portrait));
297 pump(tester, Size::new(800.0, 600.0));
298 assert_eq!(seen.get(), Some(Orientation::Landscape));
299 });
300 }
301
302 #[test]
303 fn device_orientation_builder_differs_from_orientation_builder() {
304 use crate::widgets::basic::{Column, Expanded};
305
306 test_widgets(|tester| {
307 let layout: Rc<Cell<Option<Orientation>>> = Rc::default();
308 let device: Rc<Cell<Option<Orientation>>> = Rc::default();
309 let recording = |seen: &Rc<Cell<Option<Orientation>>>| {
310 let seen = Rc::clone(seen);
311 move |_: &mut App, _: BuildContext, orientation: Orientation| {
312 seen.set(Some(orientation));
313 Container::new().into_widget()
314 }
315 };
316 tester.pump_widget(
318 MediaQuery::new(
319 MediaQueryData::new().size(Size::new(800.0, 600.0)),
320 Center::new().child(SizedBox::new().width(100.0).height(200.0).child(
321 Column::new().children(vec![
322 Expanded::new(OrientationBuilder::new(recording(&layout))).into_widget(),
323 Expanded::new(DeviceOrientationBuilder::new(recording(&device)))
324 .into_widget(),
325 ]),
326 )),
327 )
328 .into_widget(),
329 );
330 assert_eq!(
331 layout.get(),
332 Some(Orientation::Portrait),
333 "OrientationBuilder should use widget constraints"
334 );
335 assert_eq!(
336 device.get(),
337 Some(Orientation::Landscape),
338 "DeviceOrientationBuilder should use MediaQuery orientation"
339 );
340 });
341 }
342}