Skip to main content

yog_ui/
layout.rs

1use crate::text;
2use crate::widget::{Dock, 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
29/// Compute layout starting at (0,0) with given available size.
30/// Returns the root LayoutNode with absolute coordinates.
31pub 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    // Determine own size
46    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        // Leaf: size to content (text, item slot, spacer)
51        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                // Wrap only when max_w is a real constraint (not "unlimited").
55                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(_) => {} // panel with no children → size to min or available
74            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    // Flex layout for children
86    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    // Helpers for Dock
91    // Returns effective flex factor (Dock::Fill implies at least 1.0).
92    let effective_flex = |child: &Widget| -> f32 {
93        if child.style.dock == Dock::Fill { child.style.flex.max(1.0) } else { child.style.flex }
94    };
95    // Returns true if this child consumes main-axis space in the normal forward pass.
96    let in_flow = |child: &Widget| -> bool {
97        match (dir, child.style.dock) {
98            (FlexDir::Row,    Dock::Right)  => false,
99            (FlexDir::Column, Dock::Bottom) => false,
100            _ => true,
101        }
102    };
103
104    // Measure children
105    let mut child_nodes: Vec<LayoutNode> = Vec::new();
106    let mut total_flex: f32 = 0.0;
107    let mut used_main: f32  = 0.0;
108
109    for child in &w.children {
110        let dock = child.style.dock;
111        let mut cn = LayoutNode {
112            id: child.id.clone(), on_click: child.on_click.clone(),
113            enabled: child.enabled, focused: child.focused,
114            ..Default::default()
115        };
116        // Determine measurement constraints based on Dock + direction.
117        let (cmw, cmh) = match (dir, dock) {
118            // Fill: constrain both axes so text can wrap to container dimensions.
119            (FlexDir::Row,    Dock::Fill) => (content_w, content_h),
120            (FlexDir::Column, Dock::Fill) => (content_w, content_h),
121            // Cross-axis fill: constrain cross axis, unlimited main axis.
122            (FlexDir::Row,    Dock::Left | Dock::Right) => (f32::MAX, content_h),
123            (FlexDir::Column, Dock::Top  | Dock::Bottom) => (content_w, f32::MAX),
124            // Default flex behaviour.
125            (FlexDir::Row,    _) => (f32::MAX, content_h),
126            (FlexDir::Column, _) => (content_w, f32::MAX),
127        };
128        layout_widget(child, &mut cn, 0.0, 0.0, cmw, cmh);
129        if in_flow(child) {
130            if dir == FlexDir::Row { used_main += cn.rect.w; }
131            else                   { used_main += cn.rect.h; }
132        }
133        total_flex += effective_flex(child);
134        child_nodes.push(cn);
135    }
136    let flow_count = w.children.iter().filter(|c| in_flow(c)).count();
137    let gaps = s.gap * (flow_count.saturating_sub(1) as f32);
138    used_main += gaps;
139
140    let available = (if dir == FlexDir::Row { content_w } else { content_h }) - used_main;
141    let mut pos = if dir == FlexDir::Row { s.pad[3] } else { s.pad[0] };
142
143    // --- Forward pass: position in-flow children (not Dock::Right / Dock::Bottom) ---
144    for (i, child) in w.children.iter().enumerate() {
145        if !in_flow(child) { continue; }
146        let dock = child.style.dock;
147        let cn = &mut child_nodes[i];
148        if dir == FlexDir::Row {
149            let ef = effective_flex(child);
150            if ef > 0.0 && total_flex > 0.0 && available > 0.0 {
151                cn.rect.w += available * ef / total_flex;
152            }
153            if dock == Dock::Fill || dock == Dock::Left || dock == Dock::Right {
154                cn.rect.h = content_h; // stretch cross axis
155            }
156            cn.rect.x = x + pos;
157            cn.rect.y = y + s.pad[0] + match s.align {
158                Align::Center => (content_h - cn.rect.h) / 2.0,
159                Align::End    => content_h - cn.rect.h,
160                _             => 0.0,
161            };
162            pos += cn.rect.w + s.gap;
163        } else {
164            let ef = effective_flex(child);
165            if ef > 0.0 && total_flex > 0.0 && available > 0.0 {
166                cn.rect.h += available * ef / total_flex;
167            }
168            if dock == Dock::Fill || dock == Dock::Top || dock == Dock::Bottom {
169                cn.rect.w = content_w; // stretch cross axis
170            }
171            cn.rect.x = x + s.pad[3] + match s.align {
172                Align::Center => (content_w - cn.rect.w) / 2.0,
173                Align::End    => content_w - cn.rect.w,
174                _             => 0.0,
175            };
176            cn.rect.y = y + pos;
177            pos += cn.rect.h + s.gap;
178        }
179        if !child.children.is_empty() {
180            layout_widget(child, cn, cn.rect.x, cn.rect.y, cn.rect.w, cn.rect.h);
181        }
182    }
183
184    // --- Reverse pass: position Dock::Right / Dock::Bottom children from the far edge ---
185    let mut rpos = if dir == FlexDir::Row {
186        x + s.pad[3] + content_w
187    } else {
188        y + s.pad[0] + content_h
189    };
190    for (i, child) in w.children.iter().enumerate() {
191        if in_flow(child) { continue; }
192        let dock = child.style.dock;
193        let cn = &mut child_nodes[i];
194        if dir == FlexDir::Row {
195            if dock == Dock::Fill || dock == Dock::Left || dock == Dock::Right {
196                cn.rect.h = content_h;
197            }
198            rpos -= cn.rect.w;
199            cn.rect.x = rpos;
200            cn.rect.y = y + s.pad[0] + match s.align {
201                Align::Center => (content_h - cn.rect.h) / 2.0,
202                Align::End    => content_h - cn.rect.h,
203                _             => 0.0,
204            };
205            rpos -= s.gap;
206        } else {
207            if dock == Dock::Fill || dock == Dock::Top || dock == Dock::Bottom {
208                cn.rect.w = content_w;
209            }
210            rpos -= cn.rect.h;
211            cn.rect.y = rpos;
212            cn.rect.x = x + s.pad[3] + match s.align {
213                Align::Center => (content_w - cn.rect.w) / 2.0,
214                Align::End    => content_w - cn.rect.w,
215                _             => 0.0,
216            };
217            rpos -= s.gap;
218        }
219        if !child.children.is_empty() {
220            layout_widget(child, cn, cn.rect.x, cn.rect.y, cn.rect.w, cn.rect.h);
221        }
222    }
223
224    // Auto-size: shrink to content
225    if s.w <= 0.0 {
226        let cw: f32 = child_nodes.iter().map(|c| c.rect.x - x + c.rect.w).fold(0.0f32, f32::max);
227        node.rect.w = (cw + s.pad[1] + s.pad[3]).max(s.min_w).min(max_w);
228    }
229    if s.h <= 0.0 {
230        let ch: f32 = child_nodes.iter().map(|c| c.rect.y - y + c.rect.h).fold(0.0f32, f32::max);
231        node.rect.h = (ch + s.pad[0] + s.pad[2]).max(s.min_h).min(max_h);
232    }
233
234    node.children = child_nodes;
235}
236
237/// Hit-test: find deepest clickable, enabled node at (mx, my).
238pub fn hit_test(node: &LayoutNode, mx: f32, my: f32) -> Option<&LayoutNode> {
239    let r = &node.rect;
240    if mx < r.x || my < r.y || mx > r.x + r.w || my > r.y + r.h { return None; }
241    for child in node.children.iter().rev() {
242        if let Some(hit) = hit_test(child, mx, my) { return Some(hit); }
243    }
244    if node.on_click.is_some() && node.enabled { Some(node) } else { None }
245}
246
247/// Walk tree and set `focused = true` on the node whose id matches, false on all others.
248pub fn set_focus(node: &mut LayoutNode, focused_id: Option<&str>) {
249    node.focused = focused_id.is_some() && node.id.as_deref() == focused_id;
250    for child in &mut node.children { set_focus(child, focused_id); }
251}