teksilo-widgets 0.9.0

Widget library for Teksilo — over a hundred widgets and layout primitives, from Button to TreeTableView.
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
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
// SPDX-License-Identifier: MPL-2.0
// SPDX-FileCopyrightText: 2026 FernTech

//! HStack — a horizontal layout container that distributes children left-to-right.
//!
//! Children are given their intrinsic width and the stack's cross-axis height.
//! Positive slack (leftover space) is distributed among children that carry a
//! non-zero `flex` weight (e.g. `Spacer`, `Expand`); negative slack (over-constraint)
//! is absorbed by children with a non-zero `shrink` weight (e.g. a single-line
//! `TextWidget`). Vertical alignment defaults to `VAlignment::Center` and can be
//! overridden per-container or per-child.
//!
//! For a vertical counterpart see [`VStack`](crate::primitives::VStack).
//!
//! ```rust
//! # use teksilo_widgets::primitives::{HStack, TextWidget, Spacer};
//! # use teksilo_i18n::lit;
//! let _row = HStack::new()
//!     .spacing(8.0)
//!     .child(TextWidget::new(lit!("Label")))
//!     .child(Spacer::new())
//!     .child(TextWidget::new(lit!("Value")));
//! ```

use teksilo_canvas::{Point, Rect, Size, SizeProposal};
use teksilo_core::accessibility::AccessNodeBuilder;
use teksilo_core::signal::Prop;
use teksilo_core::widget::{LayoutContext, PaintContext, PendingChild, Widget, WidgetPlacement};
use teksilo_core::widget_id::WidgetId;
use teksilo_tokens::VAlignment;

use crate::primitives::linear_layout::{self, Axis};

/// Horizontal layout container that distributes children left-to-right.
///
/// Cross-axis (vertical) alignment defaults to `VAlignment::Center` and may be
/// overridden globally via [`alignment`](Self::alignment) or per-child via
/// `WidgetTree::set_alignment`.
#[derive(Debug)]
pub struct HStack {
    child_ids: Vec<WidgetId>,
    pending: Vec<PendingChild>,
    spacing: Prop<f32>,
    alignment: VAlignment,
}

impl HStack {
    /// Create an empty `HStack` with no spacing and `VAlignment::Center`.
    pub fn new() -> Self {
        Self {
            child_ids: Vec::new(),
            pending: Vec::new(),
            spacing: Prop::Static(0.0),
            alignment: VAlignment::Center,
        }
    }

    /// Set inter-child spacing. Accepts a static `f32` or a reactive
    /// `Signal<f32>` — use a signal derived from
    /// `ctx.theme_signal()` to track theme-driven spacing changes.
    pub fn spacing(mut self, spacing: impl Into<Prop<f32>>) -> Self {
        self.spacing = spacing.into();
        self
    }

    /// Set the vertical alignment for children that are shorter than the stack's height.
    pub fn alignment(mut self, alignment: VAlignment) -> Self {
        self.alignment = alignment;
        self
    }

    /// Add a pre-registered child by ID.
    pub fn add_child(mut self, id: WidgetId) -> Self {
        self.pending.push(PendingChild::Id(id));
        self
    }

    /// Add an inline child widget (deferred insertion).
    pub fn child(mut self, widget: impl Widget + 'static) -> Self {
        self.pending.push(PendingChild::Deferred(Box::new(widget)));
        self
    }

    /// Add multiple inline children from an iterator.
    pub fn children(mut self, iter: impl IntoIterator<Item = impl Widget + 'static>) -> Self {
        for widget in iter {
            self.pending.push(PendingChild::Deferred(Box::new(widget)));
        }
        self
    }

    /// Conditionally add a child. No-op if None.
    pub fn child_opt(mut self, widget: Option<impl Widget + 'static>) -> Self {
        if let Some(w) = widget {
            self.pending.push(PendingChild::Deferred(Box::new(w)));
        }
        self
    }
}

impl Default for HStack {
    fn default() -> Self {
        Self::new()
    }
}

impl Widget for HStack {
    fn layout_response(
        &self,
        proposal: SizeProposal,
        ctx: &LayoutContext,
    ) -> teksilo_core::widget::LayoutResponse {
        if self.child_ids.is_empty() {
            return proposal.resolve(0.0, 0.0).into();
        }
        // Main-then-cross negotiation along the horizontal axis: grow on
        // surplus (flex), shrink on a deficit (shrink/min), and measure each
        // child's height at its final width (height-for-width). See
        // [`linear_layout`].
        let neg = linear_layout::negotiate(
            &self.child_ids,
            ctx,
            proposal.width,
            proposal.height,
            self.spacing.get(),
            Axis::Horizontal,
        );
        linear_layout::response(&neg)
    }

    fn place_children(
        &self,
        bounds: Rect,
        _proposal: SizeProposal,
        children: &mut [WidgetPlacement],
        ctx: &LayoutContext,
    ) {
        if children.is_empty() {
            return;
        }

        let ids: Vec<WidgetId> = children.iter().map(|c| c.id).collect();
        let neg = linear_layout::negotiate(
            &ids,
            ctx,
            Some(bounds.width),
            Some(bounds.height),
            self.spacing.get(),
            Axis::Horizontal,
        );
        let widths = &neg.children.main;
        let heights = &neg.children.cross;

        // Place children along the main axis with cross-axis (vertical)
        // alignment. In RTL mode, children are placed right-to-left.
        let spacing = self.spacing.get();
        let rtl = ctx.is_rtl();
        if rtl {
            let mut x = bounds.right();
            for (i, child) in children.iter_mut().enumerate() {
                let w = widths[i];
                let h = heights[i];
                let valign = ctx
                    .child_alignment(child.id)
                    .map(|a| a.vertical)
                    .unwrap_or(self.alignment);
                let y_offset = valign.resolve(h, bounds.height);

                x -= w;
                child.origin = Point::new(x, bounds.y + y_offset);
                child.size = Size::new(w, h);
                x -= spacing;
            }
        } else {
            let mut x = bounds.x;
            for (i, child) in children.iter_mut().enumerate() {
                let w = widths[i];
                let h = heights[i];
                let valign = ctx
                    .child_alignment(child.id)
                    .map(|a| a.vertical)
                    .unwrap_or(self.alignment);
                let y_offset = valign.resolve(h, bounds.height);

                child.origin = Point::new(x, bounds.y + y_offset);
                child.size = Size::new(w, h);
                x += w + spacing;
            }
        }
    }

    fn paint(&self, _bounds: Rect, _canvas: &mut teksilo_canvas::Canvas, _ctx: &PaintContext) {}

    fn accessibility(&self, builder: &mut AccessNodeBuilder) {
        builder.set_role(teksilo_core::accesskit::Role::GenericContainer);
    }

    fn children(&self) -> Vec<WidgetId> {
        self.child_ids.clone()
    }

    fn build(&mut self, ctx: &mut teksilo_core::build_context::BuildContext) -> Vec<WidgetId> {
        let pending = std::mem::take(&mut self.pending);
        if !pending.is_empty() {
            self.child_ids = pending
                .into_iter()
                .map(|child| match child {
                    PendingChild::Id(id) => id,
                    PendingChild::Deferred(w) => ctx.add_boxed(w),
                })
                .collect();
        }
        // Register the spacing prop for dirty-tracking so theme-driven
        // signals trigger a relayout when they change.
        let self_id = ctx.self_id();
        let registry = ctx.binding_registry();
        self.spacing.register_if_bound(
            self_id,
            registry,
            teksilo_core::binding::BindingLevel::Relayout,
        );
        self.child_ids.clone()
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use teksilo_core::widget_tree::WidgetTree;

    /// A leaf that always reports a fixed intrinsic size.
    #[derive(Debug)]
    struct FixedLeaf(f32, f32);
    impl Widget for FixedLeaf {
        fn layout_response(
            &self,
            _proposal: SizeProposal,
            _ctx: &LayoutContext,
        ) -> teksilo_core::widget::LayoutResponse {
            Size::new(self.0, self.1).into()
        }
    }

    use teksilo_core::widget::LayoutResponse;

    /// A leaf with an explicit compression floor and shrink weight.
    #[derive(Debug)]
    struct ShrinkLeaf {
        wanted: f32,
        min: f32,
        shrink: f32,
        height: f32,
    }
    impl Widget for ShrinkLeaf {
        fn layout_response(&self, _p: SizeProposal, _ctx: &LayoutContext) -> LayoutResponse {
            LayoutResponse::shrinkable(
                Size::new(self.wanted, self.height),
                Size::new(self.min, self.height),
                self.shrink,
            )
        }
    }

    /// A height-for-width leaf: fixed "area", so its height is `area / width`
    /// at whatever width it is finally given (narrower → taller). Shrinkable
    /// down to `min_w`.
    #[derive(Debug)]
    struct AreaLeaf {
        area: f32,
        wanted_w: f32,
        min_w: f32,
    }
    impl Widget for AreaLeaf {
        fn layout_response(&self, p: SizeProposal, _ctx: &LayoutContext) -> LayoutResponse {
            let w = p.width.unwrap_or(self.wanted_w);
            let h = self.area / w.max(1.0);
            LayoutResponse::shrinkable(Size::new(w, h), Size::new(self.min_w, h), 1.0)
        }
    }

    #[test]
    fn children_get_intrinsic_widths() {
        let mut tree = WidgetTree::new();
        let a = tree.add(FixedLeaf(60.0, 30.0));
        let b = tree.add(FixedLeaf(40.0, 20.0));
        let _stack = tree.add(HStack::new().add_child(a).add_child(b));
        tree.layout(SizeProposal::exact(300.0, 50.0));

        assert!((tree.bounds(a).width - 60.0).abs() < 0.01);
        assert!((tree.bounds(b).width - 40.0).abs() < 0.01);
        assert!((tree.bounds(b).x - 60.0).abs() < 0.01);
    }

    // ── Over-constraint: shrink (Part B) ────────────────────────────────────

    #[test]
    fn shrink_distributes_deficit_by_weight() {
        // Two equal shrinkables, 60px deficit → 30px each.
        let mut tree = WidgetTree::new();
        let a = tree.add(ShrinkLeaf {
            wanted: 80.0,
            min: 20.0,
            shrink: 1.0,
            height: 20.0,
        });
        let b = tree.add(ShrinkLeaf {
            wanted: 80.0,
            min: 20.0,
            shrink: 1.0,
            height: 20.0,
        });
        let _stack = tree.add(HStack::new().add_child(a).add_child(b));
        tree.layout(SizeProposal::exact(100.0, 40.0));
        assert!(
            (tree.bounds(a).width - 50.0).abs() < 0.01,
            "a={}",
            tree.bounds(a).width
        );
        assert!(
            (tree.bounds(b).width - 50.0).abs() < 0.01,
            "b={}",
            tree.bounds(b).width
        );
    }

    #[test]
    fn shrink_priority_leaves_rigid_sibling_untouched() {
        // Shrinkable label absorbs the whole deficit; rigid icon keeps its size.
        let mut tree = WidgetTree::new();
        let label = tree.add(ShrinkLeaf {
            wanted: 80.0,
            min: 10.0,
            shrink: 1.0,
            height: 20.0,
        });
        let icon = tree.add(FixedLeaf(40.0, 20.0)); // rigid: shrink == 0
        let _stack = tree.add(HStack::new().add_child(label).add_child(icon));
        tree.layout(SizeProposal::exact(100.0, 40.0)); // deficit = 120 - 100 = 20
        assert!(
            (tree.bounds(label).width - 60.0).abs() < 0.01,
            "label={}",
            tree.bounds(label).width
        );
        assert!(
            (tree.bounds(icon).width - 40.0).abs() < 0.01,
            "icon={}",
            tree.bounds(icon).width
        );
    }

    #[test]
    fn shrink_clamps_at_min_with_residual_overflow() {
        // Deficit exceeds available shrink room: clamp at min, do not go below.
        let mut tree = WidgetTree::new();
        let a = tree.add(ShrinkLeaf {
            wanted: 80.0,
            min: 50.0,
            shrink: 1.0,
            height: 20.0,
        });
        let _stack = tree.add(HStack::new().add_child(a));
        tree.layout(SizeProposal::exact(30.0, 40.0)); // wants to shrink to 30, floored at 50
        assert!(
            (tree.bounds(a).width - 50.0).abs() < 0.01,
            "a={}",
            tree.bounds(a).width
        );
    }

    #[test]
    fn no_shrink_weight_overflows_unchanged() {
        // A rigid child still overflows (no silent shrink without opt-in).
        let mut tree = WidgetTree::new();
        let a = tree.add(FixedLeaf(80.0, 20.0));
        let _stack = tree.add(HStack::new().add_child(a));
        tree.layout(SizeProposal::exact(50.0, 40.0));
        assert!(
            (tree.bounds(a).width - 80.0).abs() < 0.01,
            "a={}",
            tree.bounds(a).width
        );
    }

    // ── Height-for-width (Part B) ───────────────────────────────────────────

    #[test]
    fn height_for_width_grows_cross_axis_on_shrink() {
        // AreaLeaf: area 4000, natural width 200 → height 20. Constrained to
        // width 50, it shrinks to 50 and its height becomes 4000/50 = 80; the
        // HStack's reported cross size must follow.
        let mut tree = WidgetTree::new();
        let leaf = tree.add(AreaLeaf {
            area: 4000.0,
            wanted_w: 200.0,
            min_w: 10.0,
        });
        let stack = tree.add(HStack::new().add_child(leaf));
        tree.layout(SizeProposal {
            width: Some(50.0),
            height: None, // ask the stack for its intrinsic height
        });
        assert!(
            (tree.bounds(leaf).width - 50.0).abs() < 0.01,
            "leaf w={}",
            tree.bounds(leaf).width
        );
        assert!(
            (tree.bounds(leaf).height - 80.0).abs() < 0.5,
            "leaf h={}",
            tree.bounds(leaf).height
        );
        assert!(
            (tree.bounds(stack).height - 80.0).abs() < 0.5,
            "stack height should follow height-for-width, got {}",
            tree.bounds(stack).height
        );
    }

    #[test]
    fn spacing_between_children() {
        let mut tree = WidgetTree::new();
        let a = tree.add(FixedLeaf(50.0, 30.0));
        let b = tree.add(FixedLeaf(50.0, 30.0));
        let _stack = tree.add(HStack::new().spacing(10.0).add_child(a).add_child(b));
        tree.layout(SizeProposal::exact(300.0, 50.0));

        assert!((tree.bounds(a).x - 0.0).abs() < 0.01);
        assert!((tree.bounds(b).x - 60.0).abs() < 0.01); // 50 + 10
    }

    #[test]
    fn cross_axis_center_alignment() {
        let mut tree = WidgetTree::new();
        let a = tree.add(FixedLeaf(50.0, 20.0));
        let _stack = tree.add(HStack::new().add_child(a)); // default: VAlignment::Center
        tree.layout(SizeProposal::exact(200.0, 60.0));

        // 20px child centered in 60px height: y = (60-20)/2 = 20
        assert!((tree.bounds(a).y - 20.0).abs() < 0.01);
    }

    #[test]
    fn cross_axis_top_alignment() {
        let mut tree = WidgetTree::new();
        let a = tree.add(FixedLeaf(50.0, 20.0));
        let _stack = tree.add(HStack::new().alignment(VAlignment::Top).add_child(a));
        tree.layout(SizeProposal::exact(200.0, 60.0));

        assert!((tree.bounds(a).y - 0.0).abs() < 0.01);
    }

    #[test]
    fn cross_axis_bottom_alignment() {
        let mut tree = WidgetTree::new();
        let a = tree.add(FixedLeaf(50.0, 20.0));
        let _stack = tree.add(HStack::new().alignment(VAlignment::Bottom).add_child(a));
        tree.layout(SizeProposal::exact(200.0, 60.0));

        assert!((tree.bounds(a).y - 40.0).abs() < 0.01); // 60 - 20
    }

    #[test]
    fn per_child_alignment_override() {
        let mut tree = WidgetTree::new();
        let a = tree.add(FixedLeaf(50.0, 20.0));
        let b = tree.add(FixedLeaf(50.0, 20.0));
        // Container default: Top, but b overrides to Bottom
        let _stack = tree.add(
            HStack::new()
                .alignment(VAlignment::Top)
                .add_child(a)
                .add_child(b),
        );
        tree.set_alignment(
            b,
            teksilo_tokens::Alignment {
                horizontal: teksilo_tokens::HAlignment::Center,
                vertical: teksilo_tokens::VAlignment::Bottom,
            },
        );
        tree.layout(SizeProposal::exact(200.0, 60.0));

        assert!((tree.bounds(a).y - 0.0).abs() < 0.01); // Top
        assert!((tree.bounds(b).y - 40.0).abs() < 0.01); // Bottom override
    }

    #[test]
    fn intrinsic_size_sums_children() {
        let stack = HStack::new().spacing(5.0);
        // Without arena, size_that_fits falls back to proposal
        let theme = teksilo_core::presets::intui::light();
        let ctx = LayoutContext::for_testing(&theme);
        let size = stack
            .layout_response(SizeProposal::exact(100.0, 50.0), &ctx)
            .size;
        // No children queryable without arena, so returns proposal
        assert_eq!(size.width, 100.0);
        assert_eq!(size.height, 50.0);
    }

    #[test]
    fn empty_hstack() {
        let mut tree = WidgetTree::new();
        let _stack = tree.add(HStack::new());
        tree.layout(SizeProposal::exact(200.0, 50.0));
        // No crash, no children to place
    }

    #[test]
    fn flex_distributes_proportionally() {
        // [Expand::flex(1), Expand::flex(2)] in 300px → 100, 200.
        use crate::primitives::expand::Expand;
        let mut tree = WidgetTree::new();
        let a = tree.add(Expand::new().flex(1.0));
        let b = tree.add(Expand::new().flex(2.0));
        let _stack = tree.add(HStack::new().add_child(a).add_child(b));
        tree.layout(SizeProposal::exact(300.0, 50.0));

        assert!(
            (tree.bounds(a).width - 100.0).abs() < 0.01,
            "a.width={}",
            tree.bounds(a).width
        );
        assert!(
            (tree.bounds(b).width - 200.0).abs() < 0.01,
            "b.width={}",
            tree.bounds(b).width
        );
    }

    #[test]
    fn flex_with_rigid_floor() {
        // [Fixed(100), Expand::flex(1), Expand::flex(2)] in 400 → 100, 100, 200.
        use crate::primitives::expand::Expand;
        let mut tree = WidgetTree::new();
        let fixed = tree.add(FixedLeaf(100.0, 30.0));
        let a = tree.add(Expand::new().flex(1.0));
        let b = tree.add(Expand::new().flex(2.0));
        let _stack = tree.add(HStack::new().add_child(fixed).add_child(a).add_child(b));
        tree.layout(SizeProposal::exact(400.0, 50.0));

        assert!((tree.bounds(fixed).width - 100.0).abs() < 0.01);
        assert!((tree.bounds(a).width - 100.0).abs() < 0.01);
        assert!((tree.bounds(b).width - 200.0).abs() < 0.01);
    }

    #[test]
    fn spacer_min_length_is_floor_plus_share() {
        // [Spacer::min(20), Spacer::min(20)] in 100 → 50, 50
        // (each gets 20 floor + 30 share).
        use crate::primitives::spacer::Spacer;
        let mut tree = WidgetTree::new();
        let a = tree.add(Spacer::new().min_length(20.0));
        let b = tree.add(Spacer::new().min_length(20.0));
        let _stack = tree.add(HStack::new().add_child(a).add_child(b));
        tree.layout(SizeProposal::exact(100.0, 50.0));

        assert!((tree.bounds(a).width - 50.0).abs() < 0.01);
        assert!((tree.bounds(b).width - 50.0).abs() < 0.01);
    }

    #[test]
    fn expand_default_is_flex_one_in_hstack() {
        // `Expand::new()` (no .flex(n)) inside HStack claims all leftover.
        // The footgun fix: previously `Expand::new()` collapsed without
        // `.fills_stack()`.
        use crate::primitives::expand::Expand;
        let mut tree = WidgetTree::new();
        let fixed = tree.add(FixedLeaf(80.0, 30.0));
        let expand = tree.add(Expand::new());
        let _stack = tree.add(HStack::new().add_child(fixed).add_child(expand));
        tree.layout(SizeProposal::exact(200.0, 50.0));

        assert!((tree.bounds(fixed).width - 80.0).abs() < 0.01);
        assert!((tree.bounds(expand).width - 120.0).abs() < 0.01);
    }

    #[test]
    fn respect_intrinsic_uses_child_natural_as_floor() {
        // With respect_intrinsic, Expand::flex(1) wrapping a 60px child
        // contributes 60 to the rigid pool, then gets the remaining slack.
        // [Expand::flex(1).respect_intrinsic(child=60), Fixed(100)] in 300:
        //   - Expand wants 60 (auto-basis), flex=1
        //   - Fixed wants 100, no flex
        //   - slack = 300 - 60 - 100 = 140, all to Expand
        //   - Final: Expand = 60 + 140 = 200, Fixed = 100
        use crate::primitives::expand::Expand;
        let mut tree = WidgetTree::new();
        let inner = tree.add(FixedLeaf(60.0, 20.0));
        let expand = tree.add(Expand::new().respect_intrinsic().child_id(inner));
        let fixed = tree.add(FixedLeaf(100.0, 30.0));
        let _stack = tree.add(HStack::new().add_child(expand).add_child(fixed));
        tree.layout(SizeProposal::exact(300.0, 50.0));

        assert!((tree.bounds(expand).width - 200.0).abs() < 0.01);
        assert!((tree.bounds(fixed).width - 100.0).abs() < 0.01);
    }

    #[test]
    fn rtl_reverses_child_order() {
        let mut tree = WidgetTree::new();
        tree.set_layout_direction(teksilo_core::environment::LayoutDirection::RightToLeft);
        let a = tree.add(FixedLeaf(60.0, 30.0));
        let b = tree.add(FixedLeaf(40.0, 30.0));
        let _stack = tree.add(HStack::new().add_child(a).add_child(b));
        tree.layout(SizeProposal {
            width: None,
            height: Some(50.0),
        });

        // In RTL, first child (a) is placed on the right, second (b) on the left.
        // HStack without spacers sizes to content: 60+40 = 100px wide.
        let ab = tree.bounds(a);
        let bb = tree.bounds(b);
        assert!(ab.x > bb.x, "a.x={} should be > b.x={} in RTL", ab.x, bb.x);
        // a (60px) at right edge of 100px HStack
        assert!((ab.x - 40.0).abs() < 0.01, "a.x={}", ab.x);
        // b (40px) to the left of a
        assert!((bb.x - 0.0).abs() < 0.01, "b.x={}", bb.x);
    }
}