Skip to content

Commit eee08c1

Browse files
committed
StyleBuilder is now using Style internally (#275)
1 parent 92338fb commit eee08c1

File tree

1 file changed

+10
-45
lines changed

1 file changed

+10
-45
lines changed

src/style/builder.rs

Lines changed: 10 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -43,8 +43,9 @@ impl NodeIdRef {
4343
/// * A struct of the given name, with the following fields
4444
/// * `children`: a vec of child builder
4545
/// * `node_id_ref`: a field holding a [`Option<NodeIdRef>`], wich allow for retrieving the [`NodeId`] of the built node
46+
/// * `style`: the [`Style`] that will be modified when calling the setters in the `impl` block
4647
/// * A [`Option<_>`] field for each provided field
47-
/// * An `impl`` block containing the following :
48+
/// * An `impl` block containing the following :
4849
/// * A method named after the provided field, used to set said field
4950
/// * A `build_style` method, used to generate a [`Style`](super::Style) based on data stored in the builder
5051
macro_rules! gen_builder {
@@ -80,10 +81,7 @@ macro_rules! gen_builder {
8081
pub struct $builder<'a> {
8182
children: Vec<&'a StyleBuilder<'a>>,
8283
node_id_ref: Option<NodeIdRef>,
83-
$(
84-
$(#[cfg($($cfg)+)])?
85-
$field: Option<$type>,
86-
)*
84+
style: Style,
8785
}
8886

8987
impl<'a> $builder<'a> {
@@ -93,26 +91,10 @@ macro_rules! gen_builder {
9391
#[doc = "\nresulting [`Style`](super::Style) when the [`build`](StyleBuilder::build) method is called."]
9492
#[doc = concat!("\n\nSee [`Style::", stringify!($field), "`](super::Style::", stringify!($field), ").")]
9593
pub fn $field(&mut self, $field: impl Into<$type>) -> &mut Self {
96-
self.$field = Some($field.into());
94+
self.style.$field = $field.into();
9795
self
9896
}
9997
)*
100-
101-
/// Build an [`Style`](super::Style) based on provided cconfiguration.
102-
/// Calling this without setting any field results in
103-
/// [`Style::default()`](super::Style::default)
104-
pub fn build_style(&self) -> Style {
105-
let mut style = Style::default();
106-
107-
$(
108-
$(#[cfg($($cfg)+)])?
109-
if let Some(ref value) = self.$field {
110-
style.$field = Clone::clone(value);
111-
}
112-
)*
113-
114-
style
115-
}
11698
}
11799
};
118100
}
@@ -192,8 +174,7 @@ impl<'a> StyleBuilder<'a> {
192174
/// retrieved once [`build`](StyleBuilder::build) is invoked via setting a [`NodeIdRef`]
193175
/// in each of the desired child [`StyleBuilder`]
194176
pub fn build(&self, tree: &mut TaffyTree) -> TaffyResult<NodeId> {
195-
let style = self.build_style();
196-
let node_id = tree.new_leaf(style)?;
177+
let node_id = tree.new_leaf(self.style.clone())?;
197178

198179
if let Some(node_id_ref) = self.node_id_ref.as_ref() {
199180
node_id_ref.set(node_id);
@@ -244,23 +225,13 @@ impl<'a> StyleBuilder<'a> {
244225

245226
/// Shorthand method to set the width of the resulting [`Style`]
246227
pub fn width(&'a mut self, width: Dimension) -> &'a mut StyleBuilder<'a> {
247-
match self.size {
248-
Some(size) => {
249-
self.size = Some(Size { width, ..size });
250-
}
251-
None => self.size = Some(Size { width, ..Style::DEFAULT.size }),
252-
}
228+
self.style.size.width = width;
253229
self
254230
}
255231

256232
/// Shorthand method to set the height of the resulting [`Style`]
257233
pub fn height(&'a mut self, height: Dimension) -> &'a mut StyleBuilder<'a> {
258-
match self.size {
259-
Some(size) => {
260-
self.size = Some(Size { height, ..size });
261-
}
262-
None => self.size = Some(Size { height, ..Style::DEFAULT.size }),
263-
}
234+
self.style.size.height = height;
264235
self
265236
}
266237
}
@@ -280,7 +251,7 @@ mod test {
280251

281252
#[test]
282253
fn builder_defaults_match_defaults() {
283-
assert_eq!(StyleBuilder::default().build_style(), Style::default())
254+
assert_eq!(StyleBuilder::default().style, Style::default())
284255
}
285256

286257
#[test]
@@ -362,17 +333,11 @@ mod test {
362333

363334
#[test]
364335
fn row() {
365-
assert_eq!(
366-
StyleBuilder::row().build_style(),
367-
Style { flex_direction: FlexDirection::Row, ..Default::default() }
368-
)
336+
assert_eq!(StyleBuilder::row().style, Style { flex_direction: FlexDirection::Row, ..Default::default() })
369337
}
370338

371339
#[test]
372340
fn column() {
373-
assert_eq!(
374-
StyleBuilder::column().build_style(),
375-
Style { flex_direction: FlexDirection::Column, ..Default::default() }
376-
)
341+
assert_eq!(StyleBuilder::column().style, Style { flex_direction: FlexDirection::Column, ..Default::default() })
377342
}
378343
}

0 commit comments

Comments
 (0)