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
// SPDX-FileCopyrightText: Copyright (c) 2026 Mike Li/Mikewolfli/Wei Li(mikewolfli@163.com)
// SPDX-License-Identifier: MIT
//! Form layout manager — two-column label/field row pairs.
use super::{ChildInfo, Layout};
use crate::compat::{Any, Vec};
use crate::core::{ObjectId, Rect};
use crate::style::EdgeOffsets;
/// How a form decides the width of its field column.
///
/// The equivalent of Qt's `QFormLayout::FieldGrowthPolicy`. The default keeps this layout's
/// historical behaviour (the field takes whatever the label column left), so introducing the
/// enum changes no existing document.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum FieldGrowthPolicy {
/// Every field that is not fixed takes the rest of the row width. The default.
#[default]
AllNonFixed,
/// A field stays at the width it asks for; the rest of the row is left unused.
FieldsStayAtSize,
/// Fields grow, but only within the row they were given. Bounded by the same remainder
/// as [`Self::AllNonFixed`] in this implementation: the crate has no per-field
/// `SizePolicy` on form rows (unlike `BoxLayout`), so the two differ only once one
/// exists. Documented rather than silently treated as an alias.
ExpandingFieldsGrow,
}
/// When a form puts its field on a row of its own.
///
/// The equivalent of Qt's `QFormLayout::RowWrapPolicy`. The default is `DontWrapRows`, which
/// is what this layout has always done.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum RowWrapPolicy {
/// Label and field always share a row, however narrow it is. The default.
#[default]
DontWrapRows,
/// A row wraps when the label and the field's preferred widths do not fit together.
WrapLongRows,
/// Every field goes on its own row, below its label.
WrapAllRows,
}
/// Two-column form layout storing `(label, field)` row pairs
/// plus standalone items added via the `Layout` trait.
pub struct FormLayout {
spacing: u32,
margin: u32,
/// Four-way margins. `None` means the uniform [`Self::margin`] is used on every side.
margins: Option<EdgeOffsets>,
growth_policy: FieldGrowthPolicy,
row_wrap_policy: RowWrapPolicy,
rows: Vec<(ObjectId, ObjectId)>,
/// Standalone items added via the `Layout` trait (widget_id, stretch).
items: Vec<(ObjectId, u32)>,
}
impl FormLayout {
/// Create a two-column form layout.
pub fn new(spacing: u32, margin: u32) -> Self {
Self {
spacing,
margin,
margins: None,
growth_policy: FieldGrowthPolicy::default(),
row_wrap_policy: RowWrapPolicy::default(),
rows: Vec::new(),
items: Vec::new(),
}
}
/// Sets how much of the row's width a field takes.
pub fn set_field_growth_policy(&mut self, policy: FieldGrowthPolicy) {
self.growth_policy = policy;
}
/// The field growth policy.
pub fn field_growth_policy(&self) -> FieldGrowthPolicy {
self.growth_policy
}
/// Sets when a field moves onto its own row.
pub fn set_row_wrap_policy(&mut self, policy: RowWrapPolicy) {
self.row_wrap_policy = policy;
}
/// The row wrap policy.
pub fn row_wrap_policy(&self) -> RowWrapPolicy {
self.row_wrap_policy
}
/// Sets four-way margins, overriding the uniform [`Self::margin`].
pub fn set_margins(&mut self, margins: EdgeOffsets) {
self.margins = Some(margins);
}
/// The four-way margins, or `None` when the uniform [`Self::margin`] applies.
pub fn margins(&self) -> Option<EdgeOffsets> {
self.margins
}
/// Left/top/right/bottom insets for `rect`, each clamped to half its axis.
fn insets(&self, rect: Rect) -> (u32, u32, u32, u32) {
let limit_x = rect.width / 2;
let limit_y = rect.height / 2;
match self.margins {
Some(margins) => (
margins.left.min(limit_x),
margins.top.min(limit_y),
margins.right.min(limit_x),
margins.bottom.min(limit_y),
),
None => {
let uniform = self.margin.min(limit_x.min(limit_y));
(uniform, uniform, uniform, uniform)
}
}
}
/// Add one form row as `(label, field)` pair.
pub fn add_row_pair(&mut self, label_id: ObjectId, field_id: ObjectId) {
self.rows.push((label_id, field_id));
}
/// Returns the number of rows in the form.
pub fn row_count(&self) -> usize {
self.rows.len()
}
/// Convenience method that adds a standalone widget,
/// delegating to [`Layout::add_widget`]. Returns the index of the added item.
pub fn add_row(&mut self, widget_id: ObjectId) -> usize {
self.add_widget(widget_id, 0);
self.items.len().saturating_sub(1)
}
/// Returns the number of standalone items.
pub fn item_count(&self) -> usize {
self.items.len()
}
/// Returns the spacing between form entries.
pub fn spacing(&self) -> u32 {
self.spacing
}
/// Returns the outer margin.
pub fn margin(&self) -> u32 {
self.margin
}
/// The total spacing consumed between `entries` addressable slots, saturated into a `u32`.
///
/// D09-LAYOUT-02: `(entries - 1) * spacing` is computed in `u64` so a large spacing cannot
/// wrap in `u32`; the result saturates at [`u32::MAX`], which the caller clamps out of the
/// available height — a spacing budget larger than the box leaves the entries the minimum
/// 1-pixel height instead of a wrapped, generously-sized value.
fn spacing_budget(entries: usize, spacing: u32) -> u32 {
if entries <= 1 {
return 0;
}
let product = (entries as u64 - 1) * spacing as u64;
product.min(u32::MAX as u64) as u32
}
/// A `u32` layout metric as `i32`, saturating instead of wrapping.
///
/// D09-LAYOUT-02: the layout paints in `i32` coordinates, so a margin/spacing above
/// `i32::MAX` has to become `i32::MAX`; a plain `as i32` cast wraps it negative and would
/// move a child *before* the parent's origin.
fn clamped_i32(value: u32) -> i32 {
value.min(i32::MAX as u32) as i32
}
/// The sum of two `u32` pixel budgets, saturated at [`u32::MAX`] (D09-LAYOUT-02).
///
/// Used for the label column plus the gap the field column is measured against, which is
/// the one remaining `u32 + u32` in the inner-box arithmetic.
fn sum_budget(a: u32, b: u32) -> u32 {
(a as u64 + b as u64).min(u32::MAX as u64) as u32
}
/// Remove a row by its index. Returns false if index is out of bounds.
///
/// # What this removes, and what it must not
///
/// `rows` (label/field pairs, from [`Self::add_row_pair`]) and `items` (standalone
/// widgets, from [`Self::add_row`] / [`Layout::add_widget`]) are **two separate stores**, so
/// only `rows` holds the pair being removed. The body used to run
/// `items.retain(|(id, _)| *id != label && *id != field)` as well, which could therefore
/// never remove the row it had just taken out — its only effect was to *also* delete a
/// standalone item whose id happened to equal the departed label's or field's. Since
/// `ObjectId`s are unique that coincidence needs a caller re-using an id, but the
/// statement expressed a mixed-up model of the two stores, so it is gone rather than kept
/// "just in case".
///
/// A field widget that the caller also registered standalone must be removed through
/// [`Layout::remove_widget`], which is the store that actually holds it.
pub fn remove_row(&mut self, index: usize) -> bool {
if index >= self.rows.len() {
return false;
}
self.rows.remove(index);
true
}
}
impl Layout for FormLayout {
fn as_any(&self) -> &dyn Any {
self
}
fn as_any_mut(&mut self) -> &mut dyn Any {
self
}
fn add_widget(&mut self, widget_id: ObjectId, stretch: u32) {
self.items.push((widget_id, stretch));
}
/// The hint-aware path: applies [`FieldGrowthPolicy`] and [`RowWrapPolicy`].
///
/// `update` keeps the historical two-column behaviour; this method is where the policies
/// take effect, because both of them ask "how wide does this child want to be?" — the
/// question `ChildInfo` exists to answer (LAY-N01).
///
/// Height is still shared evenly across entries, which is the model `update` uses: a form
/// gives every row the same height, and a wrapping row costs two entries.
fn arrange(&self, rect: Rect, children: &[ChildInfo], out: &mut dyn FnMut(ObjectId, Rect)) {
if self.rows.is_empty() && self.items.is_empty() {
return;
}
let (left, top, right, bottom) = self.insets(rect);
let inner_width = rect.width.saturating_sub(left).saturating_sub(right);
let available_height = rect.height.saturating_sub(top).saturating_sub(bottom);
if inner_width == 0 || available_height == 0 {
return;
}
// Whether each pair occupies two rows. `WrapLongRows` asks the two children how wide
// they want to be; a missing hint counts as zero, which keeps the row unwrapped (the
// lenient answer, matching the rest of this parser).
let wraps: Vec<bool> = self
.rows
.iter()
.map(|(label, field)| match self.row_wrap_policy {
RowWrapPolicy::DontWrapRows => false,
RowWrapPolicy::WrapAllRows => true,
RowWrapPolicy::WrapLongRows => {
let wanted = |id: ObjectId| {
ChildInfo::find(children, id).map(|c| c.hints.width.pref).unwrap_or(0)
};
wanted(*label).saturating_add(self.spacing).saturating_add(wanted(*field))
> inner_width
}
})
.collect();
let entries = self
.rows
.iter()
.zip(&wraps)
.map(|(_, wrapped)| if *wrapped { 2 } else { 1 })
.sum::<usize>()
+ self.items.len();
if entries == 0 {
return;
}
let spacing_total = Self::spacing_budget(entries, self.spacing);
let entry_height = (available_height.saturating_sub(spacing_total) / entries as u32).max(1);
let label_width = inner_width / 3;
let gap = self.spacing.min(inner_width.saturating_sub(label_width));
let remaining_field_width = inner_width.saturating_sub(Self::sum_budget(label_width, gap));
let origin_x = rect.x.saturating_add(left as i32);
let spacing = Self::clamped_i32(gap);
let entry_height_i32 = entry_height as i32;
let mut cursor_y = rect.y.saturating_add(top as i32);
for ((label, field), wrapped) in self.rows.iter().zip(&wraps) {
let row_y = cursor_y;
if *wrapped {
out(*label, Rect::new(origin_x, row_y, inner_width, entry_height));
let field_y = row_y.saturating_add(entry_height_i32).saturating_add(spacing);
out(*field, Rect::new(origin_x, field_y, inner_width, entry_height));
cursor_y = field_y.saturating_add(entry_height_i32).saturating_add(spacing);
} else {
out(*label, Rect::new(origin_x, row_y, label_width, entry_height));
let field_width = match self.growth_policy {
FieldGrowthPolicy::AllNonFixed | FieldGrowthPolicy::ExpandingFieldsGrow => {
remaining_field_width
}
FieldGrowthPolicy::FieldsStayAtSize => ChildInfo::find(children, *field)
.map(|c| c.hints.width.pref.min(remaining_field_width))
.unwrap_or(remaining_field_width),
};
out(
*field,
Rect::new(
origin_x.saturating_add(label_width as i32).saturating_add(spacing),
row_y,
field_width,
entry_height,
),
);
cursor_y = row_y.saturating_add(entry_height_i32).saturating_add(spacing);
}
}
for (id, _stretch) in &self.items {
out(*id, Rect::new(origin_x, cursor_y, inner_width, entry_height));
cursor_y = cursor_y.saturating_add(entry_height_i32).saturating_add(spacing);
}
}
fn remove_widget(&mut self, widget_id: ObjectId) {
self.rows.retain(|(label, field)| *label != widget_id && *field != widget_id);
self.items.retain(|(id, _)| *id != widget_id);
}
fn child_ids(&self) -> Vec<ObjectId> {
let mut ids = Vec::new();
for (label, field) in &self.rows {
ids.push(*label);
ids.push(*field);
}
for (id, _) in &self.items {
ids.push(*id);
}
ids
}
fn has_child(&self, id: ObjectId) -> bool {
self.rows.iter().any(|(label, field)| *label == id || *field == id)
|| self.items.iter().any(|(widget_id, _)| *widget_id == id)
}
fn clear(&mut self) {
self.rows.clear();
self.items.clear();
}
fn update(&self, rect: Rect, widgets: &mut dyn FnMut(ObjectId, Rect)) {
let total_rows = self.rows.len();
let total_items = self.items.len();
// D09-LAYOUT-02: `total_entries` counts child slots, so the doubling can only overflow
// `usize` on a build where `usize` is wider than 32 bits *and* a form holds more than
// 2^63 label/field rows — unreachable before memory is exhausted. Saturating keeps the
// arithmetic total on every target instead of relying on that argument.
let total_entries = total_rows.saturating_mul(2).saturating_add(total_items);
if total_entries == 0 {
return;
}
// D09-LAYOUT-02: `margin` and `spacing` are public `u32` values, so neither
// `margin * 2` nor `(n - 1) * spacing` may be evaluated in `u32`: the first panics in a
// build with overflow checks and wraps otherwise once `margin > u32::MAX / 2`, and the
// second does the same once the spacing budget exceeds `u32::MAX`. Both products are
// computed in `u64` and saturated back into `u32`, which defines an explicit bounded
// policy for huge values rather than letting the constructor's arguments escape the
// layout arithmetic:
//
// * a margin that cannot be paid twice consumes the whole axis, so the inner box is
// zero-width/height and the entries are clipped by the parent (see the `== 0` early
// return below) instead of receiving a negative/wrapped box;
// * a spacing budget that does not fit `u32` is saturated at the maximum, so it can
// never wrap into a small number and hand entries a larger height than they earned.
let (left, top, right, bottom) = self.insets(rect);
let inner_width = rect.width.saturating_sub(left).saturating_sub(right);
let available_height = rect.height.saturating_sub(top).saturating_sub(bottom);
if available_height == 0 || inner_width == 0 {
return;
}
let spacing_total = Self::spacing_budget(total_entries, self.spacing);
// Keep every entry addressable when the form is smaller than the
// minimum spacing budget; the parent clip/scroll container owns the
// visual overflow rather than receiving zero-height child rects.
let entry_height =
(available_height.saturating_sub(spacing_total) / total_entries as u32).max(1);
// Layout rows: each row has a label (1/3 width) and a field (2/3 width), measured
// against the same inner box the standalone items use — the parent minus *both*
// margins. Allocating the full width and then adding the left margin pushed the field
// past the inner right edge (S-57).
let label_width = inner_width / 3;
// The gap between the label and the field is paid from the inner width, so it must be
// clamped to what is left after the label column: a spacing larger than that would
// otherwise push the field out of the parent's right edge (D09-LAYOUT-02, same policy as
// the margin above). Clamping to the *remaining* budget, not just the inner width, is what
// keeps `origin_x + label_width + gap` at or before the inner right edge.
let gap = self.spacing.min(inner_width.saturating_sub(label_width));
// `label_width + gap` is `u32 + u32` too, so it takes the same widened treatment as the
// other products here (D09-LAYOUT-02).
let field_width = inner_width.saturating_sub(Self::sum_budget(label_width, gap));
let margin = Self::clamped_i32(left);
let margin_top = Self::clamped_i32(top);
// `spacing` positions the field and advances the rows. Clamped to the inner box so a huge
// value cannot wrap the painted `x`/`y` (D09-LAYOUT-02).
let spacing = Self::clamped_i32(gap);
let entry_height_i32 = entry_height as i32;
// The per-entry stride is `entry_height + spacing`; both are already `i32`-bounded, but
// their sum can still overflow, so saturate it once and reuse it for every row. The
// cumulative `y` likewise saturates rather than wrapping (D09-LAYOUT-02).
let stride = entry_height_i32.saturating_add(spacing);
let origin_x = rect.x.saturating_add(margin);
let mut index: i32 = 0;
for (label, field) in &self.rows {
let y = rect.y.saturating_add(margin_top).saturating_add(index.saturating_mul(stride));
widgets(*label, Rect::new(origin_x, y, label_width, entry_height));
widgets(
*field,
Rect::new(
origin_x.saturating_add(label_width as i32).saturating_add(spacing),
y,
field_width,
entry_height,
),
);
index = index.saturating_add(2);
}
// Layout standalone items: each gets the full inner width, sharing the same box
// the label/field pairs use.
for (id, _stretch) in &self.items {
let y = rect.y.saturating_add(margin_top).saturating_add(index.saturating_mul(stride));
widgets(*id, Rect::new(origin_x, y, inner_width, entry_height));
index = index.saturating_add(1);
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::compat::HashMap;
#[test]
fn form_layout_preserves_nonzero_height_when_entries_overflow() {
let mut layout = FormLayout::new(8, 0);
layout.add_row_pair(1, 2);
layout.add_row_pair(3, 4);
let mut geometries = Vec::new();
layout.update(Rect::new(0, 0, 200, 4), &mut |id, rect| geometries.push((id, rect)));
assert_eq!(geometries.len(), 4);
assert!(geometries.iter().all(|(_, rect)| rect.height >= 1));
}
#[test]
fn form_layout_empty_form_emits_no_children() {
let layout = FormLayout::new(8, 4);
let mut count = 0;
layout.update(Rect::new(0, 0, 200, 100), &mut |_, _| count += 1);
assert_eq!(count, 0);
}
/// S-57: a label/field pair shares the standalone items' inner box (parent minus both
/// margins), so the field never overflows the right edge.
#[test]
fn form_layout_pair_stays_inside_the_inner_box() {
let mut layout = FormLayout::new(10, 10);
layout.add_row_pair(1, 2);
let mut rects = HashMap::new();
layout.update(Rect::new(0, 0, 300, 100), &mut |id, rect| {
rects.insert(id, rect);
});
let inner_right = 300 - 10;
let label = rects.get(&1).copied().expect("label placed");
let field = rects.get(&2).copied().expect("field placed");
assert_eq!(label.x, 10, "the label starts at the left margin");
assert!(label.x + label.width as i32 <= inner_right, "label escapes: {label:?}");
assert!(field.x + field.width as i32 <= inner_right, "field escapes: {field:?}");
assert_eq!(field.x, label.x + label.width as i32 + 10, "the gap is paid once");
}
/// S-57 (non-zero origin): the same inner-box arithmetic holds away from the origin.
#[test]
fn form_layout_pair_respects_a_nonzero_parent_origin() {
let mut layout = FormLayout::new(0, 5);
layout.add_row_pair(1, 2);
let mut rects = HashMap::new();
layout.update(Rect::new(100, 50, 200, 100), &mut |id, rect| {
rects.insert(id, rect);
});
let inner_left = 100 + 5;
let inner_right = 100 + 200 - 5;
let field = rects.get(&2).copied().expect("field placed");
assert_eq!(field.x + field.width as i32, inner_right, "field reaches the inner right edge");
let label = rects.get(&1).copied().expect("label placed");
assert_eq!(label.x, inner_left, "label starts at the origin plus the margin");
}
// ── D09-LAYOUT-02: public `u32` margin/spacing multiplication must not overflow ──
/// The `margin * 2` inner-box computation previously overflowed for `margin > u32::MAX / 2`
/// (panic under overflow checks, wrap otherwise). It must now stay silent with no children
/// emitted, because the margin consumes the whole axis and the inner box is empty under the
/// bounded policy.
#[test]
fn form_layout_max_margin_does_not_panic_and_emits_nothing() {
let mut layout = FormLayout::new(0, u32::MAX);
layout.add_row_pair(1, 2);
let mut count = 0;
layout.update(Rect::new(0, 0, 200, 100), &mut |_, _| count += 1);
assert_eq!(count, 0, "a margin that cannot be paid twice leaves no addressable inner box");
}
/// The boundary just past half of `u32::MAX` is where `margin * 2` stops fitting `u32`; the
/// layout must still not panic and must not place children in a wrapped box.
#[test]
fn form_layout_boundary_margin_doubling_does_not_wrap() {
let mut layout = FormLayout::new(0, u32::MAX / 2 + 1);
layout.add_row_pair(1, 2);
let mut rects = HashMap::new();
layout.update(Rect::new(0, 0, 200, 100), &mut |id, rect| {
rects.insert(id, rect);
});
// Doubled margin exceeds the parent's height/width, so the inner box is empty and the
// entries are clipped by the parent rather than handed a large wrapped rect.
assert!(rects.is_empty(), "no child may be placed in a wrapped inner box: {rects:?}");
}
/// A multi-row form with a huge spacing exercises `(total_entries - 1) * spacing`, which used
/// to overflow before the `saturating_sub`. Every placed entry must stay inside the parent
/// and keep at least one pixel of height (D09-LAYOUT-02).
#[test]
fn form_layout_huge_spacing_keeps_children_bounded() {
let mut layout = FormLayout::new(u32::MAX, 4);
layout.add_row_pair(1, 2);
layout.add_row_pair(3, 4);
layout.add_widget(5, 0);
let parent = Rect::new(10, 20, 300, 200);
let mut rects = HashMap::new();
layout.update(parent, &mut |id, rect| {
rects.insert(id, rect);
});
assert_eq!(rects.len(), 5, "every entry is still addressable");
for (id, rect) in &rects {
assert!(rect.height >= 1, "{id} collapsed: {rect:?}");
assert!(rect.x >= parent.x, "{id} starts before the parent: {rect:?}");
assert!(rect.y >= parent.y, "{id} starts above the parent: {rect:?}");
assert!(
rect.x + rect.width as i32 <= parent.x + parent.width as i32,
"{id} escapes the parent's right edge: {rect:?}"
);
}
}
/// A single row has no `(n - 1) * spacing` term; the huge spacing must still not overflow the
/// arithmetic or the placed rect.
#[test]
fn form_layout_single_row_with_huge_spacing_is_bounded() {
let mut layout = FormLayout::new(u32::MAX, 0);
layout.add_row_pair(1, 2);
let parent = Rect::new(0, 0, 400, 300);
let mut rects = HashMap::new();
layout.update(parent, &mut |id, rect| {
rects.insert(id, rect);
});
assert_eq!(rects.len(), 2);
for (id, rect) in &rects {
assert!(rect.height >= 1, "{id} collapsed: {rect:?}");
assert!(rect.x >= parent.x && rect.y >= parent.y, "{id} misplaced: {rect:?}");
assert!(
rect.x + rect.width as i32 <= parent.x + parent.width as i32,
"{id} escapes the parent's right edge: {rect:?}"
);
}
}
/// `WrapLongRows` puts the field on its own row when the pair does not fit.
#[test]
fn wrap_long_rows_moves_the_field_below_the_label() {
let mut layout = FormLayout::new(8, 0);
layout.add_row_pair(1, 2);
layout.set_row_wrap_policy(RowWrapPolicy::WrapLongRows);
let children = [
ChildInfo::new(1, crate::layout::Hints::fixed(150, 20)),
ChildInfo::new(2, crate::layout::Hints::fixed(150, 20)),
];
let mut out = Vec::new();
layout.arrange(Rect::new(0, 0, 200, 100), &children, &mut |id, rect| out.push((id, rect)));
let label = out.iter().find(|(id, _)| *id == 1).unwrap().1;
let field = out.iter().find(|(id, _)| *id == 2).unwrap().1;
assert_eq!(label.width, 200, "a wrapped label takes the whole row");
assert_eq!(field.width, 200, "and so does the field below it");
assert!(field.y > label.y, "the field is on the next row");
}
/// `FieldsStayAtSize` gives the field its preferred width instead of the remainder.
#[test]
fn fields_stay_at_size_uses_the_requested_width() {
let mut layout = FormLayout::new(8, 0);
layout.add_row_pair(1, 2);
layout.set_field_growth_policy(FieldGrowthPolicy::FieldsStayAtSize);
let children = [
ChildInfo::new(1, crate::layout::Hints::fixed(120, 20)),
ChildInfo::new(2, crate::layout::Hints::fixed(60, 20)),
];
let mut out = Vec::new();
layout.arrange(Rect::new(0, 0, 200, 100), &children, &mut |id, rect| out.push((id, rect)));
let field = out.iter().find(|(id, _)| *id == 2).unwrap().1;
assert_eq!(field.width, 60, "the field keeps its own width");
// The default policy would have given it the rest of the row.
assert!(field.width < 200 - 200 / 3);
}
}