teksilo-widgets 0.9.1

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
// SPDX-License-Identifier: MPL-2.0
// SPDX-FileCopyrightText: 2026 FernTech

//! Widget-level integration tests for `Splitter` (headless `WidgetTree`).
//! The pure sizing engine is unit-tested in `distribute.rs`; the model in
//! `model.rs`. Here we cover placement, drag, keyboard, collapse triggers,
//! RTL, clipping, and accessibility through a real arena.

use super::*;
use teksilo_canvas::{Point, Size, SizeProposal};
use teksilo_core::event::{Key, Modifiers, PointerButton};
use teksilo_core::widget::{LayoutContext, LayoutResponse, Widget};
use teksilo_core::widget_tree::WidgetTree;

#[derive(Debug)]
struct FixedLeaf(f32, f32);

impl Widget for FixedLeaf {
    fn layout_response(&self, _proposal: SizeProposal, _ctx: &LayoutContext) -> LayoutResponse {
        Size::new(self.0, self.1).into()
    }
}

fn theme_tree() -> WidgetTree {
    WidgetTree::new().with_theme(teksilo_core::presets::intui::light())
}

/// Two-pane horizontal model with exact sizes, `min = 0`, `stretch = 0`
/// (so `distribute` returns the sizes unchanged when they fill `available`).
fn h_model(sizes: &[f32]) -> SplitterModel {
    SplitterModel::from_panes(
        sizes
            .iter()
            .map(|&s| PaneDescriptor::new().size(s).min_size(0.0).stretch(0.0))
            .collect(),
        Orientation::Horizontal,
    )
}

#[test]
fn collapsed_pane_with_collapsed_size_keeps_a_sliver_live() {
    let model = SplitterModel::from_panes(
        vec![
            PaneDescriptor::new()
                .size(200.0)
                .min_size(0.0)
                .stretch(0.0)
                .collapsed_size(32.0),
            PaneDescriptor::new().size(200.0).min_size(0.0).stretch(1.0),
        ],
        Orientation::Vertical,
    );
    let mut tree = theme_tree();
    let root = tree.add(
        Splitter::new(model.clone())
            .pane(FixedLeaf(80.0, 300.0))
            .pane(FixedLeaf(80.0, 100.0)),
    );
    tree.layout(SizeProposal::exact(200.0, 400.0));

    model.set_collapsed_immediate(0, true);
    tree.layout(SizeProposal::exact(200.0, 400.0));

    let pane0 = tree.child_widget(root, 0);
    let pb = tree.bounds(pane0);
    assert!(
        pb.height > 20.0 && pb.height < 45.0,
        "collapsed pane folds to ~32 (collapsed_size), got {}",
        pb.height
    );
    // The pane's content must stay LIVE (clipped sliver), not dormant.
    let content = tree.child_widget(pane0, 0);
    assert!(
        tree.bounds(content).height > 1.0,
        "collapsed-pane content (the sliver) must stay live, got {}",
        tree.bounds(content).height
    );
}

#[test]
fn horizontal_places_panes_and_dividers() {
    let avail = 400.0 - SPLITTER_GUTTER_THICKNESS;
    let model = h_model(&[avail * 0.25, avail * 0.75]);
    let mut tree = theme_tree();
    let root = tree.add(
        Splitter::new(model)
            .pane(FixedLeaf(100.0, 40.0))
            .pane(FixedLeaf(100.0, 40.0)),
    );
    tree.layout(SizeProposal::exact(400.0, 200.0));

    let first = tree.child_widget(root, 0);
    let handle = tree.child_widget(root, 1);
    let second = tree.child_widget(root, 2);
    assert!((tree.bounds(first).width - avail * 0.25).abs() < 0.5);
    assert!((tree.bounds(handle).width - SPLITTER_GUTTER_THICKNESS).abs() < 0.01);
    assert!((tree.bounds(second).width - avail * 0.75).abs() < 0.5);
}

#[test]
fn vertical_places_panes_and_dividers() {
    let avail = 400.0 - SPLITTER_GUTTER_THICKNESS;
    let model = SplitterModel::from_panes(
        vec![
            PaneDescriptor::new()
                .size(avail * 0.25)
                .min_size(0.0)
                .stretch(0.0),
            PaneDescriptor::new()
                .size(avail * 0.75)
                .min_size(0.0)
                .stretch(0.0),
        ],
        Orientation::Vertical,
    );
    let mut tree = theme_tree();
    let root = tree.add(
        Splitter::new(model)
            .pane(FixedLeaf(80.0, 100.0))
            .pane(FixedLeaf(80.0, 100.0)),
    );
    tree.layout(SizeProposal::exact(200.0, 400.0));

    let first = tree.child_widget(root, 0);
    let second = tree.child_widget(root, 2);
    assert!((tree.bounds(first).height - avail * 0.25).abs() < 0.5);
    assert!((tree.bounds(second).height - avail * 0.75).abs() < 0.5);
}

#[test]
fn three_panes_have_two_handles() {
    let model = SplitterModel::new(3, Orientation::Horizontal);
    let mut tree = theme_tree();
    let root = tree.add(
        Splitter::new(model)
            .pane(FixedLeaf(50.0, 40.0))
            .pane(FixedLeaf(50.0, 40.0))
            .pane(FixedLeaf(50.0, 40.0)),
    );
    tree.layout(SizeProposal::exact(600.0, 200.0));
    // children: pane,handle,pane,handle,pane = 5
    assert_eq!(tree.children(root).len(), 5);
    // Equal thirds (each gets min 0 + equal stretch slack).
    let avail = 600.0 - 2.0 * SPLITTER_GUTTER_THICKNESS;
    for idx in [0usize, 2, 4] {
        let w = tree.bounds(tree.child_widget(root, idx)).width;
        assert!((w - avail / 3.0).abs() < 1.0, "pane {idx} width {w}");
    }
}

#[test]
fn drag_moves_the_boundary() {
    let avail = 400.0 - SPLITTER_GUTTER_THICKNESS;
    let model = h_model(&[avail * 0.5, avail * 0.5]);
    let mut tree = theme_tree();
    let root = tree.add(
        Splitter::new(model.clone())
            .pane(FixedLeaf(100.0, 40.0))
            .pane(FixedLeaf(100.0, 40.0)),
    );
    tree.layout(SizeProposal::exact(400.0, 200.0));

    let handle = tree.child_widget(root, 1);
    let start = tree.bounds(handle).center();
    tree.drag(start, Point::new(start.x + 80.0, start.y));

    assert!(
        model.stored_size(0) > avail * 0.5 + 50.0,
        "pane0 should grow, got {}",
        model.stored_size(0)
    );
}

#[test]
fn drag_tracks_cursor_across_relayouts() {
    // Regression: pointer events arrive localized to the *handle*, which
    // slides as the divider is dragged. The drag must recover
    // window-absolute coords, or the size chases a moving target (lag +
    // jitter). Move in steps, relaying out between each (as the real app
    // does on every bound `version` change), and assert the divider lands
    // at the cursor — not behind it.
    let model = h_model(&[300.0, 300.0]);
    let mut tree = theme_tree();
    let root = tree.add(
        Splitter::new(model)
            .pane(FixedLeaf(50.0, 40.0))
            .pane(FixedLeaf(50.0, 40.0)),
    );
    tree.layout(SizeProposal::exact(600.0, 200.0));

    let handle = tree.child_widget(root, 1);
    let start = tree.bounds(handle).center();
    tree.pointer_down_button(start, PointerButton::Primary);
    let target_x = 440.0;
    for step_x in [start.x + 40.0, start.x + 90.0, target_x] {
        tree.pointer_move(Point::new(step_x, start.y));
        tree.layout(SizeProposal::exact(600.0, 200.0));
    }
    tree.pointer_up_button(Point::new(target_x, start.y), PointerButton::Primary);

    let container = tree.bounds(root);
    let expected_pane0 = target_x - container.x - SPLITTER_GUTTER_THICKNESS / 2.0;
    let pane0 = tree.bounds(tree.child_widget(root, 0)).width;
    assert!(
        (pane0 - expected_pane0).abs() < 4.0,
        "divider should track the cursor across relayouts: pane0={pane0}, expected~{expected_pane0}"
    );
}

#[test]
fn minimum_sizes_clamp() {
    let model = SplitterModel::from_panes(
        vec![
            PaneDescriptor::new()
                .size(10.0)
                .min_size(120.0)
                .stretch(0.0),
            PaneDescriptor::new()
                .size(10.0)
                .min_size(120.0)
                .stretch(0.0),
        ],
        Orientation::Horizontal,
    );
    let mut tree = theme_tree();
    let root = tree.add(
        Splitter::new(model)
            .pane(FixedLeaf(100.0, 40.0))
            .pane(FixedLeaf(100.0, 40.0)),
    );
    tree.layout(SizeProposal::exact(300.0, 160.0));
    assert!(tree.bounds(tree.child_widget(root, 0)).width >= 119.5);
    assert!(tree.bounds(tree.child_widget(root, 2)).width >= 119.5);
}

#[test]
fn keyboard_resizes_focused_handle() {
    let avail = 400.0 - SPLITTER_GUTTER_THICKNESS;
    let model = h_model(&[avail * 0.5, avail * 0.5]);
    let mut tree = theme_tree();
    let root = tree.add(
        Splitter::new(model.clone())
            .pane(FixedLeaf(100.0, 40.0))
            .pane(FixedLeaf(100.0, 40.0)),
    );
    tree.layout(SizeProposal::exact(400.0, 200.0));

    let handle = tree.child_widget(root, 1);
    tree.focus(handle);
    let before = model.stored_size(0);
    tree.press_key(Key::ArrowRight, Modifiers::NONE);
    assert!(model.stored_size(0) > before, "arrow-right grows pane0");
    assert_eq!(tree.focused(), Some(handle));
}

#[test]
fn rtl_horizontal_mirrors_pane_order() {
    let avail = 400.0 - SPLITTER_GUTTER_THICKNESS;
    let model = h_model(&[avail * 0.3, avail * 0.7]);
    let mut tree = theme_tree();
    tree.set_layout_direction(teksilo_core::environment::LayoutDirection::RightToLeft);
    let root = tree.add(
        Splitter::new(model)
            .pane(FixedLeaf(100.0, 40.0))
            .pane(FixedLeaf(100.0, 40.0)),
    );
    tree.layout(SizeProposal::exact(400.0, 200.0));
    // Model index 0 is the leading pane → on the RIGHT under RTL.
    let first = tree.bounds(tree.child_widget(root, 0));
    let second = tree.bounds(tree.child_widget(root, 2));
    assert!(first.x > second.x, "leading pane should sit at the right");
}

#[test]
fn rtl_vertical_still_stacks_top_to_bottom() {
    let model = SplitterModel::new(2, Orientation::Vertical);
    let mut tree = theme_tree();
    tree.set_layout_direction(teksilo_core::environment::LayoutDirection::RightToLeft);
    let root = tree.add(
        Splitter::new(model)
            .pane(FixedLeaf(80.0, 100.0))
            .pane(FixedLeaf(80.0, 100.0)),
    );
    tree.layout(SizeProposal::exact(200.0, 400.0));
    let first = tree.bounds(tree.child_widget(root, 0));
    let second = tree.bounds(tree.child_widget(root, 2));
    assert!(
        first.y < second.y,
        "first pane stays above under RTL+vertical"
    );
}

#[test]
fn panes_are_wrapped_in_clip_containers() {
    let model = SplitterModel::new(2, Orientation::Horizontal);
    let mut tree = theme_tree();
    let root = tree.add(
        Splitter::new(model)
            .pane(FixedLeaf(500.0, 40.0))
            .pane(FixedLeaf(500.0, 40.0)),
    );
    tree.layout(SizeProposal::exact(400.0, 200.0));
    assert_eq!(tree.children(tree.child_widget(root, 0)).len(), 1);
    assert_eq!(tree.children(tree.child_widget(root, 2)).len(), 1);
}

#[test]
fn handle_exposes_splitter_accessibility() {
    let model = SplitterModel::new(2, Orientation::Horizontal);
    let mut tree = theme_tree();
    tree.add(
        Splitter::new(model)
            .pane(FixedLeaf(100.0, 40.0))
            .pane(FixedLeaf(100.0, 40.0)),
    );
    tree.layout(SizeProposal::exact(400.0, 200.0));
    let handle = tree
        .find_by_role(teksilo_core::accesskit::Role::Splitter)
        .unwrap();
    let info = tree.accessibility_node(handle);
    assert_eq!(info.role(), teksilo_core::accesskit::Role::Splitter);
    assert!(
        info.actions()
            .contains(&teksilo_core::accesskit::Action::Increment)
    );
}

#[test]
fn programmatic_collapse_shrinks_pane_to_zero() {
    let avail = 400.0 - SPLITTER_GUTTER_THICKNESS;
    let model = SplitterModel::from_panes(
        vec![
            PaneDescriptor::new()
                .size(avail * 0.5)
                .collapsible(true)
                .stretch(0.0),
            PaneDescriptor::new()
                .size(avail * 0.5)
                .min_size(0.0)
                .stretch(0.0),
        ],
        Orientation::Horizontal,
    );
    let mut tree = theme_tree();
    let root = tree.add(
        Splitter::new(model.clone())
            .pane(FixedLeaf(100.0, 40.0))
            .pane(FixedLeaf(100.0, 40.0)),
    );
    tree.layout(SizeProposal::exact(400.0, 200.0));

    model.set_collapsed(0, true);
    tree.tick_animations(std::time::Duration::from_millis(400));
    tree.layout(SizeProposal::exact(400.0, 200.0));

    assert!(
        tree.bounds(tree.child_widget(root, 0)).width < 5.0,
        "collapsed pane should be ~0, got {}",
        tree.bounds(tree.child_widget(root, 0)).width
    );
    // The freed space flows to the surviving pane.
    assert!(tree.bounds(tree.child_widget(root, 2)).width > avail - 5.0);
}

#[test]
fn enter_on_focused_handle_toggles_adjacent_collapse() {
    let model = SplitterModel::from_panes(
        vec![
            PaneDescriptor::new().collapsible(true),
            PaneDescriptor::new(),
        ],
        Orientation::Horizontal,
    );
    let mut tree = theme_tree();
    let root = tree.add(
        Splitter::new(model.clone())
            .pane(FixedLeaf(100.0, 40.0))
            .pane(FixedLeaf(100.0, 40.0)),
    );
    tree.layout(SizeProposal::exact(400.0, 200.0));
    let handle = tree.child_widget(root, 1);
    tree.focus(handle);
    assert!(!model.is_collapsed(0));
    tree.press_key(Key::Enter, Modifiers::NONE);
    assert!(
        model.is_collapsed(0),
        "Enter collapses the collapsible pane"
    );
}

#[test]
fn double_click_handle_toggles_adjacent_collapse() {
    let model = SplitterModel::from_panes(
        vec![
            PaneDescriptor::new().collapsible(true),
            PaneDescriptor::new(),
        ],
        Orientation::Horizontal,
    );
    let mut tree = theme_tree();
    let root = tree.add(
        Splitter::new(model.clone())
            .pane(FixedLeaf(100.0, 40.0))
            .pane(FixedLeaf(100.0, 40.0)),
    );
    tree.layout(SizeProposal::exact(400.0, 200.0));
    let center = tree.bounds(tree.child_widget(root, 1)).center();
    // Two taps at the same spot → on_double_tap.
    tree.pointer_down_button(center, PointerButton::Primary);
    tree.pointer_up_button(center, PointerButton::Primary);
    tree.pointer_down_button(center, PointerButton::Primary);
    tree.pointer_up_button(center, PointerButton::Primary);
    assert!(model.is_collapsed(0), "double-click collapses the pane");
}

#[test]
fn drag_past_min_snaps_to_collapsed() {
    let avail = 400.0 - SPLITTER_GUTTER_THICKNESS;
    let model = SplitterModel::from_panes(
        vec![
            PaneDescriptor::new().size(avail * 0.5).collapsible(true),
            PaneDescriptor::new().size(avail * 0.5),
        ],
        Orientation::Horizontal,
    );
    let mut tree = theme_tree();
    let root = tree.add(
        Splitter::new(model.clone())
            .pane(FixedLeaf(100.0, 40.0))
            .pane(FixedLeaf(100.0, 40.0)),
    );
    tree.layout(SizeProposal::exact(400.0, 200.0));
    let handle = tree.child_widget(root, 1);
    let start = tree.bounds(handle).center();
    // Drag the divider hard to the leading edge → pane0 below min−snap.
    tree.drag(start, Point::new(8.0, start.y));
    assert!(
        model.is_collapsed(0),
        "dragging past min snaps to collapsed"
    );
}

#[test]
fn drag_restores_collapsed_pane_and_keeps_it_open() {
    // Regression: pulling a collapsed pane's divider out must restore it and
    // let it track the cursor — not pop open and immediately snap shut
    // because the cursor is still in the collapse zone (inverted hysteresis).
    let model = SplitterModel::from_panes(
        vec![
            PaneDescriptor::new()
                .collapsible(true)
                .collapsed(true)
                .min_size(96.0),
            PaneDescriptor::new().min_size(0.0),
        ],
        Orientation::Horizontal,
    );
    let mut tree = theme_tree();
    let root = tree.add(
        Splitter::new(model.clone())
            .pane(FixedLeaf(50.0, 40.0))
            .pane(FixedLeaf(50.0, 40.0)),
    );
    tree.layout(SizeProposal::exact(400.0, 200.0));
    assert!(model.is_collapsed(0));

    // Grab the (leading-edge) handle and pull it out in steps, relaying out
    // between each so the handle slides under the cursor.
    let handle = tree.child_widget(root, 1);
    let start = tree.bounds(handle).center();
    tree.pointer_down_button(start, PointerButton::Primary);
    for x in [40.0, 120.0, 210.0] {
        tree.pointer_move(Point::new(x, start.y));
        tree.layout(SizeProposal::exact(400.0, 200.0));
    }
    tree.pointer_up_button(Point::new(210.0, start.y), PointerButton::Primary);

    assert!(
        !model.is_collapsed(0),
        "pane should be restored, not re-collapsed"
    );
    assert!(
        tree.bounds(tree.child_widget(root, 0)).width > 150.0,
        "restored pane should track the cursor, got {}",
        tree.bounds(tree.child_widget(root, 0)).width
    );
}

#[test]
fn collapsed_pane_content_is_dormant() {
    // A collapsed pane's content must be parked dormant — out of paint, the
    // focus order, hit-test, and the a11y tree — not merely sized to zero.
    let model = SplitterModel::from_panes(
        vec![
            PaneDescriptor::new().collapsible(true),
            PaneDescriptor::new(),
        ],
        Orientation::Horizontal,
    );
    let mut tree = theme_tree();
    let root = tree.add(
        Splitter::new(model.clone())
            .pane(FixedLeaf(80.0, 40.0))
            .pane(FixedLeaf(80.0, 40.0)),
    );
    tree.layout(SizeProposal::exact(400.0, 200.0));

    let clip0 = tree.child_widget(root, 0);
    let inner0 = tree.child_widget(clip0, 0);
    assert!(tree.is_active(inner0), "expanded pane content is active");

    model.set_collapsed(0, true);
    tree.tick_animations(std::time::Duration::from_millis(400));
    tree.layout(SizeProposal::exact(400.0, 200.0));
    assert!(
        !tree.is_active(inner0),
        "collapsed pane content must be dormant (excluded from AT / focus)"
    );

    // Re-expanding reactivates it.
    model.set_collapsed(0, false);
    tree.tick_animations(std::time::Duration::from_millis(400));
    tree.layout(SizeProposal::exact(400.0, 200.0));
    assert!(
        tree.is_active(inner0),
        "re-expanded content must reactivate"
    );
}

#[test]
fn collapsing_content_keeps_full_layout_size() {
    // Mid-collapse, the content must stay laid out at the pane's full size
    // (and be clipped) rather than reflow into the shrinking pane.
    let model = SplitterModel::from_panes(
        vec![
            PaneDescriptor::new()
                .size(200.0)
                .min_size(0.0)
                .stretch(0.0)
                .collapsible(true),
            PaneDescriptor::new().size(194.0).min_size(0.0).stretch(0.0),
        ],
        Orientation::Horizontal,
    );
    let mut tree = theme_tree();
    let root = tree.add(
        Splitter::new(model.clone())
            .pane(FixedLeaf(50.0, 40.0))
            .pane(FixedLeaf(50.0, 40.0)),
    );
    tree.layout(SizeProposal::exact(400.0, 200.0));
    let clip0 = tree.child_widget(root, 0);
    let inner0 = tree.child_widget(clip0, 0);

    model.set_collapsed(0, true);
    // Tick to roughly the middle of the collapse tween.
    tree.tick_animations(std::time::Duration::from_millis(80));
    tree.layout(SizeProposal::exact(400.0, 200.0));

    let clip_w = tree.bounds(clip0).width;
    let inner_w = tree.bounds(inner0).width;
    assert!(clip_w < 190.0, "clip should be shrinking, got {clip_w}");
    assert!(
        inner_w > clip_w + 20.0,
        "content should keep its full width ({inner_w}) while the clip shrinks ({clip_w})"
    );
}

#[test]
fn hiding_a_pane_removes_pane_and_its_gutter() {
    let model = SplitterModel::new(2, Orientation::Horizontal);
    let mut tree = theme_tree();
    let root = tree.add(
        Splitter::new(model.clone())
            .pane(FixedLeaf(50.0, 40.0))
            .pane(FixedLeaf(50.0, 40.0)),
    );
    tree.layout(SizeProposal::exact(400.0, 200.0));

    model.set_pane_visible(0, false);
    tree.tick_animations(std::time::Duration::from_millis(400));
    tree.layout(SizeProposal::exact(400.0, 200.0));

    assert!(
        tree.bounds(tree.child_widget(root, 0)).width < 2.0,
        "hidden pane → 0"
    );
    assert!(
        tree.bounds(tree.child_widget(root, 1)).width < 2.0,
        "its gutter → 0 (handle removed)"
    );
    assert!(
        tree.bounds(tree.child_widget(root, 2)).width > 396.0,
        "remaining pane fills the whole container (no gutter)"
    );
}

#[test]
fn hiding_a_pane_with_a_collapsed_size_floor_still_folds_to_zero() {
    // A pane can carry a `collapsed_size` floor (so *collapse* folds it to a
    // header sliver) AND be *hidden* via `set_pane_visible`. Hiding must remove
    // it entirely — the collapse floor applies to collapse, not to visibility.
    let model = SplitterModel::from_panes(
        vec![
            PaneDescriptor::new()
                .size(200.0)
                .min_size(0.0)
                .stretch(0.0)
                .collapsed_size(32.0),
            PaneDescriptor::new().size(200.0).min_size(0.0).stretch(1.0),
        ],
        Orientation::Vertical,
    );
    let mut tree = theme_tree();
    let root = tree.add(
        Splitter::new(model.clone())
            .pane(FixedLeaf(80.0, 300.0))
            .pane(FixedLeaf(80.0, 100.0)),
    );
    tree.layout(SizeProposal::exact(200.0, 400.0));

    model.set_pane_visible(0, false);
    tree.tick_animations(std::time::Duration::from_millis(400));
    tree.layout(SizeProposal::exact(200.0, 400.0));

    assert!(
        tree.bounds(tree.child_widget(root, 0)).height < 2.0,
        "hidden pane folds fully to 0 despite its collapsed_size floor, got {}",
        tree.bounds(tree.child_widget(root, 0)).height
    );
}

#[test]
fn hidden_pane_content_is_dormant_and_handle_at_hidden() {
    let model = SplitterModel::new(2, Orientation::Horizontal);
    let mut tree = theme_tree();
    let root = tree.add(
        Splitter::new(model.clone())
            .pane(FixedLeaf(50.0, 40.0))
            .pane(FixedLeaf(50.0, 40.0)),
    );
    tree.layout(SizeProposal::exact(400.0, 200.0));
    let inner0 = tree.child_widget(tree.child_widget(root, 0), 0);
    assert!(
        tree.find_by_role(teksilo_core::accesskit::Role::Splitter)
            .is_some()
    );

    model.set_pane_visible(0, false);
    tree.tick_animations(std::time::Duration::from_millis(400));
    tree.layout(SizeProposal::exact(400.0, 200.0));
    assert!(!tree.is_active(inner0), "hidden pane content dormant");
    assert!(
        tree.find_by_role(teksilo_core::accesskit::Role::Splitter)
            .is_none(),
        "the now-inactive handle is removed from the AT tree"
    );

    model.set_pane_visible(0, true);
    tree.tick_animations(std::time::Duration::from_millis(400));
    tree.layout(SizeProposal::exact(400.0, 200.0));
    assert!(tree.is_active(inner0), "shown pane content reactivates");
    assert!(
        tree.find_by_role(teksilo_core::accesskit::Role::Splitter)
            .is_some()
    );
}

#[test]
fn hiding_a_middle_pane_drops_both_its_gutters() {
    let model = SplitterModel::new(3, Orientation::Horizontal);
    let mut tree = theme_tree();
    let root = tree.add(
        Splitter::new(model.clone())
            .pane(FixedLeaf(50.0, 40.0))
            .pane(FixedLeaf(50.0, 40.0))
            .pane(FixedLeaf(50.0, 40.0)),
    );
    tree.layout(SizeProposal::exact(600.0, 200.0));

    model.set_pane_visible(1, false);
    tree.tick_animations(std::time::Duration::from_millis(400));
    tree.layout(SizeProposal::exact(600.0, 200.0));

    // children: clip0, handle0, clip1, handle1, clip2
    assert!(
        tree.bounds(tree.child_widget(root, 2)).width < 2.0,
        "middle pane gone"
    );
    assert!(
        tree.bounds(tree.child_widget(root, 1)).width < 2.0,
        "gutter 0 gone"
    );
    assert!(
        tree.bounds(tree.child_widget(root, 3)).width < 2.0,
        "gutter 1 gone"
    );
    let w0 = tree.bounds(tree.child_widget(root, 0)).width;
    let w2 = tree.bounds(tree.child_widget(root, 4)).width;
    assert!(
        (w0 + w2 - 600.0).abs() < 2.0,
        "the two visible panes fill the container"
    );
}

#[test]
fn export_import_round_trips_through_widget() {
    let model = SplitterModel::new(3, Orientation::Horizontal);
    let mut tree = theme_tree();
    tree.add(
        Splitter::new(model.clone())
            .pane(FixedLeaf(50.0, 40.0))
            .pane(FixedLeaf(50.0, 40.0))
            .pane(FixedLeaf(50.0, 40.0)),
    );
    tree.layout(SizeProposal::exact(600.0, 200.0));

    model.set_stored_size(0, 111.0);
    model.set_collapsed(2, true);
    let state = model.export_state();

    let restored = SplitterModel::new(3, Orientation::Horizontal);
    assert!(restored.import_state(&state));
    assert_eq!(restored.stored_size(0), 111.0);
    assert!(restored.is_collapsed(2));
}