style/values/computed/
border.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
5//! Computed types for CSS values related to borders.
6
7use crate::properties::{LogicalGroupId, LonghandId};
8use crate::values::animated::{Context as AnimatedContext, ToAnimatedValue};
9use crate::values::computed::length::{
10    CSSPixelLength, NonNegativeLength, NonNegativeLengthPercentage,
11};
12use crate::values::computed::{NonNegativeNumber, NonNegativeNumberOrPercentage};
13use crate::values::generics::border::{
14    GenericBorderCornerRadius, GenericBorderImageSideWidth, GenericBorderImageSlice,
15    GenericBorderRadius, GenericBorderSpacing,
16};
17use crate::values::generics::rect::Rect;
18use crate::values::generics::size::Size2D;
19use crate::values::generics::NonNegative;
20use crate::values::resolved::{Context as ResolvedContext, ToResolvedValue};
21use crate::Zero;
22use app_units::Au;
23
24pub use crate::values::specified::border::BorderImageRepeat;
25
26/// A computed value for -webkit-text-stroke-width.
27pub type LineWidth = Au;
28
29/// A computed value for border-width (and the like).
30#[derive(Clone, Debug, MallocSizeOf, PartialEq, ToCss, ToTyped, From)]
31#[repr(transparent)]
32pub struct BorderSideWidth(pub Au);
33
34impl BorderSideWidth {
35    /// The `medium` value.
36    pub fn medium() -> Self {
37        Self(Au::from_px(3))
38    }
39}
40
41impl ToAnimatedValue for BorderSideWidth {
42    type AnimatedValue = CSSPixelLength;
43
44    #[inline]
45    fn to_animated_value(self, context: &AnimatedContext) -> Self::AnimatedValue {
46        self.0.to_animated_value(context)
47    }
48
49    #[inline]
50    fn from_animated_value(animated: Self::AnimatedValue) -> Self {
51        Self(Au::from_animated_value(animated))
52    }
53}
54
55impl ToResolvedValue for BorderSideWidth {
56    type ResolvedValue = CSSPixelLength;
57
58    fn to_resolved_value(self, context: &ResolvedContext) -> Self::ResolvedValue {
59        let resolved_length = CSSPixelLength::from(self.0).to_resolved_value(context);
60        if !context
61            .current_longhand
62            .is_some_and(|l| l.logical_group() == Some(LogicalGroupId::BorderWidth))
63        {
64            return resolved_length;
65        }
66        // Only for border widths, a style of none/hidden causes the resolved value to be zero.
67        let style = match context.current_longhand.unwrap() {
68            LonghandId::BorderTopWidth => context.style.clone_border_top_style(),
69            LonghandId::BorderRightWidth => context.style.clone_border_right_style(),
70            LonghandId::BorderBottomWidth => context.style.clone_border_bottom_style(),
71            LonghandId::BorderLeftWidth => context.style.clone_border_left_style(),
72            _ => {
73                debug_assert!(false, "Expected a physical longhand");
74                return resolved_length;
75            },
76        };
77        if style.none_or_hidden() {
78            return CSSPixelLength::new(0.0);
79        }
80        resolved_length
81    }
82
83    #[inline]
84    fn from_resolved_value(value: Self::ResolvedValue) -> Self {
85        Self(Au::from_f32_px(value.px()))
86    }
87}
88
89/// A computed value for outline-offset
90pub type BorderSideOffset = Au;
91
92/// A computed value for the `border-image-width` property.
93pub type BorderImageWidth = Rect<BorderImageSideWidth>;
94
95/// A computed value for a single side of a `border-image-width` property.
96pub type BorderImageSideWidth =
97    GenericBorderImageSideWidth<NonNegativeLengthPercentage, NonNegativeNumber>;
98
99/// A computed value for the `border-image-slice` property.
100pub type BorderImageSlice = GenericBorderImageSlice<NonNegativeNumberOrPercentage>;
101
102/// A computed value for the `border-radius` property.
103pub type BorderRadius = GenericBorderRadius<NonNegativeLengthPercentage>;
104
105/// A computed value for the `border-*-radius` longhand properties.
106pub type BorderCornerRadius = GenericBorderCornerRadius<NonNegativeLengthPercentage>;
107
108/// A computed value for the `border-spacing` longhand property.
109pub type BorderSpacing = GenericBorderSpacing<NonNegativeLength>;
110
111impl BorderImageSideWidth {
112    /// Returns `1`.
113    #[inline]
114    pub fn one() -> Self {
115        GenericBorderImageSideWidth::Number(NonNegative(1.))
116    }
117}
118
119impl BorderImageSlice {
120    /// Returns the `100%` value.
121    #[inline]
122    pub fn hundred_percent() -> Self {
123        GenericBorderImageSlice {
124            offsets: Rect::all(NonNegativeNumberOrPercentage::hundred_percent()),
125            fill: false,
126        }
127    }
128}
129
130impl BorderSpacing {
131    /// Returns `0 0`.
132    pub fn zero() -> Self {
133        GenericBorderSpacing(Size2D::new(
134            NonNegativeLength::zero(),
135            NonNegativeLength::zero(),
136        ))
137    }
138
139    /// Returns the horizontal spacing.
140    pub fn horizontal(&self) -> Au {
141        Au::from(*self.0.width())
142    }
143
144    /// Returns the vertical spacing.
145    pub fn vertical(&self) -> Au {
146        Au::from(*self.0.height())
147    }
148}