egui_stack 0.25.4

Docking system for egui, continuing egui_dock
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
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
//! The arithmetic of a stack of collapsible sections: how heights share
//! a panel, and what a drag on a boundary between two sections does.
//! Heights are in points, a section's body alone (its title band is the
//! caller's); folded sections take no height and are skipped.
//!
//! - [`fit`]: the heights a stack shows in the room it has. A height set
//!   by hand is kept while it fits; the sections never set by hand share
//!   the rest equally, and the last open section takes what is left, less
//!   any empty room kept under it.
//! - [`plan_drag`]: what moving a boundary does, from the heights shown.
//!   Nothing gives way in proportion: sections give in turn, the nearest
//!   first, each down to its least height, as a code editor's split view
//!   does. [`DragMode::Pull`] moves the sections below as a block instead,
//!   opening empty room under the last.
//! - [`resized_above`]: the section a boundary resizes: under a folded
//!   section, the first open one above it.
//! - [`pin_plan`]: the heights to keep by hand so that [`fit`] shows a
//!   plan, the sections it did not touch staying sized by the list.
//! - [`keep_title`]: what folding or opening a section keeps so that its
//!   title stays where it is ([`held_above`]: the sections that must not
//!   move).
//! - [`rows_least`]: the least height of a section of rows.
//!
//! ```
//! use egui_stack::sections::plan::{DragMode, Placed, plan_drag};
//!
//! let section = |height| Placed { open: true, least: 50.0, height };
//! let stack = [section(100.0), section(100.0), section(100.0)];
//! // Down, the section just below gives first, down to its least height;
//! // the last gives only then.
//! let plan = plan_drag(&stack, 0, 70.0, 0.0, DragMode::Push);
//! assert_eq!(plan.heights, [170.0, 50.0, 80.0]);
//! assert_eq!(plan.moved, 70.0);
//! ```

/// A difference between two heights too small to see.
const SAME: f32 = 0.01;

/// A section as laid out: what a drag starts from.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Placed {
    /// Whether it is open; a folded section is skipped.
    pub open: bool,
    /// The height it never goes under.
    pub least: f32,
    /// The height it has.
    pub height: f32,
}

/// How a drag moves the sections around a boundary.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
#[cfg_attr(
    feature = "serde",
    derive(serde::Deserialize, serde::Serialize),
    serde(rename_all = "kebab-case")
)]
#[non_exhaustive]
pub enum DragMode {
    /// Down, the sections below give way, the nearest first, then the
    /// last is pushed into the empty room under it, then it gives way.
    /// Up, the section above gives way, then those above it, the nearest
    /// first, and the first open section below takes all the room.
    #[default]
    Push,
    /// The sections below follow as a block, keeping their heights: only
    /// the section above changes. Up, it gives way, then those above it,
    /// the nearest first, and empty room opens under the last. Down, the
    /// block goes into the empty room, then the last section alone gives
    /// way, and the drag stops.
    Pull,
}

/// What a drag does ([`plan_drag`]).
#[derive(Clone, Debug, PartialEq)]
pub struct DragPlan {
    /// Every section's height after it, in the order given.
    pub heights: Vec<f32>,
    /// How far the boundary moved: down if positive. Less than asked
    /// when the stack stopped it; 0 when nothing could move.
    pub moved: f32,
    /// The empty room left under the last section.
    pub empty: f32,
}

/// The section a boundary resizes, the one on the line under the section
/// `line`: `line` itself when open, else the first open one above it, past
/// any folded ones (they have no height to change); `None` when nothing
/// above is open, and the boundary cannot move. `open`: whether each
/// section is, top first.
///
/// Folded bands between do not change the distance: they follow the
/// section, so the line stays under the pointer in both directions.
pub fn resized_above(open: &[bool], line: usize) -> Option<usize> {
    (0..=line).rev().find(|&i| open.get(i) == Some(&true))
}

/// What moving the boundary under the section `above` by `delta` does
/// (down if positive), from the heights in `placed`; `empty` is the room
/// under the last section.
///
/// A drag plans each move from the heights shown, by the distance between
/// the boundary and the pointer, so that it stays under the pointer: what a
/// drag shrank stays shrunk, and a move back gives the room where a fresh
/// drag would. Plans compose: a move of `a` then of `b` the same way is a
/// move of `a + b`. When `above` is not an open section or no section
/// lies below it, no boundary is there: nothing moves and the
/// plan says so (`moved` is 0). With only folded sections below it, nothing
/// else can give or take: down, it takes the empty room, and up, it opens
/// some, the folded bands following.
pub fn plan_drag(
    placed: &[Placed],
    above: usize,
    delta: f32,
    empty: f32,
    mode: DragMode,
) -> DragPlan {
    let mut plan = DragPlan {
        heights: placed.iter().map(|p| p.height).collect(),
        moved: 0.0,
        empty: empty.max(0.0),
    };
    let above_open = placed.get(above).is_some_and(|p| p.open);
    if !above_open || above + 1 >= placed.len() || !delta.is_finite() {
        return plan;
    }
    let below: Vec<usize> = (above + 1..placed.len())
        .filter(|&i| placed[i].open)
        .collect();
    match mode {
        DragMode::Push if delta > 0.0 => push_down(placed, above, &below, delta, &mut plan),
        DragMode::Push if delta < 0.0 => push_up(placed, above, &below, -delta, &mut plan),
        DragMode::Pull if delta > 0.0 => pull_down(placed, above, &below, delta, &mut plan),
        DragMode::Pull if delta < 0.0 => pull_up(placed, above, -delta, &mut plan),
        DragMode::Push | DragMode::Pull => {}
    }
    plan
}

/// Takes up to `owed` from a section, down to its least height; says how
/// much it gave.
fn give(height: &mut f32, least: f32, owed: f32) -> f32 {
    let given = (*height - least).max(0.0).min(owed);
    *height -= given;
    given
}

/// Takes up to `owed` from the sizes in `order`, in turn, each down to its
/// least size and never in proportion; says how much they gave. The rule
/// sections and the layout's solver share (`crate::layout`).
pub(crate) fn give_in_turn(
    sizes: &mut [f32],
    least: impl Fn(usize) -> f32,
    order: impl IntoIterator<Item = usize>,
    owed: f32,
) -> f32 {
    let mut left = owed;
    for i in order {
        if left <= 0.0 {
            break;
        }
        left -= give(&mut sizes[i], least(i), left);
    }
    owed - left
}

fn push_down(placed: &[Placed], above: usize, below: &[usize], asked: f32, plan: &mut DragPlan) {
    let mut owed = asked;
    let (last, between) = match below.split_last() {
        Some((&last, between)) => (Some(last), between),
        None => (None, below),
    };
    owed -= give_in_turn(
        &mut plan.heights,
        |i| placed[i].least,
        between.iter().copied(),
        owed,
    );
    let into_empty = owed.min(plan.empty);
    plan.empty -= into_empty;
    owed -= into_empty;
    if let Some(last) = last {
        owed -= give(&mut plan.heights[last], placed[last].least, owed);
    }
    plan.moved = asked - owed;
    plan.heights[above] += plan.moved;
}

fn push_up(placed: &[Placed], above: usize, below: &[usize], asked: f32, plan: &mut DragPlan) {
    let given = give_above(placed, above, asked, plan);
    plan.moved = -given;
    match below.first() {
        Some(&next) => plan.heights[next] += given,
        None => plan.empty += given,
    }
}

fn pull_down(placed: &[Placed], above: usize, below: &[usize], asked: f32, plan: &mut DragPlan) {
    let into_empty = asked.min(plan.empty);
    plan.empty -= into_empty;
    let mut owed = asked - into_empty;
    if let Some(&last) = below.last() {
        owed -= give(&mut plan.heights[last], placed[last].least, owed);
    }
    plan.moved = asked - owed;
    plan.heights[above] += plan.moved;
}

fn pull_up(placed: &[Placed], above: usize, asked: f32, plan: &mut DragPlan) {
    let given = give_above(placed, above, asked, plan);
    plan.moved = -given;
    plan.empty += given;
}

/// Takes up to `asked` from the section `above`, then those above it, the
/// nearest first; says how much they gave.
fn give_above(placed: &[Placed], above: usize, asked: f32, plan: &mut DragPlan) -> f32 {
    let order = (0..=above).rev().filter(|&i| placed[i].open);
    give_in_turn(&mut plan.heights, |i| placed[i].least, order, asked)
}

/// The open sections above `section`, with their shown heights: what must
/// not move for its title to stay where it is.
pub fn held_above(placed: &[Placed], section: usize) -> Vec<(usize, f32)> {
    if section >= placed.len() {
        return Vec::new();
    }
    placed[..section]
        .iter()
        .enumerate()
        .filter(|(_, p)| p.open)
        .map(|(i, p)| (i, p.height))
        .collect()
}

/// What to keep so that folding or opening a section leaves its title where
/// it is ([`keep_title`]).
#[derive(Clone, Debug, PartialEq)]
pub struct Kept {
    /// Every section's height to keep by hand, in the order given.
    pub hands: Vec<Option<f32>>,
    /// The empty room to keep under the last section.
    pub empty: f32,
}

/// What folding or opening the section `section` keeps, from the `heights`
/// shown and the `empty` room under them, so that its title does not move.
///
/// The open sections above keep their shown height, as if set by hand. Those
/// below move: folding, they take the room it frees; opening, they give it
/// its room, and the empty room gives only when they are at their least
/// ([`fit`]). With no open section below, the room stays empty when folding;
/// opening takes it back, up to the section's own height when it has one.
pub fn keep_title(asked: &[Asked], heights: &[f32], empty: f32, section: usize) -> Kept {
    let placed: Vec<Placed> = asked
        .iter()
        .zip(heights)
        .map(|(a, &height)| Placed {
            open: a.open,
            least: a.least,
            height,
        })
        .collect();
    let mut hands: Vec<Option<f32>> = asked.iter().map(|a| a.hand).collect();
    for (i, height) in held_above(&placed, section) {
        hands[i] = Some(height);
    }
    let empty = match placed.get(section) {
        Some(_) if asked[section + 1..].iter().any(|a| a.open) => empty,
        Some(this) if this.open => empty + this.height,
        Some(this) => match asked[section].hand {
            Some(own) => (empty - own.max(this.least)).max(0.0),
            None => 0.0,
        },
        None => empty,
    };
    Kept { hands, empty }
}

/// The least height of a section of `count` rows, each `row` tall: two
/// rows, or all of them when it has fewer.
pub fn rows_least(row: f32, count: usize) -> f32 {
    count.min(2) as f32 * row
}

/// A section as a stack asks for it: what [`fit`] lays out.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct Asked {
    /// Whether it is open; a folded section takes no height.
    pub open: bool,
    /// The height it never goes under.
    pub least: f32,
    /// The height set by hand, if any. The last open section ignores
    /// its own: it takes what the others leave.
    pub hand: Option<f32>,
}

/// The heights a stack shows ([`fit`]).
#[derive(Clone, Debug, PartialEq)]
pub struct Fit {
    /// Every section's height, in the order given (0 when folded).
    pub heights: Vec<f32>,
    /// Whether each section's height is its own, set by hand.
    pub by_hand: Vec<bool>,
    /// What the least heights lack to fit in the room: the stack scrolls
    /// by this much. 0 when they fit.
    pub overflow: f32,
    /// The empty room shown under the last section: the room left when no
    /// section is open.
    pub empty: f32,
}

/// Lays sections out in `room` points, keeping `empty` points of it under
/// the last section.
///
/// Heights set by hand are kept and the others (the last open section
/// among them) share the rest equally, each at least its least height.
/// In a panel too short for that, the sections sized by the list are
/// already at their least, so those set by hand give way, from the
/// bottom up. When even the least heights do not fit, each has its least
/// and [`Fit::overflow`] says by how much the stack is too tall. The empty
/// room gives way after the sections sized by the list and before those set
/// by hand; it comes back with the room, as they do.
pub fn fit(asked: &[Asked], room: f32, empty: f32) -> Fit {
    fit_within(asked, &[], room, empty).0
}

/// [`fit`] for a stack sized by its content: a section sized by the list,
/// the last open one included, is never laid out taller than `most`
/// (its content's height; missing or infinite, no bound) nor shorter than
/// its least. The others share what is left in turn, and the room none
/// takes goes under the last band. Heights set by hand are kept as by
/// [`fit`].
///
/// Returns the fit, its [`Fit::empty`] holding that room too, and the
/// room left by the content alone (not kept in a layout: it changes with
/// the content).
pub fn fit_within(asked: &[Asked], most: &[f32], room: f32, empty: f32) -> (Fit, f32) {
    let mut fit = fit_filled(asked, room, empty);
    if fit.overflow > 0.0 || most.is_empty() {
        return (fit, 0.0);
    }
    let listed: Vec<usize> = (0..asked.len())
        .filter(|&i| asked[i].open && !fit.by_hand[i])
        .collect();
    let shared: f32 = listed.iter().map(|&i| fit.heights[i]).sum();
    let bound = |i: usize| {
        most.get(i)
            .copied()
            .filter(|m| m.is_finite())
            .map_or(f32::INFINITY, |m| m.max(asked[i].least))
    };
    let mut left = listed;
    let mut room = shared;
    // Equal shares, each between its least and its bound.
    loop {
        if left.is_empty() {
            break;
        }
        let each = room / left.len() as f32;
        if let Some(at) = left.iter().position(|&i| asked[i].least > each) {
            let i = left.remove(at);
            fit.heights[i] = asked[i].least;
            room -= asked[i].least;
            continue;
        }
        if let Some(at) = left.iter().position(|&i| bound(i) < each) {
            let i = left.remove(at);
            fit.heights[i] = bound(i);
            room -= bound(i);
            continue;
        }
        for &i in &left {
            fit.heights[i] = each;
        }
        room = 0.0;
        break;
    }
    let spare = room.max(0.0);
    fit.empty += spare;
    (fit, spare)
}

fn fit_filled(asked: &[Asked], room: f32, empty: f32) -> Fit {
    let room = if room.is_finite() { room.max(0.0) } else { 0.0 };
    let empty = if empty.is_finite() {
        empty.max(0.0)
    } else {
        0.0
    };
    let open: Vec<usize> = (0..asked.len()).filter(|&i| asked[i].open).collect();
    let last = open.last().copied();
    let mut heights = vec![0.0; asked.len()];
    let mut by_hand = vec![false; asked.len()];
    let (mut own, mut listed) = (Vec::new(), Vec::new());
    for &i in &open {
        match asked[i].hand {
            Some(height) if Some(i) != last => {
                heights[i] = height.max(asked[i].least);
                by_hand[i] = true;
                own.push(i);
            }
            _ => listed.push(i),
        }
    }

    if open.is_empty() {
        return Fit {
            heights,
            by_hand,
            overflow: 0.0,
            empty: room,
        };
    }

    // What the sections need comes before the empty room.
    let needed: f32 = own.iter().map(|&i| heights[i]).sum::<f32>()
        + listed.iter().map(|&i| asked[i].least).sum::<f32>();
    let empty = empty.min((room - needed).max(0.0));
    let room = room - empty;

    let least: f32 = open.iter().map(|&i| asked[i].least).sum();
    if least >= room {
        for &i in &open {
            heights[i] = asked[i].least;
        }
        return Fit {
            heights,
            by_hand,
            overflow: least - room,
            empty,
        };
    }

    // What the list needs at least comes first: those set by hand give
    // way to it, from the bottom up.
    let listed_least: f32 = listed.iter().map(|&i| asked[i].least).sum();
    let kept: f32 = own.iter().map(|&i| heights[i]).sum();
    let owed = kept + listed_least - room;
    give_in_turn(
        &mut heights,
        |i| asked[i].least,
        own.iter().rev().copied(),
        owed,
    );
    let kept: f32 = own.iter().map(|&i| heights[i]).sum();
    share_out(asked, &listed, room - kept, &mut heights);
    Fit {
        heights,
        by_hand,
        overflow: 0.0,
        empty,
    }
}

/// Divides `room` equally between sections, each at least its least
/// height (`room` holds them all).
fn share_out(asked: &[Asked], sections: &[usize], room: f32, heights: &mut [f32]) {
    let mut left = sections.to_vec();
    let mut room = room;
    loop {
        let each = room / left.len().max(1) as f32;
        let (pinned, free): (Vec<usize>, Vec<usize>) =
            left.iter().partition(|&&i| asked[i].least > each);
        if pinned.is_empty() {
            for i in free {
                heights[i] = each;
            }
            return;
        }
        for i in pinned {
            heights[i] = asked[i].least;
            room -= asked[i].least;
        }
        left = free;
    }
}

/// The heights to keep by hand, section by section, so that [`fit`]
/// shows `plan` in `room`, with `empty` kept under the last section.
///
/// A section set by hand takes its height from the plan, which aligns it
/// on what is shown. One sized by the list stays so as long as [`fit`]
/// gives it the plan's height: a section the drag did not touch gets a
/// height of its own only if the list would otherwise move it. The last
/// open section and folded ones keep what they had.
pub fn pin_plan(asked: &[Asked], room: f32, empty: f32, plan: &[f32]) -> Vec<Option<f32>> {
    pin_plan_within(asked, &[], room, empty, plan)
}

/// [`pin_plan`] for a stack sized by its content, laid out by
/// [`fit_within`] with the same `most`.
pub fn pin_plan_within(
    asked: &[Asked],
    most: &[f32],
    room: f32,
    empty: f32,
    plan: &[f32],
) -> Vec<Option<f32>> {
    let mut asked = asked.to_vec();
    let last = (0..asked.len()).rev().find(|&i| asked[i].open);
    let pinnable: Vec<usize> = (0..asked.len().min(plan.len()))
        .filter(|&i| asked[i].open && Some(i) != last)
        .collect();
    for &i in &pinnable {
        if asked[i].hand.is_some() {
            asked[i].hand = Some(plan[i]);
        }
    }
    loop {
        let shown = fit_within(&asked, most, room, empty).0.heights;
        let mut changed = false;
        for &i in &pinnable {
            if asked[i].hand.is_none() && (shown[i] - plan[i]).abs() > SAME {
                asked[i].hand = Some(plan[i]);
                changed = true;
            }
        }
        if !changed {
            return asked.iter().map(|a| a.hand).collect();
        }
    }
}

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

    fn at(least: f32, height: f32) -> Placed {
        Placed {
            open: true,
            least,
            height,
        }
    }

    fn folded() -> Placed {
        Placed {
            open: false,
            least: 0.0,
            height: 0.0,
        }
    }

    fn asked(least: f32, hand: Option<f32>) -> Asked {
        Asked {
            open: true,
            least,
            hand,
        }
    }

    /// Drags from `from` to the height `wanted` of the section `above`.
    fn drag(stack: &[Placed], above: usize, wanted: f32, empty: f32) -> DragPlan {
        let delta = wanted - stack[above].height;
        plan_drag(stack, above, delta, empty, DragMode::Push)
    }

    #[test]
    fn a_drag_down_shrinks_those_below_nearest_first_then_the_last_to_a_hard_stop() {
        let stack = [at(56.0, 300.0), at(56.0, 200.0), at(28.0, 128.0)];
        // The one just below gives first, down to its least (144 to give).
        assert_eq!(drag(&stack, 0, 400.0, 0.0).heights, [400.0, 100.0, 128.0]);
        // Then the last (100 more).
        assert_eq!(drag(&stack, 0, 520.0, 0.0).heights, [520.0, 56.0, 52.0]);
        // And no further: the whole stack is at its least.
        let stop = drag(&stack, 0, 900.0, 0.0);
        assert_eq!(stop.heights, [544.0, 56.0, 28.0]);
        assert_eq!(stop.moved, 244.0);
    }

    #[test]
    fn the_empty_room_is_taken_after_the_sections_between_and_before_the_last() {
        let stack = [at(56.0, 300.0), at(56.0, 200.0), at(28.0, 128.0)];
        // The one just below gives before the last moves into the room.
        let plan = drag(&stack, 0, 400.0, 150.0);
        assert_eq!(plan.heights, [400.0, 100.0, 128.0]);
        assert_eq!(plan.empty, 150.0);
        // Then the room is taken, then the last gives.
        let plan = drag(&stack, 0, 600.0, 150.0);
        assert_eq!(plan.heights, [600.0, 56.0, 122.0]);
        assert_eq!(plan.empty, 0.0);
    }

    #[test]
    fn a_drag_up_shrinks_those_above_and_only_the_first_below_grows() {
        let stack = [at(56.0, 300.0), at(56.0, 200.0), at(28.0, 128.0)];
        assert_eq!(drag(&stack, 0, 250.0, 0.0).heights, [250.0, 250.0, 128.0]);
        // Down to its own least, the one just below growing by as much.
        let stop = drag(&stack, 0, 10.0, 0.0);
        assert_eq!(stop.heights, [56.0, 444.0, 128.0]);
        assert_eq!(stop.moved, -244.0);
        // Past its least, the sections above give, the nearest first.
        let stack = [
            at(56.0, 200.0),
            at(56.0, 100.0),
            at(56.0, 56.0),
            at(56.0, 200.0),
            at(28.0, 128.0),
        ];
        let plan = plan_drag(&stack, 2, -156.0, 0.0, DragMode::Push);
        assert_eq!(plan.heights, [88.0, 56.0, 56.0, 356.0, 128.0]);
        assert_eq!(plan.moved, -156.0);
    }

    #[test]
    fn folded_sections_are_skipped() {
        // Up: the next open one grows.
        let stack = [at(56.0, 300.0), folded(), at(28.0, 128.0)];
        assert_eq!(drag(&stack, 0, 250.0, 0.0).heights, [250.0, 0.0, 178.0]);
        // Down: a folded one gives nothing.
        assert_eq!(drag(&stack, 0, 350.0, 0.0).heights, [350.0, 0.0, 78.0]);
        // Up, past the least, through a folded one above.
        let stack = [at(10.0, 60.0), folded(), at(20.0, 20.0), at(20.0, 20.0)];
        let plan = plan_drag(&stack, 2, -30.0, 0.0, DragMode::Push);
        assert_eq!(plan.heights, [30.0, 0.0, 20.0, 50.0]);
    }

    #[test]
    fn a_boundary_under_a_folded_section_resizes_the_first_open_one_above() {
        // Open above it: that one.
        assert_eq!(resized_above(&[true, true], 0), Some(0));
        // Folded above it: the first open one, past the folded ones.
        assert_eq!(resized_above(&[true, false, true], 1), Some(0));
        assert_eq!(resized_above(&[true, false, false, true], 2), Some(0));
        // All folded above it: nothing to resize, the boundary stays.
        assert_eq!(resized_above(&[false, true], 0), None);
        assert_eq!(resized_above(&[false, false, true], 1), None);
        assert_eq!(resized_above(&[true], 5), Some(0));
        // Dragged down, a lower section never goes up: the section chosen
        // grows and the last gives, the folded band between them following.
        let stack = [at(10.0, 100.0), folded(), at(10.0, 100.0)];
        let chosen = resized_above(&[true, false, true], 1).unwrap();
        let plan = plan_drag(&stack, chosen, 30.0, 0.0, DragMode::Push);
        assert_eq!(plan.heights, [130.0, 0.0, 70.0]);
        let plan = plan_drag(&stack, chosen, -30.0, 0.0, DragMode::Push);
        assert_eq!(plan.heights, [70.0, 0.0, 130.0]);
    }

    #[test]
    fn plans_compose_and_a_move_back_does_not_undo_what_a_move_shrank() {
        let stack = [at(56.0, 300.0), at(56.0, 200.0), at(28.0, 128.0)];
        let placed = |plan: &DragPlan| -> Vec<Placed> {
            stack
                .iter()
                .zip(&plan.heights)
                .map(|(p, &height)| Placed { height, ..*p })
                .collect()
        };
        for mode in [DragMode::Push, DragMode::Pull] {
            for (a, b) in [
                (40.0, 130.0),
                (-30.0, -50.0),
                (100.0, 60.0),
                (-200.0, -90.0),
            ] {
                let once = plan_drag(&stack, 0, a + b, 20.0, mode);
                let first = plan_drag(&stack, 0, a, 20.0, mode);
                let twice = plan_drag(&placed(&first), 0, b, first.empty, mode);
                assert_eq!(once.heights, twice.heights, "{mode:?} {a} {b}");
                assert_eq!(once.empty, twice.empty, "{mode:?} {a} {b}");
            }
        }
        // Down past the second's least, the last gives 30; back up by as much
        // as it went down, the last keeps what it gave and the second takes
        // the room, as a fresh drag up would.
        let down = drag(&stack, 0, 474.0, 0.0);
        assert_eq!(down.heights, [474.0, 56.0, 98.0]);
        let back = plan_drag(&placed(&down), 0, -174.0, 0.0, DragMode::Push);
        assert_eq!(back.heights, [300.0, 230.0, 98.0]);
        // The same past a folded section: the next open one takes the room.
        let stack = [at(56.0, 300.0), folded(), at(56.0, 200.0), at(28.0, 128.0)];
        let down = plan_drag(&stack, 0, 174.0, 0.0, DragMode::Push);
        assert_eq!(down.heights, [474.0, 0.0, 56.0, 98.0]);
        let placed: Vec<Placed> = stack
            .iter()
            .zip(&down.heights)
            .map(|(p, &height)| Placed { height, ..*p })
            .collect();
        let back = plan_drag(&placed, 0, -174.0, 0.0, DragMode::Push);
        assert_eq!(back.heights, [300.0, 0.0, 230.0, 98.0]);
    }

    #[test]
    fn where_no_boundary_is_nothing_moves() {
        let stack = [at(10.0, 50.0), folded(), at(10.0, 50.0)];
        for (above, delta) in [(2, 10.0), (1, 10.0), (9, 10.0), (0, 0.0), (0, f32::NAN)] {
            let plan = plan_drag(&stack, above, delta, 5.0, DragMode::Push);
            assert_eq!(plan.heights, [50.0, 0.0, 50.0], "{above} {delta}");
            assert_eq!(plan.moved, 0.0);
            assert_eq!(plan.empty, 5.0);
        }
    }

    #[test]
    fn a_stack_at_its_least_stops_both_ways_when_no_section_can_give() {
        let stack = [at(40.0, 40.0), at(40.0, 40.0)];
        assert_eq!(drag(&stack, 0, 90.0, 0.0).moved, 0.0);
        assert_eq!(drag(&stack, 0, 10.0, 0.0).moved, 0.0);
    }

    #[test]
    fn sections_never_set_by_hand_share_the_rest_equally() {
        let f = fit(
            &[asked(10.0, None), asked(10.0, None), asked(10.0, None)],
            300.0,
            0.0,
        );
        assert_eq!(f.heights, [100.0, 100.0, 100.0]);
        assert_eq!(f.by_hand, [false, false, false]);
        assert_eq!(f.overflow, 0.0);
        // One set by hand: the others share what it leaves.
        let f = fit(
            &[
                asked(10.0, Some(200.0)),
                asked(10.0, None),
                asked(10.0, None),
            ],
            300.0,
            0.0,
        );
        assert_eq!(f.heights, [200.0, 50.0, 50.0]);
        assert_eq!(f.by_hand, [true, false, false]);
        // A least height holds, the others share the rest.
        let f = fit(&[asked(80.0, None), asked(10.0, None)], 100.0, 0.0);
        assert_eq!(f.heights, [80.0, 20.0]);
    }

    #[test]
    fn the_last_open_section_takes_what_is_left_and_never_its_own() {
        let f = fit(
            &[asked(10.0, Some(50.0)), asked(10.0, Some(70.0))],
            300.0,
            0.0,
        );
        assert_eq!(f.heights, [50.0, 250.0]);
        assert_eq!(f.by_hand, [true, false]);
        // A folded one below does not count: the one above it is last.
        let stack = [
            asked(10.0, Some(50.0)),
            asked(10.0, Some(70.0)),
            Asked {
                open: false,
                least: 10.0,
                hand: Some(90.0),
            },
        ];
        let f = fit(&stack, 300.0, 0.0);
        assert_eq!(f.heights, [50.0, 250.0, 0.0]);
        assert_eq!(f.by_hand, [true, false, false]);
    }

    #[test]
    fn in_a_short_panel_the_list_gives_first_then_those_set_by_hand_from_the_bottom() {
        let stack = [
            asked(20.0, Some(100.0)),
            asked(20.0, Some(100.0)),
            asked(20.0, None),
            asked(20.0, None),
        ];
        // Room for all: the list has the rest.
        assert_eq!(fit(&stack, 300.0, 0.0).heights, [100.0, 100.0, 50.0, 50.0]);
        // The list at its least first, those set by hand untouched.
        assert_eq!(fit(&stack, 240.0, 0.0).heights, [100.0, 100.0, 20.0, 20.0]);
        // Then the lowest set by hand, then the one above it.
        assert_eq!(fit(&stack, 200.0, 0.0).heights, [100.0, 60.0, 20.0, 20.0]);
        assert_eq!(fit(&stack, 90.0, 0.0).heights, [30.0, 20.0, 20.0, 20.0]);
        // Then even the least heights do not fit: the stack overflows.
        let f = fit(&stack, 50.0, 0.0);
        assert_eq!(f.heights, [20.0, 20.0, 20.0, 20.0]);
        assert_eq!(f.overflow, 30.0);
        assert_eq!(f.by_hand, [true, true, false, false]);
    }

    #[test]
    fn a_height_set_under_the_least_is_shown_at_the_least() {
        let f = fit(&[asked(40.0, Some(1.0)), asked(10.0, None)], 100.0, 0.0);
        assert_eq!(f.heights, [40.0, 60.0]);
    }

    #[test]
    fn nothing_open_takes_no_room_and_no_room_means_the_least_heights() {
        let folded = Asked {
            open: false,
            least: 10.0,
            hand: None,
        };
        let f = fit(&[folded, folded], 100.0, 0.0);
        assert_eq!(f.heights, [0.0, 0.0]);
        assert_eq!(f.overflow, 0.0);
        assert!(fit(&[], 100.0, 0.0).heights.is_empty());
        let f = fit(&[asked(10.0, None)], f32::INFINITY, 0.0);
        assert_eq!(f.heights, [10.0]);
    }

    #[test]
    fn pinning_a_plan_keeps_the_list_where_it_can() {
        let room = 300.0;
        // The boundary between the first two: the third, last, stays.
        let stack = [asked(10.0, None), asked(10.0, None), asked(10.0, None)];
        assert_eq!(
            pin_plan(&stack, room, 0.0, &[120.0, 80.0, 100.0]),
            [Some(120.0), Some(80.0), None]
        );
        // The last gives: the first would grow or shrink with the list,
        // so it gets its height too, and nothing moves in proportion.
        let hands = pin_plan(&stack, room, 0.0, &[100.0, 120.0, 80.0]);
        assert_eq!(hands, [Some(100.0), Some(120.0), None]);
        // With four, one in the list not touched stays in the list.
        let stack = [
            asked(10.0, None),
            asked(10.0, None),
            asked(10.0, None),
            asked(10.0, None),
        ];
        let hands = pin_plan(&stack, 400.0, 0.0, &[120.0, 80.0, 100.0, 100.0]);
        assert_eq!(hands, [Some(120.0), Some(80.0), None, None]);
    }

    #[test]
    fn a_pinned_plan_is_what_fit_shows() {
        let cases: [(&[Asked], f32, &[f32]); 3] = [
            (
                &[asked(10.0, None), asked(10.0, None), asked(10.0, None)],
                300.0,
                &[100.0, 120.0, 80.0],
            ),
            // One at its least in the list: pinning another must not free it.
            (
                &[asked(90.0, None), asked(10.0, None), asked(10.0, None)],
                210.0,
                &[90.0, 40.0, 80.0],
            ),
            (
                &[asked(10.0, Some(50.0)), asked(10.0, None)],
                100.0,
                &[70.0, 30.0],
            ),
        ];
        for (stack, room, plan) in cases {
            let hands = pin_plan(stack, room, 0.0, plan);
            let pinned: Vec<Asked> = stack
                .iter()
                .zip(hands)
                .map(|(a, hand)| Asked { hand, ..*a })
                .collect();
            assert_eq!(fit(&pinned, room, 0.0).heights, plan, "{stack:?}");
        }
    }

    #[test]
    fn a_pull_moves_the_block_below_with_its_heights_kept() {
        let pull = |stack: &[Placed], above: usize, wanted: f32, empty: f32| {
            let delta = wanted - stack[above].height;
            plan_drag(stack, above, delta, empty, DragMode::Pull)
        };
        let stack = [at(56.0, 300.0), at(56.0, 200.0), at(28.0, 128.0)];
        // Down, the last alone gives way (100 here), and no further.
        assert_eq!(pull(&stack, 0, 350.0, 0.0).heights, [350.0, 200.0, 78.0]);
        let stop = pull(&stack, 0, 900.0, 0.0);
        assert_eq!(stop.heights, [400.0, 200.0, 28.0]);
        assert_eq!(stop.moved, 100.0);
        // The empty room goes first, then the last.
        let plan = pull(&stack, 0, 320.0, 50.0);
        assert_eq!(plan.heights, [320.0, 200.0, 128.0]);
        assert_eq!(plan.empty, 30.0);
        let stop = pull(&stack, 0, 900.0, 50.0);
        assert_eq!(stop.heights, [450.0, 200.0, 28.0]);
        assert_eq!(stop.empty, 0.0);
        // Up, nothing below grows: the room stays empty.
        let plan = pull(&stack, 0, 250.0, 0.0);
        assert_eq!(plan.heights, [250.0, 200.0, 128.0]);
        assert_eq!(plan.empty, 50.0);
        assert_eq!(plan.moved, -50.0);
        // Those above still give past its least, the nearest first.
        let stack = [
            at(56.0, 200.0),
            at(56.0, 100.0),
            at(56.0, 56.0),
            at(56.0, 200.0),
            at(28.0, 128.0),
        ];
        let plan = plan_drag(&stack, 2, -156.0, 0.0, DragMode::Pull);
        assert_eq!(plan.heights, [88.0, 56.0, 56.0, 200.0, 128.0]);
        assert_eq!(plan.empty, 156.0);
    }

    #[test]
    fn a_pull_comes_back_when_the_pointer_does_and_stops_at_the_top() {
        let stack = [at(50.0, 100.0), at(50.0, 100.0)];
        // Far up: as far as the sections above can give.
        let plan = plan_drag(&stack, 0, -900.0, 10.0, DragMode::Pull);
        assert_eq!(plan.heights, [50.0, 100.0]);
        assert_eq!(plan.empty, 60.0);
        assert_eq!(plan.moved, -50.0);
        // Back from the same start, nothing has drifted.
        let plan = plan_drag(&stack, 0, 0.0, 10.0, DragMode::Pull);
        assert_eq!(plan.heights, [100.0, 100.0]);
        assert_eq!(plan.empty, 10.0);
    }

    #[test]
    fn with_only_folded_sections_below_the_bands_follow_into_the_empty_room() {
        let stack = [at(10.0, 100.0), folded()];
        for mode in [DragMode::Push, DragMode::Pull] {
            // Down, as far as the empty room goes: nothing else can give.
            let plan = plan_drag(&stack, 0, 30.0, 20.0, mode);
            assert_eq!(plan.heights, [120.0, 0.0], "{mode:?}");
            assert_eq!((plan.moved, plan.empty), (20.0, 0.0), "{mode:?}");
            // Up, it opens some.
            let plan = plan_drag(&stack, 0, -30.0, 20.0, mode);
            assert_eq!(plan.heights, [70.0, 0.0], "{mode:?}");
            assert_eq!((plan.moved, plan.empty), (-30.0, 50.0), "{mode:?}");
        }
    }

    #[test]
    fn a_plan_always_accounts_for_all_the_room() {
        let stack = [at(20.0, 100.0), folded(), at(20.0, 60.0), at(20.0, 40.0)];
        let total = |plan: &DragPlan| plan.heights.iter().sum::<f32>() + plan.empty;
        for mode in [DragMode::Push, DragMode::Pull] {
            for delta in [-500.0, -90.0, -10.0, 10.0, 25.0, 45.0, 500.0] {
                for above in [0, 2] {
                    let plan = plan_drag(&stack, above, delta, 30.0, mode);
                    assert_eq!(total(&plan), 230.0, "{mode:?} {above} {delta}");
                }
            }
        }
    }

    #[test]
    fn empty_room_is_kept_under_the_last_section() {
        let stack = [
            asked(10.0, Some(100.0)),
            asked(10.0, None),
            asked(10.0, None),
        ];
        let f = fit(&stack, 300.0, 60.0);
        assert_eq!(f.heights, [100.0, 70.0, 70.0]);
        assert_eq!(f.empty, 60.0);
        // Those sized by the list give way first, then the empty room, down
        // to none; those set by hand only then. It comes back with the room.
        let f = fit(&stack, 180.0, 60.0);
        assert_eq!(f.heights, [100.0, 10.0, 10.0]);
        assert_eq!(f.empty, 60.0);
        let f = fit(&stack, 150.0, 60.0);
        assert_eq!(f.heights, [100.0, 10.0, 10.0]);
        assert_eq!(f.empty, 30.0);
        let f = fit(&stack, 120.0, 60.0);
        assert_eq!(f.heights, [100.0, 10.0, 10.0]);
        assert_eq!(f.empty, 0.0);
        let f = fit(&stack, 90.0, 60.0);
        assert_eq!(f.heights, [70.0, 10.0, 10.0]);
        assert_eq!(f.empty, 0.0);
        // Nonsense is no room; with nothing open, all of it is empty.
        for nonsense in [f32::NAN, f32::INFINITY, -5.0] {
            assert_eq!(fit(&stack, 300.0, nonsense).empty, 0.0, "{nonsense}");
        }
        let none = Asked {
            open: false,
            least: 10.0,
            hand: None,
        };
        assert_eq!(fit(&[none, none], 100.0, 7.0).empty, 100.0);
    }

    #[test]
    fn a_pulled_plan_is_what_fit_shows() {
        let stack = [asked(10.0, None), asked(10.0, None), asked(10.0, None)];
        let placed = fit(&stack, 300.0, 0.0)
            .heights
            .iter()
            .map(|&height| at(10.0, height))
            .collect::<Vec<_>>();
        let plan = plan_drag(&placed, 0, -30.0, 0.0, DragMode::Pull);
        assert_eq!(plan.heights, [70.0, 100.0, 100.0]);
        let hands = pin_plan(&stack, 300.0, plan.empty, &plan.heights);
        // The block keeps its heights: the list would have moved them.
        assert_eq!(hands, [Some(70.0), Some(100.0), None]);
        let pinned: Vec<Asked> = stack
            .iter()
            .zip(hands)
            .map(|(a, hand)| Asked { hand, ..*a })
            .collect();
        let shown = fit(&pinned, 300.0, plan.empty);
        assert_eq!(shown.heights, plan.heights);
        assert_eq!(shown.empty, 30.0);
    }

    /// Heights are kept exact here, not rounded: rounding would move the
    /// title it is there to hold.
    #[test]
    fn folding_a_section_holds_the_open_ones_above_it() {
        let stack = [
            at(28.0, 200.0),
            folded(),
            at(28.0, 100.4),
            at(28.0, 100.0),
            at(28.0, 100.0),
        ];
        // Those above, open; the folded one has no height to hold, those
        // below move.
        assert_eq!(held_above(&stack, 3), [(0, 200.0), (2, 100.4)]);
        assert!(held_above(&stack, 0).is_empty());
        assert!(held_above(&stack, 9).is_empty());
    }

    /// Where a section's title is, in a stack shown so: what must not change.
    fn title_top(heights: &[f32], section: usize) -> f32 {
        const BAND: f32 = 28.0;
        heights[..section].iter().sum::<f32>() + section as f32 * BAND
    }

    /// `section` folded or opened with its title kept, then shown again.
    fn toggled(stack: &[Asked], room: f32, empty: f32, section: usize) -> (Vec<Asked>, Fit) {
        let shown = fit(stack, room, empty);
        let kept = keep_title(stack, &shown.heights, shown.empty, section);
        let mut next: Vec<Asked> = stack
            .iter()
            .zip(&kept.hands)
            .map(|(a, &hand)| Asked { hand, ..*a })
            .collect();
        next[section].open = !next[section].open;
        let after = fit(&next, room, kept.empty);
        assert_eq!(
            title_top(&shown.heights, section),
            title_top(&after.heights, section),
            "{stack:?} {section}"
        );
        (next, after)
    }

    #[test]
    fn folding_keeps_the_title_those_below_move_up() {
        let stack = [
            asked(10.0, None),
            asked(10.0, None),
            asked(10.0, None),
            asked(10.0, None),
        ];
        let (next, after) = toggled(&stack, 400.0, 0.0, 1);
        // Those below share what it frees.
        assert_eq!(after.heights, [100.0, 0.0, 150.0, 150.0]);
        assert_eq!(after.empty, 0.0);
        assert_eq!(after.by_hand, [true, false, false, false]);
        // Opened again, it has its share back, and they theirs.
        let (_, after) = toggled(&next, 400.0, 0.0, 1);
        assert_eq!(after.heights, [100.0, 100.0, 100.0, 100.0]);
    }

    #[test]
    fn folding_with_nothing_below_leaves_the_room_empty_and_opening_takes_it_back() {
        let stack = [asked(10.0, None), asked(10.0, None)];
        let (next, after) = toggled(&stack, 300.0, 0.0, 1);
        assert_eq!(after.heights, [150.0, 0.0]);
        assert_eq!(after.empty, 150.0);
        let (_, after) = toggled(&next, 300.0, after.empty, 1);
        assert_eq!(after.heights, [150.0, 150.0]);
        assert_eq!(after.empty, 0.0);
        // Folded alone, then opened: the whole room again.
        let alone = [asked(10.0, None)];
        let (next, after) = toggled(&alone, 300.0, 0.0, 0);
        assert_eq!(after.empty, 300.0);
        let (_, after) = toggled(&next, 300.0, after.empty, 0);
        assert_eq!((after.heights[0], after.empty), (300.0, 0.0));
    }

    #[test]
    fn opening_takes_its_own_height_from_the_empty_room_and_from_those_below_first() {
        let closed = |hand| Asked {
            open: false,
            least: 10.0,
            hand,
        };
        // With a height of its own: only that, the rest stays empty.
        let stack = [asked(10.0, Some(100.0)), closed(Some(50.0))];
        let (_, after) = toggled(&stack, 300.0, 200.0, 1);
        assert_eq!(after.heights, [100.0, 50.0]);
        assert_eq!(after.empty, 150.0);
        // With sections below: they give, the empty room stays.
        let stack = [asked(10.0, Some(100.0)), closed(None), asked(10.0, None)];
        let (_, after) = toggled(&stack, 300.0, 40.0, 1);
        assert_eq!(after.heights, [100.0, 80.0, 80.0]);
        assert_eq!(after.empty, 40.0);
        // They at their least, the empty room gives.
        let stack = [
            asked(10.0, Some(100.0)),
            closed(Some(120.0)),
            asked(10.0, None),
        ];
        let (_, after) = toggled(&stack, 300.0, 100.0, 1);
        assert_eq!(after.heights, [100.0, 120.0, 10.0]);
        assert_eq!(after.empty, 70.0);
    }

    #[test]
    fn a_section_of_rows_keeps_two_of_them_or_all_when_fewer() {
        assert_eq!(rows_least(20.0, 0), 0.0);
        assert_eq!(rows_least(20.0, 1), 20.0);
        assert_eq!(rows_least(20.0, 2), 40.0);
        assert_eq!(rows_least(20.0, 100_000), 40.0);
    }

    #[test]
    fn pinning_aligns_stored_heights_on_what_is_shown_and_keeps_the_others() {
        // Set by hand at 100, shown at 60 in a short panel: the next drag
        // starts from 60.
        let stack = [
            asked(20.0, Some(100.0)),
            asked(20.0, Some(100.0)),
            asked(20.0, None),
        ];
        let shown = fit(&stack, 140.0, 0.0).heights;
        assert_eq!(shown, [100.0, 20.0, 20.0]);
        let hands = pin_plan(&stack, 140.0, 0.0, &shown);
        assert_eq!(hands, [Some(100.0), Some(20.0), None]);
        // A folded one and the last keep what they had, for when they
        // are not last or folded any more.
        let stack = [
            asked(10.0, Some(50.0)),
            Asked {
                open: false,
                least: 10.0,
                hand: Some(33.0),
            },
            asked(10.0, Some(77.0)),
        ];
        let hands = pin_plan(&stack, 100.0, 0.0, &[60.0, 0.0, 40.0]);
        assert_eq!(hands, [Some(60.0), Some(33.0), Some(77.0)]);
    }

    #[test]
    fn a_stack_sized_by_its_content_leaves_the_room_under_the_last_band() {
        let inf = f32::INFINITY;
        let stack = [asked(20.0, None), asked(20.0, None)];
        // Both short: each as tall as its content, the rest under them.
        let (fit, spare) = fit_within(&stack, &[30.0, 50.0], 300.0, 0.0);
        assert_eq!(fit.heights, [30.0, 50.0]);
        assert_eq!(spare, 220.0);
        assert_eq!(fit.empty, 220.0);
        // A short one keeps its content's height, the long one the rest.
        let (fit, spare) = fit_within(&stack, &[30.0, 1000.0], 300.0, 0.0);
        assert_eq!(fit.heights, [30.0, 270.0]);
        assert_eq!(spare, 0.0);
        // Two long ones share; an unknown content is no bound.
        let (fit, _) = fit_within(&stack, &[1000.0, inf], 300.0, 0.0);
        assert_eq!(fit.heights, [150.0, 150.0]);
        // Never under the least height, even with less content.
        let (fit, _) = fit_within(&stack, &[5.0, 5.0], 300.0, 0.0);
        assert_eq!(fit.heights, [20.0, 20.0]);
        // A height set by hand is kept, past its content too.
        let stack = [asked(20.0, Some(100.0)), asked(20.0, None)];
        let (fit, spare) = fit_within(&stack, &[30.0, 40.0], 300.0, 0.0);
        assert_eq!(fit.heights, [100.0, 40.0]);
        assert_eq!(spare, 160.0);
        // Without bounds, as `fit`.
        let (fit, spare) = fit_within(&stack, &[], 300.0, 0.0);
        assert_eq!(fit, super::fit(&stack, 300.0, 0.0));
        assert_eq!(spare, 0.0);
        // A pinned plan comes back through the same fit.
        let stack = [asked(20.0, None), asked(20.0, None), asked(20.0, None)];
        let most = [30.0, 1000.0, 40.0];
        let hands = pin_plan_within(&stack, &most, 300.0, 0.0, &[60.0, 200.0, 40.0]);
        let pinned: Vec<Asked> = stack
            .iter()
            .zip(&hands)
            .map(|(a, &hand)| Asked { hand, ..*a })
            .collect();
        let (fit, _) = fit_within(&pinned, &most, 300.0, 0.0);
        assert_eq!(fit.heights, [60.0, 200.0, 40.0]);
    }
}