1
//! `VirtualView` lifecycle management for layout
2
//!
3
//! This module provides:
4
//! - `VirtualView` re-invocation logic for lazy loading
5
//! - Nested DOM ID management
6

            
7
use alloc::collections::BTreeMap;
8

            
9
use azul_core::{
10
    callbacks::{EdgeType, VirtualViewCallbackReason},
11
    dom::{DomId, NodeId},
12
    geom::{LogicalPosition, LogicalRect, LogicalSize},
13
};
14

            
15
use crate::managers::scroll_state::ScrollManager;
16

            
17
/// Distance in pixels from edge that triggers edge-scrolled callback
18
const EDGE_THRESHOLD: f32 = 200.0;
19

            
20
/// Manages `VirtualView` lifecycle, including re-invocation
21
///
22
/// Tracks which `VirtualViews` have been invoked, assigns unique DOM IDs to nested
23
/// virtual views, and determines when `VirtualViews` need to be re-invoked (e.g., when
24
/// the container bounds expand or the user scrolls near an edge).
25
#[derive(Debug, Clone, Default)]
26
pub struct VirtualViewManager {
27
    /// Per-`VirtualView` state keyed by (parent `DomId`, `NodeId` of virtualized view element)
28
    states: BTreeMap<(DomId, NodeId), VirtualViewState>,
29
    /// Counter for generating unique nested DOM IDs
30
    next_dom_id: usize,
31
    /// MWA-C-virtual_view: queue-time callback reasons, consumed by the very
32
    /// next `check_reinvoke` for the same view (set by
33
    /// `process_virtual_view_updates` right before the invoke). Replaces the
34
    /// `force_reinvoke` clear-flag trick that collapsed every delivered
35
    /// reason to `InitialRender`.
36
    reason_overrides: Vec<((DomId, NodeId), VirtualViewCallbackReason)>,
37
    /// Views whose CONTENT-SIZED box is out of date: the callback just
38
    /// reported a natural size (a non-scrolling view, `materialized ==
39
    /// virtual_rect`) that differs from the box it was laid out in, on an
40
    /// axis the CSS leaves to the content (`auto` / `min-content` /
41
    /// `max-content`).
42
    ///
43
    /// A `VirtualView` is sized by what it returned - but the solver reads
44
    /// that report from the PREVIOUS pass (`materialized_sizes`, snapshotted
45
    /// before layout, callbacks run after it), so a view sized by its content
46
    /// is laid out at the replaced-element default (300x150) on its first
47
    /// frame and at its OLD size after an in-place re-render that changed the
48
    /// content. Nothing re-ran layout: an icon view painted 300x150 until the
49
    /// next unrelated relayout, and a status-bar label re-rendered with a
50
    /// longer text was clipped by the box the shorter text had earned. The
51
    /// window drains this set right after the pass that filled it and lays
52
    /// the host out once more with the reported size
53
    /// (`crate::window::LayoutWindow::relayout_dom_for_virtual_view_sizes`).
54
    natural_size_stale: alloc::collections::BTreeSet<(DomId, NodeId)>,
55
}
56

            
57
/// Internal state for a single `VirtualView` instance
58
///
59
/// Tracks invocation status, content dimensions, and edge triggers
60
/// to determine when the `VirtualView` callback needs to be re-invoked.
61
#[derive(Debug, Clone)]
62
struct VirtualViewState {
63
    /// WHAT IS MATERIALIZED RIGHT NOW, in VIRTUAL space.
64
    ///
65
    /// `origin` = where this window of content begins in the document (the
66
    /// `scroll_offset` the callback reported); `size` = how much it covers.
67
    /// This is the rect the content is PLACED by:
68
    /// `content is drawn at container.origin + (materialized.origin - scroll_offset)`.
69
    ///
70
    /// Deliberately a rect, not a loose position + size: the origin and the
71
    /// extent are one fact about one window and drift apart the moment they
72
    /// are stored separately (which is how the offset ended up write-only).
73
    materialized: Option<LogicalRect>,
74
    /// THE WHOLE DOCUMENT, in VIRTUAL space — the app's current best estimate
75
    /// (`virtual_scroll_size`), which background pagination refines over time.
76
    ///
77
    /// ONLY the scrollbar reads this. Placement above does not, which is the
78
    /// property that lets the estimate change without the content jumping:
79
    /// the user sees the thumb resize and nothing else move.
80
    virtual_rect: Option<LogicalRect>,
81
    /// Whether the `VirtualView` has ever been invoked
82
    virtual_view_was_invoked: bool,
83
    /// Whether the callback has been invoked since the container last grew.
84
    /// Set by EVERY invocation (each one is shown the current bounds),
85
    /// cleared when [`VirtualViewManager::check_reinvoke`] sees a larger
86
    /// container than the one recorded — that, and only that, is a
87
    /// `BoundsExpanded`.
88
    invoked_for_current_expansion: bool,
89
    /// The scroll-driven demand (`EdgeScrolled` / `ScrollBeyondContent`) the
90
    /// CURRENT materialized window has already been asked to answer.
91
    ///
92
    /// This is the documented "fires once per edge approach; the flag clears
93
    /// once the scroll moves away" latch, and it is keyed on the ANSWER, not
94
    /// on the edge: it is set when the callback is invoked for a scroll
95
    /// demand, dropped by [`VirtualViewState::check_reinvoke_condition`] as
96
    /// soon as the geometry no longer demands anything, and dropped by
97
    /// [`VirtualViewManager::update_virtual_view_info`] when the callback
98
    /// materializes a different window (a new window is a new question). So a
99
    /// callback that answers an edge by materializing the same window again
100
    /// is asked exactly once per approach, and one that materializes too
101
    /// little is asked again until the edge is out of reach — while the old
102
    /// per-edge memory (`last_edge_triggered`) was never cleared by scrolling
103
    /// at all, so every edge fired at most ONCE per full relayout and a
104
    /// document scrolled past its second page showed bare background.
105
    served_scroll_demand: Option<VirtualViewCallbackReason>,
106
    /// Unique DOM ID assigned to this `VirtualView`'s content
107
    nested_dom_id: DomId,
108
    /// The `VirtualView`'s own on-screen box (the viewport), window coords.
109
    /// `size` is the scrollport the other two rects are compared against.
110
    container: LogicalRect,
111
    /// Scroll offset captured at `InitialRender`. Edge-scroll callbacks only fire
112
    /// once the user has scrolled away from this resting position — being at an
113
    /// edge from the very start (e.g. the top/left edge at offset 0) is the
114
    /// initial position, not a scroll-to-edge event.
115
    initial_scroll_offset: LogicalPosition,
116
}
117

            
118
/// Which edges of the materialized window the visible window is near
119
/// (within `EDGE_THRESHOLD`) AND has document left to load past.
120
#[derive(Debug, Clone, Copy, PartialEq, Default)]
121
#[allow(clippy::struct_excessive_bools)] // one independent bool per box edge
122
                                         // (top/bottom/left/right)
123
struct EdgeFlags {
124
    /// Near top edge
125
    top: bool,
126
    /// Near bottom edge
127
    bottom: bool,
128
    /// Near left edge
129
    left: bool,
130
    /// Near right edge
131
    right: bool,
132
}
133

            
134
impl VirtualViewManager {
135
    /// Creates a new `VirtualViewManager` with no tracked `VirtualViews`
136
    #[must_use]
137
7705
    pub fn new() -> Self {
138
7705
        Self {
139
7705
            next_dom_id: 1, // 0 is root
140
7705
            ..Default::default()
141
7705
        }
142
7705
    }
143

            
144
    /// Number of tracked `VirtualView` states. Used by `AZ_E2E_TEST` to watch growth.
145
    #[must_use]
146
35
    pub fn debug_counts(&self) -> usize {
147
35
        self.states.len()
148
35
    }
149

            
150
    /// MWA-C-virtual_view: stage the reason the next invoke of this view
151
    /// should deliver to the user callback (consumed by `check_reinvoke`).
152
1286
    pub fn set_reason_override(
153
1286
        &mut self,
154
1286
        dom_id: DomId,
155
1286
        node_id: NodeId,
156
1286
        reason: VirtualViewCallbackReason,
157
1286
    ) {
158
1286
        self.reason_overrides
159
1286
            .retain(|((d, n), _)| !(*d == dom_id && *n == node_id));
160
1286
        self.reason_overrides.push(((dom_id, node_id), reason));
161
1286
    }
162

            
163
    /// Gets or creates a unique nested DOM ID for a `VirtualView`
164
    ///
165
    /// Returns the existing DOM ID if the `VirtualView` was previously registered,
166
    /// otherwise allocates a new unique ID and initializes the `VirtualView` state.
167
1222
    pub fn get_or_create_nested_dom_id(&mut self, dom_id: DomId, node_id: NodeId) -> DomId {
168
1222
        let key = (dom_id, node_id);
169

            
170
        // Check if already exists
171
1222
        if let Some(state) = self.states.get(&key) {
172
883
            return state.nested_dom_id;
173
339
        }
174

            
175
        // Create new nested DOM ID
176
339
        let nested_dom_id = DomId {
177
339
            inner: self.next_dom_id,
178
339
        };
179
339
        self.next_dom_id += 1;
180

            
181
339
        self.states
182
339
            .insert(key, VirtualViewState::new(nested_dom_id));
183
339
        nested_dom_id
184
1222
    }
185

            
186
    /// Gets the nested DOM ID for a `VirtualView` if it exists
187
    #[must_use]
188
145448
    pub fn get_nested_dom_id(&self, dom_id: DomId, node_id: NodeId) -> Option<DomId> {
189
145448
        self.states.get(&(dom_id, node_id)).map(|s| s.nested_dom_id)
190
145448
    }
191

            
192
    /// Returns whether the `VirtualView` has ever been invoked
193
    #[must_use]
194
44
    pub fn was_virtual_view_invoked(&self, dom_id: DomId, node_id: NodeId) -> bool {
195
44
        self.states
196
44
            .get(&(dom_id, node_id))
197
44
            .is_some_and(|s| s.virtual_view_was_invoked)
198
44
    }
199

            
200
    /// Updates the `VirtualView`'s content size information
201
    ///
202
    /// Called after the `VirtualView` callback returns to record the actual
203
    /// content dimensions. The sizes are the callback's ANSWER, so they never
204
    /// re-arm `BoundsExpanded` — only a container that grows does that.
205
    /// The sizes the view's LAST invoke declared (`scroll_size`,
206
    /// `virtual_scroll_size`) — the reinvoke signal feeds these back so the
207
    /// callback's page math sees its own declared virtual extent (#16).
208
    #[must_use]
209
902
    pub fn get_declared_sizes(
210
902
        &self,
211
902
        dom_id: DomId,
212
902
        node_id: NodeId,
213
902
    ) -> (Option<LogicalSize>, Option<LogicalSize>) {
214
902
        self.states
215
902
            .get(&(dom_id, node_id))
216
902
            .map_or((None, None), |s| {
217
                (
218
902
                    s.materialized.map(|m| m.size),
219
902
                    s.virtual_rect.map(|v| v.size),
220
                )
221
902
            })
222
902
    }
223

            
224
    /// Every view's last MATERIALIZED size, for the layout solver.
225
    ///
226
    /// A `VirtualView` is a replaced element, and `width: auto` on one means
227
    /// what it means on an `<img>`: as big as the content. The content's size
228
    /// is exactly what the callback reported, and this is how that report
229
    /// reaches sizing - it used to reach placement and scrollbar geometry
230
    /// only, so a view could never be sized by what it returned.
231
    #[must_use]
232
8664
    pub fn materialized_sizes(&self) -> BTreeMap<(DomId, NodeId), LogicalSize> {
233
8664
        self.states
234
8664
            .iter()
235
8664
            .filter_map(|(key, state)| state.materialized.map(|m| (*key, m.size)))
236
8664
            .collect()
237
8664
    }
238

            
239
    /// Record what the callback just materialized, as RECTS in virtual space.
240
    ///
241
    /// `window_origin` is where this window of content begins in the document
242
    /// (the callback's `scroll_offset`) — the piece that used to be dropped,
243
    /// which is why content could never be placed and the view never scrolled.
244
1003
    pub fn update_virtual_view_info(
245
1003
        &mut self,
246
1003
        dom_id: DomId,
247
1003
        node_id: NodeId,
248
1003
        window_origin: LogicalPosition,
249
1003
        scroll_size: LogicalSize,
250
1003
        virtual_scroll_size: LogicalSize,
251
1003
    ) -> Option<()> {
252
1003
        let state = self.states.get_mut(&(dom_id, node_id))?;
253

            
254
1001
        let materialized = LogicalRect::new(window_origin, scroll_size);
255
        // A different window is a different question: whatever scroll demand
256
        // the previous window had been asked about is void, and the next
257
        // check re-evaluates the geometry against this one.
258
1001
        if state.materialized != Some(materialized) {
259
805
            state.served_scroll_demand = None;
260
805
        }
261
1001
        state.materialized = Some(materialized);
262
        // The document estimate lives at the virtual origin; only its SIZE is
263
        // the app's (refinable) claim. Changing it must move the scrollbar and
264
        // nothing else — placement reads `materialized`, never this.
265
1001
        state.virtual_rect = Some(LogicalRect::new(
266
1001
            LogicalPosition::zero(),
267
1001
            virtual_scroll_size,
268
1001
        ));
269

            
270
1001
        Some(())
271
1003
    }
272

            
273
    /// Where the materialized window sits in virtual space, if anything is
274
    /// materialized. The renderer places content at
275
    /// `container.origin + (window_origin - scroll_offset)`.
276
    #[must_use]
277
28536
    pub fn materialized_window_origin(
278
28536
        &self,
279
28536
        dom_id: DomId,
280
28536
        node_id: NodeId,
281
28536
    ) -> Option<LogicalPosition> {
282
28536
        self.states
283
28536
            .get(&(dom_id, node_id))
284
28536
            .and_then(|s| s.materialized)
285
28536
            .map(|m| m.origin)
286
28536
    }
287

            
288
    /// Marks a `VirtualView` as invoked for a specific reason
289
    ///
290
    /// Updates internal state flags based on the callback reason to prevent
291
    /// duplicate callbacks for the same trigger condition.
292
993
    pub fn mark_invoked(
293
993
        &mut self,
294
993
        dom_id: DomId,
295
993
        node_id: NodeId,
296
993
        reason: VirtualViewCallbackReason,
297
993
    ) -> Option<()> {
298
993
        let state = self.states.get_mut(&(dom_id, node_id))?;
299

            
300
992
        state.virtual_view_was_invoked = true;
301
        // Every invocation presents `info.bounds`, so every invocation answers
302
        // the current container — `BoundsExpanded` is "the window GREW since
303
        // the callback last saw it" (the documented once-per-expansion), not
304
        // "the container is bigger than what the callback chose to
305
        // materialize". The latter re-fired on every check for a view whose
306
        // window is narrower than its viewport.
307
992
        state.invoked_for_current_expansion = true;
308
        // Latched against the window that is materialized RIGHT NOW (the one
309
        // the demand was computed from); `update_virtual_view_info` runs after
310
        // the callback and drops the latch again if the callback answered
311
        // with a different window.
312
760
        if matches!(
313
992
            reason,
314
            VirtualViewCallbackReason::EdgeScrolled(_)
315
                | VirtualViewCallbackReason::ScrollBeyondContent
316
232
        ) {
317
232
            state.served_scroll_demand = Some(reason);
318
762
        }
319

            
320
992
        Some(())
321
993
    }
322

            
323
    /// Reset invocation flags for ALL tracked `VirtualViews`
324
    ///
325
    /// After `layout_results.clear()`, the child DOMs no longer exist in memory.
326
    /// This method ensures `check_reinvoke()` returns `InitialRender` for every
327
    /// `VirtualView`, so the callbacks re-run and re-populate `layout_results`.
328
    ///
329
    /// Called from `layout_and_generate_display_list()` after clearing layout results.
330
7421
    pub fn reset_all_invocation_flags(&mut self) {
331
7421
        for state in self.states.values_mut() {
332
143
            state.virtual_view_was_invoked = false;
333
143
            state.invoked_for_current_expansion = false;
334
143
            state.served_scroll_demand = None;
335
143
        }
336
7421
    }
337

            
338
    /// Force a `VirtualView` to be re-invoked on the next layout pass
339
    ///
340
    /// Clears all invocation flags, causing `check_reinvoke()` to return `InitialRender`.
341
    /// Used by `trigger_virtual_view_rerender()` to manually refresh `VirtualView` content.
342
5
    pub fn force_reinvoke(&mut self, dom_id: DomId, node_id: NodeId) -> Option<()> {
343
5
        let state = self.states.get_mut(&(dom_id, node_id))?;
344

            
345
4
        state.virtual_view_was_invoked = false;
346
4
        state.invoked_for_current_expansion = false;
347
4
        state.served_scroll_demand = None;
348

            
349
4
        Some(())
350
5
    }
351

            
352
    /// `(DomId, NodeId)` of every `VirtualView` registered so far (invoked at
353
    /// least once). Used to re-invoke *all* views after a shared-dataset change
354
    /// arrives out-of-band (e.g. a background tile-fetch writeback) without
355
    /// needing to know which node the data belongs to.
356
    /// Which `VirtualView` HOSTS a nested dom: the inverse of
357
    /// [`Self::get_nested_dom_id`].
358
    ///
359
    /// A nested dom's display list is 0-relative — the rasteriser composites
360
    /// it at `host_bounds.origin + content_offset` — so every geometry
361
    /// accessor that must answer in WINDOW space has to walk back up through
362
    /// its hosts. Without this there was no way to ask "where does this dom
363
    /// actually sit", and a caret rect inside a `VirtualView` was handed to the
364
    /// platform IME as if the host were at the window origin.
365
    #[must_use]
366
    pub fn host_of_nested_dom(&self, nested: DomId) -> Option<(DomId, NodeId)> {
367
        self.states
368
            .iter()
369
            .find(|(_, state)| state.nested_dom_id == nested)
370
            .map(|((dom_id, node_id), _)| (*dom_id, *node_id))
371
    }
372

            
373
    #[must_use]
374
124
    pub fn all_view_keys(&self) -> Vec<(DomId, NodeId)> {
375
124
        self.states.keys().copied().collect()
376
124
    }
377

            
378
    /// Record that `(dom_id, node_id)`'s content-sized box no longer matches
379
    /// what its callback reported (see `natural_size_stale`).
380
140
    pub fn mark_natural_size_stale(&mut self, dom_id: DomId, node_id: NodeId) {
381
140
        self.natural_size_stale.insert((dom_id, node_id));
382
140
    }
383

            
384
    /// Drain the stale content-sized views of ONE host dom, sorted by node.
385
    /// Empty when every content-sized view in `dom_id` is laid out at the
386
    /// size it reported.
387
16860
    pub fn take_natural_size_stale(&mut self, dom_id: DomId) -> Vec<NodeId> {
388
16860
        let mut taken = Vec::new();
389
16860
        self.natural_size_stale.retain(|(d, n)| {
390
420
            if *d == dom_id {
391
140
                taken.push(*n);
392
140
                false
393
            } else {
394
280
                true
395
            }
396
420
        });
397
16860
        taken
398
16860
    }
399

            
400
    /// Every host dom with at least one stale content-sized view, sorted.
401
    #[must_use]
402
280
    pub fn natural_size_stale_doms(&self) -> Vec<DomId> {
403
280
        let mut doms: Vec<DomId> = self.natural_size_stale.iter().map(|(d, _)| *d).collect();
404
280
        doms.dedup();
405
280
        doms
406
280
    }
407

            
408
    /// Checks whether a `VirtualView` needs to be re-invoked and returns the reason
409
    ///
410
    /// Returns `Some(reason)` if the `VirtualView` callback should be invoked:
411
    /// - `InitialRender`: `VirtualView` has never been invoked
412
    /// - `BoundsExpanded`: the container grew since the callback last saw it, and is now larger
413
    ///   than the materialized content
414
    /// - `ScrollBeyondContent`: the visible window left the materialized one entirely (a jump past
415
    ///   everything that is rendered)
416
    /// - `EdgeScrolled`: User scrolled near an edge (for lazy loading)
417
    ///
418
    /// Returns `None` if no re-invocation is needed — including when the
419
    /// current window has already been invoked for exactly the demand that
420
    /// is standing (see `VirtualViewState::served_scroll_demand`).
421
15617
    pub fn check_reinvoke(
422
15617
        &mut self,
423
15617
        dom_id: DomId,
424
15617
        node_id: NodeId,
425
15617
        scroll_manager: &ScrollManager,
426
15617
        layout_bounds: LogicalRect,
427
15617
    ) -> Option<VirtualViewCallbackReason> {
428
        // MWA-C-virtual_view: a staged reason override wins (set by
429
        // process_virtual_view_updates immediately before the invoke). The
430
        // old force_reinvoke path cleared was_invoked instead, which
431
        // collapsed EVERY queued re-invocation to InitialRender at delivery
432
        // time — user callbacks could never see EdgeScrolled/BoundsExpanded/
433
        // DomRecreated (the latter had zero producers at all).
434
15617
        if let Some(pos) = self
435
15617
            .reason_overrides
436
15617
            .iter()
437
15617
            .position(|((d, n), _)| *d == dom_id && *n == node_id)
438
        {
439
285
            let (_, reason) = self.reason_overrides.remove(pos);
440
285
            return Some(reason);
441
15332
        }
442

            
443
15332
        let state = self.states.entry((dom_id, node_id)).or_insert_with(|| {
444
506
            let nested_dom_id = DomId {
445
506
                inner: self.next_dom_id,
446
506
            };
447
506
            self.next_dom_id += 1;
448
506
            VirtualViewState::new(nested_dom_id)
449
506
        });
450

            
451
15332
        if !state.virtual_view_was_invoked {
452
            // Remember where we started, so edge callbacks fire on scroll-to-edge,
453
            // not for the edge we happen to rest on at the initial position.
454
676
            state.initial_scroll_offset = scroll_manager
455
676
                .get_current_offset(dom_id, node_id)
456
676
                .unwrap_or_default();
457
            // The initial render is shown these bounds too. Without recording
458
            // them, the first check after it compared the real container
459
            // against a zero one and fired `BoundsExpanded` on the first
460
            // scroll tick of every view whose window is narrower than its
461
            // viewport (a paginated document in a wide canvas, always).
462
676
            state.container = layout_bounds;
463
676
            return Some(VirtualViewCallbackReason::InitialRender);
464
14656
        }
465

            
466
        // Check for bounds expansion
467
14656
        if layout_bounds.size.width > state.container.size.width
468
14612
            || layout_bounds.size.height > state.container.size.height
469
54
        {
470
54
            state.invoked_for_current_expansion = false;
471
14602
        }
472
14656
        state.container = layout_bounds;
473

            
474
14656
        let scroll_offset = scroll_manager
475
14656
            .get_current_offset(dom_id, node_id)
476
14656
            .unwrap_or_default();
477

            
478
14656
        state.check_reinvoke_condition(scroll_offset, layout_bounds.size)
479
15617
    }
480

            
481
    /// Returns debug info for all tracked `VirtualViews`
482
    ///
483
    /// Each entry contains: (`parent_dom_id`, `parent_node_id`, `nested_dom_id`,
484
    /// `scroll_size`, `virtual_scroll_size`, `was_invoked`, `last_bounds`)
485
    #[must_use]
486
8
    pub fn get_all_virtual_view_infos(&self) -> Vec<VirtualViewDebugInfo> {
487
8
        self.states
488
8
            .iter()
489
8
            .map(|((dom_id, node_id), state)| VirtualViewDebugInfo {
490
15
                parent_dom_id: dom_id.inner,
491
15
                parent_node_id: node_id.index(),
492
15
                nested_dom_id: state.nested_dom_id.inner,
493
15
                scroll_size_width: state.materialized.map(|m| m.size.width),
494
15
                scroll_size_height: state.materialized.map(|m| m.size.height),
495
15
                virtual_scroll_size_width: state.virtual_rect.map(|v| v.size.width),
496
15
                virtual_scroll_size_height: state.virtual_rect.map(|v| v.size.height),
497
15
                was_invoked: state.virtual_view_was_invoked,
498
15
                last_bounds_x: state.container.origin.x,
499
15
                last_bounds_y: state.container.origin.y,
500
15
                last_bounds_width: state.container.size.width,
501
15
                last_bounds_height: state.container.size.height,
502
15
            })
503
8
            .collect()
504
8
    }
505
}
506

            
507
/// Debug info for a single `VirtualView`, returned by `get_all_virtual_view_infos`
508
#[derive(Copy, Debug, Clone)]
509
pub struct VirtualViewDebugInfo {
510
    pub parent_dom_id: usize,
511
    pub parent_node_id: usize,
512
    pub nested_dom_id: usize,
513
    pub scroll_size_width: Option<f32>,
514
    pub scroll_size_height: Option<f32>,
515
    pub virtual_scroll_size_width: Option<f32>,
516
    pub virtual_scroll_size_height: Option<f32>,
517
    pub was_invoked: bool,
518
    pub last_bounds_x: f32,
519
    pub last_bounds_y: f32,
520
    pub last_bounds_width: f32,
521
    pub last_bounds_height: f32,
522
}
523

            
524
impl VirtualViewState {
525
    /// Creates a new `VirtualViewState` with the given nested DOM ID
526
864
    const fn new(nested_dom_id: DomId) -> Self {
527
864
        Self {
528
864
            materialized: None,
529
864
            virtual_rect: None,
530
864
            virtual_view_was_invoked: false,
531
864
            invoked_for_current_expansion: false,
532
864
            served_scroll_demand: None,
533
864
            nested_dom_id,
534
864
            container: LogicalRect::zero(),
535
864
            initial_scroll_offset: LogicalPosition::zero(),
536
864
        }
537
864
    }
538

            
539
    /// Determines if the `VirtualView` callback should be re-invoked based on
540
    /// scroll position
541
    ///
542
    /// Checks, in this order:
543
    /// 1. Container bounds expanded beyond the materialized content (`BoundsExpanded`, once per
544
    ///    container growth — armed by [`VirtualViewManager::check_reinvoke`], served by any
545
    ///    invocation).
546
    /// 2. What the scroll position demands of the materialized window ([`Self::scroll_demand`]:
547
    ///    `ScrollBeyondContent` / `EdgeScrolled`), minus the demand this window has already been
548
    ///    invoked for.
549
    ///
550
    /// This is where the "fires once per edge approach" latch is CLEARED: a
551
    /// scroll position that demands nothing releases `served_scroll_demand`,
552
    /// so the next approach to the same edge fires again.
553
14706
    fn check_reinvoke_condition(
554
14706
        &mut self,
555
14706
        current_offset: LogicalPosition,
556
14706
        container_size: LogicalSize,
557
14706
    ) -> Option<VirtualViewCallbackReason> {
558
        // Nothing is materialized yet — nothing to be near the edge OF.
559
14706
        let materialized = self.materialized?;
560

            
561
        // Check 1: Container grew larger than the materialized content — the
562
        // window no longer fills the viewport, so ask for more.
563
14693
        if !self.invoked_for_current_expansion
564
220
            && (container_size.width > materialized.size.width
565
194
                || container_size.height > materialized.size.height)
566
        {
567
26
            return Some(VirtualViewCallbackReason::BoundsExpanded);
568
14667
        }
569

            
570
        // Check 2: the scroll position against WHAT IS MATERIALIZED.
571
14667
        let Some(demand) = self.scroll_demand(current_offset, container_size) else {
572
            // The scroll moved away from every edge: the latch releases, and
573
            // the next approach is a new event.
574
14377
            self.served_scroll_demand = None;
575
14377
            return None;
576
        };
577

            
578
        // Already asked this window exactly this question, and it answered
579
        // with this same window (a different answer would have dropped the
580
        // latch in `update_virtual_view_info`): asking again would spin.
581
290
        if self.served_scroll_demand == Some(demand) {
582
18
            return None;
583
272
        }
584

            
585
272
        Some(demand)
586
14706
    }
587

            
588
    /// What the scroll position demands of the materialized window, from the
589
    /// geometry alone — no memory of previous invocations.
590
    ///
591
    /// The visible window is `[current_offset, current_offset + container]`,
592
    /// in the same virtual space as `materialized` and the document estimate.
593
    ///
594
    /// * `ScrollBeyondContent`: the visible window does not overlap the materialized one at all (a
595
    ///   scrollbar drag or a programmatic jump landed on pages that were never materialized) while
596
    ///   the document does extend there. Everything on screen would be bare background.
597
    /// * `EdgeScrolled(edge)`: the visible window is within `EDGE_THRESHOLD` of an edge of the
598
    ///   materialized window that the document extends past (so there is something to load —
599
    ///   otherwise the ends of every document would demand a re-materialization forever). Priority
600
    ///   bottom / right / top / left, the common infinite-scroll directions first; a callback that
601
    ///   materializes around the offset clears all of them at once, one that only extends the
602
    ///   reported edge is asked about the next one on the next check.
603
    ///
604
    /// Both require the user to have actually moved from the resting position
605
    /// captured at `InitialRender`: the callback was just invoked for THAT
606
    /// offset and materialized what it wanted for it, so sitting there is not
607
    /// a scroll event.
608
    ///
609
    /// The old rule compared a VIRTUAL-space offset against the MATERIALIZED
610
    /// window's size — two different spaces — so it only ever fired at the
611
    /// absolute top/bottom of the document, and a document scrolled in the
612
    /// middle never re-materialized at all.
613
14667
    fn scroll_demand(
614
14667
        &self,
615
14667
        current_offset: LogicalPosition,
616
14667
        container_size: LogicalSize,
617
14667
    ) -> Option<VirtualViewCallbackReason> {
618
14667
        let materialized = self.materialized?;
619
        // The document estimate; falls back to the materialized window when
620
        // the app reports no virtual extent (a VirtualView used as a plain
621
        // windowed view: then materialized IS the document).
622
14667
        let virtual_rect = self.virtual_rect.unwrap_or(materialized);
623

            
624
        // Only treat an edge as "scrolled to" once the user has actually moved
625
        // from the resting position captured at InitialRender — sitting at the
626
        // initial top/left edge from the start is not an edge-scroll event.
627
14667
        if current_offset == self.initial_scroll_offset {
628
277
            return None;
629
14390
        }
630

            
631
14390
        let vis_min_x = current_offset.x;
632
14390
        let vis_min_y = current_offset.y;
633
14390
        let vis_max_x = current_offset.x + container_size.width;
634
14390
        let vis_max_y = current_offset.y + container_size.height;
635

            
636
14390
        let mat_min_x = materialized.origin.x;
637
14390
        let mat_min_y = materialized.origin.y;
638
14390
        let mat_max_x = materialized.origin.x + materialized.size.width;
639
14390
        let mat_max_y = materialized.origin.y + materialized.size.height;
640

            
641
14390
        let doc_min_x = virtual_rect.origin.x;
642
14390
        let doc_min_y = virtual_rect.origin.y;
643
14390
        let doc_max_x = virtual_rect.origin.x + virtual_rect.size.width;
644
14390
        let doc_max_y = virtual_rect.origin.y + virtual_rect.size.height;
645

            
646
        // Is there document past each edge of the materialized window?
647
14390
        let doc_above = mat_min_y > doc_min_y;
648
14390
        let doc_below = mat_max_y < doc_max_y;
649
14390
        let doc_left = mat_min_x > doc_min_x;
650
14390
        let doc_right = mat_max_x < doc_max_x;
651

            
652
        // The visible window lies entirely past an edge of the materialized
653
        // window, on the side the document continues on. Strict: a visible
654
        // window flush against the materialized edge still shows nothing of
655
        // it, and a viewport of zero height can never be "beyond" (`<=`/`>=`
656
        // on equal bounds would have made it so).
657
14390
        let beyond = (doc_below && vis_min_y >= mat_max_y && vis_min_y < doc_max_y)
658
14377
            || (doc_above && vis_max_y <= mat_min_y && vis_max_y > doc_min_y)
659
14375
            || (doc_right && vis_min_x >= mat_max_x && vis_min_x < doc_max_x)
660
14375
            || (doc_left && vis_max_x <= mat_min_x && vis_max_x > doc_min_x);
661
14390
        if beyond {
662
15
            return Some(VirtualViewCallbackReason::ScrollBeyondContent);
663
14375
        }
664

            
665
14375
        let near = EdgeFlags {
666
            // More document above what we materialized, and the view is near
667
            // the materialized window's top.
668
14375
            top: doc_above && (vis_min_y - mat_min_y) <= EDGE_THRESHOLD,
669
14375
            bottom: doc_below && (mat_max_y - vis_max_y) <= EDGE_THRESHOLD,
670
14375
            left: doc_left && (vis_min_x - mat_min_x) <= EDGE_THRESHOLD,
671
14375
            right: doc_right && (mat_max_x - vis_max_x) <= EDGE_THRESHOLD,
672
        };
673

            
674
14375
        if near.bottom {
675
153
            Some(VirtualViewCallbackReason::EdgeScrolled(EdgeType::Bottom))
676
14222
        } else if near.right {
677
13
            Some(VirtualViewCallbackReason::EdgeScrolled(EdgeType::Right))
678
14209
        } else if near.top {
679
107
            Some(VirtualViewCallbackReason::EdgeScrolled(EdgeType::Top))
680
14102
        } else if near.left {
681
2
            Some(VirtualViewCallbackReason::EdgeScrolled(EdgeType::Left))
682
        } else {
683
14100
            None
684
        }
685
14667
    }
686
}
687

            
688
impl crate::managers::NodeIdRemap for VirtualViewManager {
689
    /// Remap the `(DomId, NodeId)` keys of every tracked `VirtualView`.
690
    ///
691
    /// A `VirtualView` whose host node was unmounted has its state dropped —
692
    /// including the `nested_dom_id` binding, which would otherwise resurface
693
    /// on whatever node inherited the index (rendering the *wrong* nested DOM
694
    /// into it) and leak forever.
695
448
    fn remap_node_ids(&mut self, dom: DomId, map: &crate::managers::NodeIdMap) {
696
448
        crate::managers::remap_dom_keys(&mut self.states, dom, map);
697

            
698
448
        self.reason_overrides.retain_mut(|((d, node_id), _)| {
699
            if *d != dom {
700
                return true;
701
            }
702
            map.resolve(*node_id).is_some_and(|new_id| {
703
                *node_id = new_id;
704
                true
705
            })
706
        });
707

            
708
        // Same rule for the stale content-size marks: follow the host node,
709
        // drop the mark with an unmounted host.
710
448
        let stale = core::mem::take(&mut self.natural_size_stale);
711
448
        self.natural_size_stale = stale
712
448
            .into_iter()
713
448
            .filter_map(|(d, node_id)| {
714
40
                if d != dom {
715
40
                    return Some((d, node_id));
716
                }
717
                map.resolve(node_id).map(|new_id| (d, new_id))
718
40
            })
719
448
            .collect();
720
448
    }
721
}
722

            
723
// ============================================================================
724
// Adversarial unit tests (autotest fleet)
725
//
726
// Hostile inputs for every category in the task file: constructors (extreme
727
// args + post-construction invariants), getters/predicates (defined value on a
728
// default/empty instance), and the numeric decision functions
729
// (`check_reinvoke` / `check_reinvoke_condition` / `update_virtual_view_info`)
730
// under NaN / ±inf / f32::MAX / negative-overscroll / zero and at the exact
731
// EDGE_THRESHOLD boundary.
732
//
733
// An inline module can reach the private `states` / `next_dom_id` /
734
// `reason_overrides` fields and the private `VirtualViewState`, so the flag
735
// invariants are asserted directly rather than inferred.
736
//
737
// Every assertion documents the *actual* behavior — nothing is weakened to
738
// make it pass. Where the actual behavior looks wrong (NaN poisoning the
739
// growth check; `Default` handing out DomId 0), the test pins the current
740
// behavior and says so in a comment. The one bug this module used to pin
741
// instead of fixing — a per-edge memory that scrolling never cleared, so every
742
// edge fired at most once per full relayout — is fixed, and the tests below
743
// state the law that replaced it: an edge approach fires once, the latch
744
// releases when the scroll moves away or the window changes, and a repeat
745
// approach fires again.
746
// ============================================================================
747
#[cfg(all(test, feature = "std"))]
748
mod autotest_generated {
749
    #![allow(clippy::float_cmp)] // deterministic inputs: exact float compares are intended
750

            
751
    use std::collections::BTreeSet;
752

            
753
    use azul_core::task::{Instant, SystemTick};
754

            
755
    use super::*;
756

            
757
    // ---------------------------------------------------------------- helpers
758

            
759
    const DOM: DomId = DomId::ROOT_ID;
760
    const DOM1: DomId = DomId { inner: 1 };
761
    const DOM_MAX: DomId = DomId { inner: usize::MAX };
762

            
763
    fn n(i: usize) -> NodeId {
764
        NodeId::new(i)
765
    }
766

            
767
    fn sz(width: f32, height: f32) -> LogicalSize {
768
        LogicalSize::new(width, height)
769
    }
770

            
771
    fn pos(x: f32, y: f32) -> LogicalPosition {
772
        LogicalPosition::new(x, y)
773
    }
774

            
775
    fn rect(width: f32, height: f32) -> LogicalRect {
776
        LogicalRect::new(LogicalPosition::zero(), sz(width, height))
777
    }
778

            
779
    /// Deterministic tick-clock instant — no wall clock, no flakiness.
780
    fn at(t: u64) -> Instant {
781
        Instant::Tick(SystemTick::new(t))
782
    }
783

            
784
    /// A `ScrollManager` reporting exactly `(x, y)` for `(dom, node)`.
785
    /// Unclamped, so overscroll / absurd offsets survive to `check_reinvoke`.
786
    fn scrolled(dom: DomId, node: NodeId, x: f32, y: f32) -> ScrollManager {
787
        let mut sm = ScrollManager::new();
788
        sm.set_scroll_position_unclamped(dom, node, pos(x, y), at(0));
789
        sm
790
    }
791

            
792
    fn st(m: &VirtualViewManager, dom: DomId, node: NodeId) -> &VirtualViewState {
793
        m.states.get(&(dom, node)).expect("state must exist")
794
    }
795

            
796
    /// How much document the manager-level fixture leaves UNMATERIALIZED below
797
    /// its window. A view whose materialized window IS the whole document is
798
    /// fully loaded, and by the edge rule nothing may fire for it — so a
799
    /// fixture that wants to observe an edge has to leave something to load.
800
    const FIXTURE_DOC_TAIL: f32 = 1000.0;
801

            
802
    /// Steady state: view `(DOM, n(1))` created, invoked once, `scroll`
803
    /// materialized at the document's top-left corner — with the document
804
    /// estimate `FIXTURE_DOC_TAIL` px taller than what is materialized, i.e. a
805
    /// real virtual view that still has content to load BELOW the window (and,
806
    /// deliberately, none above it and none to either side: the window spans
807
    /// the document's full width, as a vertically-scrolling list does).
808
    /// `initial_scroll_offset` stays at the (0, 0) default, so any nonzero offset
809
    /// counts as "the user has scrolled".
810
    fn ready_view(scroll: LogicalSize) -> VirtualViewManager {
811
        let mut m = VirtualViewManager::new();
812
        m.get_or_create_nested_dom_id(DOM, n(1));
813
        m.mark_invoked(DOM, n(1), VirtualViewCallbackReason::InitialRender)
814
            .expect("view exists");
815
        m.update_virtual_view_info(
816
            DOM,
817
            n(1),
818
            LogicalPosition::zero(),
819
            scroll,
820
            sz(scroll.width, scroll.height + FIXTURE_DOC_TAIL),
821
        )
822
        .expect("view exists");
823
        m
824
    }
825

            
826
    /// The general fixture for driving the private `check_reinvoke_condition`
827
    /// directly: an already-invoked state whose materialized window is `mat`
828
    /// and whose document estimate is `doc`, BOTH in virtual space. Spelling
829
    /// the two rects out is the point — the rule is entirely about where the
830
    /// window sits inside the document, and every "why did/didn't this fire?"
831
    /// answer is read off these two rects.
832
    fn windowed_state(mat: LogicalRect, doc: LogicalRect) -> VirtualViewState {
833
        let mut s = VirtualViewState::new(DomId { inner: 7 });
834
        s.virtual_view_was_invoked = true;
835
        s.materialized = Some(mat);
836
        s.virtual_rect = Some(doc);
837
        s
838
    }
839

            
840
    /// How far into the document the fixture's materialized window starts.
841
    /// The document extends this far ABOVE and BELOW it, so both the top and
842
    /// the bottom edge of the window have more document past them — which is
843
    /// what the edge rule is about. `vpos` maps a window-relative y (what the
844
    /// tests reason in) into virtual space.
845
    const FIXTURE_WINDOW_ORIGIN_Y: f32 = 1000.0;
846

            
847
    /// Same, for the x axis of the both-axes fixture `invoked_state_2d`.
848
    const FIXTURE_WINDOW_ORIGIN_X: f32 = 1000.0;
849

            
850
    fn vpos(y: f32) -> LogicalPosition {
851
        pos(0.0, y + FIXTURE_WINDOW_ORIGIN_Y)
852
    }
853

            
854
    /// A view windowed on the Y AXIS ONLY: a `scroll`-sized window parked
855
    /// 1000 px down a document that is 1000 px taller than the window at each
856
    /// end, and exactly as WIDE as the window.
857
    ///
858
    /// HORIZONTAL JUDGEMENT (deliberate, not an oversight): left/right can
859
    /// never fire on this fixture, because `mat_min_x == doc_min_x` and
860
    /// `mat_max_x == doc_max_x` — there is no document to either side, so
861
    /// there is nothing to load and silence is the correct answer. That is the
862
    /// shape a `VirtualView` actually ships in (a vertically-scrolling list),
863
    /// and it keeps the vertical assertions free of cross-axis noise: any edge
864
    /// these tests observe is unambiguously the one they aimed at.
865
    /// `invoked_state_2d` is the both-axes fixture, used wherever left/right is
866
    /// itself the property under test.
867
    fn invoked_state(scroll: LogicalSize) -> VirtualViewState {
868
        windowed_state(
869
            LogicalRect::new(pos(0.0, FIXTURE_WINDOW_ORIGIN_Y), scroll),
870
            LogicalRect::new(
871
                LogicalPosition::zero(),
872
                sz(scroll.width, FIXTURE_WINDOW_ORIGIN_Y * 2.0 + scroll.height),
873
            ),
874
        )
875
    }
876

            
877
    /// The same idea on BOTH axes: a `scroll`-sized window parked 1000 px into
878
    /// a document that extends 1000 px past it on all four sides. Left/right
879
    /// are exactly symmetric with top/bottom here, which is what makes edge
880
    /// priority and the horizontal threshold observable at all.
881
    fn invoked_state_2d(scroll: LogicalSize) -> VirtualViewState {
882
        windowed_state(
883
            LogicalRect::new(
884
                pos(FIXTURE_WINDOW_ORIGIN_X, FIXTURE_WINDOW_ORIGIN_Y),
885
                scroll,
886
            ),
887
            LogicalRect::new(
888
                LogicalPosition::zero(),
889
                sz(
890
                    FIXTURE_WINDOW_ORIGIN_X * 2.0 + scroll.width,
891
                    FIXTURE_WINDOW_ORIGIN_Y * 2.0 + scroll.height,
892
                ),
893
            ),
894
        )
895
    }
896

            
897
    // `Option`-returning mutators (the crate denies `unused_must_use`): these
898
    // wrappers also assert that the view actually existed.
899
    fn mark(m: &mut VirtualViewManager, dom: DomId, node: NodeId, r: VirtualViewCallbackReason) {
900
        m.mark_invoked(dom, node, r).expect("view exists");
901
    }
902

            
903
    fn set_sizes(
904
        m: &mut VirtualViewManager,
905
        dom: DomId,
906
        node: NodeId,
907
        scroll: LogicalSize,
908
        virt: LogicalSize,
909
    ) {
910
        m.update_virtual_view_info(dom, node, LogicalPosition::zero(), scroll, virt)
911
            .expect("view exists");
912
    }
913

            
914
    // ------------------------------------------------------- constructors
915

            
916
    #[test]
917
    fn new_is_empty_and_reserves_dom_id_zero_for_root() {
918
        let m = VirtualViewManager::new();
919

            
920
        assert_eq!(m.debug_counts(), 0);
921
        assert!(m.all_view_keys().is_empty());
922
        assert!(m.get_all_virtual_view_infos().is_empty());
923
        assert!(m.reason_overrides.is_empty());
924
        // 0 is the root DOM — nested ids start at 1.
925
        assert_eq!(m.next_dom_id, 1);
926

            
927
        // Getters on the empty instance are defined, not panicking.
928
        assert_eq!(m.get_nested_dom_id(DOM, n(0)), None);
929
        assert_eq!(m.get_nested_dom_id(DOM_MAX, n(usize::MAX)), None);
930
        assert!(!m.was_virtual_view_invoked(DOM, n(0)));
931
        assert!(!m.was_virtual_view_invoked(DOM_MAX, n(usize::MAX)));
932
    }
933

            
934
    #[test]
935
    fn derived_default_hands_out_root_dom_id_unlike_new() {
936
        // HAZARD (pinned, not a live bug): `new()` skips 0 because "0 is root",
937
        // but the derived `Default` starts the counter at 0, so a Default-built
938
        // manager hands out DomId::ROOT_ID as its first *nested* DOM id. Every
939
        // production site builds via `new()` (LayoutWindow does not derive
940
        // Default), so this is only reachable by a future caller.
941
        assert_eq!(VirtualViewManager::default().next_dom_id, 0);
942
        assert_eq!(VirtualViewManager::new().next_dom_id, 1);
943

            
944
        let mut d = VirtualViewManager::default();
945
        assert_eq!(d.get_or_create_nested_dom_id(DOM, n(0)), DomId::ROOT_ID);
946

            
947
        let mut fresh = VirtualViewManager::new();
948
        assert_ne!(fresh.get_or_create_nested_dom_id(DOM, n(0)), DomId::ROOT_ID);
949
    }
950

            
951
    #[test]
952
    fn virtual_view_state_new_invariants_at_extreme_dom_id() {
953
        let s = VirtualViewState::new(DomId { inner: usize::MAX });
954

            
955
        assert_eq!(s.nested_dom_id.inner, usize::MAX);
956
        assert!(s.materialized.is_none());
957
        assert!(s.virtual_rect.is_none());
958
        assert!(!s.virtual_view_was_invoked);
959
        assert!(!s.invoked_for_current_expansion);
960
        assert!(s.served_scroll_demand.is_none());
961
        assert_eq!(s.container, LogicalRect::zero());
962
        assert_eq!(s.initial_scroll_offset, LogicalPosition::zero());
963

            
964
        // A brand-new state has no content size, so it can never ask to be
965
        // re-invoked, however absurd the container.
966
        let mut s = s;
967
        assert_eq!(
968
            s.check_reinvoke_condition(pos(0.0, 0.0), sz(f32::INFINITY, f32::INFINITY)),
969
            None
970
        );
971
    }
972

            
973
    // --------------------------------------------- nested DOM id allocation
974

            
975
    #[test]
976
    fn get_or_create_is_idempotent_and_unique_per_key() {
977
        let mut m = VirtualViewManager::new();
978

            
979
        let a = m.get_or_create_nested_dom_id(DOM, n(0));
980
        let a_again = m.get_or_create_nested_dom_id(DOM, n(0));
981
        assert_eq!(a, a_again, "re-registering a view must not re-allocate");
982
        assert_eq!(a, DomId { inner: 1 });
983
        assert_eq!(m.debug_counts(), 1);
984

            
985
        // Saturated key components must not panic and must get a fresh id.
986
        let b = m.get_or_create_nested_dom_id(DOM_MAX, n(usize::MAX));
987
        assert_eq!(b, DomId { inner: 2 });
988
        assert_ne!(a, b);
989
        assert_eq!(m.debug_counts(), 2);
990

            
991
        assert_eq!(m.get_nested_dom_id(DOM, n(0)), Some(a));
992
        assert_eq!(m.get_nested_dom_id(DOM_MAX, n(usize::MAX)), Some(b));
993
        assert_eq!(m.get_nested_dom_id(DOM, n(1)), None);
994
        assert_eq!(m.get_nested_dom_id(DOM1, n(0)), None);
995
    }
996

            
997
    #[test]
998
    fn nested_dom_ids_are_unique_across_many_views() {
999
        let mut m = VirtualViewManager::new();
        let mut seen = BTreeSet::new();
        for dom in 0..8_usize {
            for node in 0..32_usize {
                let id = m.get_or_create_nested_dom_id(DomId { inner: dom }, n(node));
                assert!(id.inner >= 1, "nested id must never collide with the root");
                assert!(
                    seen.insert(id.inner),
                    "nested DOM id {id:?} handed out twice"
                );
            }
        }
        assert_eq!(seen.len(), 8 * 32);
        assert_eq!(m.debug_counts(), 8 * 32);
        assert_eq!(m.next_dom_id, 8 * 32 + 1);
    }
    #[test]
    fn all_view_keys_is_sorted_and_matches_the_tracked_states() {
        let mut m = VirtualViewManager::new();
        assert!(m.all_view_keys().is_empty());
        // Insert in deliberately reversed order — BTreeMap must still yield
        // ascending (DomId, NodeId).
        m.get_or_create_nested_dom_id(DOM1, n(9));
        m.get_or_create_nested_dom_id(DOM1, n(2));
        m.get_or_create_nested_dom_id(DOM, n(7));
        let keys = m.all_view_keys();
        assert_eq!(keys, vec![(DOM, n(7)), (DOM1, n(2)), (DOM1, n(9))]);
        assert_eq!(keys.len(), m.debug_counts());
        let mut sorted = keys.clone();
        sorted.sort_unstable();
        assert_eq!(keys, sorted);
    }
    // ------------------------------------------------ Option-returning mutators
    #[test]
    fn mutators_return_none_for_unknown_view_and_never_insert() {
        let mut m = VirtualViewManager::new();
        assert_eq!(
            m.update_virtual_view_info(
                DOM,
                n(3),
                LogicalPosition::zero(),
                sz(1.0, 1.0),
                sz(1.0, 1.0)
            ),
            None
        );
        assert_eq!(
            m.mark_invoked(DOM, n(3), VirtualViewCallbackReason::InitialRender),
            None
        );
        assert_eq!(m.force_reinvoke(DOM, n(3)), None);
        assert_eq!(
            m.update_virtual_view_info(
                DOM_MAX,
                n(usize::MAX),
                LogicalPosition::zero(),
                sz(0.0, 0.0),
                sz(0.0, 0.0)
            ),
            None
        );
        // Unlike check_reinvoke, none of these may lazily create a state.
        assert_eq!(m.debug_counts(), 0);
        assert_eq!(m.next_dom_id, 1);
    }
    // ------------------------------------------------------ reason overrides
    #[test]
    fn set_reason_override_keeps_only_the_latest_per_key() {
        let mut m = VirtualViewManager::new();
        for _ in 0..1_000 {
            m.set_reason_override(DOM, n(2), VirtualViewCallbackReason::DomRecreated);
        }
        m.set_reason_override(DOM, n(2), VirtualViewCallbackReason::BoundsExpanded);
        // Re-staging must overwrite, not accumulate.
        assert_eq!(m.reason_overrides.len(), 1);
        let sm = ScrollManager::new();
        assert_eq!(
            m.check_reinvoke(DOM, n(2), &sm, rect(10.0, 10.0)),
            Some(VirtualViewCallbackReason::BoundsExpanded)
        );
    }
    #[test]
    fn reason_override_is_consumed_exactly_once_and_does_not_create_state() {
        let mut m = VirtualViewManager::new();
        let sm = ScrollManager::new();
        m.set_reason_override(DOM, n(2), VirtualViewCallbackReason::ScrollBeyondContent);
        assert_eq!(
            m.check_reinvoke(DOM, n(2), &sm, rect(10.0, 10.0)),
            Some(VirtualViewCallbackReason::ScrollBeyondContent)
        );
        // The override short-circuits before the entry() call, so no state yet.
        assert!(m.reason_overrides.is_empty());
        assert_eq!(m.debug_counts(), 0);
        // Second call falls through to the normal path, which *does* create it.
        assert_eq!(
            m.check_reinvoke(DOM, n(2), &sm, rect(10.0, 10.0)),
            Some(VirtualViewCallbackReason::InitialRender)
        );
        assert_eq!(m.debug_counts(), 1);
        assert_eq!(m.get_nested_dom_id(DOM, n(2)), Some(DomId { inner: 1 }));
    }
    #[test]
    fn reason_overrides_do_not_leak_across_keys() {
        let mut m = VirtualViewManager::new();
        let sm = ScrollManager::new();
        m.set_reason_override(
            DOM,
            n(1),
            VirtualViewCallbackReason::EdgeScrolled(EdgeType::Left),
        );
        m.set_reason_override(DOM1, n(1), VirtualViewCallbackReason::DomRecreated);
        m.set_reason_override(DOM, n(2), VirtualViewCallbackReason::BoundsExpanded);
        assert_eq!(m.reason_overrides.len(), 3);
        // A different node of the same DOM must not steal DOM/n(1)'s override.
        assert_eq!(
            m.check_reinvoke(DOM, n(2), &sm, rect(1.0, 1.0)),
            Some(VirtualViewCallbackReason::BoundsExpanded)
        );
        assert_eq!(
            m.check_reinvoke(DOM1, n(1), &sm, rect(1.0, 1.0)),
            Some(VirtualViewCallbackReason::DomRecreated)
        );
        assert_eq!(
            m.check_reinvoke(DOM, n(1), &sm, rect(1.0, 1.0)),
            Some(VirtualViewCallbackReason::EdgeScrolled(EdgeType::Left))
        );
        assert!(m.reason_overrides.is_empty());
    }
    // ------------------------------------------- update_virtual_view_info (numeric)
    #[test]
    fn update_virtual_view_info_zero_and_extreme_sizes_do_not_panic() {
        let mut m = VirtualViewManager::new();
        m.get_or_create_nested_dom_id(DOM, n(1));
        for size in [
            sz(0.0, 0.0),
            sz(-0.0, -0.0),
            sz(f32::MAX, f32::MAX),
            sz(f32::MIN, f32::MIN),
            sz(f32::INFINITY, f32::NEG_INFINITY),
            sz(-1.0e30, 1.0e30),
            sz(f32::MIN_POSITIVE, f32::EPSILON),
        ] {
            assert_eq!(
                m.update_virtual_view_info(DOM, n(1), LogicalPosition::zero(), size, size),
                Some(()),
                "size {size:?} must be recorded without panicking"
            );
            assert_eq!(st(&m, DOM, n(1)).materialized.map(|r| r.size), Some(size));
            assert_eq!(st(&m, DOM, n(1)).virtual_rect.map(|r| r.size), Some(size));
        }
        // NaN is stored verbatim (no normalization, no panic).
        assert_eq!(
            m.update_virtual_view_info(
                DOM,
                n(1),
                LogicalPosition::zero(),
                sz(f32::NAN, f32::NAN),
                sz(f32::NAN, 1.0)
            ),
            Some(())
        );
        let stored = st(&m, DOM, n(1)).materialized.map(|r| r.size).unwrap();
        assert!(stored.width.is_nan() && stored.height.is_nan());
    }
    #[test]
    fn update_virtual_view_info_never_touches_the_expansion_latch() {
        // What the callback materializes is its ANSWER to the bounds it was
        // shown, not a new question: content growth, shrinkage, infinite or
        // NaN sizes all leave `invoked_for_current_expansion` alone. (The old
        // rule cleared it on content growth, so a view that answered an
        // expansion by growing was immediately asked again.)
        let mut m = ready_view(sz(100.0, 100.0));
        mark(&mut m, DOM, n(1), VirtualViewCallbackReason::BoundsExpanded);
        assert!(st(&m, DOM, n(1)).invoked_for_current_expansion);
        for size in [
            sz(50.0, 50.0),
            sz(50.0, 50.0),
            sz(50.000_01, 50.0),
            sz(1.0e9, 1.0e9),
            sz(f32::INFINITY, 100.0),
            sz(f32::NAN, f32::NAN),
            sz(1.0e9, 1.0e9),
        ] {
            set_sizes(&mut m, DOM, n(1), size, size);
            assert!(
                st(&m, DOM, n(1)).invoked_for_current_expansion,
                "size {size:?} must not re-arm BoundsExpanded"
            );
        }
        // Only a container that GREW (check_reinvoke) or a reset re-arms it.
        m.force_reinvoke(DOM, n(1)).expect("view exists");
        assert!(!st(&m, DOM, n(1)).invoked_for_current_expansion);
    }
    // ----------------------------------------------------------- mark_invoked
    #[test]
    fn mark_invoked_serves_the_container_and_latches_only_scroll_demands() {
        // Every invocation is shown `info.bounds`, so every reason answers the
        // current expansion. Only the scroll-driven reasons latch THEMSELVES
        // — the exact demand that was served, so the same demand is
        // suppressed and a different one (another edge, or a jump beyond the
        // content) is not.
        for reason in [
            VirtualViewCallbackReason::InitialRender,
            VirtualViewCallbackReason::DomRecreated,
            VirtualViewCallbackReason::BoundsExpanded,
        ] {
            let mut m = VirtualViewManager::new();
            m.get_or_create_nested_dom_id(DOM, n(1));
            assert_eq!(m.mark_invoked(DOM, n(1), reason), Some(()));
            let s = st(&m, DOM, n(1));
            assert!(s.virtual_view_was_invoked, "{reason:?} must mark invoked");
            assert!(s.invoked_for_current_expansion, "{reason:?}");
            assert!(s.served_scroll_demand.is_none(), "{reason:?}");
            assert!(m.was_virtual_view_invoked(DOM, n(1)));
        }
        for reason in [
            VirtualViewCallbackReason::EdgeScrolled(EdgeType::Top),
            VirtualViewCallbackReason::EdgeScrolled(EdgeType::Bottom),
            VirtualViewCallbackReason::EdgeScrolled(EdgeType::Left),
            VirtualViewCallbackReason::EdgeScrolled(EdgeType::Right),
            VirtualViewCallbackReason::ScrollBeyondContent,
        ] {
            let mut m = VirtualViewManager::new();
            m.get_or_create_nested_dom_id(DOM, n(1));
            mark(&mut m, DOM, n(1), reason);
            let s = st(&m, DOM, n(1));
            assert!(s.virtual_view_was_invoked);
            assert!(s.invoked_for_current_expansion, "{reason:?}");
            assert_eq!(s.served_scroll_demand, Some(reason), "{reason:?}");
        }
    }
    // ---------------------------------------------------- reset / force_reinvoke
    #[test]
    fn reset_all_invocation_flags_on_empty_manager_is_a_noop() {
        let mut m = VirtualViewManager::new();
        m.reset_all_invocation_flags();
        assert_eq!(m.debug_counts(), 0);
        assert_eq!(m.next_dom_id, 1);
        assert!(m.all_view_keys().is_empty());
    }
    #[test]
    fn reset_all_clears_every_flag_but_preserves_identity_sizes_and_bounds() {
        let mut m = ready_view(sz(100.0, 1000.0));
        let nested = m.get_nested_dom_id(DOM, n(1)).expect("view exists");
        m.get_or_create_nested_dom_id(DOM1, n(4));
        mark(
            &mut m,
            DOM,
            n(1),
            VirtualViewCallbackReason::EdgeScrolled(EdgeType::Bottom),
        );
        mark(
            &mut m,
            DOM1,
            n(4),
            VirtualViewCallbackReason::BoundsExpanded,
        );
        // Record a non-zero last_bounds through the normal path.
        let sm = scrolled(DOM, n(1), 0.0, 900.0);
        let _ = m.check_reinvoke(DOM, n(1), &sm, rect(100.0, 100.0));
        // Overrides are a separate queue: reset must not touch them.
        m.set_reason_override(DOM, n(1), VirtualViewCallbackReason::DomRecreated);
        m.reset_all_invocation_flags();
        for (dom, node) in [(DOM, n(1)), (DOM1, n(4))] {
            let s = st(&m, dom, node);
            assert!(!s.virtual_view_was_invoked);
            assert!(!s.invoked_for_current_expansion);
            assert!(s.served_scroll_demand.is_none());
            assert!(!m.was_virtual_view_invoked(dom, node));
        }
        // Identity, content size and bounds survive — only the flags reset.
        let s = st(&m, DOM, n(1));
        assert_eq!(s.nested_dom_id, nested);
        assert_eq!(s.materialized.map(|r| r.size), Some(sz(100.0, 1000.0)));
        assert_eq!(s.container, rect(100.0, 100.0));
        assert_eq!(m.debug_counts(), 2);
        assert_eq!(m.reason_overrides.len(), 1);
    }
    #[test]
    fn force_reinvoke_yields_initial_render_and_releases_the_scroll_latch() {
        let mut m = ready_view(sz(100.0, 1000.0));
        mark(
            &mut m,
            DOM,
            n(1),
            VirtualViewCallbackReason::EdgeScrolled(EdgeType::Bottom),
        );
        assert!(st(&m, DOM, n(1)).served_scroll_demand.is_some());
        assert_eq!(m.force_reinvoke(DOM, n(1)), Some(()));
        let s = st(&m, DOM, n(1));
        assert!(!s.virtual_view_was_invoked);
        assert!(!s.invoked_for_current_expansion);
        // A forced re-invoke starts the view over: it must not carry a memory
        // of an edge the NEXT materialization has never been asked about.
        // (This used to be asymmetric with reset_all_invocation_flags, which
        // is how an infinite-scroll list stopped loading after its first page.)
        assert!(s.served_scroll_demand.is_none());
        // The documented effect still holds: the next check is an InitialRender.
        let sm = scrolled(DOM, n(1), 0.0, 900.0);
        assert_eq!(
            m.check_reinvoke(DOM, n(1), &sm, rect(100.0, 100.0)),
            Some(VirtualViewCallbackReason::InitialRender)
        );
    }
    // -------------------------------------------------- check_reinvoke (numeric)
    #[test]
    fn check_reinvoke_creates_the_state_for_an_unknown_view() {
        let mut m = VirtualViewManager::new();
        let sm = ScrollManager::new();
        assert_eq!(
            m.check_reinvoke(DOM, n(3), &sm, rect(100.0, 100.0)),
            Some(VirtualViewCallbackReason::InitialRender)
        );
        assert_eq!(m.debug_counts(), 1);
        assert_eq!(m.get_nested_dom_id(DOM, n(3)), Some(DomId { inner: 1 }));
        assert!(!m.was_virtual_view_invoked(DOM, n(3)));
        // Re-checking without marking must keep returning InitialRender and must
        // NOT keep allocating states/ids (unbounded growth guard).
        for _ in 0..16 {
            assert_eq!(
                m.check_reinvoke(DOM, n(3), &sm, rect(100.0, 100.0)),
                Some(VirtualViewCallbackReason::InitialRender)
            );
        }
        assert_eq!(m.debug_counts(), 1);
        assert_eq!(m.next_dom_id, 2);
        // Saturated key: no panic, fresh id.
        assert_eq!(
            m.check_reinvoke(DOM_MAX, n(usize::MAX), &sm, rect(0.0, 0.0)),
            Some(VirtualViewCallbackReason::InitialRender)
        );
        assert_eq!(
            m.get_nested_dom_id(DOM_MAX, n(usize::MAX)),
            Some(DomId { inner: 2 })
        );
    }
    #[test]
    fn check_reinvoke_is_none_while_no_content_size_is_known() {
        let mut m = VirtualViewManager::new();
        m.get_or_create_nested_dom_id(DOM, n(1));
        mark(&mut m, DOM, n(1), VirtualViewCallbackReason::InitialRender);
        let sm = scrolled(DOM, n(1), 0.0, 5_000.0);
        // scroll_size is still None → the `?` bails out, whatever the bounds.
        assert_eq!(
            m.check_reinvoke(DOM, n(1), &sm, rect(f32::MAX, f32::MAX)),
            None
        );
        assert_eq!(m.check_reinvoke(DOM, n(1), &sm, rect(0.0, 0.0)), None);
    }
    #[test]
    fn check_reinvoke_bounds_expanded_fires_once_per_growth() {
        let mut m = ready_view(sz(100.0, 100.0));
        let sm = ScrollManager::new();
        assert_eq!(
            m.check_reinvoke(DOM, n(1), &sm, rect(200.0, 200.0)),
            Some(VirtualViewCallbackReason::BoundsExpanded)
        );
        mark(&mut m, DOM, n(1), VirtualViewCallbackReason::BoundsExpanded);
        // Same bounds again → already invoked for this expansion → quiet.
        assert_eq!(m.check_reinvoke(DOM, n(1), &sm, rect(200.0, 200.0)), None);
        // Shrinking is never a re-invoke trigger.
        assert_eq!(m.check_reinvoke(DOM, n(1), &sm, rect(150.0, 150.0)), None);
        // Growing past the last bounds re-arms it.
        assert_eq!(
            m.check_reinvoke(DOM, n(1), &sm, rect(300.0, 300.0)),
            Some(VirtualViewCallbackReason::BoundsExpanded)
        );
        assert_eq!(st(&m, DOM, n(1)).container, rect(300.0, 300.0));
    }
    #[test]
    fn check_reinvoke_does_not_fire_an_edge_for_the_resting_start_position() {
        // Regression guard for the initial_scroll_offset rule: a view that
        // starts at offset 0 is *at* the top edge, but that is the initial
        // position, not a scroll-to-edge event.
        let mut m = VirtualViewManager::new();
        m.get_or_create_nested_dom_id(DOM, n(1));
        let sm = scrolled(DOM, n(1), 0.0, 0.0);
        assert_eq!(
            m.check_reinvoke(DOM, n(1), &sm, rect(100.0, 100.0)),
            Some(VirtualViewCallbackReason::InitialRender)
        );
        assert_eq!(st(&m, DOM, n(1)).initial_scroll_offset, pos(0.0, 0.0));
        mark(&mut m, DOM, n(1), VirtualViewCallbackReason::InitialRender);
        set_sizes(&mut m, DOM, n(1), sz(100.0, 1000.0), sz(100.0, 1000.0));
        // Still parked at the top edge, hasn't moved → no EdgeScrolled(Top).
        assert_eq!(m.check_reinvoke(DOM, n(1), &sm, rect(100.0, 100.0)), None);
    }
    #[test]
    fn an_initial_render_serves_the_bounds_it_was_shown() {
        // A paginated document: the callback materializes pages 796 px wide
        // inside a 1400 px canvas, so the container is wider than the window
        // for the whole life of the view. That is the callback's layout
        // choice, not an expansion — the first scroll tick must NOT
        // re-materialize everything as `BoundsExpanded`. (It did: the
        // InitialRender branch returned before recording the container, so
        // the first real check saw a growth from a zero container.)
        let canvas = rect(1400.0, 900.0);
        let mut m = VirtualViewManager::new();
        let sm = scrolled(DOM, n(1), 0.0, 0.0);
        assert_eq!(
            m.check_reinvoke(DOM, n(1), &sm, canvas),
            Some(VirtualViewCallbackReason::InitialRender)
        );
        assert_eq!(st(&m, DOM, n(1)).container, canvas);
        mark(&mut m, DOM, n(1), VirtualViewCallbackReason::InitialRender);
        set_sizes(&mut m, DOM, n(1), sz(796.0, 3000.0), sz(796.0, 13_000.0));
        // First wheel tick: same canvas, 20 px in — nothing to ask.
        let sm = scrolled(DOM, n(1), 0.0, 20.0);
        assert_eq!(m.check_reinvoke(DOM, n(1), &sm, canvas), None);
        // The canvas itself growing is the documented BoundsExpanded, once.
        let wider = rect(1600.0, 900.0);
        assert_eq!(
            m.check_reinvoke(DOM, n(1), &sm, wider),
            Some(VirtualViewCallbackReason::BoundsExpanded)
        );
        mark(&mut m, DOM, n(1), VirtualViewCallbackReason::BoundsExpanded);
        assert_eq!(m.check_reinvoke(DOM, n(1), &sm, wider), None);
        // Shrinking back is not an expansion either.
        assert_eq!(m.check_reinvoke(DOM, n(1), &sm, canvas), None);
    }
    #[test]
    fn check_reinvoke_edge_scrolled_bottom_then_stays_quiet() {
        let mut m = ready_view(sz(100.0, 1000.0));
        let sm = scrolled(DOM, n(1), 0.0, 900.0);
        assert_eq!(
            m.check_reinvoke(DOM, n(1), &sm, rect(100.0, 100.0)),
            Some(VirtualViewCallbackReason::EdgeScrolled(EdgeType::Bottom))
        );
        mark(
            &mut m,
            DOM,
            n(1),
            VirtualViewCallbackReason::EdgeScrolled(EdgeType::Bottom),
        );
        // Same window, same question, already answered → no duplicate. This
        // is what keeps a callback that CHOOSES not to grow (a list at its
        // real end, say) from being hammered every frame while the user sits
        // at the edge.
        assert_eq!(m.check_reinvoke(DOM, n(1), &sm, rect(100.0, 100.0)), None);
        assert_eq!(m.check_reinvoke(DOM, n(1), &sm, rect(100.0, 100.0)), None);
    }
    #[test]
    fn an_edge_approach_fires_once_and_rearms_when_the_scroll_moves_away() {
        // The documented contract: "Fires once per edge approach. The flag
        // clears once the scroll moves away." Geometry (`ready_view`): the
        // 100x1000 window is materialized at the document's ORIGIN and the
        // document is 2000 px tall, so y=900 puts the viewport's bottom flush
        // against the window's bottom edge with 1000 px still to load below;
        // y=400 is comfortably inside.
        let bottom = scrolled(DOM, n(1), 0.0, 900.0);
        let middle = scrolled(DOM, n(1), 0.0, 400.0);
        let bounds = rect(100.0, 100.0);
        let mut m = ready_view(sz(100.0, 1000.0));
        assert_eq!(
            m.check_reinvoke(DOM, n(1), &bottom, bounds),
            Some(VirtualViewCallbackReason::EdgeScrolled(EdgeType::Bottom))
        );
        mark(
            &mut m,
            DOM,
            n(1),
            VirtualViewCallbackReason::EdgeScrolled(EdgeType::Bottom),
        );
        // The callback answered with the SAME window (nothing more to show).
        set_sizes(&mut m, DOM, n(1), sz(100.0, 1000.0), sz(100.0, 2000.0));
        assert_eq!(m.check_reinvoke(DOM, n(1), &bottom, bounds), None);
        // Scrolling away releases the latch ...
        assert_eq!(m.check_reinvoke(DOM, n(1), &middle, bounds), None);
        assert!(st(&m, DOM, n(1)).served_scroll_demand.is_none());
        // ... so the next approach is a new approach and fires again.
        assert_eq!(
            m.check_reinvoke(DOM, n(1), &bottom, bounds),
            Some(VirtualViewCallbackReason::EdgeScrolled(EdgeType::Bottom))
        );
    }
    #[test]
    fn a_second_scroll_to_the_bottom_edge_fires_again_after_any_reinvoke() {
        // Regression law for the bug this module used to PIN: the old per-edge
        // memory (`last_edge_triggered`) was cleared by reset_all but not by
        // force_reinvoke, so after the first lazy-load an infinite-scroll list
        // never loaded another page until a full relayout. Both re-invoke paths
        // now start the view over; the two halves are identical and both fire.
        let bottom = scrolled(DOM, n(1), 0.0, 900.0);
        let middle = scrolled(DOM, n(1), 0.0, 400.0);
        let bounds = rect(100.0, 100.0);
        for reset_via_force in [true, false] {
            let mut m = ready_view(sz(100.0, 1000.0));
            assert_eq!(
                m.check_reinvoke(DOM, n(1), &bottom, bounds),
                Some(VirtualViewCallbackReason::EdgeScrolled(EdgeType::Bottom))
            );
            mark(
                &mut m,
                DOM,
                n(1),
                VirtualViewCallbackReason::EdgeScrolled(EdgeType::Bottom),
            );
            if reset_via_force {
                m.force_reinvoke(DOM, n(1)).expect("view exists");
            } else {
                m.reset_all_invocation_flags();
            }
            // Re-invoked while the user sits mid-list: the resting position is 400.
            assert_eq!(
                m.check_reinvoke(DOM, n(1), &middle, bounds),
                Some(VirtualViewCallbackReason::InitialRender)
            );
            mark(&mut m, DOM, n(1), VirtualViewCallbackReason::InitialRender);
            assert_eq!(st(&m, DOM, n(1)).initial_scroll_offset, pos(0.0, 400.0));
            // The user really scrolls 400 → 900: a genuine second approach.
            assert_eq!(
                m.check_reinvoke(DOM, n(1), &bottom, bounds),
                Some(VirtualViewCallbackReason::EdgeScrolled(EdgeType::Bottom)),
                "force={reset_via_force}: the second bottom-edge load must fire"
            );
        }
    }
    #[test]
    fn a_callback_that_materializes_a_new_window_is_re_evaluated_against_it() {
        // The latch remembers the ANSWER (demand + window), not the edge. When
        // the callback answers EdgeScrolled(Bottom) by materializing a window
        // further down, the latch is dropped and the standing offset is judged
        // against the NEW window: not near its edges → quiet; and when the user
        // reaches the new window's bottom edge, that is a fresh approach.
        let bounds = rect(100.0, 100.0);
        let mut m = ready_view(sz(100.0, 1000.0));
        // Document: 3000 px, window 0..1000.
        set_sizes(&mut m, DOM, n(1), sz(100.0, 1000.0), sz(100.0, 3000.0));
        let at_900 = scrolled(DOM, n(1), 0.0, 900.0);
        assert_eq!(
            m.check_reinvoke(DOM, n(1), &at_900, bounds),
            Some(VirtualViewCallbackReason::EdgeScrolled(EdgeType::Bottom))
        );
        mark(
            &mut m,
            DOM,
            n(1),
            VirtualViewCallbackReason::EdgeScrolled(EdgeType::Bottom),
        );
        // The callback re-materializes 500..1500 (a page stride down).
        m.update_virtual_view_info(
            DOM,
            n(1),
            pos(0.0, 500.0),
            sz(100.0, 1000.0),
            sz(100.0, 3000.0),
        )
        .expect("view exists");
        assert!(
            st(&m, DOM, n(1)).served_scroll_demand.is_none(),
            "a different window is a different question"
        );
        // Same offset 900: viewport 900..1000 sits 500 px above the new
        // window's bottom (1500) and 400 px below its top (500) → nothing.
        assert_eq!(m.check_reinvoke(DOM, n(1), &at_900, bounds), None);
        // 1350: viewport 1350..1450, 50 px from the new bottom → fires again.
        let at_1350 = scrolled(DOM, n(1), 0.0, 1350.0);
        assert_eq!(
            m.check_reinvoke(DOM, n(1), &at_1350, bounds),
            Some(VirtualViewCallbackReason::EdgeScrolled(EdgeType::Bottom))
        );
        mark(
            &mut m,
            DOM,
            n(1),
            VirtualViewCallbackReason::EdgeScrolled(EdgeType::Bottom),
        );
        // A callback that UNDER-DELIVERS (moves the window, but not far enough
        // to take the edge out of reach) is asked again — the demand persists
        // and the window changed, so the latch does not apply.
        m.update_virtual_view_info(
            DOM,
            n(1),
            pos(0.0, 550.0),
            sz(100.0, 1000.0),
            sz(100.0, 3000.0),
        )
        .expect("view exists");
        assert_eq!(
            m.check_reinvoke(DOM, n(1), &at_1350, bounds),
            Some(VirtualViewCallbackReason::EdgeScrolled(EdgeType::Bottom))
        );
    }
    #[test]
    fn a_smooth_scroll_through_a_paginated_document_fires_once_per_window_advance() {
        // AzWriter's page host, driven the way the wheel physics drives it:
        // 20 px per tick, one check per tick. stride = page height + gap,
        // viewport 847 px, the callback materializes `first-1 .. first+count`
        // pages around the offset (its real math). The law under test is the
        // whole point of the refactor: the callback runs ONCE per window
        // advance — never once per tick, and never twice for the same window.
        const STRIDE: f32 = 1155.0;
        const VIEWPORT: f32 = 847.0;
        const TOTAL_PAGES: f32 = 12.0;
        let bounds = rect(600.0, VIEWPORT);
        let doc = sz(600.0, TOTAL_PAGES * STRIDE);
        // The callback's answer for a given offset (AzWriter's `pages_virtual_view`).
        let materialize = |offset: f32| -> (LogicalPosition, LogicalSize) {
            let first = ((offset / STRIDE).floor() as i64 - 1).max(0) as f32;
            let visible = (VIEWPORT / STRIDE).ceil() + 2.0;
            let count = visible.max(3.0).min(TOTAL_PAGES - first);
            (pos(0.0, first * STRIDE), sz(600.0, count * STRIDE))
        };
        let mut m = VirtualViewManager::new();
        m.get_or_create_nested_dom_id(DOM, n(1));
        mark(&mut m, DOM, n(1), VirtualViewCallbackReason::InitialRender);
        let (o, s) = materialize(0.0);
        m.update_virtual_view_info(DOM, n(1), o, s, doc)
            .expect("view exists");
        let mut invocations = Vec::new();
        let mut offset = 0.0;
        let end = TOTAL_PAGES * STRIDE - VIEWPORT;
        while offset < end {
            offset = (offset + 20.0).min(end);
            let sm = scrolled(DOM, n(1), 0.0, offset);
            if let Some(reason) = m.check_reinvoke(DOM, n(1), &sm, bounds) {
                assert_eq!(
                    reason,
                    VirtualViewCallbackReason::EdgeScrolled(EdgeType::Bottom),
                    "at offset {offset}"
                );
                mark(&mut m, DOM, n(1), reason);
                let (o, s) = materialize(offset);
                m.update_virtual_view_info(DOM, n(1), o, s, doc)
                    .expect("view exists");
                invocations.push((offset, o.y));
            }
        }
        // Every invocation moved the window (no two answers with the same
        // origin back to back), and the count is one per advance: the window
        // starts at page 0 and ends at page TOTAL-3 (the last 3-page window),
        // advancing one page per fire.
        for w in invocations.windows(2) {
            assert!(
                w[1].1 > w[0].1,
                "window must advance on every fire: {invocations:?}"
            );
        }
        let last_first = TOTAL_PAGES - 3.0;
        assert_eq!(
            invocations.len(),
            last_first as usize,
            "one re-materialization per page advance, none per tick: {invocations:?}"
        );
        assert_eq!(invocations.last().map(|i| i.1), Some(last_first * STRIDE));
        // Scrolling back up through the whole document is the mirror image.
        let mut ups = 0usize;
        while offset > 0.0 {
            offset = (offset - 20.0).max(0.0);
            let sm = scrolled(DOM, n(1), 0.0, offset);
            if let Some(reason) = m.check_reinvoke(DOM, n(1), &sm, bounds) {
                assert_eq!(
                    reason,
                    VirtualViewCallbackReason::EdgeScrolled(EdgeType::Top),
                    "at offset {offset}"
                );
                mark(&mut m, DOM, n(1), reason);
                let (o, s) = materialize(offset);
                m.update_virtual_view_info(DOM, n(1), o, s, doc)
                    .expect("view exists");
                ups += 1;
            }
        }
        assert_eq!(
            ups, last_first as usize,
            "one re-materialization per page retreat"
        );
        assert_eq!(
            st(&m, DOM, n(1)).materialized.map(|r| r.origin.y),
            Some(0.0)
        );
    }
    #[test]
    fn a_jump_past_everything_materialized_is_scroll_beyond_content() {
        // "A programmatic scroll jumped the offset past the rendered
        // scroll_size" — the documented reason that had no producer. Window
        // 0..1000 of a 3000-px document; a jump to 2500 shows nothing that is
        // materialized, so the callback is asked to re-materialize there.
        let bounds = rect(100.0, 100.0);
        let mut m = ready_view(sz(100.0, 1000.0));
        set_sizes(&mut m, DOM, n(1), sz(100.0, 1000.0), sz(100.0, 3000.0));
        let far = scrolled(DOM, n(1), 0.0, 2500.0);
        assert_eq!(
            m.check_reinvoke(DOM, n(1), &far, bounds),
            Some(VirtualViewCallbackReason::ScrollBeyondContent)
        );
        mark(
            &mut m,
            DOM,
            n(1),
            VirtualViewCallbackReason::ScrollBeyondContent,
        );
        // Until the callback answers, the same jump is not asked twice.
        assert_eq!(m.check_reinvoke(DOM, n(1), &far, bounds), None);
        // The callback materializes 2000..3000 around the new offset: viewport
        // 2500..2600 is 500 px from either edge → quiet.
        m.update_virtual_view_info(
            DOM,
            n(1),
            pos(0.0, 2000.0),
            sz(100.0, 1000.0),
            sz(100.0, 3000.0),
        )
        .expect("view exists");
        assert_eq!(m.check_reinvoke(DOM, n(1), &far, bounds), None);
        // And a jump back to the top is the same demand in the other direction.
        let top = scrolled(DOM, n(1), 0.0, 100.0);
        assert_eq!(
            m.check_reinvoke(DOM, n(1), &top, bounds),
            Some(VirtualViewCallbackReason::ScrollBeyondContent)
        );
    }
    #[test]
    fn overscroll_past_the_document_end_is_not_scroll_beyond_content() {
        // Rubber-banding past the document's real end shows no unmaterialized
        // content, so it is not a jump beyond the content — and the window is
        // flush with the document end, so it is not an edge approach either.
        let bounds = rect(100.0, 100.0);
        let mut m = ready_view(sz(100.0, 1000.0));
        // Window 2000..3000 == the document's tail.
        m.update_virtual_view_info(
            DOM,
            n(1),
            pos(0.0, 2000.0),
            sz(100.0, 1000.0),
            sz(100.0, 3000.0),
        )
        .expect("view exists");
        let overscrolled = scrolled(DOM, n(1), 0.0, 3200.0);
        assert_eq!(m.check_reinvoke(DOM, n(1), &overscrolled, bounds), None);
    }
    #[test]
    fn check_reinvoke_with_nan_bounds_is_quiet_and_stores_nan() {
        let mut m = ready_view(sz(100.0, 100.0));
        let sm = ScrollManager::new();
        let nan_rect = LogicalRect::new(pos(f32::NAN, f32::NAN), sz(f32::NAN, f32::NAN));
        // Every NaN comparison is false → no expansion, no scrollable axis.
        assert_eq!(m.check_reinvoke(DOM, n(1), &sm, nan_rect), None);
        let info = m.get_all_virtual_view_infos();
        assert_eq!(info.len(), 1);
        assert!(info[0].last_bounds_x.is_nan());
        assert!(info[0].last_bounds_width.is_nan());
        assert!(info[0].last_bounds_height.is_nan());
    }
    #[test]
    fn check_reinvoke_with_infinite_bounds_reports_bounds_expanded() {
        let mut m = ready_view(sz(100.0, 100.0));
        let sm = ScrollManager::new();
        assert_eq!(
            m.check_reinvoke(DOM, n(1), &sm, rect(f32::INFINITY, f32::INFINITY)),
            Some(VirtualViewCallbackReason::BoundsExpanded)
        );
    }
    // ------------------------------- check_reinvoke_condition (private, numeric)
    #[test]
    fn edge_threshold_is_exactly_200_px_and_inclusive() {
        assert_eq!(EDGE_THRESHOLD, 200.0);
        // Every distance below is measured between the VISIBLE window
        // (`[offset, offset + container]`) and the MATERIALIZED window's edge —
        // never the document's. The document only decides whether an edge is
        // allowed to fire at all (is there anything left to load past it?).
        let mut s = invoked_state(sz(100.0, 1000.0)); // window y 1000..2000 of a 0..3000 doc
        let container = sz(100.0, 100.0);
        // Bottom edge: mat_max_y - vis_max_y == 2000 - 1800 == 200 → inclusive hit.
        assert_eq!(
            s.check_reinvoke_condition(vpos(700.0), container),
            Some(VirtualViewCallbackReason::EdgeScrolled(EdgeType::Bottom))
        );
        // One px further from the bottom (201) and not near the top → quiet.
        assert_eq!(s.check_reinvoke_condition(vpos(699.0), container), None);
        // Top edge: vis_min_y - mat_min_y == 1200 - 1000 == 200 → inclusive hit.
        assert_eq!(
            s.check_reinvoke_condition(vpos(200.0), container),
            Some(VirtualViewCallbackReason::EdgeScrolled(EdgeType::Top))
        );
        // Just past it, and still 699 px from the bottom → quiet.
        assert_eq!(s.check_reinvoke_condition(vpos(201.0), container), None);
        // The horizontal axis uses the identical threshold with identical
        // inclusivity. It needs the both-axes fixture: on `invoked_state` the
        // window spans the document's full width, so left/right have nothing to
        // load and correctly never fire whatever the distance.
        let mut s2 = invoked_state_2d(sz(1000.0, 1000.0)); // window 1000..2000 of a 0..3000 doc,
                                                           // both axes
                                                           // y is parked dead centre of the window
                                                           // (450 px from either vertical
                                                           // edge) so that only the x axis can
                                                           // speak.
        let quiet_y = FIXTURE_WINDOW_ORIGIN_Y + 450.0;
        // Left edge: vis_min_x - mat_min_x == 1200 - 1000 == 200 → inclusive hit.
        assert_eq!(
            s2.check_reinvoke_condition(pos(1200.0, quiet_y), container),
            Some(VirtualViewCallbackReason::EdgeScrolled(EdgeType::Left))
        );
        assert_eq!(
            s2.check_reinvoke_condition(pos(1201.0, quiet_y), container),
            None
        );
        // Right edge: mat_max_x - vis_max_x == 2000 - 1800 == 200 → inclusive hit.
        assert_eq!(
            s2.check_reinvoke_condition(pos(1700.0, quiet_y), container),
            Some(VirtualViewCallbackReason::EdgeScrolled(EdgeType::Right))
        );
        assert_eq!(
            s2.check_reinvoke_condition(pos(1699.0, quiet_y), container),
            None
        );
    }
    #[test]
    fn edge_priority_is_bottom_right_top_left_and_one_answer_per_window() {
        // Several edges have to be near AT ONCE for priority to mean anything,
        // which takes a small viewport inside a window that has document past
        // it on all four sides — hence the both-axes fixture. A 900 px viewport
        // sitting on the top-left corner of the 1000x1000 window is 0 px from
        // its top and left edges and 100 px from its bottom and right ones, so
        // all four are inside EDGE_THRESHOLD simultaneously.
        let mut s = invoked_state_2d(sz(1000.0, 1000.0));
        let container = sz(900.0, 900.0);
        let offset = pos(FIXTURE_WINDOW_ORIGIN_X, FIXTURE_WINDOW_ORIGIN_Y);
        let doc = s.virtual_rect.expect("fixture sets the document");
        assert_eq!(
            s.check_reinvoke_condition(offset, container),
            Some(VirtualViewCallbackReason::EdgeScrolled(EdgeType::Bottom))
        );
        // The callback answered Bottom and kept the SAME window: it has said
        // what it wants to show for this position, and asking it about the
        // other three edges could only get the same answer — so nothing more
        // fires. (The old per-edge memory "drained" Right, Top, Left here: three
        // full re-materializations for no new content.)
        s.served_scroll_demand = Some(VirtualViewCallbackReason::EdgeScrolled(EdgeType::Bottom));
        assert_eq!(s.check_reinvoke_condition(offset, container), None);
        // Priority becomes observable once the callback answers each edge with
        // a window that takes THAT edge out of reach: the rect change releases
        // the latch, and the next edge in priority order is reported.
        let mut answer = |s: &mut VirtualViewState, mat: LogicalRect| {
            // What `update_virtual_view_info` does for a changed window.
            s.materialized = Some(mat);
            s.served_scroll_demand = None;
        };
        // Grow down: bottom is 600 px away → Right is next.
        answer(
            &mut s,
            LogicalRect::new(pos(1000.0, 1000.0), sz(1000.0, 1500.0)),
        );
        assert_eq!(
            s.check_reinvoke_condition(offset, container),
            Some(VirtualViewCallbackReason::EdgeScrolled(EdgeType::Right))
        );
        s.served_scroll_demand = Some(VirtualViewCallbackReason::EdgeScrolled(EdgeType::Right));
        assert_eq!(s.check_reinvoke_condition(offset, container), None);
        // Grow right → Top.
        answer(
            &mut s,
            LogicalRect::new(pos(1000.0, 1000.0), sz(1500.0, 1500.0)),
        );
        assert_eq!(
            s.check_reinvoke_condition(offset, container),
            Some(VirtualViewCallbackReason::EdgeScrolled(EdgeType::Top))
        );
        // Grow up → Left.
        answer(
            &mut s,
            LogicalRect::new(pos(1000.0, 500.0), sz(1500.0, 2000.0)),
        );
        assert_eq!(
            s.check_reinvoke_condition(offset, container),
            Some(VirtualViewCallbackReason::EdgeScrolled(EdgeType::Left))
        );
        // Grow left: every edge is out of reach → quiet, and the document is
        // the same one throughout.
        answer(
            &mut s,
            LogicalRect::new(pos(500.0, 500.0), sz(2000.0, 2000.0)),
        );
        assert_eq!(s.check_reinvoke_condition(offset, container), None);
        assert_eq!(s.virtual_rect, Some(doc));
    }
    #[test]
    fn check_reinvoke_condition_at_zero_is_quiet() {
        // Zero content, zero container, zero offset: `0 > 0` is false so there
        // is no expansion, and the offset is exactly the resting
        // `initial_scroll_offset`, so no edge may fire either.
        let mut s = invoked_state(sz(0.0, 0.0));
        assert_eq!(
            s.check_reinvoke_condition(pos(0.0, 0.0), sz(0.0, 0.0)),
            None
        );
        // Zero-size content inside a real container *is* an expansion.
        assert_eq!(
            s.check_reinvoke_condition(pos(0.0, 0.0), sz(1.0, 1.0)),
            Some(VirtualViewCallbackReason::BoundsExpanded)
        );
    }
    #[test]
    fn check_reinvoke_condition_handles_nan_offset_and_nan_sizes() {
        let nan = f32::NAN;
        // JUDGEMENT (NaN). The rule is four independent comparisons, and every
        // comparison against NaN is false — so NaN does NOT poison the whole
        // decision, it silences exactly the edges whose arithmetic touches it
        // while the others still answer. Which edges those are depends on where
        // the NaN is, so each case is verified in both directions (the edge
        // that survives fires; the edge that was poisoned stays quiet) rather
        // than asserted wholesale as `None`. Nothing here may panic.
        // (1) NaN materialized/document SIZES. `bottom`/`right` are derived
        // from those sizes → NaN → false. `top`/`left` are derived from ORIGINS
        // only, so they are NaN-free: parked on the window's top edge with
        // 1000 px of document above it, Top still fires.
        let mut s = invoked_state(sz(nan, nan));
        assert_eq!(
            s.check_reinvoke_condition(vpos(0.0), sz(100.0, 100.0)),
            Some(VirtualViewCallbackReason::EdgeScrolled(EdgeType::Top)),
            "the top edge is computed from origins alone, so a NaN size cannot silence it"
        );
        // Far below the top, where only the (NaN-poisoned) bottom edge could
        // have fired — proof that the NaN really did silence it.
        assert_eq!(
            s.check_reinvoke_condition(vpos(5_000.0), sz(100.0, 100.0)),
            None
        );
        // (2) NaN offset AND NaN sizes: nothing is left that can compare true.
        assert_eq!(
            s.check_reinvoke_condition(pos(nan, nan), sz(nan, nan)),
            None
        );
        // (3) NaN CONTAINER size against real content. The container size only
        // enters the bottom/right distances, so Top survives again...
        let mut s = invoked_state(sz(100.0, 1000.0));
        assert_eq!(
            s.check_reinvoke_condition(vpos(0.0), sz(nan, nan)),
            Some(VirtualViewCallbackReason::EdgeScrolled(EdgeType::Top))
        );
        // ...and the bottom edge is the one that goes quiet, at an offset that
        // would otherwise be a dead-on bottom hit.
        assert_eq!(s.check_reinvoke_condition(vpos(900.0), sz(nan, nan)), None);
        // (4) NaN scroll OFFSET. It appears in all four distances, so every
        // edge predicate is false and NO edge fires — verified, not assumed —
        // even though `has_scrolled` is true: NaN quantizes to the dedicated
        // i64::MIN sentinel, which is != the quantized 0 of the resting offset.
        assert_ne!(
            pos(nan, nan),
            LogicalPosition::zero(),
            "a NaN offset counts as 'has scrolled', so the edge block IS entered"
        );
        assert_eq!(
            s.check_reinvoke_condition(pos(nan, nan), sz(100.0, 100.0)),
            None
        );
    }
    #[test]
    fn check_reinvoke_condition_handles_negative_overscroll_offsets() {
        let mut s = invoked_state(sz(100.0, 1000.0));
        let container = sz(100.0, 100.0);
        // Rubber-band overscroll far above the materialized top: the top edge
        // fires, because this window has 1000 px of document above it. (The
        // bottom is ~1e9 px away, and the fixture is not windowed on x.) It is
        // NOT a jump beyond the content: the viewport lies entirely above the
        // document's own top, where there is nothing to materialize.
        assert_eq!(
            s.check_reinvoke_condition(vpos(-1.0e9), container),
            Some(VirtualViewCallbackReason::EdgeScrolled(EdgeType::Top))
        );
        // Whereas a viewport parked at y -1..99 shows document (0..99) that
        // the 1000..2000 window does not cover at all — nothing materialized is
        // on screen, which is the jump-beyond-content case, not an approach.
        assert_eq!(
            s.check_reinvoke_condition(pos(-50.0, -1.0), container),
            Some(VirtualViewCallbackReason::ScrollBeyondContent)
        );
        // JUDGEMENT: a negative offset is not a top-edge event by ITSELF — the
        // sign of the offset is irrelevant, what matters is whether there is
        // document above the materialized window. With the window flush against
        // the document's top there is nothing left to load up there, so the
        // very same rubber-band overscroll is silence, not a reload loop
        // (which is precisely what a rubber-band bounce would otherwise cause,
        // once per frame, for the whole duration of the bounce).
        let mut at_doc_top = windowed_state(
            LogicalRect::new(LogicalPosition::zero(), sz(100.0, 1000.0)),
            LogicalRect::new(LogicalPosition::zero(), sz(100.0, 3000.0)),
        );
        assert_eq!(
            at_doc_top.check_reinvoke_condition(pos(0.0, -1.0e9), container),
            None
        );
        assert_eq!(
            at_doc_top.check_reinvoke_condition(pos(-1.0e9, -1.0e9), container),
            None
        );
        // Overscrolled past the bottom: still the bottom edge, no panic.
        assert_eq!(
            s.check_reinvoke_condition(vpos(1.0e9), container),
            Some(VirtualViewCallbackReason::EdgeScrolled(EdgeType::Bottom))
        );
        // JUDGEMENT: negative container/content sizes are nonsense input — an
        // INVERTED rect, whose max lies below its min. The rule stays total on
        // them (no panic, no NaN, a deterministic answer), but it cannot be
        // *meaningful*, and pinning `None` here would pretend the inversion is
        // detected when it is not. Totality is the guarantee; the particular
        // answer is an artefact of the garbage, recorded so a future change to
        // it is noticed rather than silently absorbed.
        //
        // What actually happens with the inverted rects: the visible window
        // (1000 down to 800) starts at or past the inverted materialized
        // "bottom" (y=900) and short of the inverted document's (y=1900), which
        // reads as "scrolled beyond the materialized content".
        let mut s = invoked_state(sz(-100.0, -100.0));
        assert_eq!(
            s.check_reinvoke_condition(vpos(0.0), sz(-200.0, -200.0)),
            Some(VirtualViewCallbackReason::ScrollBeyondContent)
        );
    }
    #[test]
    fn check_reinvoke_condition_saturates_at_f32_extremes() {
        // JUDGEMENT (extremes): the requirement is that saturated arithmetic
        // neither panics NOR invents an edge. The second half is the subtle
        // one — at f32::MAX the distances collapse to 0 and *look* like a
        // permanent edge hit, and the only thing standing between that and an
        // infinite re-materialize loop is the "does the document actually
        // extend past this edge?" guard. Both halves are pinned below.
        // A MAX-sized window inside a document that saturates to the SAME MAX
        // (`1000 + MAX == MAX` and `2000 + MAX == MAX` in f32): the window
        // covers everything there is. Scrolled to the far end the bottom
        // distance is MAX - MAX == 0 — inside the threshold — yet nothing
        // fires, because there is nothing beyond the window to load.
        let mut s = invoked_state(sz(f32::MAX, f32::MAX));
        assert_eq!(
            s.check_reinvoke_condition(pos(f32::MAX, f32::MAX), sz(0.0, 0.0)),
            None,
            "a saturated distance of 0 must not fake an edge when the window covers the document"
        );
        // The mirrored extreme: -MAX is far above the window, and the 1000 px
        // of document above it survive saturation (the window's ORIGIN is
        // still 1000, the document's still 0), so the top edge does fire.
        assert_eq!(
            s.check_reinvoke_condition(pos(-f32::MAX, -f32::MAX), sz(0.0, 0.0)),
            Some(VirtualViewCallbackReason::EdgeScrolled(EdgeType::Top))
        );
        // MAX container over MAX content: `>` is strict, so nothing grew and
        // there is no BoundsExpanded — but the viewport starts exactly on the
        // window's top edge, which has document above it, so this is a Top.
        assert_eq!(
            s.check_reinvoke_condition(vpos(0.0), sz(f32::MAX, f32::MAX)),
            Some(VirtualViewCallbackReason::EdgeScrolled(EdgeType::Top))
        );
        // Saturation where the document genuinely DOES extend past the window:
        // a MAX/2 window at the document's origin inside a MAX-tall document.
        // The bottom distance underflows to -MAX/2 (the viewport is absurdly
        // far past the window) — still <= EDGE_THRESHOLD, so the bottom edge
        // reports instead of overflowing.
        let mut huge = windowed_state(
            LogicalRect::new(LogicalPosition::zero(), sz(f32::MAX / 2.0, f32::MAX / 2.0)),
            LogicalRect::new(LogicalPosition::zero(), sz(f32::MAX, f32::MAX)),
        );
        assert_eq!(
            huge.check_reinvoke_condition(pos(f32::MAX, f32::MAX), sz(0.0, 0.0)),
            Some(VirtualViewCallbackReason::EdgeScrolled(EdgeType::Bottom))
        );
        // Mirrored: that window is flush with the document's top, so an -MAX
        // offset has nothing to load above it. The bottom distance
        // (MAX/2 + MAX) overflows to +inf, which reads as "very far away" —
        // not as a panic and not as an edge.
        assert_eq!(
            huge.check_reinvoke_condition(pos(-f32::MAX, -f32::MAX), sz(0.0, 0.0)),
            None
        );
        // Infinite container over finite content is an expansion...
        let mut s = invoked_state(sz(100.0, 100.0));
        assert_eq!(
            s.check_reinvoke_condition(vpos(0.0), sz(f32::INFINITY, f32::INFINITY)),
            Some(VirtualViewCallbackReason::BoundsExpanded)
        );
        // ...and once that expansion has been served, an infinite viewport is
        // "at" every edge at once (`mat_max - inf == -inf <= 200`), so priority
        // answers Bottom. Absurd, but deterministic and bounded: the document
        // guard is still the thing that decides, as the next case shows.
        let mut s = invoked_state(sz(100.0, 100.0));
        s.invoked_for_current_expansion = true;
        assert_eq!(
            s.check_reinvoke_condition(vpos(0.0), sz(f32::INFINITY, f32::INFINITY)),
            Some(VirtualViewCallbackReason::EdgeScrolled(EdgeType::Bottom))
        );
        // Same infinite viewport over a FULLY materialized view: every edge is
        // 0/-inf away and every one of them is vetoed, so it goes quiet instead
        // of looping.
        let mut covered = windowed_state(
            LogicalRect::new(LogicalPosition::zero(), sz(100.0, 100.0)),
            LogicalRect::new(LogicalPosition::zero(), sz(100.0, 100.0)),
        );
        covered.invoked_for_current_expansion = true;
        assert_eq!(
            covered.check_reinvoke_condition(pos(0.0, 50.0), sz(f32::INFINITY, f32::INFINITY)),
            None
        );
    }
    #[test]
    fn check_reinvoke_condition_needs_a_real_scroll_before_any_edge_fires() {
        // The view is re-invoked while the user sits at the BOTTOM of the
        // materialized window, so the resting position is itself an edge: the
        // bottom distance is 0 from the very first check. Only movement away
        // from that resting position may fire.
        let mut s = invoked_state(sz(100.0, 1000.0));
        s.initial_scroll_offset = vpos(900.0);
        let container = sz(100.0, 100.0);
        // Parked exactly where it started (on the window's bottom edge, with
        // 1000 px of document still below it): not a scroll-to-edge.
        assert_eq!(s.check_reinvoke_condition(vpos(900.0), container), None);
        // One pixel of real movement, still within the bottom threshold → fires.
        assert_eq!(
            s.check_reinvoke_condition(vpos(899.0), container),
            Some(VirtualViewCallbackReason::EdgeScrolled(EdgeType::Bottom))
        );
        // Already answered for this window → quiet while the same demand stands.
        s.served_scroll_demand = Some(VirtualViewCallbackReason::EdgeScrolled(EdgeType::Bottom));
        assert_eq!(s.check_reinvoke_condition(vpos(899.0), container), None);
    }
    #[test]
    fn a_window_scrolled_in_the_middle_of_a_document_rematerializes_at_its_edges() {
        // THE REGRESSION THIS RULE EXISTS FOR. The old rule compared a
        // VIRTUAL-space scroll offset against the MATERIALIZED window's SIZE —
        // two different coordinate spaces — so an edge could only ever fire at
        // the absolute top or bottom of the DOCUMENT, and a view parked in the
        // middle of one never asked for more content: a VirtualView could not
        // scroll. The edges that matter belong to the materialized WINDOW, and
        // they sit in the middle of the document, which is exactly what this
        // pins.
        let mut s = invoked_state(sz(100.0, 1000.0)); // window y 1000..2000 of a 0..3000 doc
        let container = sz(100.0, 100.0);
        // Dead centre of the window — 450 px from either of its edges, and also
        // the middle of the document: nothing to do.
        assert_eq!(s.check_reinvoke_condition(vpos(450.0), container), None);
        // On the window's BOTTOM edge while still 1100 px short of the
        // document's end. Under the old rule this was "not at the bottom" and
        // nothing loaded; the view could never grow downwards.
        assert_eq!(
            s.check_reinvoke_condition(vpos(900.0), container),
            Some(VirtualViewCallbackReason::EdgeScrolled(EdgeType::Bottom))
        );
        // Symmetrically on the window's TOP edge, 1000 px INTO the document
        // rather than at its start — the case a paginated document hits every
        // time the user scrolls back up.
        assert_eq!(
            s.check_reinvoke_condition(vpos(0.0), container),
            Some(VirtualViewCallbackReason::EdgeScrolled(EdgeType::Top))
        );
    }
    #[test]
    fn no_edge_fires_when_the_materialized_window_already_covers_that_side() {
        // The other half of the rule: proximity alone is not enough, the
        // document has to actually extend past that edge. Without this the
        // ends of every document would re-materialize once per frame forever.
        let container = sz(100.0, 100.0);
        // Window flush against the document's TOP: its top edge has nothing
        // behind it, so resting on it is silence — while the BOTTOM edge of the
        // very same window, which does have document behind it, still fires.
        // (Same fixture, same offsets: the only difference is which side has
        // content left.)
        let mut head = windowed_state(
            LogicalRect::new(LogicalPosition::zero(), sz(100.0, 1000.0)),
            LogicalRect::new(LogicalPosition::zero(), sz(100.0, 3000.0)),
        );
        assert_eq!(
            head.check_reinvoke_condition(pos(0.0, 1.0), container),
            None
        );
        assert_eq!(
            head.check_reinvoke_condition(pos(0.0, 900.0), container),
            Some(VirtualViewCallbackReason::EdgeScrolled(EdgeType::Bottom))
        );
        // Fully materialized (window == document): nothing is left to load on
        // ANY side, so no offset can produce an edge — including the corners,
        // where all four distances are 0 and every edge "looks" reachable.
        let mut whole = windowed_state(
            LogicalRect::new(LogicalPosition::zero(), sz(1000.0, 1000.0)),
            LogicalRect::new(LogicalPosition::zero(), sz(1000.0, 1000.0)),
        );
        for offset in [
            pos(0.0, 1.0),
            pos(1.0, 0.0),
            pos(900.0, 900.0),
            pos(500.0, 500.0),
        ] {
            assert_eq!(
                whole.check_reinvoke_condition(offset, container),
                None,
                "fully materialized: nothing to load at {offset:?}"
            );
        }
    }
    // ------------------------------------------------------- getters / predicates
    #[test]
    fn debug_counts_matches_the_number_of_tracked_views() {
        let mut m = VirtualViewManager::new();
        assert_eq!(m.debug_counts(), 0);
        for i in 0..10_usize {
            m.get_or_create_nested_dom_id(DOM, n(i));
            assert_eq!(m.debug_counts(), i + 1);
        }
        // Re-registering the same keys must not grow the map.
        for i in 0..10_usize {
            m.get_or_create_nested_dom_id(DOM, n(i));
        }
        assert_eq!(m.debug_counts(), 10);
        assert_eq!(m.debug_counts(), m.all_view_keys().len());
        assert_eq!(m.debug_counts(), m.get_all_virtual_view_infos().len());
    }
    #[test]
    fn was_virtual_view_invoked_is_false_until_marked() {
        let mut m = VirtualViewManager::new();
        assert!(!m.was_virtual_view_invoked(DOM, n(1)));
        m.get_or_create_nested_dom_id(DOM, n(1));
        assert!(
            !m.was_virtual_view_invoked(DOM, n(1)),
            "registration alone is not an invocation"
        );
        mark(&mut m, DOM, n(1), VirtualViewCallbackReason::InitialRender);
        assert!(m.was_virtual_view_invoked(DOM, n(1)));
        // A sibling node must not inherit the flag.
        assert!(!m.was_virtual_view_invoked(DOM, n(2)));
        assert!(!m.was_virtual_view_invoked(DOM1, n(1)));
        assert_eq!(m.force_reinvoke(DOM, n(1)), Some(()));
        assert!(!m.was_virtual_view_invoked(DOM, n(1)));
    }
    #[test]
    fn get_all_virtual_view_infos_reports_every_field() {
        let m = VirtualViewManager::new();
        assert!(m.get_all_virtual_view_infos().is_empty());
        let mut m = VirtualViewManager::new();
        let nested = m.get_or_create_nested_dom_id(DOM1, n(5));
        // Before any callback: sizes are None, not 0.0.
        let info = m.get_all_virtual_view_infos();
        assert_eq!(info.len(), 1);
        assert_eq!(info[0].parent_dom_id, 1);
        assert_eq!(info[0].parent_node_id, 5);
        assert_eq!(info[0].nested_dom_id, nested.inner);
        assert!(info[0].scroll_size_width.is_none());
        assert!(info[0].scroll_size_height.is_none());
        assert!(info[0].virtual_scroll_size_width.is_none());
        assert!(info[0].virtual_scroll_size_height.is_none());
        assert!(!info[0].was_invoked);
        assert_eq!(info[0].last_bounds_x, 0.0);
        assert_eq!(info[0].last_bounds_y, 0.0);
        assert_eq!(info[0].last_bounds_width, 0.0);
        assert_eq!(info[0].last_bounds_height, 0.0);
        set_sizes(&mut m, DOM1, n(5), sz(3.0, 4.0), sz(5.0, 6.0));
        mark(&mut m, DOM1, n(5), VirtualViewCallbackReason::InitialRender);
        let info = m.get_all_virtual_view_infos();
        assert_eq!(info[0].scroll_size_width, Some(3.0));
        assert_eq!(info[0].scroll_size_height, Some(4.0));
        assert_eq!(info[0].virtual_scroll_size_width, Some(5.0));
        assert_eq!(info[0].virtual_scroll_size_height, Some(6.0));
        assert!(info[0].was_invoked);
        // Infos are emitted in the same (sorted) order as all_view_keys.
        m.get_or_create_nested_dom_id(DOM, n(9));
        let keys = m.all_view_keys();
        let infos = m.get_all_virtual_view_infos();
        assert_eq!(keys.len(), infos.len());
        for (k, i) in keys.iter().zip(infos.iter()) {
            assert_eq!(k.0.inner, i.parent_dom_id);
            assert_eq!(k.1.index(), i.parent_node_id);
        }
    }
    #[test]
    fn a_view_reports_the_size_it_materialized() {
        let mut m = VirtualViewManager::new();
        let (dom, node) = (DomId::ROOT_ID, NodeId::new(3));
        m.get_or_create_nested_dom_id(dom, node);
        assert!(
            m.materialized_sizes().is_empty(),
            "before the callback has run there is nothing to size from - a view is sized from the \
             OUTSIDE first, which is the only order that terminates"
        );
        m.update_virtual_view_info(
            dom,
            node,
            LogicalPosition::zero(),
            LogicalSize::new(16.0, 16.0),
            LogicalSize::new(16.0, 16.0),
        );
        assert_eq!(
            m.materialized_sizes().get(&(dom, node)).copied(),
            Some(LogicalSize::new(16.0, 16.0)),
            "afterwards the view is as big as what it returned"
        );
    }
    /// Only views that ACTUALLY materialized report a size: a registered but
    /// never-invoked view must not claim 0x0, which would collapse its box.
    #[test]
    fn a_registered_but_unrendered_view_reports_nothing() {
        let mut m = VirtualViewManager::new();
        m.get_or_create_nested_dom_id(DomId::ROOT_ID, NodeId::new(1));
        m.get_or_create_nested_dom_id(DomId::ROOT_ID, NodeId::new(2));
        m.update_virtual_view_info(
            DomId::ROOT_ID,
            NodeId::new(2),
            LogicalPosition::zero(),
            LogicalSize::new(8.0, 8.0),
            LogicalSize::new(8.0, 8.0),
        );
        let sizes = m.materialized_sizes();
        assert_eq!(sizes.len(), 1, "only the one that rendered: {sizes:?}");
        assert!(sizes.contains_key(&(DomId::ROOT_ID, NodeId::new(2))));
    }
}