yog-ui 0.35.1

Yog UI framework — flexbox layout engine + GPU rendering via yog-gfx
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
use crate::text;
use crate::widget::{Dock, Widget, WidgetKind};

#[derive(Debug, Clone, Copy, PartialEq)]
pub enum FlexDir {
    Row,
    Column,
}
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum Align {
    Start,
    Center,
    End,
}

#[derive(Debug, Clone, Copy, Default)]
pub struct Rect {
    pub x: f32,
    pub y: f32,
    pub w: f32,
    pub h: f32,
}

#[derive(Debug, Clone)]
pub struct LayoutNode {
    pub rect: Rect,
    pub id: Option<String>,
    pub on_click: Option<String>,
    pub children: Vec<LayoutNode>,
    pub enabled: bool,
    pub focused: bool,
}

impl Default for LayoutNode {
    fn default() -> Self {
        Self {
            rect: Rect::default(),
            id: None,
            on_click: None,
            children: Vec::new(),
            enabled: true,
            focused: false,
        }
    }
}

/// Compute layout starting at (0,0) with given available size.
/// Returns the root LayoutNode with absolute coordinates.
pub fn compute(widget: &Widget, avail_w: f32, avail_h: f32) -> LayoutNode {
    let mut node = LayoutNode {
        id: widget.id.clone(),
        on_click: widget.on_click.clone(),
        enabled: widget.enabled,
        focused: widget.focused,
        ..Default::default()
    };
    layout_widget(widget, &mut node, 0.0, 0.0, avail_w, avail_h);
    node
}

fn layout_widget(w: &Widget, node: &mut LayoutNode, x: f32, y: f32, max_w: f32, max_h: f32) {
    let s = &w.style;
    let has_children = !w.children.is_empty();

    // Determine own size
    let mut ww = s.w.resolve(max_w).map(|v| v.min(max_w)).unwrap_or(max_w);
    let mut hh = s.h.resolve(max_h).map(|v| v.min(max_h)).unwrap_or(max_h);

    if !has_children {
        // Leaf: size to content (text, item slot, spacer)
        match &w.kind {
            WidgetKind::Label(t) | WidgetKind::Button(t) => {
                let avail_w = (max_w - s.pad[1] - s.pad[3]).max(0.0);
                // Wrap only when max_w is a real constraint (not "unlimited")
                // and the widget doesn't opt out of wrapping entirely.
                if avail_w < 4096.0 && !s.no_wrap {
                    ww = (avail_w + s.pad[1] + s.pad[3]).max(s.min_w).min(max_w);
                    hh = (text::text_height(t, avail_w, s.font_scale) + s.pad[0] + s.pad[2])
                        .max(s.min_h)
                        .min(max_h);
                } else {
                    let tw = text::str_width(t, s.font_scale);
                    ww = (tw + s.pad[1] + s.pad[3]).max(s.min_w).min(max_w);
                    hh = (text::LINE_H * s.font_scale + s.pad[0] + s.pad[2])
                        .max(s.min_h)
                        .min(max_h);
                }
            }
            WidgetKind::ItemSlot(_) | WidgetKind::InvSlot { .. } => {
                ww = (18.0 + s.pad[1] + s.pad[3]).max(s.min_w).min(max_w);
                hh = (18.0 + s.pad[0] + s.pad[2]).max(s.min_h).min(max_h);
            }
            WidgetKind::Spacer => {
                ww = s.min_w.max(1.0).min(max_w);
                hh = s.min_h.max(1.0).min(max_h);
            }
            WidgetKind::Panel(_) => {} // panel with no children → size to min or available
            WidgetKind::McImage { img_w, img_h, .. } => {
                ww = (*img_w + s.pad[1] + s.pad[3]).max(s.min_w).min(max_w);
                hh = (*img_h + s.pad[0] + s.pad[2]).max(s.min_h).min(max_h);
            }
        }
        // Explicit width/height always wins over content sizing.
        if let Some(v) = s.w.resolve(max_w) {
            ww = v.min(max_w);
        }
        if let Some(v) = s.h.resolve(max_h) {
            hh = v.min(max_h);
        }
    }

    node.rect = Rect { x, y, w: ww, h: hh };

    if !has_children {
        return;
    }

    // Flex layout for children
    let dir = if matches!(w.kind, WidgetKind::Panel(_)) && w.flex_dir == FlexDir::Row {
        FlexDir::Row
    } else {
        FlexDir::Column
    };
    let content_w = ww - s.pad[1] - s.pad[3];
    let content_h = hh - s.pad[0] - s.pad[2];

    // Helpers for Dock
    // Returns effective flex factor (Dock::Fill implies at least 1.0).
    let effective_flex = |child: &Widget| -> f32 {
        if child.style.dock == Dock::Fill {
            child.style.flex.max(1.0)
        } else {
            child.style.flex
        }
    };
    // Returns true if this child consumes main-axis space in the normal forward pass.
    let in_flow = |child: &Widget| -> bool {
        child.style.pos.is_none()
            && !matches!(
                (dir, child.style.dock),
                (FlexDir::Row, Dock::Right) | (FlexDir::Column, Dock::Bottom)
            )
    };

    // Measure children
    let mut child_nodes: Vec<LayoutNode> = Vec::new();
    let mut total_flex: f32 = 0.0;
    let mut used_main: f32 = 0.0;

    for child in &w.children {
        let dock = child.style.dock;
        let mut cn = LayoutNode {
            id: child.id.clone(),
            on_click: child.on_click.clone(),
            enabled: child.enabled,
            focused: child.focused,
            ..Default::default()
        };
        // Absolutely positioned: measured against what's left of the content
        // box past its offset, placed after the flow passes.
        if let Some((px, py)) = child.style.pos {
            let mw = (content_w - px).max(0.0);
            let mh = (content_h - py).max(0.0);
            layout_widget(child, &mut cn, 0.0, 0.0, mw, mh);
            child_nodes.push(cn);
            continue;
        }
        // Determine measurement constraints based on Dock + direction.
        let (cmw, cmh) = match (dir, dock) {
            // Fill: constrain both axes so text can wrap to container dimensions.
            (FlexDir::Row, Dock::Fill) => (content_w, content_h),
            (FlexDir::Column, Dock::Fill) => (content_w, content_h),
            // Cross-axis fill: constrain cross axis, unlimited main axis.
            (FlexDir::Row, Dock::Left | Dock::Right) => (f32::MAX, content_h),
            (FlexDir::Column, Dock::Top | Dock::Bottom) => (content_w, f32::MAX),
            // Default flex behaviour.
            (FlexDir::Row, _) => (f32::MAX, content_h),
            (FlexDir::Column, _) => (content_w, f32::MAX),
        };
        layout_widget(child, &mut cn, 0.0, 0.0, cmw, cmh);
        if in_flow(child) {
            if dir == FlexDir::Row {
                used_main += cn.rect.w;
            } else {
                used_main += cn.rect.h;
            }
        }
        total_flex += effective_flex(child);
        child_nodes.push(cn);
    }
    let flow_count = w.children.iter().filter(|c| in_flow(c)).count();
    let gaps = s.gap * (flow_count.saturating_sub(1) as f32);
    used_main += gaps;

    let available = (if dir == FlexDir::Row {
        content_w
    } else {
        content_h
    }) - used_main;
    let mut pos = if dir == FlexDir::Row {
        s.pad[3]
    } else {
        s.pad[0]
    };

    // --- Forward pass: position in-flow children (not Dock::Right / Dock::Bottom) ---
    for (i, child) in w.children.iter().enumerate() {
        if !in_flow(child) {
            continue;
        }
        let dock = child.style.dock;
        let cn = &mut child_nodes[i];
        if dir == FlexDir::Row {
            let ef = effective_flex(child);
            if ef > 0.0 && total_flex > 0.0 && available > 0.0 {
                cn.rect.w += available * ef / total_flex;
            }
            if dock == Dock::Fill || dock == Dock::Left || dock == Dock::Right {
                cn.rect.h = content_h; // stretch cross axis
            }
            cn.rect.x = x + pos;
            cn.rect.y = y
                + s.pad[0]
                + match s.align {
                    Align::Center => (content_h - cn.rect.h) / 2.0,
                    Align::End => content_h - cn.rect.h,
                    _ => 0.0,
                };
            pos += cn.rect.w + s.gap;
        } else {
            let ef = effective_flex(child);
            if ef > 0.0 && total_flex > 0.0 && available > 0.0 {
                cn.rect.h += available * ef / total_flex;
            }
            if dock == Dock::Fill || dock == Dock::Top || dock == Dock::Bottom {
                cn.rect.w = content_w; // stretch cross axis
            }
            cn.rect.x = x
                + s.pad[3]
                + match s.align {
                    Align::Center => (content_w - cn.rect.w) / 2.0,
                    Align::End => content_w - cn.rect.w,
                    _ => 0.0,
                };
            cn.rect.y = y + pos;
            pos += cn.rect.h + s.gap;
        }
        if !child.children.is_empty() {
            layout_widget(child, cn, cn.rect.x, cn.rect.y, cn.rect.w, cn.rect.h);
        }
    }

    // --- Reverse pass: position Dock::Right / Dock::Bottom children from the far edge ---
    let mut rpos = if dir == FlexDir::Row {
        x + s.pad[3] + content_w
    } else {
        y + s.pad[0] + content_h
    };
    for (i, child) in w.children.iter().enumerate() {
        if in_flow(child) || child.style.pos.is_some() {
            continue;
        }
        let dock = child.style.dock;
        let cn = &mut child_nodes[i];
        if dir == FlexDir::Row {
            if dock == Dock::Fill || dock == Dock::Left || dock == Dock::Right {
                cn.rect.h = content_h;
            }
            rpos -= cn.rect.w;
            cn.rect.x = rpos;
            cn.rect.y = y
                + s.pad[0]
                + match s.align {
                    Align::Center => (content_h - cn.rect.h) / 2.0,
                    Align::End => content_h - cn.rect.h,
                    _ => 0.0,
                };
            rpos -= s.gap;
        } else {
            if dock == Dock::Fill || dock == Dock::Top || dock == Dock::Bottom {
                cn.rect.w = content_w;
            }
            rpos -= cn.rect.h;
            cn.rect.y = rpos;
            cn.rect.x = x
                + s.pad[3]
                + match s.align {
                    Align::Center => (content_w - cn.rect.w) / 2.0,
                    Align::End => content_w - cn.rect.w,
                    _ => 0.0,
                };
            rpos -= s.gap;
        }
        if !child.children.is_empty() {
            layout_widget(child, cn, cn.rect.x, cn.rect.y, cn.rect.w, cn.rect.h);
        }
    }

    // --- Absolutely positioned children (see `Style::pos`) ---
    for (i, child) in w.children.iter().enumerate() {
        let Some((px, py)) = child.style.pos else {
            continue;
        };
        let cn = &mut child_nodes[i];
        cn.rect.x = x + s.pad[3] + px;
        cn.rect.y = y + s.pad[0] + py;
        if !child.children.is_empty() {
            layout_widget(child, cn, cn.rect.x, cn.rect.y, cn.rect.w, cn.rect.h);
        }
    }

    // Auto-size: shrink to content
    if s.w.is_auto() {
        let cw: f32 = child_nodes
            .iter()
            .map(|c| c.rect.x - x + c.rect.w)
            .fold(0.0f32, f32::max);
        node.rect.w = (cw + s.pad[1] + s.pad[3]).max(s.min_w).min(max_w);
    }
    if s.h.is_auto() {
        let ch: f32 = child_nodes
            .iter()
            .map(|c| c.rect.y - y + c.rect.h)
            .fold(0.0f32, f32::max);
        node.rect.h = (ch + s.pad[0] + s.pad[2]).max(s.min_h).min(max_h);
    }

    node.children = child_nodes;
}

/// Hit-test: find deepest clickable, enabled node at (mx, my).
pub fn hit_test(node: &LayoutNode, mx: f32, my: f32) -> Option<&LayoutNode> {
    let r = &node.rect;
    if mx < r.x || my < r.y || mx > r.x + r.w || my > r.y + r.h {
        return None;
    }
    for child in node.children.iter().rev() {
        if let Some(hit) = hit_test(child, mx, my) {
            return Some(hit);
        }
    }
    if node.on_click.is_some() && node.enabled {
        Some(node)
    } else {
        None
    }
}

/// Find the resolved rect of the (first) node tagged `id` (see `Widget::id`).
/// Meant for baking a widget tree's computed positions into some other
/// system at "measure time" (e.g. a mod reporting real-slot pixel offsets to
/// its host) — a WinForms-style measure-then-use pass, not a per-frame call.
pub fn find_by_id(node: &LayoutNode, id: &str) -> Option<Rect> {
    if node.id.as_deref() == Some(id) {
        return Some(node.rect);
    }
    node.children.iter().find_map(|c| find_by_id(c, id))
}

/// Walk tree and set `focused = true` on the node whose id matches, false on all others.
pub fn set_focus(node: &mut LayoutNode, focused_id: Option<&str>) {
    node.focused = focused_id.is_some() && node.id.as_deref() == focused_id;
    for child in &mut node.children {
        set_focus(child, focused_id);
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::widget::{self, Units};

    #[test]
    fn positioned_children_leave_the_flow() {
        let root = widget::panel(FlexDir::Column)
            .w(200.0)
            .h(100.0)
            .padding(4.0, 4.0, 4.0, 4.0)
            .child(widget::spacer().min_w(10.0).min_h(10.0))
            .child(widget::button("Hi").w(40.0).h(20.0).at(50.0, 30.0))
            .child(widget::spacer().min_w(10.0).min_h(10.0));
        let node = compute(&root, 400.0, 400.0);
        let r = node.children[1].rect;
        assert_eq!((r.x, r.y, r.w, r.h), (54.0, 34.0, 40.0, 20.0));
        // The flow children stack as if the positioned one weren't there.
        assert_eq!(node.children[0].rect.y, 4.0);
        assert_eq!(node.children[2].rect.y, 4.0 + 10.0 + 2.0);
    }

    #[test]
    fn positioned_container_lays_out_its_children_in_place() {
        let root = widget::panel(FlexDir::Column)
            .w(Units::Percent(100.0))
            .h(Units::Percent(100.0))
            .child(
                widget::panel(FlexDir::Row)
                    .w(100.0)
                    .h(20.0)
                    .gap(0.0)
                    .at(10.0, 20.0)
                    .child(widget::spacer().min_w(30.0).min_h(5.0))
                    .child(widget::spacer().min_w(30.0).min_h(5.0)),
            );
        let node = compute(&root, 320.0, 240.0);
        let panel = &node.children[0];
        assert_eq!((panel.rect.x, panel.rect.y), (10.0, 20.0));
        assert_eq!(panel.children[1].rect.x, 40.0);
        assert_eq!(panel.children[1].rect.y, 20.0);
    }

    #[test]
    fn auto_sized_parent_grows_to_fit_positioned_children() {
        let root = widget::panel(FlexDir::Column)
            .child(widget::spacer().min_w(10.0).min_h(10.0).at(30.0, 40.0));
        let node = compute(&root, 300.0, 300.0);
        assert_eq!((node.rect.w, node.rect.h), (40.0, 50.0));
    }

    #[test]
    fn find_mut_and_set_text() {
        let mut root = widget::panel(FlexDir::Column).child(widget::label("a").id("title"));
        assert!(root.find_mut("title").unwrap().set_text("b"));
        match &root.children[0].kind {
            crate::widget::WidgetKind::Label(t) => assert_eq!(t, "b"),
            _ => unreachable!(),
        }
        assert!(root.find_mut("missing").is_none());
    }
}