1
//! Publishing layout's scroll containers into the [`ScrollManager`].
2
//!
3
//! This ran in the dll, AFTER `layout_and_generate_display_list` returned, and
4
//! the e2e runner carried a hand-maintained port of it. So there were three
5
//! answers to "which nodes are scrollable and how big are they": the dll's,
6
//! the runner's, and whatever each `layout/tests` test hand-seeded — and a
7
//! test could seed a scroll state production would never produce, which is
8
//! exactly the divergence that let scroll bugs hide from the suite.
9
//!
10
//! It lives here now and all three call it.
11

            
12
use azul_core::{
13
    dom::{DomId, DomNodeId, NodeId},
14
    task::Instant,
15
};
16

            
17
use crate::{solver3::layout_tree::LayoutNodeId, window::LayoutWindow};
18

            
19
/// Extra scrollable width given to the box that hosts the active caret.
20
///
21
/// THE CARET IS CONTENT, and `overflow_content_size` does not know that: it
22
/// measures the TEXT. At the end of the value the caret stands ON the text's
23
/// right edge, so its own width lies past it — and in a horizontally scrolled
24
/// field that edge is exactly `max_scroll_x`, which the reveal cannot scroll
25
/// beyond by definition. The blinking caret was therefore clipped away
26
/// precisely when the field started to overflow, which is the one moment it
27
/// matters. Trailing padding cannot fix this either: azul measures content
28
/// against the PADDING box, so padding shrinks the scroll range rather than
29
/// extending it.
30
///
31
/// Sized as the default caret width (1px) plus the 5px the caret reveal
32
/// (`calculate_instant_scroll_delta`) asks for, so the margin the reveal wants
33
/// is actually reachable instead of being clamped away at the end of the line.
34
pub const CARET_SCROLL_GUTTER_PX: f32 = 6.0;
35

            
36
7904
pub fn register_scroll_nodes(layout_window: &mut LayoutWindow, now: &Instant) {
37
    // Which node owns the active caret. Snapshotted BEFORE the loop below takes
38
    // `layout_results` mutably.
39
7904
    let caret_node: Option<DomNodeId> = layout_window
40
7904
        .text_edit_manager
41
7904
        .multi_cursor
42
7904
        .as_ref()
43
7904
        .map(|mc| mc.node_id);
44

            
45
16380
    for (dom_id, layout_result) in &mut layout_window.layout_results {
46
        // ONLY the node the caret sits on — which in a TextInput is the value
47
        // <p>, i.e. both the IFC root and the horizontal scroll box.
48
        //
49
        // This deliberately does NOT walk the ancestor chain. Widening every
50
        // ancestor by the gutter makes the BODY 6px wider than the viewport, so
51
        // the whole page turns horizontally scrollable the moment a text field
52
        // takes focus — which is a far worse bug than the one being fixed.
53
8476
        let caret_host: Option<NodeId> = caret_node
54
8476
            .filter(|c| c.dom == *dom_id)
55
8476
            .and_then(|c| c.node.into_crate_internal());
56
        // What the VirtualView callbacks published for this DOM, read the way
57
        // `display_list::paint_scrollbars` reads it — same producer, same
58
        // `children_rect.size`, so the two cannot disagree about the extent.
59
        // Snapshotted before the registration loop mutates the manager;
60
        // registration never touches `virtual_scroll_size`.
61
8476
        let scroll_states = layout_window
62
8476
            .scroll_manager
63
8476
            .get_scroll_states_for_dom(*dom_id);
64

            
65
254779
        for node_idx in 0..layout_result.layout_tree.nodes.len() {
66
254779
            let node = &layout_result.layout_tree.nodes[node_idx];
67
254779
            let Some(dom_node_id) = node.dom_node_id else {
68
133
                continue;
69
            };
70

            
71
            // THE SCROLLPORT IS THE PADDING BOX. CSS Overflow 3 §2: "scrolling
72
            // occurs within the padding box", and `compute_scrollbar_geometry`
73
            // agrees (inner_rect = paint_rect − borders).
74
            //
75
            // ORIGIN AND SIZE now both come off that one box. This used to
76
            // publish a padding-box SIZE with a border-box ORIGIN — a
77
            // rectangle that is not any CSS box — so every consumer reading
78
            // `container_rect` as a whole (the auto-scroll timer's edge tests,
79
            // `scroll_into_view`'s `visible_rect`) was off by the border width
80
            // on the top and left, in window space, against a raw pointer.
81
            //
82
            // Coordinates are STATIC (unscrolled) layout, NOT window — the
83
            // comment here once claimed otherwise. `scroll_selection_into_view`
84
            // depends on static, because it compares against a caret rect
85
            // measured the same way. A consumer that needs where the container
86
            // APPEARS must subtract the scroll of its ANCESTORS itself.
87
254646
            let border_box_origin = layout_result
88
254646
                .calculated_positions
89
254646
                .get(node_idx)
90
254646
                .copied()
91
254646
                .unwrap_or_else(azul_core::geom::LogicalPosition::zero);
92
254646
            let scrollport =
93
254646
                crate::solver3::display_list::BorderBoxRect(azul_core::geom::LogicalRect {
94
254646
                    origin: border_box_origin,
95
254646
                    size: node.used_size.unwrap_or_default(),
96
254646
                })
97
254646
                .to_padding_box(&node.box_props.unpack().border)
98
254646
                .rect();
99
254646
            let container_size = azul_core::geom::LogicalSize {
100
254646
                width: scrollport.size.width.max(0.0),
101
254646
                height: scrollport.size.height.max(0.0),
102
254646
            };
103
254646
            let container_origin = scrollport.origin;
104

            
105
254646
            let Some(mut scrollbar_info) = layout_result
106
254646
                .layout_tree
107
254646
                .warm(LayoutNodeId::new(node_idx))
108
254646
                .and_then(|w| w.scrollbar_info)
109
            else {
110
87149
                continue;
111
            };
112

            
113
            // A VirtualView's scrollable extent does not exist during layout —
114
            // it is whatever its callback just published. Amend the flags here,
115
            // where both the tree and the ScrollManager are in reach, and store
116
            // the answer back so every later consumer (the GPU thumb updater
117
            // `update_scrollbar_transforms`, the registration below, the
118
            // hit-test scrollbar states) reads one decision rather than
119
            // re-deriving it. `paint_scrollbars` calls the same function while
120
            // building the display list, which is why the numbers agree.
121
167497
            if let Some(pos) = scroll_states.get(&dom_node_id) {
122
947
                let raised = crate::solver3::cache::apply_virtual_scroll_necessity(
123
947
                    &layout_result.styled_dom,
124
947
                    dom_node_id,
125
947
                    pos.children_rect.size,
126
947
                    container_size,
127
947
                    &mut scrollbar_info,
128
                );
129
947
                if raised {
130
140
                    if let Some(warm) = layout_result
131
140
                        .layout_tree
132
140
                        .warm_mut(LayoutNodeId::new(node_idx))
133
140
                    {
134
140
                        warm.scrollbar_info = Some(scrollbar_info);
135
140
                    }
136
807
                }
137
166550
            }
138

            
139
            // The same amendment for ORDINARY nodes: a text edit can grow an
140
            // IFC's content after layout (`reshape_text_node` refreshes
141
            // `overflow_content_size` but nothing re-runs Phase 3), so re-derive
142
            // the necessity from the CURRENT content size. For nodes whose
143
            // content is unchanged since layout this reads the same
144
            // `overflow_content_size` Phase 3 wrote and is a no-op; flags are
145
            // only ever raised, never lowered.
146
            {
147
167497
                let content_now = layout_result
148
167497
                    .layout_tree
149
167497
                    .get_content_size(LayoutNodeId::new(node_idx));
150
167497
                let raised = crate::solver3::cache::apply_content_scroll_necessity(
151
167497
                    &layout_result.styled_dom,
152
167497
                    dom_node_id,
153
167497
                    content_now,
154
167497
                    container_size,
155
167497
                    &mut scrollbar_info,
156
                );
157
167497
                if raised {
158
40
                    if let Some(warm) = layout_result
159
40
                        .layout_tree
160
40
                        .warm_mut(LayoutNodeId::new(node_idx))
161
40
                    {
162
40
                        warm.scrollbar_info = Some(scrollbar_info);
163
40
                    }
164
167457
                }
165
            }
166

            
167
167497
            if !(scrollbar_info.needs_vertical || scrollbar_info.needs_horizontal) {
168
166559
                continue;
169
938
            }
170

            
171
938
            let container_rect = azul_core::geom::LogicalRect {
172
938
                origin: container_origin,
173
938
                size: container_size,
174
938
            };
175

            
176
            // `overscroll-behavior` from CSS, resolved per axis. Until this
177
            // was wired the two fields were hardcoded to `Auto` at every
178
            // construction site, so `contain` (stop scroll CHAINING to an
179
            // ancestor) and `none` (also suppress the local bounce) were
180
            // unreachable — the enum and every physics branch reading it had
181
            // existed all along with nothing to set them.
182
938
            let node_state = layout_result
183
938
                .styled_dom
184
938
                .styled_nodes
185
938
                .as_container()
186
938
                .get(dom_node_id)
187
938
                .map(|n| n.styled_node_state)
188
938
                .unwrap_or_default();
189
938
            let overscroll_x = match crate::solver3::getters::get_overscroll_behavior_x(
190
938
                &layout_result.styled_dom,
191
938
                dom_node_id,
192
938
                &node_state,
193
938
            ) {
194
                crate::solver3::getters::MultiValue::Exact(v) => v,
195
938
                _ => azul_css::props::style::scrollbar::OverscrollBehavior::Auto,
196
            };
197
938
            let overscroll_y = match crate::solver3::getters::get_overscroll_behavior_y(
198
938
                &layout_result.styled_dom,
199
938
                dom_node_id,
200
938
                &node_state,
201
938
            ) {
202
                crate::solver3::getters::MultiValue::Exact(v) => v,
203
938
                _ => azul_css::props::style::scrollbar::OverscrollBehavior::Auto,
204
            };
205

            
206
938
            let mut content_size = layout_result
207
938
                .layout_tree
208
938
                .get_content_size(LayoutNodeId::new(node_idx));
209
            // See [`CARET_SCROLL_GUTTER_PX`]: the caret is content the text
210
            // extent does not account for, so without this the reveal has
211
            // nowhere to scroll to and the caret is clipped at the end of an
212
            // overflowing line.
213
938
            if caret_host == Some(dom_node_id) {
214
174
                content_size.width += CARET_SCROLL_GUTTER_PX;
215
764
            }
216

            
217
            // Use the layout-computed scrollbar width, not the
218
            // hardcoded default. On macOS with overlay scrollbars,
219
            // scrollbar_width is 0.0 (no layout space reserved).
220
            // The scroll_manager falls back to DEFAULT_SCROLLBAR_WIDTH_PX
221
            // when thickness is 0 for hit-test geometry.
222
938
            let scrollbar_thickness = scrollbar_info
223
938
                .scrollbar_width
224
938
                .max(scrollbar_info.scrollbar_height);
225

            
226
938
            layout_window.scroll_manager.set_overscroll_behavior(
227
938
                *dom_id,
228
938
                dom_node_id,
229
938
                overscroll_x,
230
938
                overscroll_y,
231
            );
232
938
            layout_window.scroll_manager.register_or_update_scroll_node(
233
938
                *dom_id,
234
938
                dom_node_id,
235
938
                container_rect,
236
938
                content_size,
237
938
                now.clone(),
238
938
                scrollbar_thickness,
239
938
                scrollbar_info.visual_width_px,
240
938
                scrollbar_info.needs_horizontal,
241
938
                scrollbar_info.needs_vertical,
242
            );
243
        }
244
    }
245
7904
    layout_window.scroll_manager.calculate_scrollbar_states();
246
7904
}