1
//! Scrollbar geometry computation — single source of truth for the layout solver.
2
//!
3
//! Provides [`ScrollbarRequirements`] (whether scrollbars are needed and how much
4
//! space they reserve) and [`ScrollbarGeometry`] (track, thumb, and button rects).
5
//!
6
//! The main entry point is [`compute_scrollbar_geometry`], whose output is consumed by:
7
//! - Display list painting (`paint_scrollbars`)
8
//! - GPU transform updates (`update_scrollbar_transforms`)
9
//! - Hit-testing (`hit_test_component`)
10
//! - Drag delta conversion (`handle_scrollbar_drag`)
11

            
12
use azul_core::{
13
    dom::ScrollbarOrientation,
14
    geom::{LogicalPosition, LogicalRect, LogicalSize},
15
};
16

            
17
/// Information about scrollbar requirements and dimensions
18
// +spec:overflow:55c244 - scrollbar appearance, size, and edge placement are UA-defined
19
#[derive(Copy, Debug, Clone, Default)]
20
#[repr(C)]
21
pub struct ScrollbarRequirements {
22
    pub needs_horizontal: bool,
23
    pub needs_vertical: bool,
24
    /// Layout-reserved width for a vertical scrollbar (0.0 for overlay)
25
    pub scrollbar_width: f32,
26
    /// Layout-reserved height for a horizontal scrollbar (0.0 for overlay)
27
    pub scrollbar_height: f32,
28
    /// Visual rendering width of the scrollbar in CSS pixels (e.g. 8.0 for thin).
29
    /// Non-zero even for overlay scrollbars. Used by GPU state for thumb positioning.
30
    pub visual_width_px: f32,
31
}
32

            
33
impl ScrollbarRequirements {
34
    /// Checks if the presence of scrollbars reduces the available inner size,
35
    /// which would necessitate a reflow of the content.
36
    #[must_use]
37
28204
    pub fn needs_reflow(&self) -> bool {
38
28204
        self.scrollbar_width > 0.0 || self.scrollbar_height > 0.0
39
28204
    }
40

            
41
    // +spec:box-model:20c3c8 - scrollbar space reserved between inner border edge and outer padding
42
    // edge +spec:box-model:32cd53 - scrollbar space subtracted from containing block dimensions
43
    // +spec:overflow:30a49c - scrollbar space subtracted from content area
44
    /// Takes a size (representing a content-box) and returns a new size
45
    /// reduced by the dimensions of any active scrollbars.
46
    #[must_use]
47
53847
    pub fn shrink_size(&self, size: LogicalSize) -> LogicalSize {
48
53847
        LogicalSize {
49
53847
            width: (size.width - self.scrollbar_width).max(0.0),
50
53847
            height: (size.height - self.scrollbar_height).max(0.0),
51
53847
        }
52
53847
    }
53
}
54

            
55
/// Single source of truth for scrollbar geometry.
56
///
57
/// Computed once by [`compute_scrollbar_geometry`], then used by:
58
/// - Display list painting (`paint_scrollbars`)
59
/// - GPU transform updates (`update_scrollbar_transforms`)
60
/// - Hit-testing (`hit_test_component`)
61
/// - Drag delta conversion (`handle_scrollbar_drag`)
62
#[derive(Debug, Clone, Copy)]
63
pub struct ScrollbarGeometry {
64
    /// Orientation (vertical or horizontal)
65
    pub orientation: ScrollbarOrientation,
66
    /// The full track rect in the container's coordinate space
67
    pub track_rect: LogicalRect,
68
    /// Button size (square: width = height = `scrollbar_width_px`)
69
    pub button_size: f32,
70
    /// Usable track length after subtracting buttons and corner
71
    /// = `track_total` - 2*`button_size`
72
    pub usable_track_length: f32,
73
    /// The thumb length (min-clamped to 2*`width_px`)
74
    pub thumb_length: f32,
75
    /// Thumb size as ratio of viewport / content (0.0–1.0)
76
    pub thumb_size_ratio: f32,
77
    /// Scroll ratio (0.0 at top/left, 1.0 at bottom/right)
78
    pub scroll_ratio: f32,
79
    /// Thumb offset in pixels from the start of the usable track region
80
    pub thumb_offset: f32,
81
    /// Max scroll distance in content pixels
82
    pub max_scroll: f32,
83
    /// CSS-specified scrollbar thickness (width for vertical, height for horizontal)
84
    pub width_px: f32,
85
}
86

            
87
impl Default for ScrollbarGeometry {
88
1
    fn default() -> Self {
89
1
        Self {
90
1
            orientation: ScrollbarOrientation::Vertical,
91
1
            track_rect: LogicalRect::zero(),
92
1
            button_size: 0.0,
93
1
            usable_track_length: 0.0,
94
1
            thumb_length: 0.0,
95
1
            thumb_size_ratio: 0.0,
96
1
            scroll_ratio: 0.0,
97
1
            thumb_offset: 0.0,
98
1
            max_scroll: 0.0,
99
1
            width_px: 0.0,
100
1
        }
101
1
    }
102
}
103

            
104
/// Compute scrollbar geometry for one axis.
105
///
106
/// This is the **single source of truth** for all scrollbar calculations.
107
/// All consumers (display list painting, GPU transforms, hit-testing, drag)
108
/// must use this function to ensure consistent geometry.
109
///
110
/// # Parameters
111
/// - `orientation`: Vertical or horizontal scrollbar
112
/// - `inner_rect`: The padding-box (border-box minus borders) of the scroll container, in the
113
///   container's coordinate space (absolute window coordinates)
114
/// - `content_size`: Total content size (from `get_content_size()` or `virtual_scroll_size`)
115
/// - `scroll_offset`: Current scroll offset (y for vertical, x for horizontal; positive =
116
///   scrolled). A negative value is overscroll past the scroll origin and pins the thumb at the
117
///   track start.
118
/// - `scrollbar_width_px`: CSS-resolved scrollbar thickness in pixels
119
/// - `has_other_scrollbar`: Whether the perpendicular scrollbar is also visible (reduces track
120
///   length by one `scrollbar_width_px` for the corner)
121
#[must_use]
122
58
pub fn compute_scrollbar_geometry(
123
58
    orientation: ScrollbarOrientation,
124
58
    inner_rect: LogicalRect,
125
58
    content_size: LogicalSize,
126
58
    scroll_offset: f32,
127
58
    scrollbar_width_px: f32,
128
58
    has_other_scrollbar: bool,
129
58
) -> ScrollbarGeometry {
130
    // For macOS-style overlay scrollbars, callers should pass button_size=0.
131
    // For legacy scrollbars with arrow buttons, button_size=scrollbar_width_px.
132
58
    compute_scrollbar_geometry_with_button_size(
133
58
        orientation,
134
58
        inner_rect,
135
58
        content_size,
136
58
        scroll_offset,
137
58
        scrollbar_width_px,
138
58
        has_other_scrollbar,
139
58
        scrollbar_width_px, // default: reserve button space
140
    )
141
58
}
142

            
143
/// Snap a thumb offset to whole pixels for the value that DRIVES THE PAINT.
144
///
145
/// A scrollbar thumb's position reaches the rasteriser as a GPU value, not as
146
/// a display-list rect, and that value is the ONLY channel that can raise
147
/// damage for the bar (the `ScrollBarStyled` items themselves compare equal
148
/// across a scroll). So an unquantised thumb turns every sub-pixel scroll
149
/// frame into a repaint of the whole bar: a high-resolution trackpad delivers
150
/// deltas around 0.2 px, which on a 100 px viewport over 600 px of content
151
/// moves the thumb by ~0.03 px — a move no one can see, on a window whose
152
/// CONTENT is deliberately left alone by the half-device-pixel scroll
153
/// threshold in the frame builder. Damaging the gutter anyway is the same
154
/// class of false per-frame damage that the idle-skip path exists to kill.
155
///
156
/// Quantising the VALUE (rather than teaching the damage diff to ignore small
157
/// deltas) is what keeps paint and damage in agreement: if we skipped the
158
/// damage while still painting an anti-aliased edge 0.03 px lower, the frame
159
/// would keep stale thumb pixels. Rounded, the two frames are byte-identical,
160
/// so "no damage" is the truth and not an approximation. Accumulation falls
161
/// out for free — the offset is a pure function of the scroll offset, so once
162
/// enough scroll has accrued to cross a pixel, the rounded value changes and
163
/// the bar is damaged then.
164
///
165
/// The unit is the LOGICAL pixel, because the display list (and therefore the
166
/// baked `thumb_initial_transform` this must agree with) is DPI-free by
167
/// construction: the rasteriser multiplies by the DPI factor, and no layout
168
/// stage knows it. On a `HiDPI` screen that costs at most one device pixel of
169
/// thumb precision — against a track that has only `usable_track_length`
170
/// distinct positions to begin with.
171
///
172
/// Both producers of the thumb transform — `paint_scrollbars` (which bakes
173
/// the initial value into the display list) and
174
/// `GpuStateManager::update_scrollbar_transforms` (which overwrites it on
175
/// every scroll) — MUST call this, or the two disagree by a fraction of a
176
/// pixel and the disagreement itself becomes a spurious damage event.
177
#[must_use]
178
19437
pub const fn quantize_thumb_offset(thumb_offset: f32) -> f32 {
179
19437
    if thumb_offset.is_finite() {
180
19432
        thumb_offset.round()
181
    } else {
182
5
        0.0
183
    }
184
19437
}
185

            
186
/// Like [`compute_scrollbar_geometry`] but allows overriding the button size.
187
/// Pass `button_size = 0.0` for macOS-style overlay scrollbars (no arrow buttons).
188
#[must_use]
189
45727
pub fn compute_scrollbar_geometry_with_button_size(
190
45727
    orientation: ScrollbarOrientation,
191
45727
    inner_rect: LogicalRect,
192
45727
    content_size: LogicalSize,
193
45727
    scroll_offset: f32,
194
45727
    scrollbar_width_px: f32,
195
45727
    has_other_scrollbar: bool,
196
45727
    button_size: f32,
197
45727
) -> ScrollbarGeometry {
198
45727
    let (track_total, viewport_length, content_length, track_rect) = match orientation {
199
        ScrollbarOrientation::Vertical => {
200
38954
            let track_total = if has_other_scrollbar {
201
3290
                inner_rect.size.height - scrollbar_width_px
202
            } else {
203
35664
                inner_rect.size.height
204
            };
205
38954
            let track_rect = LogicalRect {
206
38954
                origin: LogicalPosition::new(
207
38954
                    inner_rect.origin.x + inner_rect.size.width - scrollbar_width_px,
208
38954
                    inner_rect.origin.y,
209
38954
                ),
210
38954
                size: LogicalSize::new(scrollbar_width_px, track_total),
211
38954
            };
212
38954
            (
213
38954
                track_total,
214
38954
                inner_rect.size.height,
215
38954
                content_size.height,
216
38954
                track_rect,
217
38954
            )
218
        }
219
        ScrollbarOrientation::Horizontal => {
220
6773
            let track_total = if has_other_scrollbar {
221
3227
                inner_rect.size.width - scrollbar_width_px
222
            } else {
223
3546
                inner_rect.size.width
224
            };
225
6773
            let track_rect = LogicalRect {
226
6773
                origin: LogicalPosition::new(
227
6773
                    inner_rect.origin.x,
228
6773
                    inner_rect.origin.y + inner_rect.size.height - scrollbar_width_px,
229
6773
                ),
230
6773
                size: LogicalSize::new(track_total, scrollbar_width_px),
231
6773
            };
232
6773
            (
233
6773
                track_total,
234
6773
                inner_rect.size.width,
235
6773
                content_size.width,
236
6773
                track_rect,
237
6773
            )
238
        }
239
    };
240

            
241
45727
    compute_thumb_geometry(
242
45727
        orientation,
243
45727
        track_rect,
244
45727
        track_total,
245
45727
        viewport_length,
246
45727
        content_length,
247
45727
        button_size,
248
45727
        scrollbar_width_px,
249
45727
        scroll_offset,
250
    )
251
45727
}
252

            
253
#[allow(clippy::suboptimal_flops)] // mul_add not guaranteed faster/available without target +fma;
254
                                   // keep explicit a*b+c
255
45732
fn compute_thumb_geometry(
256
45732
    orientation: ScrollbarOrientation,
257
45732
    track_rect: LogicalRect,
258
45732
    track_total: f32,
259
45732
    viewport_length: f32,
260
45732
    content_length: f32,
261
45732
    button_size: f32,
262
45732
    scrollbar_width_px: f32,
263
45732
    scroll_offset: f32,
264
45732
) -> ScrollbarGeometry {
265
45732
    let usable_track_length = (track_total - 2.0 * button_size).max(0.0);
266

            
267
45732
    let thumb_size_ratio = if content_length > 0.0 {
268
43304
        (viewport_length / content_length).min(1.0)
269
    } else {
270
2428
        1.0
271
    };
272
45732
    let thumb_length = (usable_track_length * thumb_size_ratio)
273
45732
        .max(scrollbar_width_px * 2.0)
274
45732
        .min(usable_track_length);
275

            
276
45732
    let max_scroll = (content_length - viewport_length).max(0.0);
277
    // A negative offset only ever comes from the rubber-band overscroll of
278
    // `ScrollManager::set_scroll_position_unclamped` (every other writer clamps to
279
    // `[0, max_scroll]`), i.e. the user is pulling *past the scroll origin* — the
280
    // clamp below pins the thumb at the start of the track. Taking the absolute
281
    // value instead would send it down the track while the pull goes up.
282
45732
    let scroll_ratio = if max_scroll > 0.0 {
283
38517
        (scroll_offset / max_scroll).clamp(0.0, 1.0)
284
    } else {
285
7215
        0.0
286
    };
287

            
288
45732
    let thumb_offset = (usable_track_length - thumb_length) * scroll_ratio;
289

            
290
45732
    ScrollbarGeometry {
291
45732
        orientation,
292
45732
        track_rect,
293
45732
        button_size,
294
45732
        usable_track_length,
295
45732
        thumb_length,
296
45732
        thumb_size_ratio,
297
45732
        scroll_ratio,
298
45732
        thumb_offset,
299
45732
        max_scroll,
300
45732
        width_px: scrollbar_width_px,
301
45732
    }
302
45732
}
303

            
304
#[cfg(test)]
305
mod autotest_generated {
306
    use super::*;
307

            
308
    // ---------------------------------------------------------------------
309
    // helpers
310
    // ---------------------------------------------------------------------
311

            
312
    fn rect(x: f32, y: f32, w: f32, h: f32) -> LogicalRect {
313
        LogicalRect::new(LogicalPosition::new(x, y), LogicalSize::new(w, h))
314
    }
315

            
316
    fn reqs(width: f32, height: f32) -> ScrollbarRequirements {
317
        ScrollbarRequirements {
318
            needs_horizontal: height > 0.0,
319
            needs_vertical: width > 0.0,
320
            scrollbar_width: width,
321
            scrollbar_height: height,
322
            visual_width_px: 15.0,
323
        }
324
    }
325

            
326
    #[track_caller]
327
    fn approx(actual: f32, expected: f32) {
328
        assert!(
329
            (actual - expected).abs() <= 1e-3,
330
            "expected {expected}, got {actual}"
331
        );
332
    }
333

            
334
    // ---------------------------------------------------------------------
335
    // quantize_thumb_offset  (paint/damage quantiser)
336
    // ---------------------------------------------------------------------
337

            
338
    #[test]
339
    fn quantize_thumb_offset_rounds_to_the_nearest_whole_pixel() {
340
        approx(quantize_thumb_offset(0.0), 0.0);
341
        approx(quantize_thumb_offset(0.03), 0.0);
342
        approx(quantize_thumb_offset(0.499), 0.0);
343
        approx(quantize_thumb_offset(0.5), 1.0);
344
        approx(quantize_thumb_offset(3.12), 3.0);
345
        approx(quantize_thumb_offset(5.0), 5.0);
346
        approx(quantize_thumb_offset(83.33), 83.0);
347
    }
348

            
349
    #[test]
350
    fn quantize_thumb_offset_is_the_finite_guard_on_the_transform() {
351
        // (inf - inf) * ratio = NaN reaches here from an infinite used_size;
352
        // a NaN in a translation matrix never compares equal to itself, so the
353
        // GPU cache would re-emit a Changed event every frame forever and
354
        // WebRender would be handed a NaN transform.
355
        assert_eq!(quantize_thumb_offset(f32::NAN), 0.0);
356
        assert_eq!(quantize_thumb_offset(f32::INFINITY), 0.0);
357
        assert_eq!(quantize_thumb_offset(f32::NEG_INFINITY), 0.0);
358
    }
359

            
360
    #[test]
361
    fn quantize_thumb_offset_accumulates_rather_than_swallowing_slow_scrolls() {
362
        // The offset is a pure function of the scroll offset, so no separate
363
        // accumulator is needed: sub-pixel steps report the same value until
364
        // enough scroll has accrued to cross a pixel, and then they report the
365
        // next one. (0.2/0.4/0.6 px of scroll on a 100-over-600 viewport ->
366
        // 0.033/0.067/0.1 px of thumb; 6 px of scroll -> 1 px of thumb.)
367
        let thumb_for = |scroll: f32| {
368
            quantize_thumb_offset(
369
                compute_scrollbar_geometry(
370
                    ScrollbarOrientation::Vertical,
371
                    rect(0.0, 0.0, 200.0, 100.0),
372
                    LogicalSize::new(180.0, 600.0),
373
                    scroll,
374
                    8.0,
375
                    false,
376
                )
377
                .thumb_offset,
378
            )
379
        };
380
        assert_eq!(thumb_for(0.0), thumb_for(0.2));
381
        assert_eq!(thumb_for(0.0), thumb_for(0.6));
382
        assert!(
383
            thumb_for(30.0) > thumb_for(0.0),
384
            "a scroll big enough to move the thumb a whole pixel must still move it"
385
        );
386
    }
387

            
388
    // ---------------------------------------------------------------------
389
    // ScrollbarRequirements::needs_reflow  (getter / predicate)
390
    // ---------------------------------------------------------------------
391

            
392
    #[test]
393
    fn needs_reflow_default_instance_is_false() {
394
        assert!(!ScrollbarRequirements::default().needs_reflow());
395
    }
396

            
397
    #[test]
398
    fn needs_reflow_true_when_either_axis_reserves_space() {
399
        assert!(reqs(15.0, 0.0).needs_reflow());
400
        assert!(reqs(0.0, 15.0).needs_reflow());
401
        assert!(reqs(15.0, 15.0).needs_reflow());
402
        assert!(!reqs(0.0, 0.0).needs_reflow());
403
    }
404

            
405
    #[test]
406
    fn needs_reflow_is_false_for_overlay_scrollbars() {
407
        // Overlay scrollbars are visible (visual_width_px > 0) but reserve no
408
        // layout space, so they must never trigger a reflow.
409
        let overlay = ScrollbarRequirements {
410
            needs_horizontal: true,
411
            needs_vertical: true,
412
            scrollbar_width: 0.0,
413
            scrollbar_height: 0.0,
414
            visual_width_px: 12.0,
415
        };
416
        assert!(!overlay.needs_reflow());
417
    }
418

            
419
    #[test]
420
    fn needs_reflow_does_not_panic_on_extreme_values() {
421
        // NaN compares false against every bound, so it reads as "no space reserved".
422
        assert!(!reqs(f32::NAN, f32::NAN).needs_reflow());
423
        // Negative / -inf reservations are nonsense but must not report a reflow.
424
        assert!(!reqs(-1.0, -1.0).needs_reflow());
425
        assert!(!reqs(f32::NEG_INFINITY, f32::NEG_INFINITY).needs_reflow());
426
        assert!(!reqs(f32::MIN, f32::MIN).needs_reflow());
427
        assert!(!reqs(-0.0, -0.0).needs_reflow());
428
        // Anything strictly positive, however small or large, does.
429
        assert!(reqs(f32::MIN_POSITIVE, 0.0).needs_reflow());
430
        assert!(reqs(0.0, f32::MAX).needs_reflow());
431
        assert!(reqs(f32::INFINITY, 0.0).needs_reflow());
432
    }
433

            
434
    // ---------------------------------------------------------------------
435
    // ScrollbarRequirements::shrink_size  (numeric)
436
    // ---------------------------------------------------------------------
437

            
438
    #[test]
439
    fn shrink_size_zero_reservation_is_the_identity() {
440
        let out = reqs(0.0, 0.0).shrink_size(LogicalSize::new(800.0, 600.0));
441
        approx(out.width, 800.0);
442
        approx(out.height, 600.0);
443
    }
444

            
445
    #[test]
446
    fn shrink_size_subtracts_each_axis_independently() {
447
        let out = reqs(15.0, 0.0).shrink_size(LogicalSize::new(100.0, 200.0));
448
        approx(out.width, 85.0);
449
        approx(out.height, 200.0);
450

            
451
        let out = reqs(0.0, 15.0).shrink_size(LogicalSize::new(100.0, 200.0));
452
        approx(out.width, 100.0);
453
        approx(out.height, 185.0);
454
    }
455

            
456
    #[test]
457
    fn shrink_size_clamps_at_zero_and_never_returns_negative() {
458
        let out = reqs(100.0, 100.0).shrink_size(LogicalSize::new(10.0, 10.0));
459
        approx(out.width, 0.0);
460
        approx(out.height, 0.0);
461
        assert!(out.width >= 0.0 && out.height >= 0.0);
462

            
463
        // Zero-sized content box stays at zero.
464
        let out = reqs(15.0, 15.0).shrink_size(LogicalSize::new(0.0, 0.0));
465
        approx(out.width, 0.0);
466
        approx(out.height, 0.0);
467
    }
468

            
469
    #[test]
470
    fn shrink_size_at_float_min_max_does_not_panic() {
471
        // MAX - MAX == 0.0
472
        let out = reqs(f32::MAX, f32::MAX).shrink_size(LogicalSize::new(f32::MAX, f32::MAX));
473
        approx(out.width, 0.0);
474
        approx(out.height, 0.0);
475

            
476
        // MAX with nothing reserved survives unchanged (no overflow).
477
        let out = reqs(0.0, 0.0).shrink_size(LogicalSize::new(f32::MAX, f32::MAX));
478
        assert!(out.width.is_finite() && out.height.is_finite());
479
        approx(out.width / f32::MAX, 1.0);
480

            
481
        // A negative (MIN) input size is clamped up to zero.
482
        let out = reqs(0.0, 0.0).shrink_size(LogicalSize::new(f32::MIN, f32::MIN));
483
        approx(out.width, 0.0);
484
        approx(out.height, 0.0);
485
    }
486

            
487
    #[test]
488
    fn shrink_size_with_nan_or_infinite_inputs_is_defined() {
489
        // NaN propagates into the subtraction, but f32::max(NaN, 0.0) == 0.0,
490
        // so the result is a defined (zero) size rather than a NaN size.
491
        let out = reqs(f32::NAN, f32::NAN).shrink_size(LogicalSize::new(100.0, 100.0));
492
        approx(out.width, 0.0);
493
        approx(out.height, 0.0);
494

            
495
        let out = reqs(0.0, 0.0).shrink_size(LogicalSize::new(f32::NAN, f32::NAN));
496
        approx(out.width, 0.0);
497
        approx(out.height, 0.0);
498

            
499
        // inf - inf == NaN -> clamped to 0.0
500
        let out = reqs(f32::INFINITY, f32::INFINITY)
501
            .shrink_size(LogicalSize::new(f32::INFINITY, f32::INFINITY));
502
        approx(out.width, 0.0);
503
        approx(out.height, 0.0);
504

            
505
        // An infinite content box with a finite reservation stays infinite.
506
        let out = reqs(15.0, 15.0).shrink_size(LogicalSize::new(f32::INFINITY, f32::INFINITY));
507
        assert!(out.width.is_infinite() && out.width.is_sign_positive());
508
        assert!(out.height.is_infinite() && out.height.is_sign_positive());
509
    }
510

            
511
    #[test]
512
    fn shrink_size_with_negative_reservation_grows_the_box() {
513
        // Characterization: shrink_size does not clamp the *reservation*, only the
514
        // result. A negative reserved width therefore enlarges the content box.
515
        // Negative reservations are not producible by the CSS resolver today; this
516
        // pins the behaviour so a future clamp is a deliberate, visible change.
517
        let out = reqs(-10.0, -10.0).shrink_size(LogicalSize::new(100.0, 100.0));
518
        approx(out.width, 110.0);
519
        approx(out.height, 110.0);
520
    }
521

            
522
    #[test]
523
    fn shrink_size_is_identity_exactly_when_no_reflow_is_needed() {
524
        // Property: for the non-negative reservations the solver can actually
525
        // produce, !needs_reflow() <=> shrink_size() leaves the size untouched.
526
        for w in [0.0_f32, 1.0, 8.0, 15.0, 100.0] {
527
            for h in [0.0_f32, 1.0, 8.0, 15.0, 100.0] {
528
                let r = reqs(w, h);
529
                let size = LogicalSize::new(500.0, 500.0);
530
                let out = r.shrink_size(size);
531
                let unchanged = (out.width - size.width).abs() < f32::EPSILON
532
                    && (out.height - size.height).abs() < f32::EPSILON;
533
                assert_eq!(!r.needs_reflow(), unchanged, "w={w} h={h}");
534
            }
535
        }
536
    }
537

            
538
    // ---------------------------------------------------------------------
539
    // compute_scrollbar_geometry  (numeric)
540
    // ---------------------------------------------------------------------
541

            
542
    #[test]
543
    fn vertical_geometry_places_the_track_on_the_right_inner_edge() {
544
        let g = compute_scrollbar_geometry(
545
            ScrollbarOrientation::Vertical,
546
            rect(10.0, 20.0, 100.0, 200.0),
547
            LogicalSize::new(100.0, 400.0),
548
            0.0,
549
            15.0,
550
            false,
551
        );
552
        assert_eq!(g.orientation, ScrollbarOrientation::Vertical);
553
        approx(g.track_rect.origin.x, 10.0 + 100.0 - 15.0);
554
        approx(g.track_rect.origin.y, 20.0);
555
        approx(g.track_rect.size.width, 15.0);
556
        approx(g.track_rect.size.height, 200.0);
557
        approx(g.button_size, 15.0);
558
        approx(g.usable_track_length, 200.0 - 2.0 * 15.0);
559
        approx(g.thumb_size_ratio, 0.5);
560
        approx(g.thumb_length, 85.0);
561
        approx(g.max_scroll, 200.0);
562
        approx(g.scroll_ratio, 0.0);
563
        approx(g.thumb_offset, 0.0);
564
        approx(g.width_px, 15.0);
565
    }
566

            
567
    #[test]
568
    fn horizontal_geometry_places_the_track_on_the_bottom_inner_edge() {
569
        let g = compute_scrollbar_geometry(
570
            ScrollbarOrientation::Horizontal,
571
            rect(10.0, 20.0, 100.0, 200.0),
572
            LogicalSize::new(300.0, 200.0),
573
            100.0,
574
            15.0,
575
            false,
576
        );
577
        assert_eq!(g.orientation, ScrollbarOrientation::Horizontal);
578
        approx(g.track_rect.origin.x, 10.0);
579
        approx(g.track_rect.origin.y, 20.0 + 200.0 - 15.0);
580
        approx(g.track_rect.size.width, 100.0);
581
        approx(g.track_rect.size.height, 15.0);
582
        approx(g.usable_track_length, 70.0);
583
        approx(g.thumb_size_ratio, 1.0 / 3.0);
584
        // 70 * 0.333 = 23.3 -> lifted to the 2*width minimum (30)
585
        approx(g.thumb_length, 30.0);
586
        approx(g.max_scroll, 200.0);
587
        approx(g.scroll_ratio, 0.5);
588
        approx(g.thumb_offset, (70.0 - 30.0) * 0.5);
589
    }
590

            
591
    #[test]
592
    fn the_other_scrollbar_steals_exactly_one_width_from_the_track() {
593
        let without = compute_scrollbar_geometry(
594
            ScrollbarOrientation::Vertical,
595
            rect(0.0, 0.0, 100.0, 200.0),
596
            LogicalSize::new(100.0, 400.0),
597
            0.0,
598
            15.0,
599
            false,
600
        );
601
        let with = compute_scrollbar_geometry(
602
            ScrollbarOrientation::Vertical,
603
            rect(0.0, 0.0, 100.0, 200.0),
604
            LogicalSize::new(100.0, 400.0),
605
            0.0,
606
            15.0,
607
            true,
608
        );
609
        approx(
610
            without.track_rect.size.height - with.track_rect.size.height,
611
            15.0,
612
        );
613
        approx(without.usable_track_length - with.usable_track_length, 15.0);
614
        approx(with.usable_track_length, 200.0 - 15.0 - 30.0);
615
    }
616

            
617
    #[test]
618
    fn compute_scrollbar_geometry_defaults_to_button_size_equal_to_width() {
619
        for orientation in [
620
            ScrollbarOrientation::Vertical,
621
            ScrollbarOrientation::Horizontal,
622
        ] {
623
            let a = compute_scrollbar_geometry(
624
                orientation,
625
                rect(5.0, 7.0, 120.0, 240.0),
626
                LogicalSize::new(500.0, 900.0),
627
                33.0,
628
                17.0,
629
                true,
630
            );
631
            let b = compute_scrollbar_geometry_with_button_size(
632
                orientation,
633
                rect(5.0, 7.0, 120.0, 240.0),
634
                LogicalSize::new(500.0, 900.0),
635
                33.0,
636
                17.0,
637
                true,
638
                17.0,
639
            );
640
            approx(a.button_size, b.button_size);
641
            approx(a.usable_track_length, b.usable_track_length);
642
            approx(a.thumb_length, b.thumb_length);
643
            approx(a.thumb_offset, b.thumb_offset);
644
            approx(a.max_scroll, b.max_scroll);
645
        }
646
    }
647

            
648
    #[test]
649
    fn everything_zero_yields_an_all_zero_geometry() {
650
        let g = compute_scrollbar_geometry(
651
            ScrollbarOrientation::Vertical,
652
            LogicalRect::zero(),
653
            LogicalSize::new(0.0, 0.0),
654
            0.0,
655
            0.0,
656
            false,
657
        );
658
        approx(g.usable_track_length, 0.0);
659
        approx(g.thumb_length, 0.0);
660
        approx(g.thumb_offset, 0.0);
661
        approx(g.max_scroll, 0.0);
662
        approx(g.scroll_ratio, 0.0);
663
        // Zero content means "everything fits" -> the thumb covers the whole track.
664
        approx(g.thumb_size_ratio, 1.0);
665
    }
666

            
667
    #[test]
668
    fn content_smaller_than_viewport_is_not_scrollable() {
669
        let g = compute_scrollbar_geometry(
670
            ScrollbarOrientation::Vertical,
671
            rect(0.0, 0.0, 100.0, 400.0),
672
            LogicalSize::new(100.0, 50.0),
673
            9999.0, // bogus scroll offset on a non-scrollable box
674
            15.0,
675
            false,
676
        );
677
        approx(g.thumb_size_ratio, 1.0);
678
        approx(g.max_scroll, 0.0);
679
        approx(g.scroll_ratio, 0.0);
680
        approx(g.thumb_offset, 0.0);
681
        approx(g.thumb_length, g.usable_track_length);
682
    }
683

            
684
    #[test]
685
    fn overscroll_clamps_the_thumb_to_the_end_of_the_track() {
686
        let g = compute_scrollbar_geometry(
687
            ScrollbarOrientation::Vertical,
688
            rect(0.0, 0.0, 100.0, 200.0),
689
            LogicalSize::new(100.0, 400.0),
690
            1.0e9, // far past max_scroll (200)
691
            15.0,
692
            false,
693
        );
694
        approx(g.scroll_ratio, 1.0);
695
        approx(g.thumb_offset, g.usable_track_length - g.thumb_length);
696
        assert!(g.thumb_offset + g.thumb_length <= g.usable_track_length + 1e-3);
697
    }
698

            
699
    #[test]
700
    fn a_negative_overscroll_offset_pins_the_thumb_to_the_start_of_the_track() {
701
        // The only writer that can produce a negative offset is the physics timer's
702
        // `set_scroll_position_unclamped` rubber-band, i.e. the user pulling past the
703
        // top. The thumb must stay parked at the track start, NOT mirror the pull
704
        // downwards the way an `.abs()` on the ratio used to make it.
705
        //   usable = 200 - 2*15 = 170, thumb = max(170 * 200/400, 30) = 85,
706
        //   max_scroll = 200 -> travel = 85.
707
        let neg = compute_scrollbar_geometry(
708
            ScrollbarOrientation::Vertical,
709
            rect(0.0, 0.0, 100.0, 200.0),
710
            LogicalSize::new(100.0, 400.0),
711
            -50.0,
712
            15.0,
713
            false,
714
        );
715
        approx(neg.scroll_ratio, 0.0);
716
        approx(neg.thumb_offset, 0.0);
717

            
718
        // ...and the mirrored positive offset is a genuinely different position.
719
        let pos = compute_scrollbar_geometry(
720
            ScrollbarOrientation::Vertical,
721
            rect(0.0, 0.0, 100.0, 200.0),
722
            LogicalSize::new(100.0, 400.0),
723
            50.0,
724
            15.0,
725
            false,
726
        );
727
        approx(pos.scroll_ratio, 0.25);
728
        approx(pos.thumb_offset, 21.25);
729
        assert!(pos.thumb_offset > neg.thumb_offset);
730
    }
731

            
732
    #[test]
733
    fn very_long_content_lifts_the_thumb_to_the_minimum_length() {
734
        let g = compute_scrollbar_geometry(
735
            ScrollbarOrientation::Vertical,
736
            rect(0.0, 0.0, 100.0, 200.0),
737
            LogicalSize::new(100.0, 1.0e6),
738
            0.0,
739
            15.0,
740
            false,
741
        );
742
        // proportional thumb would be ~0.03px; the 2*width floor kicks in
743
        approx(g.thumb_length, 30.0);
744
        assert!(g.thumb_length <= g.usable_track_length);
745
    }
746

            
747
    #[test]
748
    fn the_minimum_thumb_length_is_capped_by_the_usable_track() {
749
        // Track (40px) is barely bigger than the two buttons (2*15) -> 10px usable.
750
        // The 30px minimum thumb must be clamped down, never overflow the track.
751
        let g = compute_scrollbar_geometry(
752
            ScrollbarOrientation::Vertical,
753
            rect(0.0, 0.0, 100.0, 40.0),
754
            LogicalSize::new(100.0, 1000.0),
755
            500.0,
756
            15.0,
757
            false,
758
        );
759
        approx(g.usable_track_length, 10.0);
760
        approx(g.thumb_length, 10.0);
761
        approx(g.thumb_offset, 0.0);
762
        assert!(g.thumb_offset + g.thumb_length <= g.usable_track_length + 1e-3);
763
    }
764

            
765
    #[test]
766
    fn a_container_thinner_than_its_scrollbar_still_produces_safe_lengths() {
767
        // 10px tall container, 15px scrollbar, corner reserved => track_total = -5.
768
        // Characterization: the *track rect* is allowed to go negative here (it is
769
        // clipped by the painter), but every derived length stays non-negative.
770
        let g = compute_scrollbar_geometry(
771
            ScrollbarOrientation::Vertical,
772
            rect(0.0, 0.0, 100.0, 10.0),
773
            LogicalSize::new(100.0, 400.0),
774
            10.0,
775
            15.0,
776
            true,
777
        );
778
        assert!(g.track_rect.size.height < 0.0, "track rect goes negative");
779
        approx(g.usable_track_length, 0.0);
780
        approx(g.thumb_length, 0.0);
781
        approx(g.thumb_offset, 0.0);
782
    }
783

            
784
    #[test]
785
    fn zero_button_size_gives_the_whole_track_to_the_thumb() {
786
        let g = compute_scrollbar_geometry_with_button_size(
787
            ScrollbarOrientation::Vertical,
788
            rect(0.0, 0.0, 100.0, 200.0),
789
            LogicalSize::new(100.0, 400.0),
790
            0.0,
791
            8.0,
792
            false,
793
            0.0, // overlay scrollbar: no arrow buttons
794
        );
795
        approx(g.button_size, 0.0);
796
        approx(g.usable_track_length, 200.0);
797
        approx(g.thumb_length, 100.0);
798
        approx(g.width_px, 8.0);
799
    }
800

            
801
    #[test]
802
    fn zero_width_scrollbar_does_not_panic() {
803
        let g = compute_scrollbar_geometry(
804
            ScrollbarOrientation::Horizontal,
805
            rect(0.0, 0.0, 100.0, 200.0),
806
            LogicalSize::new(400.0, 200.0),
807
            100.0,
808
            0.0,
809
            false,
810
        );
811
        approx(g.usable_track_length, 100.0);
812
        approx(g.width_px, 0.0);
813
        approx(g.thumb_size_ratio, 0.25);
814
        approx(g.thumb_length, 25.0);
815
        assert!(g.thumb_offset >= 0.0);
816
    }
817

            
818
    #[test]
819
    fn negative_content_size_is_treated_as_unscrollable() {
820
        let g = compute_scrollbar_geometry(
821
            ScrollbarOrientation::Vertical,
822
            rect(0.0, 0.0, 100.0, 200.0),
823
            LogicalSize::new(100.0, -400.0),
824
            50.0,
825
            15.0,
826
            false,
827
        );
828
        // `content_length > 0.0` is false -> ratio 1.0, no scrolling.
829
        approx(g.thumb_size_ratio, 1.0);
830
        approx(g.max_scroll, 0.0);
831
        approx(g.scroll_ratio, 0.0);
832
        approx(g.thumb_offset, 0.0);
833
        approx(g.thumb_length, g.usable_track_length);
834
    }
835

            
836
    #[test]
837
    fn a_negative_viewport_still_produces_non_negative_lengths() {
838
        // Degenerate inner rect (negative height). The size *ratio* goes negative,
839
        // but the lengths that reach the painter must not.
840
        let g = compute_scrollbar_geometry(
841
            ScrollbarOrientation::Vertical,
842
            rect(0.0, 0.0, 100.0, -100.0),
843
            LogicalSize::new(100.0, 200.0),
844
            50.0,
845
            15.0,
846
            false,
847
        );
848
        approx(g.thumb_size_ratio, -0.5);
849
        approx(g.usable_track_length, 0.0);
850
        approx(g.thumb_length, 0.0);
851
        approx(g.thumb_offset, 0.0);
852
        assert!(g.max_scroll >= 0.0);
853
    }
854

            
855
    #[test]
856
    fn float_max_inputs_stay_finite() {
857
        let g = compute_scrollbar_geometry(
858
            ScrollbarOrientation::Vertical,
859
            rect(0.0, 0.0, f32::MAX, f32::MAX),
860
            LogicalSize::new(f32::MAX, f32::MAX),
861
            f32::MAX,
862
            15.0,
863
            true,
864
        );
865
        assert!(g.usable_track_length.is_finite());
866
        assert!(g.thumb_length.is_finite());
867
        assert!(g.thumb_offset.is_finite());
868
        assert!(g.max_scroll.is_finite());
869
        assert!(g.scroll_ratio.is_finite());
870
        assert!(g.thumb_size_ratio.is_finite());
871
        approx(g.thumb_size_ratio, 1.0);
872
        approx(g.max_scroll, 0.0);
873
        approx(g.thumb_offset, 0.0);
874
    }
875

            
876
    #[test]
877
    fn infinite_content_length_does_not_panic() {
878
        let g = compute_scrollbar_geometry(
879
            ScrollbarOrientation::Vertical,
880
            rect(0.0, 0.0, 100.0, 200.0),
881
            LogicalSize::new(100.0, f32::INFINITY),
882
            100.0,
883
            15.0,
884
            false,
885
        );
886
        approx(g.thumb_size_ratio, 0.0);
887
        approx(g.thumb_length, 30.0); // floored at 2 * width
888
        assert!(g.max_scroll.is_infinite());
889
        // finite_offset / inf == 0 -> the thumb parks at the start
890
        approx(g.scroll_ratio, 0.0);
891
        approx(g.thumb_offset, 0.0);
892
    }
893

            
894
    #[test]
895
    fn nan_scrollbar_width_does_not_panic_and_collapses_the_track() {
896
        let g = compute_scrollbar_geometry(
897
            ScrollbarOrientation::Vertical,
898
            rect(0.0, 0.0, 100.0, 200.0),
899
            LogicalSize::new(100.0, 400.0),
900
            0.0,
901
            f32::NAN,
902
            false,
903
        );
904
        // NaN width => NaN button size => the usable track clamps to 0 and the
905
        // thumb collapses. Nothing paints, but nothing panics either.
906
        approx(g.usable_track_length, 0.0);
907
        approx(g.thumb_length, 0.0);
908
        approx(g.thumb_offset, 0.0);
909
        assert!(g.width_px.is_nan());
910
        assert!(g.track_rect.origin.x.is_nan());
911
    }
912

            
913
    #[test]
914
    fn nan_content_size_does_not_panic() {
915
        let g = compute_scrollbar_geometry(
916
            ScrollbarOrientation::Vertical,
917
            rect(0.0, 0.0, 100.0, 200.0),
918
            LogicalSize::new(100.0, f32::NAN),
919
            50.0,
920
            15.0,
921
            false,
922
        );
923
        // `NaN > 0.0` is false -> ratio 1.0; `NaN - v` is NaN -> max_scroll 0.
924
        approx(g.thumb_size_ratio, 1.0);
925
        approx(g.max_scroll, 0.0);
926
        approx(g.scroll_ratio, 0.0);
927
        approx(g.thumb_offset, 0.0);
928
        approx(g.thumb_length, g.usable_track_length);
929
        assert!(g.thumb_length.is_finite());
930
    }
931

            
932
    #[test]
933
    fn infinite_scroll_offsets_saturate_the_scroll_ratio() {
934
        // +INF saturates at the END of the track; -INF is an overscroll past
935
        // the start and PINS AT 0 (the `.abs()` that used to mirror it down
936
        // the track was removed with the origin-convention fix — see
937
        // `a_negative_overscroll_offset_pins_the_thumb_to_the_start_of_the_track`).
938
        for (offset, want_ratio) in [(f32::INFINITY, 1.0), (f32::NEG_INFINITY, 0.0)] {
939
            let g = compute_scrollbar_geometry(
940
                ScrollbarOrientation::Vertical,
941
                rect(0.0, 0.0, 100.0, 200.0),
942
                LogicalSize::new(100.0, 400.0),
943
                offset,
944
                15.0,
945
                false,
946
            );
947
            approx(g.scroll_ratio, want_ratio);
948
            approx(
949
                g.thumb_offset,
950
                (g.usable_track_length - g.thumb_length) * want_ratio,
951
            );
952
        }
953
    }
954

            
955
    #[test]
956
    fn nan_scroll_offset_leaks_nan_into_scroll_ratio_and_thumb_offset() {
957
        // FINDING (characterization, not a panic): `f32::clamp` returns NaN for a
958
        // NaN input, so a NaN scroll offset survives into `scroll_ratio` and then
959
        // `thumb_offset`. Every other field stays well-defined. A NaN offset would
960
        // paint the thumb at an undefined position rather than clamping to 0.
961
        let g = compute_scrollbar_geometry(
962
            ScrollbarOrientation::Vertical,
963
            rect(0.0, 0.0, 100.0, 200.0),
964
            LogicalSize::new(100.0, 400.0),
965
            f32::NAN,
966
            15.0,
967
            false,
968
        );
969
        assert!(g.scroll_ratio.is_nan());
970
        assert!(g.thumb_offset.is_nan());
971
        approx(g.usable_track_length, 170.0);
972
        approx(g.thumb_length, 85.0);
973
        approx(g.max_scroll, 200.0);
974
    }
975

            
976
    #[test]
977
    fn infinite_viewport_leaks_nan_into_thumb_offset() {
978
        // FINDING (characterization, not a panic): an infinite inner rect makes both
979
        // `usable_track_length` and `thumb_length` infinite, so `usable - thumb` is
980
        // NaN and the offset follows. No panic; the value is simply undefined.
981
        let g = compute_scrollbar_geometry(
982
            ScrollbarOrientation::Vertical,
983
            rect(0.0, 0.0, 100.0, f32::INFINITY),
984
            LogicalSize::new(100.0, 1000.0),
985
            0.0,
986
            15.0,
987
            false,
988
        );
989
        assert!(g.usable_track_length.is_infinite());
990
        assert!(g.thumb_length.is_infinite());
991
        assert!(g.thumb_offset.is_nan());
992
        approx(g.max_scroll, 0.0);
993
        approx(g.scroll_ratio, 0.0);
994
    }
995

            
996
    // ---------------------------------------------------------------------
997
    // compute_thumb_geometry  (private, numeric)
998
    // ---------------------------------------------------------------------
999

            
    #[test]
    fn thumb_geometry_math_is_exact_for_a_known_case() {
        let track = rect(1.0, 2.0, 3.0, 4.0);
        let g = compute_thumb_geometry(
            ScrollbarOrientation::Horizontal,
            track,
            200.0, // track_total
            100.0, // viewport_length
            200.0, // content_length
            10.0,  // button_size
            10.0,  // scrollbar_width_px
            50.0,  // scroll_offset
        );
        approx(g.usable_track_length, 180.0); // 200 - 2*10
        approx(g.thumb_size_ratio, 0.5); // 100 / 200
        approx(g.thumb_length, 90.0); // 180 * 0.5
        approx(g.max_scroll, 100.0); // 200 - 100
        approx(g.scroll_ratio, 0.5); // 50 / 100
        approx(g.thumb_offset, 45.0); // (180 - 90) * 0.5
                                      // the track rect is passed straight through, never recomputed
        approx(g.track_rect.origin.x, track.origin.x);
        approx(g.track_rect.size.height, track.size.height);
        assert_eq!(g.orientation, ScrollbarOrientation::Horizontal);
    }
    #[test]
    fn thumb_geometry_buttons_larger_than_the_track_collapse_the_usable_length() {
        let g = compute_thumb_geometry(
            ScrollbarOrientation::Vertical,
            LogicalRect::zero(),
            20.0,   // track_total
            100.0,  // viewport_length
            1000.0, // content_length
            500.0,  // button_size >> track
            15.0,
            250.0,
        );
        approx(g.usable_track_length, 0.0);
        approx(g.thumb_length, 0.0);
        approx(g.thumb_offset, 0.0);
        assert!(g.scroll_ratio >= 0.0 && g.scroll_ratio <= 1.0);
    }
    #[test]
    fn thumb_geometry_ignores_scroll_offset_when_content_fits() {
        let g = compute_thumb_geometry(
            ScrollbarOrientation::Vertical,
            LogicalRect::zero(),
            200.0,
            200.0, // viewport == content
            200.0,
            10.0,
            10.0,
            1.0e9, // absurd offset
        );
        approx(g.max_scroll, 0.0);
        approx(g.scroll_ratio, 0.0);
        approx(g.thumb_offset, 0.0);
        approx(g.thumb_size_ratio, 1.0);
        approx(g.thumb_length, g.usable_track_length);
    }
    #[test]
    fn thumb_geometry_clamps_an_oversized_viewport_ratio_to_one() {
        let g = compute_thumb_geometry(
            ScrollbarOrientation::Vertical,
            LogicalRect::zero(),
            200.0,
            400.0, // viewport bigger than content
            100.0,
            10.0,
            10.0,
            0.0,
        );
        approx(g.thumb_size_ratio, 1.0);
        approx(g.thumb_length, g.usable_track_length);
        approx(g.max_scroll, 0.0);
    }
    #[test]
    fn thumb_geometry_survives_an_all_nan_call() {
        let g = compute_thumb_geometry(
            ScrollbarOrientation::Vertical,
            LogicalRect::zero(),
            f32::NAN,
            f32::NAN,
            f32::NAN,
            f32::NAN,
            f32::NAN,
            f32::NAN,
        );
        // max()/min() drop NaN in favour of the other operand, and the
        // `content_length > 0.0` / `max_scroll > 0.0` guards both read false.
        approx(g.usable_track_length, 0.0);
        approx(g.thumb_length, 0.0);
        approx(g.thumb_size_ratio, 1.0);
        approx(g.max_scroll, 0.0);
        approx(g.scroll_ratio, 0.0);
        approx(g.thumb_offset, 0.0);
    }
    // ---------------------------------------------------------------------
    // round-trip + invariants
    // ---------------------------------------------------------------------
    #[test]
    fn thumb_offset_round_trips_back_to_the_scroll_offset() {
        // This is the inverse used by `handle_scrollbar_drag`: dragging the thumb to
        // `thumb_offset` must map back to the scroll offset that produced it.
        for &offset in &[0.0_f32, 25.0, 50.0, 100.0, 150.0, 200.0] {
            let g = compute_scrollbar_geometry(
                ScrollbarOrientation::Vertical,
                rect(0.0, 0.0, 100.0, 200.0),
                LogicalSize::new(100.0, 400.0),
                offset,
                15.0,
                false,
            );
            let drag_range = g.usable_track_length - g.thumb_length;
            assert!(drag_range > 0.0);
            let recovered = (g.thumb_offset / drag_range) * g.max_scroll;
            approx(recovered, offset);
        }
    }
    #[test]
    fn thumb_offset_is_monotonic_in_the_scroll_offset() {
        let mut previous = f32::NEG_INFINITY;
        for step in 0..=20 {
            let offset = step as f32 * 15.0; // 0 .. 300, past max_scroll (200)
            let g = compute_scrollbar_geometry(
                ScrollbarOrientation::Horizontal,
                rect(0.0, 0.0, 200.0, 100.0),
                LogicalSize::new(400.0, 100.0),
                offset,
                15.0,
                false,
            );
            assert!(
                g.thumb_offset >= previous - 1e-4,
                "thumb went backwards at offset {offset}: {} < {previous}",
                g.thumb_offset
            );
            previous = g.thumb_offset;
        }
    }
    #[test]
    fn geometry_invariants_hold_across_a_finite_input_grid() {
        let orientations = [
            ScrollbarOrientation::Vertical,
            ScrollbarOrientation::Horizontal,
        ];
        let rects = [
            rect(0.0, 0.0, 0.0, 0.0),
            rect(0.0, 0.0, 1.0, 1.0),
            rect(-50.0, -50.0, 100.0, 200.0),
            rect(10.0, 20.0, 800.0, 600.0),
            rect(0.0, 0.0, f32::MAX, f32::MAX),
        ];
        let contents = [
            LogicalSize::new(0.0, 0.0),
            LogicalSize::new(1.0, 1.0),
            LogicalSize::new(100.0, 200.0),
            LogicalSize::new(1.0e9, 1.0e9),
            LogicalSize::new(f32::MAX, f32::MAX),
        ];
        let offsets = [0.0_f32, -1.0, 1.0, 12_345.0, -12_345.0, f32::MAX];
        let widths = [0.0_f32, 1.0, 8.0, 15.0, 1000.0];
        let buttons = [0.0_f32, 1.0, 15.0, 1000.0];
        for &orientation in &orientations {
            for &inner in &rects {
                for &content in &contents {
                    for &offset in &offsets {
                        for &width in &widths {
                            for &button in &buttons {
                                for &other in &[false, true] {
                                    let g = compute_scrollbar_geometry_with_button_size(
                                        orientation,
                                        inner,
                                        content,
                                        offset,
                                        width,
                                        other,
                                        button,
                                    );
                                    let ctx = format!(
                                        "inner={inner:?} content={content:?} offset={offset} \
                                         width={width} button={button} other={other}"
                                    );
                                    assert!(g.usable_track_length.is_finite(), "{ctx}");
                                    assert!(g.thumb_length.is_finite(), "{ctx}");
                                    assert!(g.thumb_offset.is_finite(), "{ctx}");
                                    assert!(g.max_scroll.is_finite(), "{ctx}");
                                    assert!(g.usable_track_length >= 0.0, "{ctx}");
                                    assert!(g.max_scroll >= 0.0, "{ctx}");
                                    assert!(g.thumb_offset >= 0.0, "{ctx}");
                                    assert!(
                                        g.thumb_length >= 0.0
                                            && g.thumb_length <= g.usable_track_length,
                                        "thumb escapes the track: {ctx}"
                                    );
                                    assert!(
                                        (0.0..=1.0).contains(&g.thumb_size_ratio),
                                        "size ratio out of range: {ctx}"
                                    );
                                    assert!(
                                        (0.0..=1.0).contains(&g.scroll_ratio),
                                        "scroll ratio out of range: {ctx}"
                                    );
                                    let slack = g.usable_track_length
                                        + g.usable_track_length.abs() * 1e-5
                                        + 1e-3;
                                    assert!(
                                        g.thumb_offset + g.thumb_length <= slack,
                                        "thumb overruns the track end: {ctx}"
                                    );
                                }
                            }
                        }
                    }
                }
            }
        }
    }
    #[test]
    fn default_geometry_is_inert() {
        let g = ScrollbarGeometry::default();
        assert_eq!(g.orientation, ScrollbarOrientation::Vertical);
        approx(g.button_size, 0.0);
        approx(g.usable_track_length, 0.0);
        approx(g.thumb_length, 0.0);
        approx(g.thumb_size_ratio, 0.0);
        approx(g.scroll_ratio, 0.0);
        approx(g.thumb_offset, 0.0);
        approx(g.max_scroll, 0.0);
        approx(g.width_px, 0.0);
        assert_eq!(g.track_rect, LogicalRect::zero());
    }
}