style/values/computed/
border.rs1use crate::derives::*;
8use crate::properties::{LogicalGroupId, LonghandId};
9use crate::typed_om::{ToTyped, TypedValue};
10use crate::values::animated::{Context as AnimatedContext, ToAnimatedValue};
11use crate::values::computed::length::{
12 CSSPixelLength, NonNegativeLength, NonNegativeLengthPercentage,
13};
14use crate::values::computed::{NonNegativeNumber, NonNegativeNumberOrPercentage};
15use crate::values::generics::border::{
16 GenericBorderCornerRadius, GenericBorderImageSideWidth, GenericBorderImageSlice,
17 GenericBorderRadius, GenericBorderSpacing,
18};
19use crate::values::generics::rect::Rect;
20use crate::values::generics::size::Size2D;
21use crate::values::generics::NonNegative;
22use crate::values::resolved::{Context as ResolvedContext, ToResolvedValue};
23use crate::Zero;
24use app_units::Au;
25use thin_vec::ThinVec;
26
27pub use crate::values::specified::border::BorderImageRepeat;
28
29pub type LineWidth = Au;
31
32#[derive(Clone, Debug, MallocSizeOf, PartialEq, ToCss, ToTyped, From)]
34#[repr(transparent)]
35pub struct BorderSideWidth(pub Au);
36
37impl BorderSideWidth {
38 pub fn medium() -> Self {
40 Self(Au::from_px(3))
41 }
42}
43
44impl ToAnimatedValue for BorderSideWidth {
45 type AnimatedValue = CSSPixelLength;
46
47 #[inline]
48 fn to_animated_value(self, context: &AnimatedContext) -> Self::AnimatedValue {
49 self.0.to_animated_value(context)
50 }
51
52 #[inline]
53 fn from_animated_value(animated: Self::AnimatedValue) -> Self {
54 Self(Au::from_animated_value(animated))
55 }
56}
57
58impl ToResolvedValue for BorderSideWidth {
59 type ResolvedValue = CSSPixelLength;
60
61 fn to_resolved_value(self, context: &ResolvedContext) -> Self::ResolvedValue {
62 let resolved_length = CSSPixelLength::from(self.0).to_resolved_value(context);
63 if !context
64 .current_longhand
65 .is_some_and(|l| l.logical_group() == Some(LogicalGroupId::BorderWidth))
66 {
67 return resolved_length;
68 }
69 let style = match context.current_longhand.unwrap() {
71 LonghandId::BorderTopWidth => context.style.clone_border_top_style(),
72 LonghandId::BorderRightWidth => context.style.clone_border_right_style(),
73 LonghandId::BorderBottomWidth => context.style.clone_border_bottom_style(),
74 LonghandId::BorderLeftWidth => context.style.clone_border_left_style(),
75 _ => {
76 debug_assert!(false, "Expected a physical longhand");
77 return resolved_length;
78 },
79 };
80 if style.none_or_hidden() {
81 return CSSPixelLength::new(0.0);
82 }
83 resolved_length
84 }
85
86 #[inline]
87 fn from_resolved_value(value: Self::ResolvedValue) -> Self {
88 Self(Au::from_f32_px(value.px()))
89 }
90}
91
92pub type BorderSideOffset = Au;
94
95pub type BorderImageWidth = Rect<BorderImageSideWidth>;
97
98impl ToTyped for BorderImageWidth {
99 fn to_typed(&self, _dest: &mut ThinVec<TypedValue>) -> Result<(), ()> {
100 return Err(());
101 }
102}
103
104pub type BorderImageSideWidth =
106 GenericBorderImageSideWidth<NonNegativeLengthPercentage, NonNegativeNumber>;
107
108pub type BorderImageSlice = GenericBorderImageSlice<NonNegativeNumberOrPercentage>;
110
111impl ToTyped for BorderImageSlice {
112 fn to_typed(&self, _dest: &mut ThinVec<TypedValue>) -> Result<(), ()> {
113 return Err(());
114 }
115}
116
117pub type BorderRadius = GenericBorderRadius<NonNegativeLengthPercentage>;
119
120pub type BorderCornerRadius = GenericBorderCornerRadius<NonNegativeLengthPercentage>;
122
123pub type BorderSpacing = GenericBorderSpacing<NonNegativeLength>;
125
126impl BorderImageSideWidth {
127 #[inline]
129 pub fn one() -> Self {
130 GenericBorderImageSideWidth::Number(NonNegative(1.))
131 }
132}
133
134impl BorderImageSlice {
135 #[inline]
137 pub fn hundred_percent() -> Self {
138 GenericBorderImageSlice {
139 offsets: Rect::all(NonNegativeNumberOrPercentage::hundred_percent()),
140 fill: false,
141 }
142 }
143}
144
145impl BorderSpacing {
146 pub fn zero() -> Self {
148 GenericBorderSpacing(Size2D::new(
149 NonNegativeLength::zero(),
150 NonNegativeLength::zero(),
151 ))
152 }
153
154 pub fn horizontal(&self) -> Au {
156 Au::from(*self.0.width())
157 }
158
159 pub fn vertical(&self) -> Au {
161 Au::from(*self.0.height())
162 }
163}