rungui 0.3.2

Small portable native GUI library: a thin layer over GTK3, Win32 and Cocoa with hand-declared FFI
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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
//! Rust-side layout: stacks (HBox/VBox, also used by Window/Page/GroupBox as vertical stacks)
//! and grids, computed from backend-reported preferred sizes. Pure functions over the registry;
//! `compute` returns the `Bounds` the core must push to the backend (only those that changed).
//!
//! Space model: along the stack axis children get their natural size, plus a share of leftover
//! space proportional to `expand`; across the axis they follow `Align` (default Fill).
//! Hidden children take no space. Layout never shrinks below natural sizes (content overflows
//! rather than being squeezed), keeping the algorithm tiny and predictable.

use crate::backend::{Backend, Kind, Native as B, Prop};
use crate::core::{Node, Registry, panes_of};
use crate::types::*;
use std::collections::HashMap;

/// What layout pushes: `Prop::Bounds`, plus `Prop::Visible` for splitter sashes.
type Out = Vec<(WidgetId, Prop<'static>)>;

thread_local! {
    static RTL: std::cell::Cell<bool> = const { std::cell::Cell::new(false) };
    /// Natural sizes measured during the current `compute` pass. Arranging a container re-measures
    /// its children, so without this a deep tree costs O(nodes x depth) backend queries. Nothing
    /// `measure` reads changes while a pass runs, so a node is measured once per pass.
    static MEASURED: std::cell::RefCell<Option<HashMap<WidgetId, Size>>> = const { std::cell::RefCell::new(None) };
}

/// Enables the measure cache for its lifetime (one layout pass).
struct MeasurePass;
impl MeasurePass {
    fn begin() -> MeasurePass {
        MEASURED.with(|m| *m.borrow_mut() = Some(HashMap::new()));
        MeasurePass
    }
}
impl Drop for MeasurePass {
    fn drop(&mut self) {
        MEASURED.with(|m| *m.borrow_mut() = None);
    }
}

/// Mirror horizontal placement (right-to-left locales). The caller must relayout.
pub fn set_rtl(v: bool) {
    RTL.with(|c| c.set(v));
}
fn rtl() -> bool {
    RTL.with(|c| c.get())
}
pub fn is_rtl() -> bool {
    rtl()
}

/// The visible panes of a splitter (at most two).
fn split_panes(r: &Registry, id: WidgetId) -> Vec<WidgetId> {
    let Some(n) = r.nodes.get(&id) else {
        return vec![];
    };
    panes_of(r, n)
        .into_iter()
        .filter(|c| r.nodes.get(c).is_some_and(|c| c.visible))
        .take(2)
        .collect()
}

fn visible_children(r: &Registry, id: WidgetId) -> Vec<WidgetId> {
    r.nodes.get(&id).map_or_else(Vec::new, |n| {
        n.children
            .iter()
            .copied()
            .filter(|c| {
                r.nodes
                    .get(c)
                    .is_some_and(|c| c.visible && c.kind.in_layout())
            })
            .collect()
    })
}

/// One placed child of a grid: `(child, col, row, colspan, rowspan)`.
type GridCell = (WidgetId, usize, usize, usize, usize);

/// Resolved grid cells plus (ncols, nrows).
fn grid_cells(r: &Registry, id: WidgetId) -> (Vec<GridCell>, usize, usize) {
    let cols = r.nodes[&id].lay.cols.max(1);
    let mut next = 0usize;
    let mut cells = vec![];
    for c in visible_children(r, id) {
        let (col, row, cs, rs) = match r.nodes[&c].lay.cell {
            Some((a, b, cs, rs)) => (a, b, cs.max(1), rs.max(1)),
            None => {
                let p = (next % cols, next / cols, 1, 1);
                next += 1;
                p
            }
        };
        cells.push((c, col, row, cs, rs));
    }
    let nc = cells
        .iter()
        .map(|c| c.1 + c.3)
        .max()
        .unwrap_or(0)
        .max(cols.min(cells.len()));
    let nr = cells.iter().map(|c| c.2 + c.4).max().unwrap_or(0);
    (cells, nc, nr)
}

/// Per-axis (columns, rows) track values of a grid.
type Tracks<T> = [Vec<T>; 2];

/// Column widths / row heights and expand weights of a grid.
fn grid_tracks(r: &Registry, id: WidgetId) -> (Vec<GridCell>, Tracks<i32>, Tracks<f32>) {
    let (cells, nc, nr) = grid_cells(r, id);
    let mut size = [vec![0; nc], vec![0; nr]];
    let mut exp = [vec![0.0f32; nc], vec![0.0f32; nr]];
    let sp = r.nodes[&id].lay.spacing;
    for pass_span in [false, true] {
        for &(c, col, row, cs, rs) in &cells {
            if (cs > 1 || rs > 1) != pass_span {
                continue;
            }
            let m = measure(r, c);
            let e = r.nodes[&c].lay.expand;
            for (axis, (start, span, need)) in
                [(col, cs, m.w), (row, rs, m.h)].into_iter().enumerate()
            {
                let end = start + span - 1;
                let have: i32 =
                    size[axis][start..=end].iter().sum::<i32>() + sp * (span as i32 - 1);
                if need > have {
                    size[axis][end] += need - have; // spanning cells grow their last track
                }
                if !pass_span {
                    exp[axis][start] = exp[axis][start].max(e);
                }
            }
        }
    }
    (cells, size, exp)
}

fn is_horizontal(k: Kind) -> bool {
    k == Kind::HBox
}

/// Total spacing between `n` children.
fn gaps(spacing: i32, n: usize) -> i32 {
    spacing.saturating_mul(n.saturating_sub(1).min(i32::MAX as usize) as i32)
}

/// Size needed by the children (no padding, no chrome).
fn measure_children(r: &Registry, id: WidgetId, n: &Node) -> Size {
    let sp = n.lay.spacing;
    if let Some(split) = n.split() {
        // natural: first pane at the requested position (else natural), sash, second pane
        let horiz = split.orient == Orientation::Horizontal;
        let ms: Vec<Size> = split_panes(r, id).iter().map(|c| measure(r, *c)).collect();
        let along = |m: &Size| if horiz { m.w } else { m.h };
        let across = |m: &Size| if horiz { m.h } else { m.w };
        let mut main = 0;
        for (i, m) in ms.iter().enumerate() {
            let a = if i == 0 && ms.len() == 2 {
                split.pos.unwrap_or(along(m))
            } else {
                along(m)
            };
            main += a.max(if i == 0 { split.min.0 } else { split.min.1 });
        }
        if ms.len() == 2 && split.pos.is_none() {
            // no requested position: layout splits evenly, so both panes need the larger share
            let big = ms
                .iter()
                .map(along)
                .chain([split.min.0, split.min.1])
                .max()
                .unwrap_or(0);
            main = 2 * big;
        }
        if ms.len() == 2 {
            main += sp;
        }
        let cross = ms.iter().map(across).max().unwrap_or(0);
        return if horiz {
            Size::new(main, cross)
        } else {
            Size::new(cross, main)
        };
    }
    if n.kind == Kind::Grid {
        let (_, size, _) = grid_tracks(r, id);
        let tot = |v: &Vec<i32>| v.iter().sum::<i32>() + sp * (v.len() as i32 - 1).max(0);
        return Size::new(tot(&size[0]), tot(&size[1]));
    }
    let kids = visible_children(r, id);
    let (mut main, mut cross) = (0i32, 0i32);
    for c in &kids {
        let m = measure(r, *c);
        let (a, b) = if is_horizontal(n.kind) {
            (m.w, m.h)
        } else {
            (m.h, m.w)
        };
        main = main.saturating_add(a);
        cross = cross.max(b);
    }
    main = main.saturating_add(gaps(sp, kids.len()));
    if is_horizontal(n.kind) {
        Size::new(main, cross)
    } else {
        Size::new(cross, main)
    }
}

/// Natural size of a widget (honouring min/fixed size); cached for the duration of a pass.
fn measure(r: &Registry, id: WidgetId) -> Size {
    let cached = MEASURED.with(|m| m.borrow().as_ref().and_then(|m| m.get(&id).copied()));
    if let Some(s) = cached {
        return s;
    }
    let s = measure_uncached(r, id);
    MEASURED.with(|m| {
        if let Some(m) = m.borrow_mut().as_mut() {
            m.insert(id, s);
        }
    });
    s
}

fn measure_uncached(r: &Registry, id: WidgetId) -> Size {
    let Some(n) = r.nodes.get(&id) else {
        return Size::default();
    };
    let pad = 2 * n.lay.padding;
    let mut s = match n.kind {
        Kind::HBox
        | Kind::VBox
        | Kind::Grid
        | Kind::Splitter
        | Kind::Window
        | Kind::Page
        | Kind::GroupBox => {
            let c = measure_children(r, id, n);
            let ch = if n.kind == Kind::GroupBox {
                B::chrome(id)
            } else {
                Size::default()
            };
            Size::new(
                c.w.saturating_add(pad).saturating_add(ch.w),
                c.h.saturating_add(pad).saturating_add(ch.h),
            )
        }
        Kind::Tabs => {
            let ch = B::chrome(id);
            let (mut w, mut h) = (0, 0);
            for p in &n.children {
                let m = measure(r, *p);
                w = w.max(m.w);
                h = h.max(m.h);
            }
            Size::new(w + ch.w, h + ch.h)
        }
        Kind::Spacer => Size::default(),
        _ => B::preferred_size(id),
    };
    if let Some(f) = n.lay.fixed {
        s = f;
    }
    Size::new(
        s.w.max(n.lay.min.w).clamp(0, MAX_PX),
        s.h.max(n.lay.min.h).clamp(0, MAX_PX),
    )
}

/// Keep a rectangle within what toolkits accept (see [`MAX_PX`]).
fn clamp_rect(r: Rect) -> Rect {
    Rect::new(
        r.x.clamp(-MAX_PX, MAX_PX),
        r.y.clamp(-MAX_PX, MAX_PX),
        r.w.clamp(0, MAX_PX),
        r.h.clamp(0, MAX_PX),
    )
}

fn place(r: &mut Registry, id: WidgetId, rect: Rect, out: &mut Out) {
    let rect = clamp_rect(rect);
    let Some(n) = r.nodes.get_mut(&id) else {
        return;
    };
    let kind = n.kind;
    if kind.is_native() && n.bounds != rect {
        n.bounds = rect;
        out.push((id, Prop::Bounds(rect)));
    } else if kind == Kind::Splitter {
        n.bounds = rect; // virtual, but its area is handy (Widget::bounds, sash hit maths)
    }
    match kind {
        Kind::Tabs => {
            let ch = B::chrome(id);
            let inner = Rect::new(0, 0, (rect.w - ch.w).max(0), (rect.h - ch.h).max(0));
            let pages = r.nodes[&id].children.clone();
            for p in pages {
                place(r, p, inner, out);
            }
        }
        Kind::Window | Kind::Page | Kind::GroupBox => {
            let ch = if kind == Kind::GroupBox {
                B::chrome(id)
            } else {
                Size::default()
            };
            let inner = Rect::new(0, 0, (rect.w - ch.w).max(0), (rect.h - ch.h).max(0));
            arrange_children(r, id, inner, out);
        }
        Kind::HBox | Kind::VBox | Kind::Grid | Kind::Splitter => arrange_children(r, id, rect, out),
        _ => {}
    }
}

/// Offset/size a child of natural size `m` inside `avail` along one axis.
fn aligned(align: Align, fixed: bool, avail: i32, m: i32) -> (i32, i32) {
    let align = if fixed && align == Align::Fill {
        Align::Start
    } else {
        align
    };
    match align {
        Align::Fill => (0, avail.max(0)),
        Align::Start => (0, m),
        Align::Center => ((avail - m) / 2, m),
        Align::End => (avail - m, m),
    }
}

fn arrange_children(r: &mut Registry, id: WidgetId, area: Rect, out: &mut Out) {
    let n = &r.nodes[&id];
    let (kind, p, sp) = (n.kind, n.lay.padding, n.lay.spacing);
    let inner = Rect::new(
        area.x + p,
        area.y + p,
        (area.w - 2 * p).max(0),
        (area.h - 2 * p).max(0),
    );
    let mut jobs: Vec<(WidgetId, Rect)> = vec![];
    if kind == Kind::Splitter {
        arrange_split(r, id, inner, &mut jobs, out);
    } else if kind == Kind::Grid {
        let (cells, size, exp) = grid_tracks(r, id);
        let mut size = size;
        for axis in 0..2 {
            let avail = if axis == 0 { inner.w } else { inner.h };
            let used: i32 =
                size[axis].iter().sum::<i32>() + sp * (size[axis].len() as i32 - 1).max(0);
            distribute(&mut size[axis], &exp[axis], avail - used);
        }
        let origin =
            |axis: usize, i: usize| -> i32 { size[axis][..i].iter().sum::<i32>() + sp * i as i32 };
        for (c, col, row, cs, rs) in cells {
            let span = |axis: usize, s: usize, i: usize| -> i32 {
                size[axis][i..i + s].iter().sum::<i32>() + sp * (s as i32 - 1)
            };
            let (cw, ch) = (span(0, cs, col), span(1, rs, row));
            let m = measure(r, c);
            let l = &r.nodes[&c].lay;
            let (ox, w) = aligned(l.align, l.fixed.is_some(), cw, m.w);
            let (oy, h) = aligned(l.align, l.fixed.is_some(), ch, m.h);
            jobs.push((
                c,
                Rect::new(
                    inner.x + origin(0, col) + ox,
                    inner.y + origin(1, row) + oy,
                    w,
                    h,
                ),
            ));
        }
    } else {
        let horiz = is_horizontal(kind);
        let kids = visible_children(r, id);
        let ms: Vec<Size> = kids.iter().map(|c| measure(r, *c)).collect();
        let mut main: Vec<i32> = ms.iter().map(|m| if horiz { m.w } else { m.h }).collect();
        let weights: Vec<f32> = kids.iter().map(|c| r.nodes[c].lay.expand).collect();
        let total: i32 = main
            .iter()
            .fold(gaps(sp, kids.len()), |t, m| t.saturating_add(*m));
        distribute(
            &mut main,
            &weights,
            (if horiz { inner.w } else { inner.h }) - total,
        );
        let cross_avail = if horiz { inner.h } else { inner.w };
        let mut pos = if horiz { inner.x } else { inner.y };
        for (i, c) in kids.iter().enumerate() {
            let l = &r.nodes[c].lay;
            let cm = if horiz { ms[i].h } else { ms[i].w };
            let (off, cs) = aligned(l.align, l.fixed.is_some(), cross_avail, cm);
            let rect = if horiz {
                Rect::new(pos, inner.y + off, main[i], cs)
            } else {
                Rect::new(inner.x + off, pos, cs, main[i])
            };
            jobs.push((*c, rect));
            pos = pos.saturating_add(main[i]).saturating_add(sp);
        }
    }
    if rtl() {
        for (_, rc) in jobs.iter_mut() {
            // mirror inside `inner`: new_x = 2 * inner.x + inner.w - (x + w)
            let mirrored =
                2 * i64::from(inner.x) + i64::from(inner.w) - i64::from(rc.x) - i64::from(rc.w);
            rc.x = mirrored.clamp(-i64::from(MAX_PX), i64::from(MAX_PX)) as i32;
        }
    }
    for (c, rect) in jobs {
        place(r, c, rect, out);
    }
}

/// Splitter: both panes fill the cross axis; the first gets the (clamped) position, the sash
/// `spacing` pixels, the second the rest. With fewer than two visible panes the sash is hidden
/// and a lone pane takes everything. `jobs` are in unmirrored coordinates (the caller mirrors).
fn arrange_split(
    r: &mut Registry,
    id: WidgetId,
    inner: Rect,
    jobs: &mut Vec<(WidgetId, Rect)>,
    out: &mut Out,
) {
    let panes = split_panes(r, id);
    let thick = r.nodes[&id].lay.spacing;
    let Some(sp) = r.nodes.get_mut(&id).and_then(|n| n.split_mut()) else {
        return;
    };
    let horiz = sp.orient == Orientation::Horizontal;
    let sash = sp.sash;
    let two = panes.len() == 2;
    if two {
        sp.thick = thick;
        sp.area = inner;
        let avail = if horiz { inner.w } else { inner.h };
        let want = sp.pos.unwrap_or((avail - thick) / 2);
        let p = sp.clamp(want, avail);
        sp.actual = p;
        sp.laid_out = true;
        let rest = (avail - thick - p).max(0);
        let (a, s, b) = if horiz {
            (
                Rect::new(inner.x, inner.y, p, inner.h),
                Rect::new(inner.x + p, inner.y, thick.min(avail - p), inner.h),
                Rect::new(inner.x + p + thick, inner.y, rest, inner.h),
            )
        } else {
            (
                Rect::new(inner.x, inner.y, inner.w, p),
                Rect::new(inner.x, inner.y + p, inner.w, thick.min(avail - p)),
                Rect::new(inner.x, inner.y + p + thick, inner.w, rest),
            )
        };
        jobs.push((panes[0], a));
        jobs.push((panes[1], b));
        if let Some(s_id) = sash {
            jobs.push((s_id, s));
        }
    } else if let Some(only) = panes.first() {
        jobs.push((*only, inner));
    }
    if let Some((s_id, n)) = sash
        .and_then(|s| Some(s).zip(r.nodes.get_mut(&s)))
        .filter(|(_, n)| n.visible != two)
    {
        n.visible = two;
        out.push((s_id, Prop::Visible(two)));
    }
}

/// Give `extra` pixels to entries proportionally to `weights` (no-op if extra <= 0 or no weights).
fn distribute(sizes: &mut [i32], weights: &[f32], extra: i32) {
    let total: f32 = weights.iter().sum();
    if extra <= 0 || total <= 0.0 {
        return;
    }
    let last = weights.iter().rposition(|w| *w > 0.0).unwrap_or(0);
    let mut given = 0;
    for (i, (s, w)) in sizes.iter_mut().zip(weights).enumerate() {
        if *w <= 0.0 {
            continue;
        }
        let share = if i == last {
            extra - given
        } else {
            (extra as f32 * w / total) as i32
        };
        *s += share;
        given += share;
    }
}

/// Lay out one window. Returns the `Bounds` updates to push (window size first).
pub fn compute(r: &mut Registry, w: WidgetId) -> Out {
    let mut out = vec![];
    let Some(n) = r.nodes.get(&w) else { return out };
    if n.kind != Kind::Window {
        return out;
    }
    let _pass = MeasurePass::begin();
    let m = measure(r, w);
    let Some(n) = r.nodes.get_mut(&w) else {
        return out;
    };
    let min = n.lay.min;
    let Some(win) = n.window_mut() else {
        return out;
    };
    if !win.explicit_size {
        win.client = m;
    }
    // a window is never laid out below its minimum size (backends may not enforce Prop::MinSize)
    // nor beyond what a display can show
    win.client = Size::new(
        win.client.w.max(min.w).clamp(0, MAX_WINDOW_PX),
        win.client.h.max(min.h).clamp(0, MAX_WINDOW_PX),
    );
    let rect = Rect::new(0, 0, win.client.w, win.client.h);
    place(r, w, rect, &mut out);
    out
}