layout/fragment_tree/
hoisted_shared_fragment.rs

1/* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */
4
5use app_units::Au;
6use malloc_size_of_derive::MallocSizeOf;
7use style::logical_geometry::WritingMode;
8use style::values::specified::align::AlignFlags;
9
10use super::Fragment;
11use crate::geom::{LogicalVec2, PhysicalRect, PhysicalVec};
12
13/// A reference to a Fragment which is shared between `HoistedAbsolutelyPositionedBox`
14/// and its placeholder `AbsoluteOrFixedPositionedFragment` in the original tree position.
15/// This will be used later in order to paint this hoisted box in tree order.
16#[derive(MallocSizeOf)]
17pub(crate) struct HoistedSharedFragment {
18    pub fragment: Option<Fragment>,
19    /// The "static-position rect" of this absolutely positioned box. This is defined by the
20    /// layout mode from which the box originates.
21    ///
22    /// See <https://drafts.csswg.org/css-position-3/#staticpos-rect>
23    pub static_position_rect: PhysicalRect<Au>,
24    /// The resolved alignment values used for aligning this absolutely positioned element
25    /// if the "static-position rect" ends up being the "inset-modified containing block".
26    /// These values are dependent on the layout mode (currently only interesting for
27    /// flexbox).
28    pub resolved_alignment: LogicalVec2<AlignFlags>,
29    /// This is the [`WritingMode`] of the original parent of the element that created this
30    /// hoisted absolutely-positioned fragment. This helps to interpret the offset for
31    /// static positioning. If the writing mode is right-to-left or bottom-to-top, the static
32    /// offset needs to be adjusted by the absolutely positioned element's inline size.
33    pub original_parent_writing_mode: WritingMode,
34}
35
36impl HoistedSharedFragment {
37    pub(crate) fn new(
38        static_position_rect: PhysicalRect<Au>,
39        resolved_alignment: LogicalVec2<AlignFlags>,
40        original_parent_writing_mode: WritingMode,
41    ) -> Self {
42        HoistedSharedFragment {
43            fragment: None,
44            static_position_rect,
45            resolved_alignment,
46            original_parent_writing_mode,
47        }
48    }
49}
50
51impl HoistedSharedFragment {
52    /// `inset: auto`-positioned elements do not know their precise position until after
53    /// they're hoisted. This lets us adjust auto values after the fact.
54    pub(crate) fn adjust_offsets(&mut self, offset: &PhysicalVec<Au>) {
55        self.static_position_rect = self.static_position_rect.translate(*offset);
56    }
57}