Skip to content

Commit 2938792

Browse files
committed
Upgrade to Taffy 0.2 (#6743)
# Objective Upgrade to Taffy 0.2 ## Solution Do it ## Changelog Upgraded to Taffy 0.2, improving UI layout performance significantly and adding the flexbox `gap` property and `AlignContent::SpaceEvenly`. ## Notes `many_buttons` is 8% faster! speed improvements for more highly nested UIs will be much more dramatic. Great work, Team Taffy.
1 parent 025996b commit 2938792

File tree

4 files changed

+28
-23
lines changed

4 files changed

+28
-23
lines changed

crates/bevy_ui/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ bevy_window = { path = "../bevy_window", version = "0.9.0" }
3030
bevy_utils = { path = "../bevy_utils", version = "0.9.0" }
3131

3232
# other
33-
taffy = "0.1.0"
33+
taffy = "0.2.2"
3434
serde = { version = "1", features = ["derive"] }
3535
smallvec = { version = "1.6", features = ["union", "const_generics"] }
3636
bytemuck = { version = "1.5", features = ["derive"] }

crates/bevy_ui/src/flex/convert.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ pub fn from_rect(
88
rect: UiRect,
99
) -> taffy::geometry::Rect<taffy::style::Dimension> {
1010
taffy::geometry::Rect {
11-
start: from_val(scale_factor, rect.left),
12-
end: from_val(scale_factor, rect.right),
11+
left: from_val(scale_factor, rect.left),
12+
right: from_val(scale_factor, rect.right),
1313
top: from_val(scale_factor, rect.top),
1414
bottom: from_val(scale_factor, rect.bottom),
1515
}
@@ -52,10 +52,8 @@ pub fn from_style(scale_factor: f64, value: &Style) -> taffy::style::Style {
5252
size: from_val_size(scale_factor, value.size),
5353
min_size: from_val_size(scale_factor, value.min_size),
5454
max_size: from_val_size(scale_factor, value.max_size),
55-
aspect_ratio: match value.aspect_ratio {
56-
Some(value) => taffy::number::Number::Defined(value),
57-
None => taffy::number::Number::Undefined,
58-
},
55+
aspect_ratio: value.aspect_ratio,
56+
gap: from_val_size(scale_factor, value.gap),
5957
}
6058
}
6159

crates/bevy_ui/src/flex/mod.rs

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,10 @@ use bevy_transform::components::Transform;
1414
use bevy_utils::HashMap;
1515
use bevy_window::{Window, WindowId, WindowScaleFactorChanged, Windows};
1616
use std::fmt;
17-
use taffy::{number::Number, Taffy};
17+
use taffy::{
18+
prelude::{AvailableSpace, Size},
19+
Taffy,
20+
};
1821

1922
#[derive(Resource)]
2023
pub struct FlexSurface {
@@ -63,7 +66,7 @@ impl FlexSurface {
6366
let taffy_style = convert::from_style(scale_factor, style);
6467
let taffy_node = self.entity_to_taffy.entry(entity).or_insert_with(|| {
6568
added = true;
66-
taffy.new_node(taffy_style, &Vec::new()).unwrap()
69+
taffy.new_leaf(taffy_style).unwrap()
6770
});
6871

6972
if !added {
@@ -81,23 +84,23 @@ impl FlexSurface {
8184
let taffy = &mut self.taffy;
8285
let taffy_style = convert::from_style(scale_factor, style);
8386
let measure = taffy::node::MeasureFunc::Boxed(Box::new(
84-
move |constraints: taffy::geometry::Size<Number>| {
87+
move |constraints: Size<Option<f32>>, _available: Size<AvailableSpace>| {
8588
let mut size = convert::from_f32_size(scale_factor, calculated_size.size);
8689
match (constraints.width, constraints.height) {
87-
(Number::Undefined, Number::Undefined) => {}
88-
(Number::Defined(width), Number::Undefined) => {
90+
(None, None) => {}
91+
(Some(width), None) => {
8992
if calculated_size.preserve_aspect_ratio {
9093
size.height = width * size.height / size.width;
9194
}
9295
size.width = width;
9396
}
94-
(Number::Undefined, Number::Defined(height)) => {
97+
(None, Some(height)) => {
9598
if calculated_size.preserve_aspect_ratio {
9699
size.width = height * size.width / size.height;
97100
}
98101
size.height = height;
99102
}
100-
(Number::Defined(width), Number::Defined(height)) => {
103+
(Some(width), Some(height)) => {
101104
size.width = width;
102105
size.height = height;
103106
}
@@ -110,7 +113,7 @@ impl FlexSurface {
110113
self.taffy.set_style(*taffy_node, taffy_style).unwrap();
111114
self.taffy.set_measure(*taffy_node, Some(measure)).unwrap();
112115
} else {
113-
let taffy_node = taffy.new_leaf(taffy_style, measure).unwrap();
116+
let taffy_node = taffy.new_leaf(taffy_style).unwrap();
114117
self.entity_to_taffy.insert(entity, taffy_node);
115118
}
116119
}
@@ -143,11 +146,10 @@ without UI components as a child of an entity with UI components, results may be
143146

144147
pub fn update_window(&mut self, window: &Window) {
145148
let taffy = &mut self.taffy;
146-
let node = self.window_nodes.entry(window.id()).or_insert_with(|| {
147-
taffy
148-
.new_node(taffy::style::Style::default(), &Vec::new())
149-
.unwrap()
150-
});
149+
let node = self
150+
.window_nodes
151+
.entry(window.id())
152+
.or_insert_with(|| taffy.new_leaf(taffy::style::Style::default()).unwrap());
151153

152154
taffy
153155
.set_style(
@@ -178,7 +180,7 @@ without UI components as a child of an entity with UI components, results may be
178180
pub fn compute_window_layouts(&mut self) {
179181
for window_node in self.window_nodes.values() {
180182
self.taffy
181-
.compute_layout(*window_node, taffy::geometry::Size::undefined())
183+
.compute_layout(*window_node, Size::MAX_CONTENT)
182184
.unwrap();
183185
}
184186
}
@@ -187,7 +189,7 @@ without UI components as a child of an entity with UI components, results may be
187189
pub fn remove_entities(&mut self, entities: impl IntoIterator<Item = Entity>) {
188190
for entity in entities {
189191
if let Some(node) = self.entity_to_taffy.remove(&entity) {
190-
self.taffy.remove(node);
192+
self.taffy.remove(node).unwrap();
191193
}
192194
}
193195
}
@@ -210,7 +212,7 @@ with UI components as a child of an entity without UI components, results may be
210212
#[derive(Debug)]
211213
pub enum FlexError {
212214
InvalidHierarchy,
213-
TaffyError(taffy::Error),
215+
TaffyError(taffy::error::TaffyError),
214216
}
215217

216218
#[allow(clippy::too_many_arguments)]

crates/bevy_ui/src/ui_node.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -237,6 +237,10 @@ pub struct Style {
237237
pub aspect_ratio: Option<f32>,
238238
/// How to handle overflow
239239
pub overflow: Overflow,
240+
/// The size of the gutters between the rows and columns of the flexbox layout
241+
///
242+
/// Values of `Size::UNDEFINED` and `Size::AUTO` are treated as zero.
243+
pub gap: Size,
240244
}
241245

242246
impl Default for Style {
@@ -263,6 +267,7 @@ impl Default for Style {
263267
max_size: Size::AUTO,
264268
aspect_ratio: Default::default(),
265269
overflow: Default::default(),
270+
gap: Size::UNDEFINED,
266271
}
267272
}
268273
}

0 commit comments

Comments
 (0)