1use crate::{
2 fonts::TextStyle,
3 geometry::{
4 rect::RectF,
5 vector::{vec2f, Vector2F},
6 },
7 json::{json, ToJson},
8 DebugContext, Element, ElementBox, Event, EventContext, LayoutContext, PaintContext,
9 SizeConstraint,
10};
11
12pub struct LineBox {
13 child: ElementBox,
14 style: TextStyle,
15}
16
17impl LineBox {
18 pub fn new(child: ElementBox, style: TextStyle) -> Self {
19 Self { child, style }
20 }
21}
22
23impl Element for LineBox {
24 type LayoutState = f32;
25 type PaintState = ();
26
27 fn layout(
28 &mut self,
29 constraint: SizeConstraint,
30 cx: &mut LayoutContext,
31 ) -> (Vector2F, Self::LayoutState) {
32 let line_height = cx
33 .font_cache
34 .line_height(self.style.font_id, self.style.font_size);
35 let character_height = cx
36 .font_cache
37 .ascent(self.style.font_id, self.style.font_size)
38 + cx.font_cache
39 .descent(self.style.font_id, self.style.font_size);
40 let child_max = vec2f(constraint.max.x(), character_height);
41 let child_size = self.child.layout(
42 SizeConstraint::new(constraint.min.min(child_max), child_max),
43 cx,
44 );
45 let size = vec2f(child_size.x(), line_height);
46 (size, (line_height - character_height) / 2.)
47 }
48
49 fn paint(
50 &mut self,
51 bounds: RectF,
52 visible_bounds: RectF,
53 padding_top: &mut f32,
54 cx: &mut PaintContext,
55 ) -> Self::PaintState {
56 self.child.paint(
57 bounds.origin() + vec2f(0., *padding_top),
58 visible_bounds,
59 cx,
60 );
61 }
62
63 fn dispatch_event(
64 &mut self,
65 event: &Event,
66 _: RectF,
67 _: &mut Self::LayoutState,
68 _: &mut Self::PaintState,
69 cx: &mut EventContext,
70 ) -> bool {
71 self.child.dispatch_event(event, cx)
72 }
73
74 fn debug(
75 &self,
76 bounds: RectF,
77 _: &Self::LayoutState,
78 _: &Self::PaintState,
79 cx: &DebugContext,
80 ) -> serde_json::Value {
81 json!({
82 "bounds": bounds.to_json(),
83 "style": self.style.to_json(),
84 "child": self.child.debug(cx),
85 })
86 }
87}