From 74869b3a048d51a4dfeccdf90ddd70641fd44399 Mon Sep 17 00:00:00 2001 From: Rob Parrett Date: Wed, 23 Nov 2022 11:23:34 -0700 Subject: [PATCH 1/6] Upgrade to Taffy 0.2 --- crates/bevy_ui/Cargo.toml | 2 +- crates/bevy_ui/src/flex/convert.rs | 10 ++++------ crates/bevy_ui/src/flex/mod.rs | 27 +++++++++++++++------------ crates/bevy_ui/src/ui_node.rs | 3 +++ 4 files changed, 23 insertions(+), 19 deletions(-) diff --git a/crates/bevy_ui/Cargo.toml b/crates/bevy_ui/Cargo.toml index 8b85a40b088a3..67141be3959b5 100644 --- a/crates/bevy_ui/Cargo.toml +++ b/crates/bevy_ui/Cargo.toml @@ -30,7 +30,7 @@ bevy_window = { path = "../bevy_window", version = "0.9.0" } bevy_utils = { path = "../bevy_utils", version = "0.9.0" } # other -taffy = "0.1.0" +taffy = "0.2.0" serde = { version = "1", features = ["derive"] } smallvec = { version = "1.6", features = ["union", "const_generics"] } bytemuck = { version = "1.5", features = ["derive"] } diff --git a/crates/bevy_ui/src/flex/convert.rs b/crates/bevy_ui/src/flex/convert.rs index c959aa9dba0b2..c60feb97cd591 100644 --- a/crates/bevy_ui/src/flex/convert.rs +++ b/crates/bevy_ui/src/flex/convert.rs @@ -8,8 +8,8 @@ pub fn from_rect( rect: UiRect, ) -> taffy::geometry::Rect { taffy::geometry::Rect { - start: from_val(scale_factor, rect.left), - end: from_val(scale_factor, rect.right), + left: from_val(scale_factor, rect.left), + right: from_val(scale_factor, rect.right), top: from_val(scale_factor, rect.top), bottom: from_val(scale_factor, rect.bottom), } @@ -52,10 +52,8 @@ pub fn from_style(scale_factor: f64, value: &Style) -> taffy::style::Style { size: from_val_size(scale_factor, value.size), min_size: from_val_size(scale_factor, value.min_size), max_size: from_val_size(scale_factor, value.max_size), - aspect_ratio: match value.aspect_ratio { - Some(value) => taffy::number::Number::Defined(value), - None => taffy::number::Number::Undefined, - }, + aspect_ratio: value.aspect_ratio, + gap: from_val_size(scale_factor, value.gap), } } diff --git a/crates/bevy_ui/src/flex/mod.rs b/crates/bevy_ui/src/flex/mod.rs index da31a692d5630..94e1acedd691e 100644 --- a/crates/bevy_ui/src/flex/mod.rs +++ b/crates/bevy_ui/src/flex/mod.rs @@ -14,7 +14,10 @@ use bevy_transform::components::Transform; use bevy_utils::HashMap; use bevy_window::{Window, WindowId, WindowScaleFactorChanged, Windows}; use std::fmt; -use taffy::{number::Number, Taffy}; +use taffy::{ + prelude::{AvailableSpace, Size}, + Taffy, +}; #[derive(Resource)] pub struct FlexSurface { @@ -63,7 +66,7 @@ impl FlexSurface { let taffy_style = convert::from_style(scale_factor, style); let taffy_node = self.entity_to_taffy.entry(entity).or_insert_with(|| { added = true; - taffy.new_node(taffy_style, &Vec::new()).unwrap() + taffy.new_with_children(taffy_style, &Vec::new()).unwrap() }); if !added { @@ -81,23 +84,23 @@ impl FlexSurface { let taffy = &mut self.taffy; let taffy_style = convert::from_style(scale_factor, style); let measure = taffy::node::MeasureFunc::Boxed(Box::new( - move |constraints: taffy::geometry::Size| { + move |constraints: Size>, _available: Size| { let mut size = convert::from_f32_size(scale_factor, calculated_size.size); match (constraints.width, constraints.height) { - (Number::Undefined, Number::Undefined) => {} - (Number::Defined(width), Number::Undefined) => { + (None, None) => {} + (Some(width), None) => { if calculated_size.preserve_aspect_ratio { size.height = width * size.height / size.width; } size.width = width; } - (Number::Undefined, Number::Defined(height)) => { + (None, Some(height)) => { if calculated_size.preserve_aspect_ratio { size.width = height * size.width / size.height; } size.height = height; } - (Number::Defined(width), Number::Defined(height)) => { + (Some(width), Some(height)) => { size.width = width; size.height = height; } @@ -110,7 +113,7 @@ impl FlexSurface { self.taffy.set_style(*taffy_node, taffy_style).unwrap(); self.taffy.set_measure(*taffy_node, Some(measure)).unwrap(); } else { - let taffy_node = taffy.new_leaf(taffy_style, measure).unwrap(); + let taffy_node = taffy.new_leaf(taffy_style).unwrap(); self.entity_to_taffy.insert(entity, taffy_node); } } @@ -145,7 +148,7 @@ without UI components as a child of an entity with UI components, results may be let taffy = &mut self.taffy; let node = self.window_nodes.entry(window.id()).or_insert_with(|| { taffy - .new_node(taffy::style::Style::default(), &Vec::new()) + .new_with_children(taffy::style::Style::default(), &Vec::new()) .unwrap() }); @@ -178,7 +181,7 @@ without UI components as a child of an entity with UI components, results may be pub fn compute_window_layouts(&mut self) { for window_node in self.window_nodes.values() { self.taffy - .compute_layout(*window_node, taffy::geometry::Size::undefined()) + .compute_layout(*window_node, Size::MAX_CONTENT) .unwrap(); } } @@ -187,7 +190,7 @@ without UI components as a child of an entity with UI components, results may be pub fn remove_entities(&mut self, entities: impl IntoIterator) { for entity in entities { if let Some(node) = self.entity_to_taffy.remove(&entity) { - self.taffy.remove(node); + self.taffy.remove(node).unwrap(); } } } @@ -210,7 +213,7 @@ with UI components as a child of an entity without UI components, results may be #[derive(Debug)] pub enum FlexError { InvalidHierarchy, - TaffyError(taffy::Error), + TaffyError(taffy::error::TaffyError), } #[allow(clippy::too_many_arguments)] diff --git a/crates/bevy_ui/src/ui_node.rs b/crates/bevy_ui/src/ui_node.rs index bc6661b2ed81c..bb5f66ab41178 100644 --- a/crates/bevy_ui/src/ui_node.rs +++ b/crates/bevy_ui/src/ui_node.rs @@ -237,6 +237,8 @@ pub struct Style { pub aspect_ratio: Option, /// How to handle overflow pub overflow: Overflow, + /// The size of the gutters between the rows and columns of the flexbox layout + pub gap: Size, } impl Default for Style { @@ -263,6 +265,7 @@ impl Default for Style { max_size: Size::AUTO, aspect_ratio: Default::default(), overflow: Default::default(), + gap: Size::AUTO, } } } From 0adf15584accadec355559db783200fde5aeb516 Mon Sep 17 00:00:00 2001 From: Rob Parrett Date: Thu, 24 Nov 2022 09:17:33 -0700 Subject: [PATCH 2/6] Use new_leaf when inserting childless nodes --- crates/bevy_ui/src/flex/mod.rs | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/crates/bevy_ui/src/flex/mod.rs b/crates/bevy_ui/src/flex/mod.rs index 94e1acedd691e..43715a8cbd4ae 100644 --- a/crates/bevy_ui/src/flex/mod.rs +++ b/crates/bevy_ui/src/flex/mod.rs @@ -66,7 +66,7 @@ impl FlexSurface { let taffy_style = convert::from_style(scale_factor, style); let taffy_node = self.entity_to_taffy.entry(entity).or_insert_with(|| { added = true; - taffy.new_with_children(taffy_style, &Vec::new()).unwrap() + taffy.new_leaf(taffy_style).unwrap() }); if !added { @@ -146,11 +146,10 @@ without UI components as a child of an entity with UI components, results may be pub fn update_window(&mut self, window: &Window) { let taffy = &mut self.taffy; - let node = self.window_nodes.entry(window.id()).or_insert_with(|| { - taffy - .new_with_children(taffy::style::Style::default(), &Vec::new()) - .unwrap() - }); + let node = self + .window_nodes + .entry(window.id()) + .or_insert_with(|| taffy.new_leaf(taffy::style::Style::default()).unwrap()); taffy .set_style( From 047c1ac406480dade0110dde28dc56af08bed2d8 Mon Sep 17 00:00:00 2001 From: Rob Parrett Date: Fri, 25 Nov 2022 12:35:18 -0700 Subject: [PATCH 3/6] Don't use nonsensical value as default --- crates/bevy_ui/src/ui_node.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_ui/src/ui_node.rs b/crates/bevy_ui/src/ui_node.rs index bb5f66ab41178..dcd1aa8295645 100644 --- a/crates/bevy_ui/src/ui_node.rs +++ b/crates/bevy_ui/src/ui_node.rs @@ -265,7 +265,7 @@ impl Default for Style { max_size: Size::AUTO, aspect_ratio: Default::default(), overflow: Default::default(), - gap: Size::AUTO, + gap: Size::UNDEFINED, } } } From 7a479369775eb2a02d64de80d83a4efbfee5b25e Mon Sep 17 00:00:00 2001 From: Rob Parrett Date: Mon, 28 Nov 2022 07:02:35 -0700 Subject: [PATCH 4/6] Add doc --- crates/bevy_ui/src/ui_node.rs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/crates/bevy_ui/src/ui_node.rs b/crates/bevy_ui/src/ui_node.rs index dcd1aa8295645..6b1dd0343029c 100644 --- a/crates/bevy_ui/src/ui_node.rs +++ b/crates/bevy_ui/src/ui_node.rs @@ -238,6 +238,8 @@ pub struct Style { /// How to handle overflow pub overflow: Overflow, /// The size of the gutters between the rows and columns of the flexbox layout + /// + /// Values of `Size::UNDEFINED` and `Size::AUTO` are treated as zero. pub gap: Size, } From 60caa70eee85a40ea7d8fe814b45302fe51e4519 Mon Sep 17 00:00:00 2001 From: Rob Parrett Date: Mon, 28 Nov 2022 10:47:00 -0700 Subject: [PATCH 5/6] Use taffy 0.2.1 --- crates/bevy_ui/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_ui/Cargo.toml b/crates/bevy_ui/Cargo.toml index 67141be3959b5..82bdce22a150a 100644 --- a/crates/bevy_ui/Cargo.toml +++ b/crates/bevy_ui/Cargo.toml @@ -30,7 +30,7 @@ bevy_window = { path = "../bevy_window", version = "0.9.0" } bevy_utils = { path = "../bevy_utils", version = "0.9.0" } # other -taffy = "0.2.0" +taffy = "0.2.1" serde = { version = "1", features = ["derive"] } smallvec = { version = "1.6", features = ["union", "const_generics"] } bytemuck = { version = "1.5", features = ["derive"] } From 14c2a5933e9704104bedb58cde923794295b5212 Mon Sep 17 00:00:00 2001 From: Rob Parrett Date: Tue, 20 Dec 2022 11:37:42 -0700 Subject: [PATCH 6/6] Use taffy 0.2.2 --- crates/bevy_ui/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/bevy_ui/Cargo.toml b/crates/bevy_ui/Cargo.toml index 82bdce22a150a..f0724e65d5e97 100644 --- a/crates/bevy_ui/Cargo.toml +++ b/crates/bevy_ui/Cargo.toml @@ -30,7 +30,7 @@ bevy_window = { path = "../bevy_window", version = "0.9.0" } bevy_utils = { path = "../bevy_utils", version = "0.9.0" } # other -taffy = "0.2.1" +taffy = "0.2.2" serde = { version = "1", features = ["derive"] } smallvec = { version = "1.6", features = ["union", "const_generics"] } bytemuck = { version = "1.5", features = ["derive"] }