1use crate::{
2 color::Color,
3 font_cache::FamilyId,
4 fonts::{FontId, TextStyle},
5 geometry::{
6 rect::RectF,
7 vector::{vec2f, Vector2F},
8 },
9 json::{ToJson, Value},
10 text_layout::Line,
11 DebugContext, Element, Event, EventContext, FontCache, LayoutContext, PaintContext,
12 SizeConstraint,
13};
14use serde::Deserialize;
15use serde_json::json;
16use smallvec::{smallvec, SmallVec};
17
18pub struct Text {
19 text: String,
20 family_id: FamilyId,
21 font_size: f32,
22 style: TextStyle,
23}
24
25impl Text {
26 pub fn new(text: String, family_id: FamilyId, font_size: f32) -> Self {
27 Self {
28 text,
29 family_id,
30 font_size,
31 style: Default::default(),
32 }
33 }
34
35 pub fn with_style(mut self, style: &TextStyle) -> Self {
36 self.style = style.clone();
37 self
38 }
39
40 pub fn with_default_color(mut self, color: Color) -> Self {
41 self.style.color = color;
42 self
43 }
44}
45
46impl Element for Text {
47 type LayoutState = Line;
48 type PaintState = ();
49
50 fn layout(
51 &mut self,
52 constraint: SizeConstraint,
53 cx: &mut LayoutContext,
54 ) -> (Vector2F, Self::LayoutState) {
55 let font_id = cx
56 .font_cache
57 .select_font(self.family_id, &self.style.font_properties)
58 .unwrap();
59 todo!()
60 // let line =
61 // cx.text_layout_cache
62 // .layout_str(self.text.as_str(), self.font_size, runs.as_slice());
63
64 // let size = vec2f(
65 // line.width().max(constraint.min.x()).min(constraint.max.x()),
66 // cx.font_cache.line_height(font_id, self.font_size).ceil(),
67 // );
68
69 // (size, line)
70 }
71
72 fn paint(
73 &mut self,
74 bounds: RectF,
75 line: &mut Self::LayoutState,
76 cx: &mut PaintContext,
77 ) -> Self::PaintState {
78 line.paint(
79 bounds.origin(),
80 RectF::new(vec2f(0., 0.), bounds.size()),
81 cx,
82 )
83 }
84
85 fn dispatch_event(
86 &mut self,
87 _: &Event,
88 _: RectF,
89 _: &mut Self::LayoutState,
90 _: &mut Self::PaintState,
91 _: &mut EventContext,
92 ) -> bool {
93 false
94 }
95
96 fn debug(
97 &self,
98 bounds: RectF,
99 _: &Self::LayoutState,
100 _: &Self::PaintState,
101 cx: &DebugContext,
102 ) -> Value {
103 json!({
104 "type": "Label",
105 "bounds": bounds.to_json(),
106 "text": &self.text,
107 "font_family": cx.font_cache.family_name(self.family_id).unwrap(),
108 "font_size": self.font_size,
109 "style": self.style.to_json(),
110 })
111 }
112}