1use std::ops::Range;
2
3use crate::{
4 geometry::{rect::RectF, vector::Vector2F},
5 json, AnyElement, Element, SizeConstraint, ViewContext,
6};
7use serde_json::json;
8
9pub struct Expanded<V> {
10 child: AnyElement<V>,
11 full_width: bool,
12 full_height: bool,
13}
14
15impl<V: 'static> 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: 'static> 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 ViewContext<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 bounds: RectF,
60 visible_bounds: RectF,
61 _: &mut Self::LayoutState,
62 view: &mut V,
63 cx: &mut ViewContext<V>,
64 ) -> Self::PaintState {
65 self.child.paint(bounds.origin(), visible_bounds, view, cx);
66 }
67
68 fn rect_for_text_range(
69 &self,
70 range_utf16: Range<usize>,
71 _: RectF,
72 _: RectF,
73 _: &Self::LayoutState,
74 _: &Self::PaintState,
75 view: &V,
76 cx: &ViewContext<V>,
77 ) -> Option<RectF> {
78 self.child.rect_for_text_range(range_utf16, view, cx)
79 }
80
81 fn debug(
82 &self,
83 _: RectF,
84 _: &Self::LayoutState,
85 _: &Self::PaintState,
86 view: &V,
87 cx: &ViewContext<V>,
88 ) -> json::Value {
89 json!({
90 "type": "Expanded",
91 "full_width": self.full_width,
92 "full_height": self.full_height,
93 "child": self.child.debug(view, cx)
94 })
95 }
96}