teksilo-widgets 0.9.2

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
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
// SPDX-License-Identifier: MPL-2.0
// SPDX-FileCopyrightText: 2026 FernTech

//! FormLayout — a two-column settings or preferences form layout.
//!
//! Children are added as label/field pairs via [`FormLayout::line`] (inline
//! widgets) or [`FormLayout::line_ids`] (pre-registered IDs). Full-width rows
//! that span both columns — section headers, `Divider`s, or banners — are
//! added via [`FormLayout::full_width`] / [`FormLayout::full_width_id`]. The
//! label column auto-sizes to the widest label across all pairs so all field
//! inputs are left-aligned. RTL layouts are handled automatically: the label
//! column migrates to the trailing side and the field column moves to the
//! leading side. Dormant rows are excluded from both measurement and
//! placement.
//!
//! When an accessible name is provided via [`FormLayout::label`], the widget
//! emits `Role::Form` so screen-reader users can navigate directly to the
//! form. Without a name it demotes to a presentational `GenericContainer`.
//!
//! ```rust
//! # use teksilo_widgets::primitives::{FormLayout, TextWidget, RectWidget};
//! # use teksilo_i18n::lit;
//! let _form = FormLayout::new()
//!     .label_gap(8.0)
//!     .row_spacing(6.0)
//!     .line(TextWidget::new(lit!("Name:")),  RectWidget::new())
//!     .line(TextWidget::new(lit!("Email:")), RectWidget::new());
//! ```

use teksilo_canvas::{Point, Rect, Size, SizeProposal};
use teksilo_core::accessibility::AccessNodeBuilder;
use teksilo_core::widget::{LayoutContext, PaintContext, PendingChild, Widget, WidgetPlacement};
use teksilo_core::widget_id::WidgetId;
use teksilo_i18n::LocalizedString;

/// A resolved form row after `build()`.
#[derive(Debug, Clone)]
enum FormRow {
    /// A label/field pair occupying two columns.
    Pair(WidgetId, WidgetId),
    /// A single widget spanning the full width.
    FullWidth(WidgetId),
}

/// A pending form row before `build()`.
enum PendingFormRow {
    Pair(PendingChild, PendingChild),
    FullWidth(PendingChild),
}

impl std::fmt::Debug for PendingFormRow {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Pair(..) => f.write_str("PendingFormRow::Pair(..)"),
            Self::FullWidth(..) => f.write_str("PendingFormRow::FullWidth(..)"),
        }
    }
}

/// A two-column form layout with auto-sized label column.
///
/// Children are added as label/field pairs via [`line()`](Self::line) or as
/// full-width rows via [`full_width()`](Self::full_width). The label column
/// auto-sizes to the widest label; the field column takes the remaining
/// space.
///
/// ```text
/// ┌─ label col ─┐ gap ┌── field col ──────────────┐
/// │ Name:       │     │ [___________________]      │
/// │ Email:      │     │ [___________________]      │
/// ├─────────────┴─────┴────────────────────────────┤
/// │ ── Advanced ──────────────────────────────────  │  ← full_width
/// ├─ label col ─┐ gap ┌── field col ──────────────┐
/// │ Port:       │     │ [____]                     │
/// └─────────────┘     └────────────────────────────┘
/// ```
#[derive(Debug)]
pub struct FormLayout {
    rows: Vec<FormRow>,
    pending_rows: Vec<PendingFormRow>,
    label_gap: f32,
    row_spacing: f32,
    /// Stored unresolved so the AT name re-localizes on a locale change
    /// (the tree re-walks `accessibility()` and re-resolves) instead of
    /// freezing at build time.
    a11y_label: Option<LocalizedString>,
}

impl FormLayout {
    /// Create an empty `FormLayout` with zero label gap and zero row spacing.
    pub fn new() -> Self {
        Self {
            rows: Vec::new(),
            pending_rows: Vec::new(),
            label_gap: 0.0,
            row_spacing: 0.0,
            a11y_label: None,
        }
    }

    /// Horizontal gap between the label column and the field column.
    pub fn label_gap(mut self, gap: f32) -> Self {
        self.label_gap = gap;
        self
    }

    /// Vertical gap between rows.
    pub fn row_spacing(mut self, spacing: f32) -> Self {
        self.row_spacing = spacing;
        self
    }

    /// Set an accessible name for this form. When set, the widget emits
    /// the `Role::Form` landmark so assistive-technology users can
    /// navigate directly to it and distinguish it from other forms on
    /// the page. When unset, the widget demotes to a presentational
    /// `GenericContainer` — an unnamed landmark is worse than no
    /// landmark for AT users.
    pub fn label(mut self, label: impl Into<LocalizedString>) -> Self {
        self.a11y_label = Some(label.into());
        self
    }

    /// Add a label/field pair row.
    pub fn line(mut self, label: impl Widget + 'static, field: impl Widget + 'static) -> Self {
        self.pending_rows.push(PendingFormRow::Pair(
            PendingChild::Deferred(Box::new(label)),
            PendingChild::Deferred(Box::new(field)),
        ));
        self
    }

    /// Add a label/field pair row with pre-registered widget IDs.
    pub fn line_ids(mut self, label_id: WidgetId, field_id: WidgetId) -> Self {
        self.pending_rows.push(PendingFormRow::Pair(
            PendingChild::Id(label_id),
            PendingChild::Id(field_id),
        ));
        self
    }

    /// Add a full-width row spanning both columns.
    pub fn full_width(mut self, widget: impl Widget + 'static) -> Self {
        self.pending_rows
            .push(PendingFormRow::FullWidth(PendingChild::Deferred(Box::new(
                widget,
            ))));
        self
    }

    /// Add a full-width row with a pre-registered widget ID.
    pub fn full_width_id(mut self, id: WidgetId) -> Self {
        self.pending_rows
            .push(PendingFormRow::FullWidth(PendingChild::Id(id)));
        self
    }

    /// Flatten all rows into a child ID list.
    fn all_child_ids(&self) -> Vec<WidgetId> {
        let mut ids = Vec::new();
        for row in &self.rows {
            match row {
                FormRow::Pair(l, f) => {
                    ids.push(*l);
                    ids.push(*f);
                }
                FormRow::FullWidth(id) => ids.push(*id),
            }
        }
        ids
    }

    /// Width of the label column (max intrinsic width of all pair labels).
    fn compute_label_width(&self, ctx: &LayoutContext) -> f32 {
        let mut max_w = 0.0_f32;
        for row in &self.rows {
            if let FormRow::Pair(label_id, _) = row
                && let Some(s) = ctx.child_size(*label_id, SizeProposal::unspecified())
            {
                max_w = max_w.max(s.width);
            }
        }
        max_w
    }
}

fn resolve_pending(
    p: PendingChild,
    ctx: &mut teksilo_core::build_context::BuildContext,
) -> WidgetId {
    match p {
        PendingChild::Id(id) => id,
        PendingChild::Deferred(w) => ctx.add_boxed(w),
    }
}

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

impl Widget for FormLayout {
    fn build(&mut self, ctx: &mut teksilo_core::build_context::BuildContext) -> Vec<WidgetId> {
        let pending = std::mem::take(&mut self.pending_rows);
        if !pending.is_empty() {
            self.rows = pending
                .into_iter()
                .map(|row| match row {
                    PendingFormRow::Pair(label, field) => {
                        let l = resolve_pending(label, ctx);
                        let f = resolve_pending(field, ctx);
                        // WCAG 3.3.2 / EN 301 549 11.5.2.7: name the field after
                        // its visible label so assistive tech reads "<label>,
                        // edit text" instead of an unlabelled field. Serves both
                        // `line()` (deferred) and `line_ids()` (pre-registered).
                        ctx.access_labelled_by(f, l);
                        FormRow::Pair(l, f)
                    }
                    PendingFormRow::FullWidth(child) => {
                        FormRow::FullWidth(resolve_pending(child, ctx))
                    }
                })
                .collect();
        }
        self.all_child_ids()
    }

    fn layout_response(
        &self,
        proposal: SizeProposal,
        ctx: &LayoutContext,
    ) -> teksilo_core::widget::LayoutResponse {
        if self.rows.is_empty() {
            return (proposal.resolve(0.0, 0.0)).into();
        }

        let label_col_width = self.compute_label_width(ctx);

        // Determine available width and field column width.
        let (available_width, field_col_width) = if let Some(w) = proposal.width {
            let fcw = (w - label_col_width - self.label_gap).max(0.0);
            (w, fcw)
        } else {
            // Unbounded: compute intrinsic width.
            let mut max_field_w = 0.0_f32;
            let mut max_full_w = 0.0_f32;
            for row in &self.rows {
                match row {
                    FormRow::Pair(_, field_id) => {
                        if let Some(s) = ctx.child_size(*field_id, SizeProposal::unspecified()) {
                            max_field_w = max_field_w.max(s.width);
                        }
                    }
                    FormRow::FullWidth(id) => {
                        if let Some(s) = ctx.child_size(*id, SizeProposal::unspecified()) {
                            max_full_w = max_full_w.max(s.width);
                        }
                    }
                }
            }
            let pair_width = if label_col_width > 0.0 || max_field_w > 0.0 {
                label_col_width + self.label_gap + max_field_w
            } else {
                0.0
            };
            let total_w = pair_width.max(max_full_w);
            let fcw = (total_w - label_col_width - self.label_gap).max(0.0);
            (total_w, fcw)
        };

        // Compute total height from row heights.
        let label_proposal = SizeProposal::with_width(label_col_width);
        let field_proposal = SizeProposal::with_width(field_col_width);
        let full_proposal = SizeProposal::with_width(available_width);

        let mut total_height = 0.0_f32;
        let mut active_count = 0_usize;

        for row in &self.rows {
            let row_h = match row {
                FormRow::Pair(label_id, field_id) => {
                    let lh = ctx.child_size(*label_id, label_proposal).map(|s| s.height);
                    let fh = ctx.child_size(*field_id, field_proposal).map(|s| s.height);
                    match (lh, fh) {
                        (Some(l), Some(f)) => l.max(f),
                        (Some(l), None) => l,
                        (None, Some(f)) => f,
                        (None, None) => continue, // both dormant
                    }
                }
                FormRow::FullWidth(id) => match ctx.child_size(*id, full_proposal) {
                    Some(s) => s.height,
                    None => continue, // dormant
                },
            };
            total_height += row_h;
            active_count += 1;
        }

        if active_count > 1 {
            total_height += self.row_spacing * (active_count as f32 - 1.0);
        }

        Size::new(available_width, total_height).into()
    }

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

        let label_col_width = self.compute_label_width(ctx);
        let field_col_width = (bounds.width - label_col_width - self.label_gap).max(0.0);

        let rtl = ctx.is_rtl();
        let (label_x, field_x) = if rtl {
            (bounds.x + field_col_width + self.label_gap, bounds.x)
        } else {
            (bounds.x, bounds.x + label_col_width + self.label_gap)
        };

        let label_proposal = SizeProposal::with_width(label_col_width);
        let field_proposal = SizeProposal::with_width(field_col_width);
        let full_proposal = SizeProposal::with_width(bounds.width);

        let mut child_idx = 0;
        let mut y = bounds.y;
        let mut first_active = true;

        for row in &self.rows {
            match row {
                FormRow::Pair(label_id, field_id) => {
                    let label_active =
                        child_idx < children.len() && children[child_idx].id == *label_id;
                    let field_check_idx = if label_active {
                        child_idx + 1
                    } else {
                        child_idx
                    };
                    let field_active = field_check_idx < children.len()
                        && children[field_check_idx].id == *field_id;

                    if !label_active && !field_active {
                        continue; // entire row dormant
                    }

                    if !first_active {
                        y += self.row_spacing;
                    }
                    first_active = false;

                    let label_h = if label_active {
                        ctx.child_size(*label_id, label_proposal)
                            .map(|s| s.height)
                            .unwrap_or(0.0)
                    } else {
                        0.0
                    };
                    let field_h = if field_active {
                        ctx.child_size(*field_id, field_proposal)
                            .map(|s| s.height)
                            .unwrap_or(0.0)
                    } else {
                        0.0
                    };
                    let row_h = label_h.max(field_h);

                    if label_active {
                        children[child_idx].origin = Point::new(label_x, y);
                        children[child_idx].size = Size::new(label_col_width, row_h);
                        child_idx += 1;
                    }
                    if field_active {
                        children[child_idx].origin = Point::new(field_x, y);
                        children[child_idx].size = Size::new(field_col_width, row_h);
                        child_idx += 1;
                    }

                    y += row_h;
                }
                FormRow::FullWidth(id) => {
                    if child_idx >= children.len() || children[child_idx].id != *id {
                        continue; // dormant
                    }

                    if !first_active {
                        y += self.row_spacing;
                    }
                    first_active = false;

                    let child_h = ctx
                        .child_size(*id, full_proposal)
                        .map(|s| s.height)
                        .unwrap_or(0.0);

                    children[child_idx].origin = Point::new(bounds.x, y);
                    children[child_idx].size = Size::new(bounds.width, child_h);
                    child_idx += 1;

                    y += child_h;
                }
            }
        }
    }

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

    fn accessibility(&self, builder: &mut AccessNodeBuilder) {
        match self.a11y_label.as_ref() {
            Some(ls) => {
                builder.set_role(teksilo_core::accesskit::Role::Form);
                builder.set_name(ls.resolve_now());
            }
            None => {
                builder.set_role(teksilo_core::accesskit::Role::GenericContainer);
            }
        }
    }

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

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

    #[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()
        }
    }

    #[test]
    fn basic_form_places_label_and_field() {
        let mut tree = WidgetTree::new();
        let label = tree.add(FixedLeaf(60.0, 20.0));
        let field = tree.add(FixedLeaf(100.0, 25.0));
        let _form = tree.add(FormLayout::new().line_ids(label, field));
        tree.layout(SizeProposal::exact(300.0, 200.0));

        assert!((tree.bounds(label).x - 0.0).abs() < 0.01);
        assert!((tree.bounds(label).y - 0.0).abs() < 0.01);
        assert!((tree.bounds(field).x - 60.0).abs() < 0.01);
        assert!((tree.bounds(field).y - 0.0).abs() < 0.01);
        // Row height = max(20, 25) = 25
        assert!((tree.bounds(label).height - 25.0).abs() < 0.01);
        assert!((tree.bounds(field).height - 25.0).abs() < 0.01);
    }

    #[test]
    fn labeled_form_emits_form_role_and_name() {
        // The AT name is resolved lazily in `accessibility()` (not frozen at
        // build time), so a localized label still resolves correctly when the
        // tree is walked.
        let mut tree = WidgetTree::new();
        let form = tree.add(FormLayout::new().label(teksilo_i18n::lit!("Account")));
        tree.layout(SizeProposal::exact(300.0, 200.0));
        let info = tree.accessibility_node(form);
        assert_eq!(info.role(), teksilo_core::accesskit::Role::Form);
        assert_eq!(info.name(), Some("Account"));
    }

    #[test]
    fn line_wires_field_labelled_by_label() {
        // WCAG 3.3.2 (audit G8): a form field is named after its visible label
        // via a `labelled_by` relation, so assistive tech reads the label as
        // the field's accessible name.
        let mut tree = WidgetTree::new();
        let label = tree.add(FixedLeaf(60.0, 20.0));
        let field = tree.add(FixedLeaf(100.0, 25.0));
        let _form = tree.add(FormLayout::new().line_ids(label, field));
        tree.layout(SizeProposal::exact(300.0, 200.0));

        let update = tree.sync_accessibility();
        let field_nid = teksilo_core::accessibility::widget_id_to_node_id(field);
        let label_nid = teksilo_core::accessibility::widget_id_to_node_id(label);
        let field_node = update
            .nodes
            .iter()
            .find(|(id, _)| *id == field_nid)
            .map(|(_, n)| n)
            .expect("field node present in AT tree");
        assert!(
            field_node.labelled_by().contains(&label_nid),
            "field must be labelled_by its label node"
        );
    }

    #[test]
    fn unlabeled_form_is_presentational() {
        let mut tree = WidgetTree::new();
        let form = tree.add(FormLayout::new());
        tree.layout(SizeProposal::exact(300.0, 200.0));
        let info = tree.accessibility_node(form);
        assert_eq!(info.role(), teksilo_core::accesskit::Role::GenericContainer);
    }

    #[test]
    fn label_column_auto_sizes_to_widest() {
        let mut tree = WidgetTree::new();
        let l1 = tree.add(FixedLeaf(60.0, 20.0));
        let f1 = tree.add(FixedLeaf(100.0, 20.0));
        let l2 = tree.add(FixedLeaf(100.0, 20.0)); // wider label
        let f2 = tree.add(FixedLeaf(80.0, 20.0));
        let _form = tree.add(FormLayout::new().line_ids(l1, f1).line_ids(l2, f2));
        tree.layout(SizeProposal::exact(400.0, 200.0));

        // Label column = 100 (widest label). Both fields start at x=100.
        assert!((tree.bounds(f1).x - 100.0).abs() < 0.01);
        assert!((tree.bounds(f2).x - 100.0).abs() < 0.01);
        // Label column width = 100 for both labels
        assert!((tree.bounds(l1).width - 100.0).abs() < 0.01);
        assert!((tree.bounds(l2).width - 100.0).abs() < 0.01);
    }

    #[test]
    fn full_width_row_spans_entire_width() {
        let mut tree = WidgetTree::new();
        let fw = tree.add(FixedLeaf(50.0, 30.0));
        let _form = tree.add(FormLayout::new().full_width_id(fw));
        tree.layout(SizeProposal::exact(400.0, 200.0));

        assert!((tree.bounds(fw).x - 0.0).abs() < 0.01);
        assert!((tree.bounds(fw).width - 400.0).abs() < 0.01);
        assert!((tree.bounds(fw).height - 30.0).abs() < 0.01);
    }

    #[test]
    fn mixed_pair_and_full_width_rows() {
        let mut tree = WidgetTree::new();
        let l1 = tree.add(FixedLeaf(80.0, 20.0));
        let f1 = tree.add(FixedLeaf(100.0, 25.0));
        let fw = tree.add(FixedLeaf(200.0, 30.0));
        let l2 = tree.add(FixedLeaf(60.0, 20.0));
        let f2 = tree.add(FixedLeaf(100.0, 20.0));
        let _form = tree.add(
            FormLayout::new()
                .line_ids(l1, f1)
                .full_width_id(fw)
                .line_ids(l2, f2),
        );
        tree.layout(SizeProposal::exact(400.0, 200.0));

        // Row 0 (Pair): height 25, y=0
        assert!((tree.bounds(l1).y - 0.0).abs() < 0.01);
        // Row 1 (FullWidth): height 30, y=25
        assert!((tree.bounds(fw).y - 25.0).abs() < 0.01);
        // Row 2 (Pair): height 20, y=55
        assert!((tree.bounds(l2).y - 55.0).abs() < 0.01);
    }

    #[test]
    fn label_gap_applied() {
        let mut tree = WidgetTree::new();
        let label = tree.add(FixedLeaf(80.0, 20.0));
        let field = tree.add(FixedLeaf(100.0, 20.0));
        let _form = tree.add(FormLayout::new().label_gap(12.0).line_ids(label, field));
        tree.layout(SizeProposal::exact(400.0, 200.0));

        // Field starts at label_width + gap = 80 + 12 = 92
        assert!((tree.bounds(field).x - 92.0).abs() < 0.01);
    }

    #[test]
    fn row_spacing_applied() {
        let mut tree = WidgetTree::new();
        let l1 = tree.add(FixedLeaf(60.0, 20.0));
        let f1 = tree.add(FixedLeaf(100.0, 25.0));
        let l2 = tree.add(FixedLeaf(60.0, 20.0));
        let f2 = tree.add(FixedLeaf(100.0, 20.0));
        let _form = tree.add(
            FormLayout::new()
                .row_spacing(10.0)
                .line_ids(l1, f1)
                .line_ids(l2, f2),
        );
        tree.layout(SizeProposal::exact(400.0, 200.0));

        // Row 0 at y=0, height=25. Row 1 at y=25+10=35.
        assert!((tree.bounds(l1).y - 0.0).abs() < 0.01);
        assert!((tree.bounds(l2).y - 35.0).abs() < 0.01);
    }

    #[test]
    fn intrinsic_height_sums_rows() {
        let mut tree = WidgetTree::new();
        let l1 = tree.add(FixedLeaf(60.0, 20.0));
        let f1 = tree.add(FixedLeaf(100.0, 25.0));
        let l2 = tree.add(FixedLeaf(60.0, 30.0));
        let f2 = tree.add(FixedLeaf(100.0, 20.0));
        let form = tree.add(
            FormLayout::new()
                .row_spacing(5.0)
                .line_ids(l1, f1)
                .line_ids(l2, f2),
        );
        tree.layout(SizeProposal {
            width: Some(400.0),
            height: None,
        });

        // Row 0: max(20,25)=25. Row 1: max(30,20)=30. Total: 25+5+30=60
        assert!((tree.bounds(form).height - 60.0).abs() < 0.01);
    }

    #[test]
    fn field_column_gets_remaining_width() {
        let mut tree = WidgetTree::new();
        let label = tree.add(FixedLeaf(80.0, 20.0));
        let field = tree.add(FixedLeaf(100.0, 20.0));
        let _form = tree.add(FormLayout::new().label_gap(10.0).line_ids(label, field));
        tree.layout(SizeProposal::exact(400.0, 200.0));

        // Field width = 400 - 80 - 10 = 310
        assert!((tree.bounds(field).width - 310.0).abs() < 0.01);
    }

    #[test]
    fn single_pair_row() {
        let mut tree = WidgetTree::new();
        let label = tree.add(FixedLeaf(70.0, 20.0));
        let field = tree.add(FixedLeaf(100.0, 20.0));
        let _form = tree.add(FormLayout::new().line_ids(label, field));
        tree.layout(SizeProposal::exact(300.0, 200.0));

        assert!((tree.bounds(label).x - 0.0).abs() < 0.01);
        assert!((tree.bounds(field).x - 70.0).abs() < 0.01);
    }

    #[test]
    fn empty_form() {
        let mut tree = WidgetTree::new();
        let form = tree.add(FormLayout::new());
        tree.layout(SizeProposal {
            width: Some(300.0),
            height: None,
        });

        assert!((tree.bounds(form).height - 0.0).abs() < 0.01);
    }

    #[test]
    fn dormant_row_excluded_from_layout() {
        let mut tree = WidgetTree::new();
        let l1 = tree.add(FixedLeaf(60.0, 20.0));
        let f1 = tree.add(FixedLeaf(100.0, 25.0));
        let l2 = tree.add(FixedLeaf(60.0, 20.0));
        let f2 = tree.add(FixedLeaf(100.0, 30.0));
        let l3 = tree.add(FixedLeaf(60.0, 20.0));
        let f3 = tree.add(FixedLeaf(100.0, 20.0));
        let form = tree.add(
            FormLayout::new()
                .row_spacing(10.0)
                .line_ids(l1, f1)
                .line_ids(l2, f2)
                .line_ids(l3, f3),
        );
        tree.layout(SizeProposal::exact(400.0, 300.0));

        // Before dormant: row 0 at y=0 (h=25), row 1 at y=35 (h=30), row 2 at y=75
        assert!((tree.bounds(l3).y - 75.0).abs() < 0.01);

        // Make row 1 dormant
        tree.set_dormant(l2);
        tree.set_dormant(f2);
        tree.layout(SizeProposal {
            width: Some(400.0),
            height: None,
        });

        // Row 2 should move up: y = 25 + 10 = 35
        assert!((tree.bounds(l3).y - 35.0).abs() < 0.01);
        assert!((tree.bounds(f3).y - 35.0).abs() < 0.01);
        // Form height = 25 + 10 + 20 = 55
        assert!((tree.bounds(form).height - 55.0).abs() < 0.01);
    }

    #[test]
    fn unbounded_width_uses_intrinsic() {
        let mut tree = WidgetTree::new();
        let l1 = tree.add(FixedLeaf(80.0, 20.0));
        let f1 = tree.add(FixedLeaf(200.0, 20.0));
        let form = tree.add(FormLayout::new().label_gap(10.0).line_ids(l1, f1));
        tree.layout(SizeProposal {
            width: None,
            height: Some(200.0),
        });

        // Intrinsic width = 80 + 10 + 200 = 290
        assert!((tree.bounds(form).width - 290.0).abs() < 0.01);
    }

    #[test]
    fn deferred_line_api_works() {
        let mut tree = WidgetTree::new();
        let form = tree.add(
            FormLayout::new()
                .label_gap(10.0)
                .line(FixedLeaf(70.0, 20.0), FixedLeaf(150.0, 25.0)),
        );
        tree.layout(SizeProposal {
            width: Some(400.0),
            height: None,
        });

        // Form should have 2 children, height = max(20, 25) = 25
        assert!((tree.bounds(form).height - 25.0).abs() < 0.01);
    }

    #[test]
    fn rtl_swaps_label_and_field_columns() {
        let mut tree = WidgetTree::new();
        tree.set_layout_direction(teksilo_core::environment::LayoutDirection::RightToLeft);
        let label = tree.add(FixedLeaf(80.0, 20.0));
        let field = tree.add(FixedLeaf(100.0, 20.0));
        let _form = tree.add(FormLayout::new().label_gap(10.0).line_ids(label, field));
        tree.layout(SizeProposal::exact(400.0, 200.0));

        // field_col_width = 400 - 80 - 10 = 310
        // RTL: field at x=0, label at x=310+10=320
        assert!((tree.bounds(field).x - 0.0).abs() < 0.01);
        assert!((tree.bounds(label).x - 320.0).abs() < 0.01);
    }
}