diff --git a/crates/bevy_ui/Cargo.toml b/crates/bevy_ui/Cargo.toml index 968a14a53d..170b26b0ff 100644 --- a/crates/bevy_ui/Cargo.toml +++ b/crates/bevy_ui/Cargo.toml @@ -19,7 +19,9 @@ bevy_hierarchy = { path = "../bevy_hierarchy", version = "0.8.0-dev" } bevy_input = { path = "../bevy_input", version = "0.8.0-dev" } bevy_log = { path = "../bevy_log", version = "0.8.0-dev" } bevy_math = { path = "../bevy_math", version = "0.8.0-dev" } -bevy_reflect = { path = "../bevy_reflect", version = "0.8.0-dev", features = ["bevy"] } +bevy_reflect = { path = "../bevy_reflect", version = "0.8.0-dev", features = [ + "bevy", +] } bevy_render = { path = "../bevy_render", version = "0.8.0-dev" } bevy_sprite = { path = "../bevy_sprite", version = "0.8.0-dev" } bevy_text = { path = "../bevy_text", version = "0.8.0-dev" } @@ -28,7 +30,7 @@ bevy_window = { path = "../bevy_window", version = "0.8.0-dev" } bevy_utils = { path = "../bevy_utils", version = "0.8.0-dev" } # other -stretch = "0.3.2" +taffy = "0.1.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 3c73b1f566..57c17cdad9 100644 --- a/crates/bevy_ui/src/flex/convert.rs +++ b/crates/bevy_ui/src/flex/convert.rs @@ -1,13 +1,13 @@ use crate::{ - AlignContent, AlignItems, AlignSelf, Direction, Display, FlexDirection, FlexWrap, - JustifyContent, PositionType, Size, Style, UiRect, Val, + AlignContent, AlignItems, AlignSelf, Display, FlexDirection, FlexWrap, JustifyContent, + PositionType, Size, Style, UiRect, Val, }; pub fn from_rect( scale_factor: f64, rect: UiRect, -) -> stretch::geometry::Rect { - stretch::geometry::Rect { +) -> taffy::geometry::Rect { + taffy::geometry::Rect { start: from_val(scale_factor, rect.left), end: from_val(scale_factor, rect.right), // NOTE: top and bottom are intentionally flipped. stretch has a flipped y-axis @@ -16,8 +16,8 @@ pub fn from_rect( } } -pub fn from_f32_size(scale_factor: f64, size: Size) -> stretch::geometry::Size { - stretch::geometry::Size { +pub fn from_f32_size(scale_factor: f64, size: Size) -> taffy::geometry::Size { + taffy::geometry::Size { width: (scale_factor * size.width as f64) as f32, height: (scale_factor * size.height as f64) as f32, } @@ -26,19 +26,17 @@ pub fn from_f32_size(scale_factor: f64, size: Size) -> stretch::geometry::S pub fn from_val_size( scale_factor: f64, size: Size, -) -> stretch::geometry::Size { - stretch::geometry::Size { +) -> taffy::geometry::Size { + taffy::geometry::Size { width: from_val(scale_factor, size.width), height: from_val(scale_factor, size.height), } } -pub fn from_style(scale_factor: f64, value: &Style) -> stretch::style::Style { - stretch::style::Style { - overflow: stretch::style::Overflow::Visible, +pub fn from_style(scale_factor: f64, value: &Style) -> taffy::style::Style { + taffy::style::Style { display: value.display.into(), position_type: value.position_type.into(), - direction: value.direction.into(), flex_direction: value.flex_direction.into(), flex_wrap: value.flex_wrap.into(), align_items: value.align_items.into(), @@ -56,117 +54,107 @@ pub fn from_style(scale_factor: f64, value: &Style) -> stretch::style::Style { 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) => stretch::number::Number::Defined(value), - None => stretch::number::Number::Undefined, + Some(value) => taffy::number::Number::Defined(value), + None => taffy::number::Number::Undefined, }, } } -pub fn from_val(scale_factor: f64, val: Val) -> stretch::style::Dimension { +pub fn from_val(scale_factor: f64, val: Val) -> taffy::style::Dimension { match val { - Val::Auto => stretch::style::Dimension::Auto, - Val::Percent(value) => stretch::style::Dimension::Percent(value / 100.0), - Val::Px(value) => stretch::style::Dimension::Points((scale_factor * value as f64) as f32), - Val::Undefined => stretch::style::Dimension::Undefined, + Val::Auto => taffy::style::Dimension::Auto, + Val::Percent(value) => taffy::style::Dimension::Percent(value / 100.0), + Val::Px(value) => taffy::style::Dimension::Points((scale_factor * value as f64) as f32), + Val::Undefined => taffy::style::Dimension::Undefined, } } -impl From for stretch::style::AlignItems { +impl From for taffy::style::AlignItems { fn from(value: AlignItems) -> Self { match value { - AlignItems::FlexStart => stretch::style::AlignItems::FlexStart, - AlignItems::FlexEnd => stretch::style::AlignItems::FlexEnd, - AlignItems::Center => stretch::style::AlignItems::Center, - AlignItems::Baseline => stretch::style::AlignItems::Baseline, - AlignItems::Stretch => stretch::style::AlignItems::Stretch, + AlignItems::FlexStart => taffy::style::AlignItems::FlexStart, + AlignItems::FlexEnd => taffy::style::AlignItems::FlexEnd, + AlignItems::Center => taffy::style::AlignItems::Center, + AlignItems::Baseline => taffy::style::AlignItems::Baseline, + AlignItems::Stretch => taffy::style::AlignItems::Stretch, } } } -impl From for stretch::style::AlignSelf { +impl From for taffy::style::AlignSelf { fn from(value: AlignSelf) -> Self { match value { - AlignSelf::Auto => stretch::style::AlignSelf::Auto, - AlignSelf::FlexStart => stretch::style::AlignSelf::FlexStart, - AlignSelf::FlexEnd => stretch::style::AlignSelf::FlexEnd, - AlignSelf::Center => stretch::style::AlignSelf::Center, - AlignSelf::Baseline => stretch::style::AlignSelf::Baseline, - AlignSelf::Stretch => stretch::style::AlignSelf::Stretch, + AlignSelf::Auto => taffy::style::AlignSelf::Auto, + AlignSelf::FlexStart => taffy::style::AlignSelf::FlexStart, + AlignSelf::FlexEnd => taffy::style::AlignSelf::FlexEnd, + AlignSelf::Center => taffy::style::AlignSelf::Center, + AlignSelf::Baseline => taffy::style::AlignSelf::Baseline, + AlignSelf::Stretch => taffy::style::AlignSelf::Stretch, } } } -impl From for stretch::style::AlignContent { +impl From for taffy::style::AlignContent { fn from(value: AlignContent) -> Self { match value { - AlignContent::FlexStart => stretch::style::AlignContent::FlexStart, - AlignContent::FlexEnd => stretch::style::AlignContent::FlexEnd, - AlignContent::Center => stretch::style::AlignContent::Center, - AlignContent::Stretch => stretch::style::AlignContent::Stretch, - AlignContent::SpaceBetween => stretch::style::AlignContent::SpaceBetween, - AlignContent::SpaceAround => stretch::style::AlignContent::SpaceAround, + AlignContent::FlexStart => taffy::style::AlignContent::FlexStart, + AlignContent::FlexEnd => taffy::style::AlignContent::FlexEnd, + AlignContent::Center => taffy::style::AlignContent::Center, + AlignContent::Stretch => taffy::style::AlignContent::Stretch, + AlignContent::SpaceBetween => taffy::style::AlignContent::SpaceBetween, + AlignContent::SpaceAround => taffy::style::AlignContent::SpaceAround, } } } -impl From for stretch::style::Direction { - fn from(value: Direction) -> Self { - match value { - Direction::Inherit => stretch::style::Direction::Inherit, - Direction::LeftToRight => stretch::style::Direction::LTR, - Direction::RightToLeft => stretch::style::Direction::RTL, - } - } -} - -impl From for stretch::style::Display { +impl From for taffy::style::Display { fn from(value: Display) -> Self { match value { - Display::Flex => stretch::style::Display::Flex, - Display::None => stretch::style::Display::None, + Display::Flex => taffy::style::Display::Flex, + Display::None => taffy::style::Display::None, } } } -impl From for stretch::style::FlexDirection { +impl From for taffy::style::FlexDirection { fn from(value: FlexDirection) -> Self { match value { - FlexDirection::Row => stretch::style::FlexDirection::Row, - FlexDirection::Column => stretch::style::FlexDirection::Column, - FlexDirection::RowReverse => stretch::style::FlexDirection::RowReverse, - FlexDirection::ColumnReverse => stretch::style::FlexDirection::ColumnReverse, + FlexDirection::Row => taffy::style::FlexDirection::Row, + FlexDirection::Column => taffy::style::FlexDirection::Column, + FlexDirection::RowReverse => taffy::style::FlexDirection::RowReverse, + FlexDirection::ColumnReverse => taffy::style::FlexDirection::ColumnReverse, } } } -impl From for stretch::style::JustifyContent { +impl From for taffy::style::JustifyContent { fn from(value: JustifyContent) -> Self { match value { - JustifyContent::FlexStart => stretch::style::JustifyContent::FlexStart, - JustifyContent::FlexEnd => stretch::style::JustifyContent::FlexEnd, - JustifyContent::Center => stretch::style::JustifyContent::Center, - JustifyContent::SpaceBetween => stretch::style::JustifyContent::SpaceBetween, - JustifyContent::SpaceAround => stretch::style::JustifyContent::SpaceAround, - JustifyContent::SpaceEvenly => stretch::style::JustifyContent::SpaceEvenly, + JustifyContent::FlexStart => taffy::style::JustifyContent::FlexStart, + JustifyContent::FlexEnd => taffy::style::JustifyContent::FlexEnd, + JustifyContent::Center => taffy::style::JustifyContent::Center, + JustifyContent::SpaceBetween => taffy::style::JustifyContent::SpaceBetween, + JustifyContent::SpaceAround => taffy::style::JustifyContent::SpaceAround, + JustifyContent::SpaceEvenly => taffy::style::JustifyContent::SpaceEvenly, } } } -impl From for stretch::style::PositionType { +impl From for taffy::style::PositionType { fn from(value: PositionType) -> Self { match value { - PositionType::Relative => stretch::style::PositionType::Relative, - PositionType::Absolute => stretch::style::PositionType::Absolute, + PositionType::Relative => taffy::style::PositionType::Relative, + PositionType::Absolute => taffy::style::PositionType::Absolute, } } } -impl From for stretch::style::FlexWrap { +impl From for taffy::style::FlexWrap { fn from(value: FlexWrap) -> Self { match value { - FlexWrap::NoWrap => stretch::style::FlexWrap::NoWrap, - FlexWrap::Wrap => stretch::style::FlexWrap::Wrap, - FlexWrap::WrapReverse => stretch::style::FlexWrap::WrapReverse, + FlexWrap::NoWrap => taffy::style::FlexWrap::NoWrap, + FlexWrap::Wrap => taffy::style::FlexWrap::Wrap, + FlexWrap::WrapReverse => taffy::style::FlexWrap::WrapReverse, } } } diff --git a/crates/bevy_ui/src/flex/mod.rs b/crates/bevy_ui/src/flex/mod.rs index 607daaacbc..8bc4a2fe10 100644 --- a/crates/bevy_ui/src/flex/mod.rs +++ b/crates/bevy_ui/src/flex/mod.rs @@ -14,15 +14,15 @@ use bevy_transform::components::Transform; use bevy_utils::HashMap; use bevy_window::{Window, WindowId, WindowScaleFactorChanged, Windows}; use std::fmt; -use stretch::{number::Number, Stretch}; +use taffy::{number::Number, Taffy}; pub struct FlexSurface { - entity_to_stretch: HashMap, - window_nodes: HashMap, - stretch: Stretch, + entity_to_taffy: HashMap, + window_nodes: HashMap, + taffy: Taffy, } -// SAFE: as long as MeasureFunc is Send + Sync. https://github.com/vislyhq/stretch/issues/69 +// SAFE: as long as MeasureFunc is Send + Sync. https://github.com/DioxusLabs/taffy/issues/146 // TODO: remove allow on lint - https://github.com/bevyengine/bevy/issues/3666 #[allow(clippy::non_send_fields_in_send_ty)] unsafe impl Send for FlexSurface {} @@ -30,16 +30,16 @@ unsafe impl Sync for FlexSurface {} fn _assert_send_sync_flex_surface_impl_safe() { fn _assert_send_sync() {} - _assert_send_sync::>(); - _assert_send_sync::>(); - // FIXME https://github.com/vislyhq/stretch/issues/69 - // _assert_send_sync::(); + _assert_send_sync::>(); + _assert_send_sync::>(); + // FIXME https://github.com/DioxusLabs/taffy/issues/146 + // _assert_send_sync::(); } impl fmt::Debug for FlexSurface { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { f.debug_struct("FlexSurface") - .field("entity_to_stretch", &self.entity_to_stretch) + .field("entity_to_taffy", &self.entity_to_taffy) .field("window_nodes", &self.window_nodes) .finish() } @@ -48,9 +48,9 @@ impl fmt::Debug for FlexSurface { impl Default for FlexSurface { fn default() -> Self { Self { - entity_to_stretch: Default::default(), + entity_to_taffy: Default::default(), window_nodes: Default::default(), - stretch: Stretch::new(), + taffy: Taffy::new(), } } } @@ -58,17 +58,15 @@ impl Default for FlexSurface { impl FlexSurface { pub fn upsert_node(&mut self, entity: Entity, style: &Style, scale_factor: f64) { let mut added = false; - let stretch = &mut self.stretch; - let stretch_style = convert::from_style(scale_factor, style); - let stretch_node = self.entity_to_stretch.entry(entity).or_insert_with(|| { + let taffy = &mut self.taffy; + let taffy_style = convert::from_style(scale_factor, style); + let taffy_node = self.entity_to_taffy.entry(entity).or_insert_with(|| { added = true; - stretch.new_node(stretch_style, Vec::new()).unwrap() + taffy.new_node(taffy_style, &Vec::new()).unwrap() }); if !added { - self.stretch - .set_style(*stretch_node, stretch_style) - .unwrap(); + self.taffy.set_style(*taffy_node, taffy_style).unwrap(); } } @@ -79,46 +77,44 @@ impl FlexSurface { calculated_size: CalculatedSize, scale_factor: f64, ) { - let stretch = &mut self.stretch; - let stretch_style = convert::from_style(scale_factor, style); - let measure = Box::new(move |constraints: stretch::geometry::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) => { - size.height = width * size.height / size.width; - size.width = width; + 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| { + 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) => { + size.height = width * size.height / size.width; + size.width = width; + } + (Number::Undefined, Number::Defined(height)) => { + size.width = height * size.width / size.height; + size.height = height; + } + (Number::Defined(width), Number::Defined(height)) => { + size.width = width; + size.height = height; + } } - (Number::Undefined, Number::Defined(height)) => { - size.width = height * size.width / size.height; - size.height = height; - } - (Number::Defined(width), Number::Defined(height)) => { - size.width = width; - size.height = height; - } - } - Ok(size) - }); + size + }, + )); - if let Some(stretch_node) = self.entity_to_stretch.get(&entity) { - self.stretch - .set_style(*stretch_node, stretch_style) - .unwrap(); - self.stretch - .set_measure(*stretch_node, Some(measure)) - .unwrap(); + if let Some(taffy_node) = self.entity_to_taffy.get(&entity) { + self.taffy.set_style(*taffy_node, taffy_style).unwrap(); + self.taffy.set_measure(*taffy_node, Some(measure)).unwrap(); } else { - let stretch_node = stretch.new_leaf(stretch_style, measure).unwrap(); - self.entity_to_stretch.insert(entity, stretch_node); + let taffy_node = taffy.new_leaf(taffy_style, measure).unwrap(); + self.entity_to_taffy.insert(entity, taffy_node); } } pub fn update_children(&mut self, entity: Entity, children: &Children) { - let mut stretch_children = Vec::with_capacity(children.len()); + let mut taffy_children = Vec::with_capacity(children.len()); for child in children.iter() { - if let Some(stretch_node) = self.entity_to_stretch.get(child) { - stretch_children.push(*stretch_node); + if let Some(taffy_node) = self.entity_to_taffy.get(child) { + taffy_children.push(*taffy_node); } else { warn!( "Unstyled child in a UI entity hierarchy. You are using an entity \ @@ -127,27 +123,27 @@ without UI components as a child of an entity with UI components, results may be } } - let stretch_node = self.entity_to_stretch.get(&entity).unwrap(); - self.stretch - .set_children(*stretch_node, stretch_children) + let taffy_node = self.entity_to_taffy.get(&entity).unwrap(); + self.taffy + .set_children(*taffy_node, &taffy_children) .unwrap(); } pub fn update_window(&mut self, window: &Window) { - let stretch = &mut self.stretch; + let taffy = &mut self.taffy; let node = self.window_nodes.entry(window.id()).or_insert_with(|| { - stretch - .new_node(stretch::style::Style::default(), Vec::new()) + taffy + .new_node(taffy::style::Style::default(), &Vec::new()) .unwrap() }); - stretch + taffy .set_style( *node, - stretch::style::Style { - size: stretch::geometry::Size { - width: stretch::style::Dimension::Points(window.physical_width() as f32), - height: stretch::style::Dimension::Points(window.physical_height() as f32), + taffy::style::Style { + size: taffy::geometry::Size { + width: taffy::style::Dimension::Points(window.physical_width() as f32), + height: taffy::style::Dimension::Points(window.physical_height() as f32), }, ..Default::default() }, @@ -160,28 +156,26 @@ without UI components as a child of an entity with UI components, results may be window_id: WindowId, children: impl Iterator, ) { - let stretch_node = self.window_nodes.get(&window_id).unwrap(); + let taffy_node = self.window_nodes.get(&window_id).unwrap(); let child_nodes = children - .map(|e| *self.entity_to_stretch.get(&e).unwrap()) - .collect::>(); - self.stretch - .set_children(*stretch_node, child_nodes) - .unwrap(); + .map(|e| *self.entity_to_taffy.get(&e).unwrap()) + .collect::>(); + self.taffy.set_children(*taffy_node, &child_nodes).unwrap(); } pub fn compute_window_layouts(&mut self) { for window_node in self.window_nodes.values() { - self.stretch - .compute_layout(*window_node, stretch::geometry::Size::undefined()) + self.taffy + .compute_layout(*window_node, taffy::geometry::Size::undefined()) .unwrap(); } } - pub fn get_layout(&self, entity: Entity) -> Result<&stretch::result::Layout, FlexError> { - if let Some(stretch_node) = self.entity_to_stretch.get(&entity) { - self.stretch - .layout(*stretch_node) - .map_err(FlexError::StretchError) + pub fn get_layout(&self, entity: Entity) -> Result<&taffy::layout::Layout, FlexError> { + if let Some(taffy_node) = self.entity_to_taffy.get(&entity) { + self.taffy + .layout(*taffy_node) + .map_err(FlexError::TaffyError) } else { warn!( "Styled child in a non-UI entity hierarchy. You are using an entity \ @@ -195,7 +189,7 @@ with UI components as a child of an entity without UI components, results may be #[derive(Debug)] pub enum FlexError { InvalidHierarchy, - StretchError(stretch::Error), + TaffyError(taffy::Error), } #[allow(clippy::too_many_arguments)] diff --git a/deny.toml b/deny.toml index 4f4f007f73..bbabea5a99 100644 --- a/deny.toml +++ b/deny.toml @@ -25,11 +25,6 @@ allow = [ ] default = "deny" -[[licenses.clarify]] -name = "stretch" -expression = "MIT" -license-files = [] - [bans] multiple-versions = "deny" wildcards = "deny"