Skip to main content

strata_layout/
positioning.rs

1//! CSS Positioning Scheme Engine (Relative, Absolute, Fixed, Sticky).
2//!
3//! Implements W3C CSS 2.1 Section 10.1 (Containing Block), Section 10.3.7 & 10.6.4
4//! (Absolute Positioning Equations, Double-Anchored Inset Stretching, Auto-Margin Centering),
5//! and CSS Positioned Layout Level 3 Section 6 (Sticky Constraints).
6
7use crate::box_model::{
8    clamp_dimension, clamp_dimension_with_node, resolve_dimension, resolve_dimension_with_node,
9};
10use crate::geometry::{LayoutGeometry, Rect};
11use crate::tree::LayoutNode;
12use strata_style::types::{
13    AlignItems, AlignSelf, Dimension, Display, FlexDirection, JustifyContent, Position,
14};
15
16/// Active containing block context propagated down the layout hierarchy.
17#[derive(Debug, Clone, Copy, PartialEq)]
18pub struct ContainingBlock {
19    /// The padding box of the containing block in global coordinates.
20    pub padding_box: Rect,
21    /// The viewport bounds (for `position: fixed`, overridden by `transform`/`filter` ancestors).
22    pub viewport: Rect,
23    /// The root initial containing block viewport (for Top-Layer `<Portal>` elements).
24    pub initial_viewport: Rect,
25    /// Scroll offset of the viewport or scroll container (scroll_x, scroll_y).
26    pub scroll_offset: (f32, f32),
27}
28
29impl ContainingBlock {
30    /// Creates a containing block from a padding box and viewport.
31    pub const fn new(padding_box: Rect, viewport: Rect) -> Self {
32        Self {
33            padding_box,
34            viewport,
35            initial_viewport: viewport,
36            scroll_offset: (0.0, 0.0),
37        }
38    }
39
40    /// Creates an initial containing block matching the viewport.
41    pub const fn from_viewport(viewport_width: f32, viewport_height: f32) -> Self {
42        let vp = Rect::new(0.0, 0.0, viewport_width, viewport_height);
43        Self {
44            padding_box: vp,
45            viewport: vp,
46            initial_viewport: vp,
47            scroll_offset: (0.0, 0.0),
48        }
49    }
50
51    /// Creates a containing block with an explicit scroll offset.
52    pub const fn with_scroll(
53        padding_box: Rect,
54        viewport: Rect,
55        scroll_x: f32,
56        scroll_y: f32,
57    ) -> Self {
58        Self {
59            padding_box,
60            viewport,
61            initial_viewport: viewport,
62            scroll_offset: (scroll_x, scroll_y),
63        }
64    }
65}
66
67/// Resolves out-of-flow and relative positioning offsets for a container's children
68/// and recursively propagates containing blocks down through all descendants.
69pub fn solve_positioning(container: &mut LayoutNode, viewport_width: f32, viewport_height: f32) {
70    solve_positioning_guarded(
71        container,
72        viewport_width,
73        viewport_height,
74        crate::tree::LayoutDepthGuard::root(),
75    );
76}
77
78/// Resolves positioning with an active depth guard to prevent call-stack exhaustion.
79pub fn solve_positioning_guarded(
80    container: &mut LayoutNode,
81    viewport_width: f32,
82    viewport_height: f32,
83    guard: crate::tree::LayoutDepthGuard,
84) {
85    let padding_box = if container.geometry.padding_box.width > 0.0
86        || container.geometry.padding_box.height > 0.0
87    {
88        container.geometry.padding_box
89    } else {
90        Rect::new(0.0, 0.0, viewport_width, viewport_height)
91    };
92
93    let initial_cb = ContainingBlock::new(
94        padding_box,
95        Rect::new(0.0, 0.0, viewport_width, viewport_height),
96    );
97
98    solve_positioning_children_guarded(container, initial_cb, guard);
99}
100
101/// Repositions absolutely positioned flex children that rely on the static
102/// position on one or both axes.
103fn apply_flex_static_positions(container: &mut LayoutNode) {
104    let content = container.geometry.content_box;
105    if content.width <= 0.0 && content.height <= 0.0 {
106        return;
107    }
108    let is_row = matches!(
109        container.style.flex_direction,
110        FlexDirection::Row | FlexDirection::RowReverse
111    );
112
113    for child in &mut container.children {
114        if child.style.position != Position::Absolute {
115            continue;
116        }
117        let left_auto = matches!(child.style.insets.left, Dimension::Auto);
118        let right_auto = matches!(child.style.insets.right, Dimension::Auto);
119        let top_auto = matches!(child.style.insets.top, Dimension::Auto);
120        let bottom_auto = matches!(child.style.insets.bottom, Dimension::Auto);
121        let horiz_static = left_auto && right_auto;
122        let vert_static = top_auto && bottom_auto;
123        if !(horiz_static || vert_static) {
124            continue;
125        }
126
127        let item_w =
128            child.geometry.border_box.width + child.style.margin.left + child.style.margin.right;
129        let item_h =
130            child.geometry.border_box.height + child.style.margin.top + child.style.margin.bottom;
131
132        let main_offset = if is_row {
133            justify_offset(container.style.justify_content, content.width, item_w)
134        } else {
135            justify_offset(container.style.justify_content, content.height, item_h)
136        };
137
138        let effective_align = match child.style.align_self {
139            AlignSelf::Auto => container.style.align_items,
140            AlignSelf::Stretch => AlignItems::Stretch,
141            AlignSelf::FlexStart => AlignItems::FlexStart,
142            AlignSelf::FlexEnd => AlignItems::FlexEnd,
143            AlignSelf::Center => AlignItems::Center,
144            AlignSelf::Baseline => AlignItems::Baseline,
145        };
146        let cross_offset = if is_row {
147            align_offset(effective_align, content.height, item_h)
148        } else {
149            align_offset(effective_align, content.width, item_w)
150        };
151
152        let static_x = if is_row {
153            content.x + main_offset + child.style.margin.left
154        } else {
155            content.x + cross_offset + child.style.margin.left
156        };
157        let static_y = if is_row {
158            content.y + cross_offset + child.style.margin.top
159        } else {
160            content.y + main_offset + child.style.margin.top
161        };
162
163        // W3C CSS Flexbox Level 1 §4.1: static position on an axis applies only
164        // when BOTH insets on that axis are `auto`. Any specified inset (`left`
165        // OR `right`, `top` OR `bottom`) anchors to the containing block.
166        let dx = if horiz_static {
167            static_x - child.geometry.border_box.x
168        } else {
169            0.0
170        };
171        let dy = if vert_static {
172            static_y - child.geometry.border_box.y
173        } else {
174            0.0
175        };
176        if dx != 0.0 || dy != 0.0 {
177            child.translate(dx, dy);
178        }
179    }
180}
181
182/// Resolves a `justify-content` offset for a single flex item.
183fn justify_offset(justify: JustifyContent, available: f32, item: f32) -> f32 {
184    let free = (available - item).max(0.0);
185    match justify {
186        JustifyContent::FlexStart => 0.0,
187        JustifyContent::FlexEnd => free,
188        JustifyContent::Center => free / 2.0,
189        JustifyContent::SpaceBetween => 0.0,
190        JustifyContent::SpaceAround | JustifyContent::SpaceEvenly => free / 2.0,
191    }
192}
193
194/// Resolves a cross-axis `align-items` offset for a single flex item.
195fn align_offset(align: AlignItems, available: f32, item: f32) -> f32 {
196    let free = (available - item).max(0.0);
197    match align {
198        AlignItems::FlexStart | AlignItems::Stretch | AlignItems::Baseline => 0.0,
199        AlignItems::FlexEnd => free,
200        AlignItems::Center => free / 2.0,
201    }
202}
203
204/// Recursively traverses child nodes, resolving positioning and propagating the containing block.
205pub fn solve_positioning_children(container: &mut LayoutNode, cb: ContainingBlock) {
206    solve_positioning_children_guarded(container, cb, crate::tree::LayoutDepthGuard::root());
207}
208
209/// Recursively traverses child nodes with an active depth guard to prevent stack overflow.
210pub fn solve_positioning_children_guarded(
211    container: &mut LayoutNode,
212    cb: ContainingBlock,
213    guard: crate::tree::LayoutDepthGuard,
214) {
215    if guard.is_exhausted() {
216        return;
217    }
218    let child_guard = match guard.enter() {
219        Some(g) => g,
220        None => return,
221    };
222    for child in &mut container.children {
223        match child.style.position {
224            Position::Static => {
225                // Static elements do not establish a containing block unless they have a
226                // transform, filter, or backdrop-filter (CSS Transforms §2 / Filter Effects §5).
227                if !child.children.is_empty() {
228                    let child_cb = if child.style.establishes_fixed_containing_block() {
229                        ContainingBlock {
230                            padding_box: child.geometry.padding_box,
231                            viewport: child.geometry.padding_box,
232                            initial_viewport: cb.initial_viewport,
233                            scroll_offset: cb.scroll_offset,
234                        }
235                    } else {
236                        cb
237                    };
238                    solve_positioning_children_guarded(child, child_cb, child_guard);
239                }
240            }
241            Position::Relative => {
242                // 1. In-flow offset relative to normal position
243                let mut dx = 0.0;
244                let mut dy = 0.0;
245
246                if let Some(top_px) =
247                    resolve_dimension(child.style.insets.top, cb.padding_box.height)
248                {
249                    dy += top_px;
250                } else if let Some(bot_px) =
251                    resolve_dimension(child.style.insets.bottom, cb.padding_box.height)
252                {
253                    dy -= bot_px;
254                }
255
256                if let Some(left_px) =
257                    resolve_dimension(child.style.insets.left, cb.padding_box.width)
258                {
259                    dx += left_px;
260                } else if let Some(right_px) =
261                    resolve_dimension(child.style.insets.right, cb.padding_box.width)
262                {
263                    dx -= right_px;
264                }
265
266                if dx != 0.0 || dy != 0.0 {
267                    child.translate(dx, dy);
268                }
269
270                // 2. Relative elements establish a containing block for absolute descendants
271                let child_vp = if child.style.establishes_fixed_containing_block() {
272                    child.geometry.padding_box
273                } else {
274                    cb.viewport
275                };
276                let child_cb = ContainingBlock {
277                    padding_box: child.geometry.padding_box,
278                    viewport: child_vp,
279                    initial_viewport: cb.initial_viewport,
280                    scroll_offset: cb.scroll_offset,
281                };
282
283                if !child.children.is_empty() {
284                    solve_positioning_children_guarded(child, child_cb, child_guard);
285                }
286            }
287            Position::Absolute => {
288                // Out-of-flow positioning relative to nearest non-static ancestor (cb.padding_box)
289                solve_out_of_flow_box_guarded(
290                    child,
291                    cb.padding_box,
292                    cb.viewport,
293                    false,
294                    child_guard,
295                );
296
297                // Absolute elements establish a containing block for descendants
298                let child_vp = if child.style.establishes_fixed_containing_block() {
299                    child.geometry.padding_box
300                } else {
301                    cb.viewport
302                };
303                let child_cb = ContainingBlock {
304                    padding_box: child.geometry.padding_box,
305                    viewport: child_vp,
306                    initial_viewport: cb.initial_viewport,
307                    scroll_offset: cb.scroll_offset,
308                };
309
310                if !child.children.is_empty() {
311                    solve_positioning_children_guarded(child, child_cb, child_guard);
312                }
313            }
314            Position::Fixed => {
315                // Top-layer `<Portal>` elements always resolve against the root initial viewport,
316                // escaping even `transform` / `filter` / `backdrop-filter` ancestors.
317                let fixed_vp = if child.is_top_layer {
318                    cb.initial_viewport
319                } else {
320                    cb.viewport
321                };
322                solve_out_of_flow_box_guarded(child, fixed_vp, fixed_vp, true, child_guard);
323
324                // Fixed elements establish a containing block for descendants
325                let child_vp = if child.style.establishes_fixed_containing_block() {
326                    child.geometry.padding_box
327                } else {
328                    fixed_vp
329                };
330                let child_cb = ContainingBlock {
331                    padding_box: child.geometry.padding_box,
332                    viewport: child_vp,
333                    initial_viewport: cb.initial_viewport,
334                    scroll_offset: (0.0, 0.0),
335                };
336
337                if !child.children.is_empty() {
338                    solve_positioning_children_guarded(child, child_cb, child_guard);
339                }
340            }
341            Position::Sticky => {
342                // In CSS Positioned Layout Level 3 §6, the containing block for a sticky element
343                // is established by its nearest block container ancestor (`container`), not the
344                // nearest positioned ancestor.
345                let parent_box = if container.geometry.padding_box.width > 0.0
346                    || container.geometry.padding_box.height > 0.0
347                {
348                    container.geometry.padding_box
349                } else {
350                    cb.padding_box
351                };
352                let sticky_cb = ContainingBlock {
353                    padding_box: parent_box,
354                    viewport: cb.viewport,
355                    initial_viewport: cb.initial_viewport,
356                    scroll_offset: cb.scroll_offset,
357                };
358                solve_sticky_box(child, sticky_cb);
359
360                let child_vp = if child.style.establishes_fixed_containing_block() {
361                    child.geometry.padding_box
362                } else {
363                    cb.viewport
364                };
365                let child_cb = ContainingBlock {
366                    padding_box: child.geometry.padding_box,
367                    viewport: child_vp,
368                    initial_viewport: cb.initial_viewport,
369                    scroll_offset: cb.scroll_offset,
370                };
371
372                if !child.children.is_empty() {
373                    solve_positioning_children_guarded(child, child_cb, child_guard);
374                }
375            }
376        }
377    }
378
379    // W3C CSS Flexbox Level 1 §4.1: apply flex static positions to direct
380    // absolutely positioned children whose horizontal or vertical inset pair is `auto`.
381    if matches!(container.style.display, Display::Flex | Display::InlineFlex) {
382        apply_flex_static_positions(container);
383    }
384}
385
386/// Solves out-of-flow geometry for an absolute or fixed element according to W3C CSS 2.1 §10.3.7 & §10.6.4.
387pub fn solve_out_of_flow_box(
388    child: &mut LayoutNode,
389    cb_rect: Rect,
390    viewport: Rect,
391    is_fixed: bool,
392) {
393    solve_out_of_flow_box_guarded(
394        child,
395        cb_rect,
396        viewport,
397        is_fixed,
398        crate::tree::LayoutDepthGuard::root(),
399    );
400}
401
402/// Solves out-of-flow geometry with an active depth guard.
403pub fn solve_out_of_flow_box_guarded(
404    child: &mut LayoutNode,
405    cb_rect: Rect,
406    _viewport: Rect,
407    _is_fixed: bool,
408    guard: crate::tree::LayoutDepthGuard,
409) {
410    let left = resolve_dimension(child.style.insets.left, cb_rect.width);
411    let right = resolve_dimension(child.style.insets.right, cb_rect.width);
412    let top = resolve_dimension(child.style.insets.top, cb_rect.height);
413    let bottom = resolve_dimension(child.style.insets.bottom, cb_rect.height);
414
415    let prev_x = child.geometry.border_box.x;
416    let prev_y = child.geometry.border_box.y;
417    let pre_existing_w = child.geometry.border_box.width;
418    let pre_existing_h = child.geometry.border_box.height;
419
420    if prev_x != 0.0 || prev_y != 0.0 {
421        child.translate(-prev_x, -prev_y);
422    }
423
424    // 1. Horizontal Sizing & Intrinsic Shrink-to-Fit Resolution (W3C CSS 2.1 §10.3.7)
425    let (tentative_w, width_is_auto) = match (left, right, child.style.width) {
426        (Some(l), Some(r), Dimension::Auto) => {
427            let available =
428                (cb_rect.width - l - r - child.style.margin.left - child.style.margin.right)
429                    .max(0.0);
430            (
431                clamp_dimension_with_node(
432                    available,
433                    child.style.min_width,
434                    child.style.max_width,
435                    cb_rect.width,
436                    child,
437                ),
438                false,
439            )
440        }
441        (_, _, Dimension::Auto) => {
442            let inset_sum_x = left.unwrap_or(0.0)
443                + right.unwrap_or(0.0)
444                + child.style.margin.left
445                + child.style.margin.right;
446            let avail_for_stf = (cb_rect.width - inset_sum_x).max(0.0);
447            let stf_w = crate::intrinsic::compute_min_max_sizes(child, Some(cb_rect.width))
448                .shrink_to_fit(avail_for_stf);
449            let w = if stf_w > 0.0 { stf_w } else { pre_existing_w };
450            (
451                clamp_dimension_with_node(
452                    w,
453                    child.style.min_width,
454                    child.style.max_width,
455                    cb_rect.width,
456                    child,
457                ),
458                true,
459            )
460        }
461        _ => {
462            let w = resolve_dimension_with_node(child.style.width, cb_rect.width, child)
463                .unwrap_or(pre_existing_w);
464            (
465                clamp_dimension_with_node(
466                    w,
467                    child.style.min_width,
468                    child.style.max_width,
469                    cb_rect.width,
470                    child,
471                ),
472                false,
473            )
474        }
475    };
476
477    // 2. Vertical Definite vs Auto Height Pre-Resolution (W3C CSS 2.1 §10.6.4)
478    let definite_h = match (top, bottom, child.style.height) {
479        (Some(t), Some(b), Dimension::Auto) => {
480            let available =
481                (cb_rect.height - t - b - child.style.margin.top - child.style.margin.bottom)
482                    .max(0.0);
483            Some(clamp_dimension(
484                available,
485                child.style.min_height,
486                child.style.max_height,
487                cb_rect.height,
488            ))
489        }
490        (_, _, Dimension::Auto) => None,
491        _ => {
492            let h = resolve_dimension(child.style.height, cb_rect.height).unwrap_or(pre_existing_h);
493            Some(clamp_dimension(
494                h,
495                child.style.min_height,
496                child.style.max_height,
497                cb_rect.height,
498            ))
499        }
500    };
501
502    // 3. Format child subtree at origin (0, 0) BEFORE computing right/bottom inset coordinates
503    // so auto width and auto height reflect the laid-out content dimensions.
504    child.geometry = LayoutGeometry::from_border_box(
505        Rect::new(0.0, 0.0, tentative_w, definite_h.unwrap_or(0.0)),
506        child.style.padding,
507        child.style.border_width,
508        child.style.margin,
509    );
510
511    if !child.children.is_empty() || !child.inline_items.is_empty() {
512        child.layout_children_inner_guarded(guard, false);
513    } else if let Some(ref text_str) = child.text {
514        let content_w = child.geometry.content_box.width.max(0.0);
515        let wrap_w = if matches!(
516            child.style.white_space,
517            strata_style::types::WhiteSpace::NoWrap | strata_style::types::WhiteSpace::Pre
518        ) {
519            None
520        } else {
521            Some(content_w)
522        };
523        let (_, text_h) = crate::text::measure_text_bounds_styled_with_family(
524            text_str,
525            child.style.font_size,
526            child.style.line_height,
527            wrap_w,
528            crate::text::is_monospace_family(child.style.font_family),
529            child.style.font_family,
530            child.style.font_weight,
531            child.style.font_style,
532        );
533        if definite_h.is_none() {
534            child.geometry.border_box.height = text_h
535                + child.style.padding.top
536                + child.style.padding.bottom
537                + child.style.border_width.top
538                + child.style.border_width.bottom;
539        }
540    } else if let Some(ref img) = child.image {
541        if definite_h.is_none() && img.height > 0 {
542            let content_w = child.geometry.content_box.width.max(0.0);
543            let img_h = if img.width > 0 && content_w > 0.0 {
544                content_w * (img.height as f32 / img.width as f32)
545            } else {
546                img.height as f32
547            };
548            child.geometry.border_box.height = img_h
549                + child.style.padding.top
550                + child.style.padding.bottom
551                + child.style.border_width.top
552                + child.style.border_width.bottom;
553        }
554    }
555
556    let raw_w = if width_is_auto {
557        child.geometry.border_box.width.max(tentative_w)
558    } else {
559        tentative_w
560    };
561    let item_w = clamp_dimension_with_node(
562        raw_w,
563        child.style.min_width,
564        child.style.max_width,
565        cb_rect.width,
566        child,
567    );
568
569    let item_h = match definite_h {
570        Some(h) => h,
571        None => {
572            let auto_h = if child.geometry.border_box.height > 0.0 {
573                child.geometry.border_box.height
574            } else {
575                pre_existing_h
576            };
577            clamp_dimension(
578                auto_h,
579                child.style.min_height,
580                child.style.max_height,
581                cb_rect.height,
582            )
583        }
584    };
585
586    // 4. Horizontal Position & Auto-Margin Centering (mx-auto / m-auto)
587    let item_x = match (left, right) {
588        (Some(l), Some(r)) if child.style.margin_auto_left && child.style.margin_auto_right => {
589            let free_space = (cb_rect.width - l - r) - item_w;
590            let auto_m = (free_space / 2.0).max(0.0);
591            cb_rect.x + l + auto_m
592        }
593        (Some(l), _) if child.style.margin_auto_left => {
594            let free_space = (cb_rect.width - l) - item_w;
595            cb_rect.x + l + free_space.max(0.0)
596        }
597        (Some(l), _) => cb_rect.x + l + child.style.margin.left,
598        (None, Some(r)) => cb_rect.x + cb_rect.width - r - child.style.margin.right - item_w,
599        (None, None) => cb_rect.x + child.style.margin.left,
600    };
601
602    // 5. Vertical Position & Auto-Margin Centering (my-auto / m-auto)
603    let item_y = match (top, bottom) {
604        (Some(t), Some(b)) if child.style.margin_auto_top && child.style.margin_auto_bottom => {
605            let free_space = (cb_rect.height - t - b) - item_h;
606            let auto_m = (free_space / 2.0).max(0.0);
607            cb_rect.y + t + auto_m
608        }
609        (Some(t), _) if child.style.margin_auto_top => {
610            let free_space = (cb_rect.height - t) - item_h;
611            cb_rect.y + t + free_space.max(0.0)
612        }
613        (Some(t), _) => cb_rect.y + t + child.style.margin.top,
614        (None, Some(b)) => cb_rect.y + cb_rect.height - b - child.style.margin.bottom - item_h,
615        (None, None) => cb_rect.y + child.style.margin.top,
616    };
617
618    // 6. Finalize child box at origin and translate entire subtree to (item_x, item_y)
619    child.geometry = LayoutGeometry::from_border_box(
620        Rect::new(0.0, 0.0, item_w, item_h),
621        child.style.padding,
622        child.style.border_width,
623        child.style.margin,
624    );
625    if item_x != 0.0 || item_y != 0.0 {
626        child.translate(item_x, item_y);
627    }
628    child.geometry.snap_to_pixel();
629}
630
631/// Solves sticky positioning by evaluating insets against scroll offset and clamping to parent bounds.
632fn solve_sticky_box(child: &mut LayoutNode, cb: ContainingBlock) {
633    let normal_x = child.geometry.border_box.x;
634    let normal_y = child.geometry.border_box.y;
635
636    let mut dx = 0.0;
637    let mut dy = 0.0;
638
639    // Vertical sticky evaluation
640    if let Some(top_px) = resolve_dimension(child.style.insets.top, cb.viewport.height) {
641        let target_y = cb.scroll_offset.1 + top_px;
642        let sticky_y = normal_y.max(target_y);
643        let max_sticky_y = cb.padding_box.y + cb.padding_box.height
644            - child.geometry.border_box.height
645            - child.style.margin.bottom;
646        let clamped_y = sticky_y.min(max_sticky_y.max(normal_y));
647        dy = clamped_y - normal_y;
648    } else if let Some(bot_px) = resolve_dimension(child.style.insets.bottom, cb.viewport.height) {
649        let target_y =
650            cb.scroll_offset.1 + cb.viewport.height - bot_px - child.geometry.border_box.height;
651        let sticky_y = normal_y.min(target_y);
652        let min_sticky_y = cb.padding_box.y + child.style.margin.top;
653        let clamped_y = sticky_y.max(min_sticky_y.min(normal_y));
654        dy = clamped_y - normal_y;
655    }
656
657    // Horizontal sticky evaluation
658    if let Some(left_px) = resolve_dimension(child.style.insets.left, cb.viewport.width) {
659        let target_x = cb.scroll_offset.0 + left_px;
660        let sticky_x = normal_x.max(target_x);
661        let max_sticky_x = cb.padding_box.x + cb.padding_box.width
662            - child.geometry.border_box.width
663            - child.style.margin.right;
664        let clamped_x = sticky_x.min(max_sticky_x.max(normal_x));
665        dx = clamped_x - normal_x;
666    } else if let Some(right_px) = resolve_dimension(child.style.insets.right, cb.viewport.width) {
667        let target_x =
668            cb.scroll_offset.0 + cb.viewport.width - right_px - child.geometry.border_box.width;
669        let sticky_x = normal_x.min(target_x);
670        let min_sticky_x = cb.padding_box.x + child.style.margin.left;
671        let clamped_x = sticky_x.max(min_sticky_x.min(normal_x));
672        dx = clamped_x - normal_x;
673    }
674
675    if dx != 0.0 || dy != 0.0 {
676        child.translate(dx, dy);
677    }
678}