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