1use crate::text;
2use crate::widget::{Widget, WidgetKind};
3
4#[derive(Debug, Clone, Copy, PartialEq)]
5pub enum FlexDir { Row, Column }
6#[derive(Debug, Clone, Copy, PartialEq)]
7pub enum Align { Start, Center, End }
8
9#[derive(Debug, Clone, Copy, Default)]
10pub struct Rect { pub x: f32, pub y: f32, pub w: f32, pub h: f32 }
11
12#[derive(Debug, Clone)]
13pub struct LayoutNode {
14 pub rect: Rect,
15 pub id: Option<String>,
16 pub on_click: Option<String>,
17 pub children: Vec<LayoutNode>,
18 pub enabled: bool,
19 pub focused: bool,
20}
21
22impl Default for LayoutNode {
23 fn default() -> Self {
24 Self { rect: Rect::default(), id: None, on_click: None,
25 children: Vec::new(), enabled: true, focused: false }
26 }
27}
28
29pub fn compute(widget: &Widget, avail_w: f32, avail_h: f32) -> LayoutNode {
32 let mut node = LayoutNode {
33 id: widget.id.clone(), on_click: widget.on_click.clone(),
34 enabled: widget.enabled, focused: widget.focused,
35 ..Default::default()
36 };
37 layout_widget(widget, &mut node, 0.0, 0.0, avail_w, avail_h);
38 node
39}
40
41fn layout_widget(w: &Widget, node: &mut LayoutNode, x: f32, y: f32, max_w: f32, max_h: f32) {
42 let s = &w.style;
43 let has_children = !w.children.is_empty();
44
45 let mut ww = if s.w > 0.0 { s.w.min(max_w) } else { max_w };
47 let mut hh = if s.h > 0.0 { s.h.min(max_h) } else { max_h };
48
49 if !has_children {
50 match &w.kind {
52 WidgetKind::Label(t) | WidgetKind::Button(t) => {
53 let avail_w = (max_w - s.pad[1] - s.pad[3]).max(0.0);
54 if avail_w < 4096.0 {
56 ww = (avail_w + s.pad[1] + s.pad[3]).max(s.min_w).min(max_w);
57 hh = (text::text_height(t, avail_w, s.font_scale) + s.pad[0] + s.pad[2])
58 .max(s.min_h).min(max_h);
59 } else {
60 let tw = t.len() as f32 * text::CHAR_W * s.font_scale;
61 ww = (tw + s.pad[1] + s.pad[3]).max(s.min_w).min(max_w);
62 hh = (text::LINE_H * s.font_scale + s.pad[0] + s.pad[2]).max(s.min_h).min(max_h);
63 }
64 }
65 WidgetKind::ItemSlot(_) => {
66 ww = (18.0 + s.pad[1] + s.pad[3]).max(s.min_w).min(max_w);
67 hh = (18.0 + s.pad[0] + s.pad[2]).max(s.min_h).min(max_h);
68 }
69 WidgetKind::Spacer => {
70 ww = s.min_w.max(1.0).min(max_w);
71 hh = s.min_h.max(1.0).min(max_h);
72 }
73 WidgetKind::Panel(_) => {} WidgetKind::McImage { img_w, img_h, .. } => {
75 ww = (*img_w + s.pad[1] + s.pad[3]).max(s.min_w).min(max_w);
76 hh = (*img_h + s.pad[0] + s.pad[2]).max(s.min_h).min(max_h);
77 }
78 }
79 }
80
81 node.rect = Rect { x, y, w: ww, h: hh };
82
83 if !has_children { return; }
84
85 let dir = if matches!(w.kind, WidgetKind::Panel(_)) && w.flex_dir == FlexDir::Row { FlexDir::Row } else { FlexDir::Column };
87 let content_w = ww - s.pad[1] - s.pad[3];
88 let content_h = hh - s.pad[0] - s.pad[2];
89
90 let mut child_nodes: Vec<LayoutNode> = Vec::new();
92 let mut total_flex: f32 = 0.0;
93 let mut used_main: f32 = 0.0;
94
95 for child in &w.children {
96 let mut cn = LayoutNode {
97 id: child.id.clone(), on_click: child.on_click.clone(),
98 enabled: child.enabled, focused: child.focused,
99 ..Default::default()
100 };
101 let cmw = if dir == FlexDir::Row { f32::MAX } else { content_w };
102 let cmh = if dir == FlexDir::Column { f32::MAX } else { content_h };
103 layout_widget(child, &mut cn, 0.0, 0.0, cmw, cmh);
104 if dir == FlexDir::Row { used_main += cn.rect.w; }
105 else { used_main += cn.rect.h; }
106 total_flex += child.style.flex;
107 child_nodes.push(cn);
108 }
109 let gaps = s.gap * (w.children.len().saturating_sub(1) as f32);
110 used_main += gaps;
111
112 let available = (if dir == FlexDir::Row { content_w } else { content_h }) - used_main;
113 let mut pos = if dir == FlexDir::Row { s.pad[3] } else { s.pad[0] };
114
115 for (i, child) in w.children.iter().enumerate() {
117 let cn = &mut child_nodes[i];
118 if dir == FlexDir::Row {
119 if child.style.flex > 0.0 && total_flex > 0.0 && available > 0.0 {
120 cn.rect.w += available * child.style.flex / total_flex;
121 }
122 cn.rect.x = x + pos;
123 cn.rect.y = y + s.pad[0] + match s.align {
124 Align::Center => (content_h - cn.rect.h) / 2.0,
125 Align::End => content_h - cn.rect.h,
126 _ => 0.0,
127 };
128 pos += cn.rect.w + s.gap;
129 } else {
130 if child.style.flex > 0.0 && total_flex > 0.0 && available > 0.0 {
131 cn.rect.h += available * child.style.flex / total_flex;
132 }
133 cn.rect.x = x + s.pad[3] + match s.align {
134 Align::Center => (content_w - cn.rect.w) / 2.0,
135 Align::End => content_w - cn.rect.w,
136 _ => 0.0,
137 };
138 cn.rect.y = y + pos;
139 pos += cn.rect.h + s.gap;
140 }
141 if !child.children.is_empty() {
143 layout_widget(child, cn, cn.rect.x, cn.rect.y, cn.rect.w, cn.rect.h);
144 }
145 }
146
147 if s.w <= 0.0 {
149 let cw: f32 = child_nodes.iter().map(|c| c.rect.x - x + c.rect.w).fold(0.0f32, f32::max);
150 node.rect.w = (cw + s.pad[1] + s.pad[3]).max(s.min_w).min(max_w);
151 }
152 if s.h <= 0.0 {
153 let ch: f32 = child_nodes.iter().map(|c| c.rect.y - y + c.rect.h).fold(0.0f32, f32::max);
154 node.rect.h = (ch + s.pad[0] + s.pad[2]).max(s.min_h).min(max_h);
155 }
156
157 node.children = child_nodes;
158}
159
160pub fn hit_test(node: &LayoutNode, mx: f32, my: f32) -> Option<&LayoutNode> {
162 let r = &node.rect;
163 if mx < r.x || my < r.y || mx > r.x + r.w || my > r.y + r.h { return None; }
164 for child in node.children.iter().rev() {
165 if let Some(hit) = hit_test(child, mx, my) { return Some(hit); }
166 }
167 if node.on_click.is_some() && node.enabled { Some(node) } else { None }
168}
169
170pub fn set_focus(node: &mut LayoutNode, focused_id: Option<&str>) {
172 node.focused = focused_id.is_some() && node.id.as_deref() == focused_id;
173 for child in &mut node.children { set_focus(child, focused_id); }
174}