From 26a029d407be480d791972afb5975cf62c9360a6 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Fri, 19 Apr 2024 02:47:55 +0200 Subject: Adding upstream version 124.0.1. Signed-off-by: Daniel Baumann --- servo/components/style/values/generics/position.rs | 238 +++++++++++++++++++++ 1 file changed, 238 insertions(+) create mode 100644 servo/components/style/values/generics/position.rs (limited to 'servo/components/style/values/generics/position.rs') diff --git a/servo/components/style/values/generics/position.rs b/servo/components/style/values/generics/position.rs new file mode 100644 index 0000000000..8a4a8c9e24 --- /dev/null +++ b/servo/components/style/values/generics/position.rs @@ -0,0 +1,238 @@ +/* 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/. */ + +//! Generic types for CSS handling of specified and computed values of +//! [`position`](https://drafts.csswg.org/css-backgrounds-3/#position) + +use crate::values::animated::ToAnimatedZero; +use crate::values::generics::ratio::Ratio; + +/// A generic type for representing a CSS [position](https://drafts.csswg.org/css-values/#position). +#[derive( + Animate, + Clone, + ComputeSquaredDistance, + Copy, + Debug, + Deserialize, + MallocSizeOf, + PartialEq, + Serialize, + SpecifiedValueInfo, + ToAnimatedValue, + ToAnimatedZero, + ToComputedValue, + ToResolvedValue, + ToShmem, +)] +#[repr(C)] +pub struct GenericPosition { + /// The horizontal component of position. + pub horizontal: H, + /// The vertical component of position. + pub vertical: V, +} + +impl PositionComponent for Position +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 Position { + /// Returns a new position. + pub fn new(horizontal: H, vertical: V) -> Self { + Self { + horizontal, + vertical, + } + } +} + +/// Implements a method that checks if the position is centered. +pub trait PositionComponent { + /// Returns if the position component is 50% or center. + /// For pixel lengths, it always returns false. + fn is_center(&self) -> bool; +} + +/// A generic type for representing an `Auto | `. +/// This is used by for now. +/// https://drafts.fxtf.org/motion-1/#offset-anchor-property +#[derive( + Animate, + Clone, + ComputeSquaredDistance, + Copy, + Debug, + Deserialize, + MallocSizeOf, + Parse, + PartialEq, + Serialize, + SpecifiedValueInfo, + ToAnimatedZero, + ToAnimatedValue, + ToComputedValue, + ToCss, + ToResolvedValue, + ToShmem, +)] +#[repr(C, u8)] +pub enum GenericPositionOrAuto { + /// The value. + Position(Pos), + /// The keyword `auto`. + Auto, +} + +pub use self::GenericPositionOrAuto as PositionOrAuto; + +impl PositionOrAuto { + /// Return `auto`. + #[inline] + pub fn auto() -> Self { + PositionOrAuto::Auto + } + + /// Return true if it is 'auto'. + #[inline] + pub fn is_auto(&self) -> bool { + matches!(self, PositionOrAuto::Auto) + } +} + +/// A generic value for the `z-index` property. +#[derive( + Animate, + Clone, + ComputeSquaredDistance, + Copy, + Debug, + MallocSizeOf, + PartialEq, + Parse, + SpecifiedValueInfo, + ToAnimatedZero, + ToComputedValue, + ToCss, + ToResolvedValue, + ToShmem, +)] +#[repr(C, u8)] +pub enum GenericZIndex { + /// An integer value. + Integer(I), + /// The keyword `auto`. + Auto, +} + +pub use self::GenericZIndex as ZIndex; + +impl ZIndex { + /// Returns `auto` + #[inline] + pub fn auto() -> Self { + ZIndex::Auto + } + + /// Returns whether `self` is `auto`. + #[inline] + pub fn is_auto(self) -> bool { + matches!(self, ZIndex::Auto) + } + + /// Returns the integer value if it is an integer, or `auto`. + #[inline] + pub fn integer_or(self, auto: Integer) -> Integer { + match self { + ZIndex::Integer(n) => n, + ZIndex::Auto => auto, + } + } +} + +/// Ratio or None. +#[derive( + Animate, + Clone, + ComputeSquaredDistance, + Copy, + Debug, + MallocSizeOf, + PartialEq, + SpecifiedValueInfo, + ToComputedValue, + ToCss, + ToResolvedValue, + ToShmem, +)] +#[repr(C, u8)] +pub enum PreferredRatio { + /// Without specified ratio + #[css(skip)] + None, + /// With specified ratio + Ratio( + #[animation(field_bound)] + #[css(field_bound)] + #[distance(field_bound)] + Ratio, + ), +} + +/// A generic value for the `aspect-ratio` property, the value is `auto || `. +#[derive( + Animate, + Clone, + ComputeSquaredDistance, + Copy, + Debug, + MallocSizeOf, + PartialEq, + SpecifiedValueInfo, + ToComputedValue, + ToCss, + ToResolvedValue, + ToShmem, +)] +#[repr(C)] +pub struct GenericAspectRatio { + /// Specifiy auto or not. + #[animation(constant)] + #[css(represents_keyword)] + pub auto: bool, + /// The preferred aspect-ratio value. + #[animation(field_bound)] + #[css(field_bound)] + #[distance(field_bound)] + pub ratio: PreferredRatio, +} + +pub use self::GenericAspectRatio as AspectRatio; + +impl AspectRatio { + /// Returns `auto` + #[inline] + pub fn auto() -> Self { + AspectRatio { + auto: true, + ratio: PreferredRatio::None, + } + } +} + +impl ToAnimatedZero for AspectRatio { + #[inline] + fn to_animated_zero(&self) -> Result { + Err(()) + } +} -- cgit v1.2.3