use cssparser::Parser;
use std::fmt::Write;
use style_derive::Animate;
use style_traits::CssWriter;
use style_traits::ParseError;
use style_traits::SpecifiedValueInfo;
use style_traits::ToCss;
use crate::derives::*;
use crate::logical_geometry::PhysicalSide;
use crate::parser::{Parse, ParserContext};
use crate::rule_tree::CascadeLevel;
use crate::values::animated::ToAnimatedZero;
use crate::values::computed::position::TryTacticAdjustment;
use crate::values::generics::box_::PositionProperty;
use crate::values::generics::length::GenericAnchorSizeFunction;
use crate::values::generics::ratio::Ratio;
use crate::values::generics::Optional;
use crate::values::DashedIdent;
use crate::values::computed::Context;
use crate::values::computed::ToComputedValue;
pub trait IsTreeScoped {
fn is_tree_scoped(&self) -> bool {
true
}
}
#[repr(C)]
#[derive(
Clone,
Copy,
Debug,
MallocSizeOf,
SpecifiedValueInfo,
ToAnimatedValue,
ToCss,
ToResolvedValue,
ToShmem,
ToTyped,
Serialize,
Deserialize,
)]
pub struct TreeScoped<T> {
pub value: T,
#[css(skip)]
pub scope: CascadeLevel,
}
impl<T: IsTreeScoped + PartialEq> PartialEq for TreeScoped<T> {
fn eq(&self, other: &Self) -> bool {
let tree_scoped = self.value.is_tree_scoped();
if tree_scoped != other.value.is_tree_scoped() {
return false;
}
let scopes_equal = self.scope == other.scope;
if !scopes_equal && tree_scoped {
return false;
}
self.value == other.value
}
}
impl<T> TreeScoped<T> {
pub fn new(value: T, scope: CascadeLevel) -> Self {
Self { value, scope }
}
pub fn with_default_level(value: T) -> Self {
Self {
value,
scope: CascadeLevel::same_tree_author_normal(),
}
}
}
impl<T> Parse for TreeScoped<T>
where
T: Parse,
{
fn parse<'i, 't>(
context: &ParserContext,
input: &mut Parser<'i, 't>,
) -> Result<Self, ParseError<'i>> {
Ok(TreeScoped {
value: T::parse(context, input)?,
scope: CascadeLevel::same_tree_author_normal(),
})
}
}
impl<T> ToComputedValue for TreeScoped<T>
where
T: ToComputedValue + IsTreeScoped,
T::ComputedValue: IsTreeScoped,
{
type ComputedValue = TreeScoped<T::ComputedValue>;
fn to_computed_value(&self, context: &Context) -> Self::ComputedValue {
TreeScoped {
value: self.value.to_computed_value(context),
scope: if context.current_scope().is_tree() {
context.current_scope()
} else {
self.scope.clone()
},
}
}
fn from_computed_value(computed: &Self::ComputedValue) -> Self {
Self {
value: ToComputedValue::from_computed_value(&computed.value),
scope: computed.scope.clone(),
}
}
}
#[derive(
Animate,
Clone,
ComputeSquaredDistance,
Copy,
Debug,
Deserialize,
MallocSizeOf,
PartialEq,
Serialize,
SpecifiedValueInfo,
ToAnimatedValue,
ToAnimatedZero,
ToComputedValue,
ToResolvedValue,
ToShmem,
ToTyped,
)]
#[repr(C)]
pub struct GenericPosition<H, V> {
pub horizontal: H,
pub vertical: V,
}
impl<H, V> PositionComponent for Position<H, V>
where
H: PositionComponent,
V: PositionComponent,
{
#[inline]
fn is_center(&self) -> bool {
self.horizontal.is_center() && self.vertical.is_center()
}
}
pub use self::GenericPosition as Position;
impl<H, V> Position<H, V> {
pub fn new(horizontal: H, vertical: V) -> Self {
Self {
horizontal,
vertical,
}
}
}
pub trait PositionComponent {
fn is_center(&self) -> bool;
}
#[derive(
Animate,
Clone,
ComputeSquaredDistance,
Copy,
Debug,
Deserialize,
MallocSizeOf,
Parse,
PartialEq,
Serialize,
SpecifiedValueInfo,
ToAnimatedZero,
ToAnimatedValue,
ToComputedValue,
ToCss,
ToResolvedValue,
ToShmem,
ToTyped,
)]
#[repr(C, u8)]
pub enum GenericPositionOrAuto<Pos> {
Position(Pos),
Auto,
}
pub use self::GenericPositionOrAuto as PositionOrAuto;
impl<Pos> PositionOrAuto<Pos> {
#[inline]
pub fn auto() -> Self {
PositionOrAuto::Auto
}
#[inline]
pub fn is_auto(&self) -> bool {
matches!(self, PositionOrAuto::Auto)
}
}
#[derive(
Animate,
Clone,
ComputeSquaredDistance,
Copy,
Debug,
MallocSizeOf,
PartialEq,
Parse,
SpecifiedValueInfo,
ToAnimatedValue,
ToAnimatedZero,
ToComputedValue,
ToCss,
ToResolvedValue,
ToShmem,
ToTyped,
)]
#[repr(C, u8)]
pub enum GenericZIndex<I> {
Integer(I),
Auto,
}
pub use self::GenericZIndex as ZIndex;
impl<Integer> ZIndex<Integer> {
#[inline]
pub fn auto() -> Self {
ZIndex::Auto
}
#[inline]
pub fn is_auto(self) -> bool {
matches!(self, ZIndex::Auto)
}
#[inline]
pub fn integer_or(self, auto: Integer) -> Integer {
match self {
ZIndex::Integer(n) => n,
ZIndex::Auto => auto,
}
}
}
#[derive(
Animate,
Clone,
ComputeSquaredDistance,
Copy,
Debug,
MallocSizeOf,
PartialEq,
SpecifiedValueInfo,
ToAnimatedValue,
ToComputedValue,
ToCss,
ToResolvedValue,
ToShmem,
)]
#[repr(C, u8)]
pub enum PreferredRatio<N> {
#[css(skip)]
None,
Ratio(
#[animation(field_bound)]
#[css(field_bound)]
#[distance(field_bound)]
Ratio<N>,
),
}
#[derive(
Animate,
Clone,
ComputeSquaredDistance,
Copy,
Debug,
MallocSizeOf,
PartialEq,
SpecifiedValueInfo,
ToAnimatedValue,
ToComputedValue,
ToCss,
ToResolvedValue,
ToShmem,
ToTyped,
)]
#[repr(C)]
#[typed(todo_derive_fields)]
pub struct GenericAspectRatio<N> {
#[animation(constant)]
#[css(represents_keyword)]
pub auto: bool,
#[animation(field_bound)]
#[css(field_bound)]
#[distance(field_bound)]
pub ratio: PreferredRatio<N>,
}
pub use self::GenericAspectRatio as AspectRatio;
impl<N> AspectRatio<N> {
#[inline]
pub fn auto() -> Self {
AspectRatio {
auto: true,
ratio: PreferredRatio::None,
}
}
}
impl<N> ToAnimatedZero for AspectRatio<N> {
#[inline]
fn to_animated_zero(&self) -> Result<Self, ()> {
Err(())
}
}
#[derive(
Animate,
Clone,
ComputeSquaredDistance,
Debug,
MallocSizeOf,
PartialEq,
ToCss,
ToShmem,
ToAnimatedValue,
ToAnimatedZero,
ToComputedValue,
ToResolvedValue,
ToTyped,
)]
#[repr(C)]
pub enum GenericInset<P, LP> {
LengthPercentage(LP),
Auto,
AnchorFunction(Box<GenericAnchorFunction<P, Self>>),
AnchorSizeFunction(Box<GenericAnchorSizeFunction<Self>>),
AnchorContainingCalcFunction(LP),
}
impl<P, LP> SpecifiedValueInfo for GenericInset<P, LP>
where
LP: SpecifiedValueInfo,
{
fn collect_completion_keywords(f: style_traits::KeywordsCollectFn) {
LP::collect_completion_keywords(f);
f(&["auto"]);
if static_prefs::pref!("layout.css.anchor-positioning.enabled") {
f(&["anchor", "anchor-size"]);
}
}
}
impl<P, LP> GenericInset<P, LP> {
#[inline]
pub fn auto() -> Self {
Self::Auto
}
#[inline]
#[cfg(feature = "servo")]
pub fn is_auto(&self) -> bool {
matches!(self, Self::Auto)
}
}
pub use self::GenericInset as Inset;
#[derive(
Animate,
Clone,
ComputeSquaredDistance,
Debug,
MallocSizeOf,
PartialEq,
SpecifiedValueInfo,
ToShmem,
ToAnimatedValue,
ToAnimatedZero,
ToComputedValue,
ToResolvedValue,
Serialize,
Deserialize,
ToTyped,
)]
#[repr(C)]
#[typed(todo_derive_fields)]
pub struct GenericAnchorFunction<Percentage, Fallback> {
#[animation(constant)]
pub target_element: TreeScoped<DashedIdent>,
pub side: GenericAnchorSide<Percentage>,
pub fallback: Optional<Fallback>,
}
impl<Percentage, Fallback> ToCss for GenericAnchorFunction<Percentage, Fallback>
where
Percentage: ToCss,
Fallback: ToCss,
{
fn to_css<W>(&self, dest: &mut CssWriter<W>) -> std::fmt::Result
where
W: Write,
{
dest.write_str("anchor(")?;
if !self.target_element.value.is_empty() {
self.target_element.to_css(dest)?;
dest.write_str(" ")?;
}
self.side.to_css(dest)?;
if let Some(f) = self.fallback.as_ref() {
dest.write_str(", ")?;
f.to_css(dest)?;
}
dest.write_str(")")
}
}
impl<Percentage, Fallback> GenericAnchorFunction<Percentage, Fallback> {
pub fn valid_for(&self, side: PhysicalSide, position_property: PositionProperty) -> bool {
position_property.is_absolutely_positioned() && self.side.valid_for(side)
}
}
#[derive(
Animate,
Clone,
ComputeSquaredDistance,
Copy,
Debug,
MallocSizeOf,
PartialEq,
SpecifiedValueInfo,
ToCss,
ToShmem,
Parse,
ToAnimatedValue,
ToAnimatedZero,
ToComputedValue,
ToResolvedValue,
Serialize,
Deserialize,
)]
#[repr(u8)]
pub enum AnchorSideKeyword {
Inside,
Outside,
Top,
Left,
Right,
Bottom,
Start,
End,
SelfStart,
SelfEnd,
Center,
}
impl AnchorSideKeyword {
fn from_physical_side(side: PhysicalSide) -> Self {
match side {
PhysicalSide::Top => Self::Top,
PhysicalSide::Right => Self::Right,
PhysicalSide::Bottom => Self::Bottom,
PhysicalSide::Left => Self::Left,
}
}
fn physical_side(self) -> Option<PhysicalSide> {
Some(match self {
Self::Top => PhysicalSide::Top,
Self::Right => PhysicalSide::Right,
Self::Bottom => PhysicalSide::Bottom,
Self::Left => PhysicalSide::Left,
_ => return None,
})
}
}
impl TryTacticAdjustment for AnchorSideKeyword {
fn try_tactic_adjustment(&mut self, old_side: PhysicalSide, new_side: PhysicalSide) {
if !old_side.parallel_to(new_side) {
let Some(s) = self.physical_side() else {
return;
};
*self = Self::from_physical_side(if s == new_side {
old_side
} else if s == old_side {
new_side
} else if s == new_side.opposite_side() {
old_side.opposite_side()
} else {
debug_assert_eq!(s, old_side.opposite_side());
new_side.opposite_side()
});
return;
}
*self = match self {
Self::Center | Self::Inside | Self::Outside => *self,
Self::SelfStart => Self::SelfEnd,
Self::SelfEnd => Self::SelfStart,
Self::Start => Self::End,
Self::End => Self::Start,
Self::Top => Self::Bottom,
Self::Bottom => Self::Top,
Self::Left => Self::Right,
Self::Right => Self::Left,
}
}
}
impl AnchorSideKeyword {
fn valid_for(&self, side: PhysicalSide) -> bool {
match self {
Self::Left | Self::Right => matches!(side, PhysicalSide::Left | PhysicalSide::Right),
Self::Top | Self::Bottom => matches!(side, PhysicalSide::Top | PhysicalSide::Bottom),
Self::Inside
| Self::Outside
| Self::Start
| Self::End
| Self::SelfStart
| Self::SelfEnd
| Self::Center => true,
}
}
}
#[derive(
Animate,
Clone,
ComputeSquaredDistance,
Copy,
Debug,
MallocSizeOf,
PartialEq,
Parse,
SpecifiedValueInfo,
ToCss,
ToShmem,
ToAnimatedValue,
ToAnimatedZero,
ToComputedValue,
ToResolvedValue,
Serialize,
Deserialize,
)]
#[repr(C)]
pub enum GenericAnchorSide<P> {
Keyword(AnchorSideKeyword),
Percentage(P),
}
impl<P> GenericAnchorSide<P> {
pub fn valid_for(&self, side: PhysicalSide) -> bool {
match self {
Self::Keyword(k) => k.valid_for(side),
Self::Percentage(_) => true,
}
}
}