use super::*;
#[derive(Debug, Clone, Copy)]
pub(in crate::layout) struct RelativeOffset {
pub(in crate::layout) vector: ContainerVector,
}
impl RelativeOffset {
pub(in crate::layout) fn zero() -> Self {
Self {
vector: ContainerVector::zero(),
}
}
pub(in crate::layout) fn x(self) -> f32 {
self.vector.x
}
pub(in crate::layout) fn y(self) -> f32 {
self.vector.y
}
pub(in crate::layout) fn is_zero(self) -> bool {
self.x().abs() <= 0.01 && self.y().abs() <= 0.01
}
}
pub(in crate::layout) fn relative_position_offset(
style: &ComputedStyle,
containing_block: ContainingBlock,
) -> RelativeOffset {
if !matches!(style.position, Position::Relative | Position::Sticky) {
return RelativeOffset::zero();
}
let left = used_inset_left(style, containing_block);
let right = used_inset_right(style, containing_block);
let top = used_inset_top(style, containing_block);
let bottom = used_inset_bottom(style, containing_block);
RelativeOffset {
vector: ContainerVector::new(
left.unwrap_or_else(|| -right.unwrap_or(0.0)),
bottom.unwrap_or_else(|| -top.unwrap_or(0.0)),
),
}
}
pub(in crate::layout) fn relative_position_offset_with_bases(
style: &ComputedStyle,
inline_basis: PercentageBasis<ContentBoxLength>,
block_basis: PercentageBasis<ContentBoxLength>,
) -> RelativeOffset {
if !matches!(style.position, Position::Relative | Position::Sticky) {
return RelativeOffset::zero();
}
let left = used_length_percentage_or_auto_with_basis(
style.box_values.inset_left.clone(),
inline_basis,
)
.map(|length| length.points());
let right = used_length_percentage_or_auto_with_basis(
style.box_values.inset_right.clone(),
inline_basis,
)
.map(|length| length.points());
let top =
used_length_percentage_or_auto_with_basis(style.box_values.inset_top.clone(), block_basis)
.map(|length| length.points());
let bottom = used_length_percentage_or_auto_with_basis(
style.box_values.inset_bottom.clone(),
block_basis,
)
.map(|length| length.points());
let x = left.unwrap_or_else(|| -right.unwrap_or(0.0));
let y = bottom.unwrap_or_else(|| -top.unwrap_or(0.0));
RelativeOffset {
vector: ContainerVector::new(x, y),
}
}
impl<'a> LayoutBuilder<'a> {
pub(in crate::layout) fn normal_flow_relative_position_offset(
&self,
style: &ComputedStyle,
) -> RelativeOffset {
let Some(containing_block) = self.normal_flow_relative_containing_blocks.last() else {
return relative_position_offset(style, self.current_containing_block());
};
if !matches!(style.position, Position::Relative | Position::Sticky) {
return RelativeOffset::zero();
}
let width_basis =
PercentageBasis::definite(containing_block.physical_content_width.content_box_length());
let height_basis = containing_block
.physical_content_height
.map(|height| PercentageBasis::definite(height.content_box_length()))
.unwrap_or_else(PercentageBasis::indefinite);
let left = used_length_percentage_or_auto_with_basis(
style.box_values.inset_left.clone(),
width_basis,
)
.map(|length| length.points());
let right = used_length_percentage_or_auto_with_basis(
style.box_values.inset_right.clone(),
width_basis,
)
.map(|length| length.points());
let top = used_length_percentage_or_auto_with_basis(
style.box_values.inset_top.clone(),
height_basis,
)
.map(|length| length.points());
let bottom = used_length_percentage_or_auto_with_basis(
style.box_values.inset_bottom.clone(),
height_basis,
)
.map(|length| length.points());
RelativeOffset {
vector: ContainerVector::new(
left.unwrap_or_else(|| -right.unwrap_or(0.0)),
bottom.unwrap_or_else(|| -top.unwrap_or(0.0)),
),
}
}
}