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