1mod common {
2 pub mod image;
3 pub mod text;
4}
5use common::image::{image_measure_function, ImageContext};
6use common::text::{text_measure_function, FontMetrics, TextContext, WritingMode, LOREM_IPSUM};
7use takia::prelude::*;
8
9enum NodeContext {
10 Text(TextContext),
11 Image(ImageContext),
12}
13
14fn measure_function(
15 known_dimensions: takia::geometry::Size<Option<f32>>,
16 available_space: takia::geometry::Size<takia::style::AvailableSpace>,
17 node_context: Option<&mut NodeContext>,
18 font_metrics: &FontMetrics,
19) -> Size<f32> {
20 if let Size { width: Some(width), height: Some(height) } = known_dimensions {
21 return Size { width, height };
22 }
23
24 match node_context {
25 None => Size::ZERO,
26 Some(NodeContext::Text(text_context)) => {
27 text_measure_function(known_dimensions, available_space, &*text_context, font_metrics)
28 }
29 Some(NodeContext::Image(image_context)) => image_measure_function(known_dimensions, image_context),
30 }
31}
32
33fn main() -> Result<(), takia::TakiaError> {
34 let mut takia: TakiaTree<NodeContext> = TakiaTree::new();
35
36 let font_metrics = FontMetrics { char_width: 10.0, char_height: 10.0 };
37
38 let text_node = takia.new_leaf_with_context(
39 Style::default(),
40 NodeContext::Text(TextContext { text_content: LOREM_IPSUM.into(), writing_mode: WritingMode::Horizontal }),
41 )?;
42
43 let image_node = takia
44 .new_leaf_with_context(Style::default(), NodeContext::Image(ImageContext { width: 400.0, height: 300.0 }))?;
45
46 let root = takia.new_with_children(
47 Style {
48 display: Display::Flex,
49 flex_direction: FlexDirection::Column,
50 size: Size { width: length(200.0), height: auto() },
51 ..Default::default()
52 },
53 &[text_node, image_node],
54 )?;
55
56 takia.compute_layout_with_measure(
58 root,
59 Size::MAX_CONTENT,
60 |inputs, _node_id, node_context, style| {
63 takia::compute_leaf_layout(
64 inputs,
65 style,
66 |_, _| 0.0,
67 |known_dimensions, available_space| {
68 measure_function(known_dimensions, available_space, node_context, &font_metrics)
69 },
70 )
71 },
72 )?;
73 takia.print_tree(root);
74
75 Ok(())
76}