1use crate::values::{
8 BorderStyle, Color, Corners, FontFamily, FontStyle, FontWeight, Sides, TextAlign,
9};
10
11#[derive(Clone, Copy, Debug, PartialEq)]
13pub enum LengthPercentage {
14 Px(f32),
15 Percent(f32),
17}
18
19impl LengthPercentage {
20 pub const ZERO: LengthPercentage = LengthPercentage::Px(0.0);
21
22 pub fn resolve(self, basis: f32) -> f32 {
24 match self {
25 Self::Px(px) => px,
26 Self::Percent(p) => p * basis,
27 }
28 }
29}
30
31#[derive(Clone, Copy, Debug, PartialEq, Default)]
33pub enum LengthPercentageOrAuto {
34 #[default]
35 Auto,
36 Px(f32),
37 Percent(f32),
39}
40
41impl LengthPercentageOrAuto {
42 pub fn resolve(self, basis: f32) -> Option<f32> {
44 match self {
45 Self::Auto => None,
46 Self::Px(px) => Some(px),
47 Self::Percent(p) => Some(p * basis),
48 }
49 }
50}
51
52#[derive(Clone, Copy, Debug, PartialEq, Default)]
54pub enum LengthPercentageOrNone {
55 #[default]
56 None,
57 Px(f32),
58 Percent(f32),
60}
61
62impl LengthPercentageOrNone {
63 pub fn resolve(self, basis: f32) -> Option<f32> {
65 match self {
66 Self::None => None,
67 Self::Px(px) => Some(px),
68 Self::Percent(p) => Some(p * basis),
69 }
70 }
71}
72
73#[derive(Clone, Copy, Debug, PartialEq, Default)]
75pub enum LineHeight {
76 #[default]
79 Normal,
80 Number(f32),
82 Px(f32),
83}
84
85impl LineHeight {
86 pub fn resolve(self, font_size: f32) -> Option<f32> {
88 match self {
89 Self::Normal => None,
90 Self::Number(n) => Some(n * font_size),
91 Self::Px(px) => Some(px),
92 }
93 }
94}
95
96#[derive(Clone, Debug, PartialEq)]
106pub struct ComputedStyle {
107 pub width: LengthPercentageOrAuto,
108 pub height: LengthPercentageOrAuto,
109 pub min_width: LengthPercentageOrAuto,
110 pub min_height: LengthPercentageOrAuto,
111 pub max_width: LengthPercentageOrNone,
112 pub max_height: LengthPercentageOrNone,
113
114 pub margin: Sides<LengthPercentageOrAuto>,
115 pub padding: Sides<LengthPercentage>,
116 pub row_gap: LengthPercentage,
117 pub column_gap: LengthPercentage,
118
119 pub color: Color,
120 pub background_color: Color,
121 pub opacity: f32,
123
124 pub border_width: Sides<f32>,
127 pub border_style: Sides<BorderStyle>,
128 pub border_color: Sides<Color>,
129 pub border_radius: Corners<LengthPercentage>,
130
131 pub font_family: FontFamily,
132 pub font_size: f32,
134 pub font_weight: FontWeight,
135 pub font_style: FontStyle,
136 pub line_height: LineHeight,
137 pub letter_spacing: f32,
139 pub text_align: TextAlign,
140}
141
142impl ComputedStyle {
143 pub fn initial() -> Self {
145 Self {
146 width: LengthPercentageOrAuto::Auto,
147 height: LengthPercentageOrAuto::Auto,
148 min_width: LengthPercentageOrAuto::Auto,
149 min_height: LengthPercentageOrAuto::Auto,
150 max_width: LengthPercentageOrNone::None,
151 max_height: LengthPercentageOrNone::None,
152 margin: Sides::all(LengthPercentageOrAuto::Px(0.0)),
153 padding: Sides::all(LengthPercentage::ZERO),
154 row_gap: LengthPercentage::ZERO,
155 column_gap: LengthPercentage::ZERO,
156 color: Color::BLACK,
157 background_color: Color::TRANSPARENT,
158 opacity: 1.0,
159 border_width: Sides::all(0.0),
160 border_style: Sides::all(BorderStyle::None),
161 border_color: Sides::all(Color::BLACK),
162 border_radius: Corners::all(LengthPercentage::ZERO),
163 font_family: FontFamily::default(),
164 font_size: 16.0,
165 font_weight: FontWeight::NORMAL,
166 font_style: FontStyle::Normal,
167 line_height: LineHeight::Normal,
168 letter_spacing: 0.0,
169 text_align: TextAlign::Start,
170 }
171 }
172
173 pub(crate) fn inherit_from(&mut self, parent: &ComputedStyle) {
175 self.color = parent.color;
176 self.font_family = parent.font_family.clone();
177 self.font_size = parent.font_size;
178 self.font_weight = parent.font_weight;
179 self.font_style = parent.font_style;
180 self.line_height = parent.line_height;
181 self.letter_spacing = parent.letter_spacing;
182 self.text_align = parent.text_align;
183 }
184}
185
186impl Default for ComputedStyle {
187 fn default() -> Self {
188 Self::initial()
189 }
190}