aboutsummaryrefslogtreecommitdiffstats
path: root/components/layout_2020/fragment_tree/hoisted_shared_fragment.rs
blob: ed2a9b253dc755319e60dab59f16653c4208ffa1 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at https://mozilla.org/MPL/2.0/. */

use app_units::Au;
use serde::Serialize;
use style::values::computed::LengthPercentage;

use super::Fragment;
use crate::cell::ArcRefCell;
use crate::geom::LogicalVec2;

/// A reference to a Fragment which is shared between `HoistedAbsolutelyPositionedBox`
/// and its placeholder `AbsoluteOrFixedPositionedFragment` in the original tree position.
/// This will be used later in order to paint this hoisted box in tree order.
#[derive(Serialize)]
pub(crate) struct HoistedSharedFragment {
    pub fragment: Option<ArcRefCell<Fragment>>,
    pub box_offsets: LogicalVec2<AbsoluteBoxOffsets>,
}

impl HoistedSharedFragment {
    pub(crate) fn new(box_offsets: LogicalVec2<AbsoluteBoxOffsets>) -> Self {
        HoistedSharedFragment {
            fragment: None,
            box_offsets,
        }
    }
}

impl HoistedSharedFragment {
    /// In some cases `inset: auto`-positioned elements do not know their precise
    /// position until after they're hoisted. This lets us adjust auto values
    /// after the fact.
    pub(crate) fn adjust_offsets(&mut self, offsets: LogicalVec2<Au>) {
        self.box_offsets.inline.adjust_offset(offsets.inline);
        self.box_offsets.block.adjust_offset(offsets.block);
    }
}

#[derive(Clone, Debug, Serialize)]
pub(crate) enum AbsoluteBoxOffsets {
    StaticStart {
        start: Au,
    },
    Start {
        start: LengthPercentage,
    },
    End {
        end: LengthPercentage,
    },
    Both {
        start: LengthPercentage,
        end: LengthPercentage,
    },
}

impl AbsoluteBoxOffsets {
    pub(crate) fn both_specified(&self) -> bool {
        matches!(self, AbsoluteBoxOffsets::Both { .. })
    }

    pub(crate) fn adjust_offset(&mut self, new_offset: Au) {
        if let AbsoluteBoxOffsets::StaticStart { ref mut start } = *self {
            *start = new_offset
        }
    }
}