mirror of
https://github.com/bevyengine/bevy
synced 2024-11-10 07:04:33 +00:00
depend on dioxus(and bevy)-maintained fork of stretch (taffy) (#4716)
# Objective DioxusLabs and Bevy have taken over maintaining what was our abandoned ui layout dependency [stretch](https://github.com/vislyhq/stretch). Dioxus' fork has had a lot of work done on it by @alice-i-cecile, @Weibye , @jkelleyrtp, @mockersf, @HackerFoo, @TimJentzsch and a dozen other contributors and now is in much better shape than stretch was. The updated crate is called taffy and is available on github [here](https://github.com/DioxusLabs/taffy) ([taffy](https://crates.io/crates/taffy) on crates.io). The goal of this PR is to replace stretch v0.3.2 with taffy v0.1.0. ## Solution I changed the bevy_ui Cargo.toml to depend on taffy instead of stretch and fixed all the errors rustc complained about. --- ## Changelog Changed bevy_ui layout dependency from stretch to taffy (the maintained fork of stretch). fixes #677 ## Migration Guide The public api of taffy is different from that of stretch so please advise me on what to do here @alice-i-cecile.
This commit is contained in:
parent
114d169dce
commit
86dd6f065d
4 changed files with 133 additions and 154 deletions
|
@ -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_input = { path = "../bevy_input", version = "0.8.0-dev" }
|
||||||
bevy_log = { path = "../bevy_log", 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_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_render = { path = "../bevy_render", version = "0.8.0-dev" }
|
||||||
bevy_sprite = { path = "../bevy_sprite", 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" }
|
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" }
|
bevy_utils = { path = "../bevy_utils", version = "0.8.0-dev" }
|
||||||
|
|
||||||
# other
|
# other
|
||||||
stretch = "0.3.2"
|
taffy = "0.1.0"
|
||||||
serde = { version = "1", features = ["derive"] }
|
serde = { version = "1", features = ["derive"] }
|
||||||
smallvec = { version = "1.6", features = ["union", "const_generics"] }
|
smallvec = { version = "1.6", features = ["union", "const_generics"] }
|
||||||
bytemuck = { version = "1.5", features = ["derive"] }
|
bytemuck = { version = "1.5", features = ["derive"] }
|
||||||
|
|
|
@ -1,13 +1,13 @@
|
||||||
use crate::{
|
use crate::{
|
||||||
AlignContent, AlignItems, AlignSelf, Direction, Display, FlexDirection, FlexWrap,
|
AlignContent, AlignItems, AlignSelf, Display, FlexDirection, FlexWrap, JustifyContent,
|
||||||
JustifyContent, PositionType, Size, Style, UiRect, Val,
|
PositionType, Size, Style, UiRect, Val,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub fn from_rect(
|
pub fn from_rect(
|
||||||
scale_factor: f64,
|
scale_factor: f64,
|
||||||
rect: UiRect<Val>,
|
rect: UiRect<Val>,
|
||||||
) -> stretch::geometry::Rect<stretch::style::Dimension> {
|
) -> taffy::geometry::Rect<taffy::style::Dimension> {
|
||||||
stretch::geometry::Rect {
|
taffy::geometry::Rect {
|
||||||
start: from_val(scale_factor, rect.left),
|
start: from_val(scale_factor, rect.left),
|
||||||
end: from_val(scale_factor, rect.right),
|
end: from_val(scale_factor, rect.right),
|
||||||
// NOTE: top and bottom are intentionally flipped. stretch has a flipped y-axis
|
// 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<f32>) -> stretch::geometry::Size<f32> {
|
pub fn from_f32_size(scale_factor: f64, size: Size<f32>) -> taffy::geometry::Size<f32> {
|
||||||
stretch::geometry::Size {
|
taffy::geometry::Size {
|
||||||
width: (scale_factor * size.width as f64) as f32,
|
width: (scale_factor * size.width as f64) as f32,
|
||||||
height: (scale_factor * size.height 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<f32>) -> stretch::geometry::S
|
||||||
pub fn from_val_size(
|
pub fn from_val_size(
|
||||||
scale_factor: f64,
|
scale_factor: f64,
|
||||||
size: Size<Val>,
|
size: Size<Val>,
|
||||||
) -> stretch::geometry::Size<stretch::style::Dimension> {
|
) -> taffy::geometry::Size<taffy::style::Dimension> {
|
||||||
stretch::geometry::Size {
|
taffy::geometry::Size {
|
||||||
width: from_val(scale_factor, size.width),
|
width: from_val(scale_factor, size.width),
|
||||||
height: from_val(scale_factor, size.height),
|
height: from_val(scale_factor, size.height),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn from_style(scale_factor: f64, value: &Style) -> stretch::style::Style {
|
pub fn from_style(scale_factor: f64, value: &Style) -> taffy::style::Style {
|
||||||
stretch::style::Style {
|
taffy::style::Style {
|
||||||
overflow: stretch::style::Overflow::Visible,
|
|
||||||
display: value.display.into(),
|
display: value.display.into(),
|
||||||
position_type: value.position_type.into(),
|
position_type: value.position_type.into(),
|
||||||
direction: value.direction.into(),
|
|
||||||
flex_direction: value.flex_direction.into(),
|
flex_direction: value.flex_direction.into(),
|
||||||
flex_wrap: value.flex_wrap.into(),
|
flex_wrap: value.flex_wrap.into(),
|
||||||
align_items: value.align_items.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),
|
min_size: from_val_size(scale_factor, value.min_size),
|
||||||
max_size: from_val_size(scale_factor, value.max_size),
|
max_size: from_val_size(scale_factor, value.max_size),
|
||||||
aspect_ratio: match value.aspect_ratio {
|
aspect_ratio: match value.aspect_ratio {
|
||||||
Some(value) => stretch::number::Number::Defined(value),
|
Some(value) => taffy::number::Number::Defined(value),
|
||||||
None => stretch::number::Number::Undefined,
|
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 {
|
match val {
|
||||||
Val::Auto => stretch::style::Dimension::Auto,
|
Val::Auto => taffy::style::Dimension::Auto,
|
||||||
Val::Percent(value) => stretch::style::Dimension::Percent(value / 100.0),
|
Val::Percent(value) => taffy::style::Dimension::Percent(value / 100.0),
|
||||||
Val::Px(value) => stretch::style::Dimension::Points((scale_factor * value as f64) as f32),
|
Val::Px(value) => taffy::style::Dimension::Points((scale_factor * value as f64) as f32),
|
||||||
Val::Undefined => stretch::style::Dimension::Undefined,
|
Val::Undefined => taffy::style::Dimension::Undefined,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<AlignItems> for stretch::style::AlignItems {
|
impl From<AlignItems> for taffy::style::AlignItems {
|
||||||
fn from(value: AlignItems) -> Self {
|
fn from(value: AlignItems) -> Self {
|
||||||
match value {
|
match value {
|
||||||
AlignItems::FlexStart => stretch::style::AlignItems::FlexStart,
|
AlignItems::FlexStart => taffy::style::AlignItems::FlexStart,
|
||||||
AlignItems::FlexEnd => stretch::style::AlignItems::FlexEnd,
|
AlignItems::FlexEnd => taffy::style::AlignItems::FlexEnd,
|
||||||
AlignItems::Center => stretch::style::AlignItems::Center,
|
AlignItems::Center => taffy::style::AlignItems::Center,
|
||||||
AlignItems::Baseline => stretch::style::AlignItems::Baseline,
|
AlignItems::Baseline => taffy::style::AlignItems::Baseline,
|
||||||
AlignItems::Stretch => stretch::style::AlignItems::Stretch,
|
AlignItems::Stretch => taffy::style::AlignItems::Stretch,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<AlignSelf> for stretch::style::AlignSelf {
|
impl From<AlignSelf> for taffy::style::AlignSelf {
|
||||||
fn from(value: AlignSelf) -> Self {
|
fn from(value: AlignSelf) -> Self {
|
||||||
match value {
|
match value {
|
||||||
AlignSelf::Auto => stretch::style::AlignSelf::Auto,
|
AlignSelf::Auto => taffy::style::AlignSelf::Auto,
|
||||||
AlignSelf::FlexStart => stretch::style::AlignSelf::FlexStart,
|
AlignSelf::FlexStart => taffy::style::AlignSelf::FlexStart,
|
||||||
AlignSelf::FlexEnd => stretch::style::AlignSelf::FlexEnd,
|
AlignSelf::FlexEnd => taffy::style::AlignSelf::FlexEnd,
|
||||||
AlignSelf::Center => stretch::style::AlignSelf::Center,
|
AlignSelf::Center => taffy::style::AlignSelf::Center,
|
||||||
AlignSelf::Baseline => stretch::style::AlignSelf::Baseline,
|
AlignSelf::Baseline => taffy::style::AlignSelf::Baseline,
|
||||||
AlignSelf::Stretch => stretch::style::AlignSelf::Stretch,
|
AlignSelf::Stretch => taffy::style::AlignSelf::Stretch,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<AlignContent> for stretch::style::AlignContent {
|
impl From<AlignContent> for taffy::style::AlignContent {
|
||||||
fn from(value: AlignContent) -> Self {
|
fn from(value: AlignContent) -> Self {
|
||||||
match value {
|
match value {
|
||||||
AlignContent::FlexStart => stretch::style::AlignContent::FlexStart,
|
AlignContent::FlexStart => taffy::style::AlignContent::FlexStart,
|
||||||
AlignContent::FlexEnd => stretch::style::AlignContent::FlexEnd,
|
AlignContent::FlexEnd => taffy::style::AlignContent::FlexEnd,
|
||||||
AlignContent::Center => stretch::style::AlignContent::Center,
|
AlignContent::Center => taffy::style::AlignContent::Center,
|
||||||
AlignContent::Stretch => stretch::style::AlignContent::Stretch,
|
AlignContent::Stretch => taffy::style::AlignContent::Stretch,
|
||||||
AlignContent::SpaceBetween => stretch::style::AlignContent::SpaceBetween,
|
AlignContent::SpaceBetween => taffy::style::AlignContent::SpaceBetween,
|
||||||
AlignContent::SpaceAround => stretch::style::AlignContent::SpaceAround,
|
AlignContent::SpaceAround => taffy::style::AlignContent::SpaceAround,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<Direction> for stretch::style::Direction {
|
impl From<Display> for taffy::style::Display {
|
||||||
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<Display> for stretch::style::Display {
|
|
||||||
fn from(value: Display) -> Self {
|
fn from(value: Display) -> Self {
|
||||||
match value {
|
match value {
|
||||||
Display::Flex => stretch::style::Display::Flex,
|
Display::Flex => taffy::style::Display::Flex,
|
||||||
Display::None => stretch::style::Display::None,
|
Display::None => taffy::style::Display::None,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<FlexDirection> for stretch::style::FlexDirection {
|
impl From<FlexDirection> for taffy::style::FlexDirection {
|
||||||
fn from(value: FlexDirection) -> Self {
|
fn from(value: FlexDirection) -> Self {
|
||||||
match value {
|
match value {
|
||||||
FlexDirection::Row => stretch::style::FlexDirection::Row,
|
FlexDirection::Row => taffy::style::FlexDirection::Row,
|
||||||
FlexDirection::Column => stretch::style::FlexDirection::Column,
|
FlexDirection::Column => taffy::style::FlexDirection::Column,
|
||||||
FlexDirection::RowReverse => stretch::style::FlexDirection::RowReverse,
|
FlexDirection::RowReverse => taffy::style::FlexDirection::RowReverse,
|
||||||
FlexDirection::ColumnReverse => stretch::style::FlexDirection::ColumnReverse,
|
FlexDirection::ColumnReverse => taffy::style::FlexDirection::ColumnReverse,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<JustifyContent> for stretch::style::JustifyContent {
|
impl From<JustifyContent> for taffy::style::JustifyContent {
|
||||||
fn from(value: JustifyContent) -> Self {
|
fn from(value: JustifyContent) -> Self {
|
||||||
match value {
|
match value {
|
||||||
JustifyContent::FlexStart => stretch::style::JustifyContent::FlexStart,
|
JustifyContent::FlexStart => taffy::style::JustifyContent::FlexStart,
|
||||||
JustifyContent::FlexEnd => stretch::style::JustifyContent::FlexEnd,
|
JustifyContent::FlexEnd => taffy::style::JustifyContent::FlexEnd,
|
||||||
JustifyContent::Center => stretch::style::JustifyContent::Center,
|
JustifyContent::Center => taffy::style::JustifyContent::Center,
|
||||||
JustifyContent::SpaceBetween => stretch::style::JustifyContent::SpaceBetween,
|
JustifyContent::SpaceBetween => taffy::style::JustifyContent::SpaceBetween,
|
||||||
JustifyContent::SpaceAround => stretch::style::JustifyContent::SpaceAround,
|
JustifyContent::SpaceAround => taffy::style::JustifyContent::SpaceAround,
|
||||||
JustifyContent::SpaceEvenly => stretch::style::JustifyContent::SpaceEvenly,
|
JustifyContent::SpaceEvenly => taffy::style::JustifyContent::SpaceEvenly,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<PositionType> for stretch::style::PositionType {
|
impl From<PositionType> for taffy::style::PositionType {
|
||||||
fn from(value: PositionType) -> Self {
|
fn from(value: PositionType) -> Self {
|
||||||
match value {
|
match value {
|
||||||
PositionType::Relative => stretch::style::PositionType::Relative,
|
PositionType::Relative => taffy::style::PositionType::Relative,
|
||||||
PositionType::Absolute => stretch::style::PositionType::Absolute,
|
PositionType::Absolute => taffy::style::PositionType::Absolute,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl From<FlexWrap> for stretch::style::FlexWrap {
|
impl From<FlexWrap> for taffy::style::FlexWrap {
|
||||||
fn from(value: FlexWrap) -> Self {
|
fn from(value: FlexWrap) -> Self {
|
||||||
match value {
|
match value {
|
||||||
FlexWrap::NoWrap => stretch::style::FlexWrap::NoWrap,
|
FlexWrap::NoWrap => taffy::style::FlexWrap::NoWrap,
|
||||||
FlexWrap::Wrap => stretch::style::FlexWrap::Wrap,
|
FlexWrap::Wrap => taffy::style::FlexWrap::Wrap,
|
||||||
FlexWrap::WrapReverse => stretch::style::FlexWrap::WrapReverse,
|
FlexWrap::WrapReverse => taffy::style::FlexWrap::WrapReverse,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -14,15 +14,15 @@ use bevy_transform::components::Transform;
|
||||||
use bevy_utils::HashMap;
|
use bevy_utils::HashMap;
|
||||||
use bevy_window::{Window, WindowId, WindowScaleFactorChanged, Windows};
|
use bevy_window::{Window, WindowId, WindowScaleFactorChanged, Windows};
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use stretch::{number::Number, Stretch};
|
use taffy::{number::Number, Taffy};
|
||||||
|
|
||||||
pub struct FlexSurface {
|
pub struct FlexSurface {
|
||||||
entity_to_stretch: HashMap<Entity, stretch::node::Node>,
|
entity_to_taffy: HashMap<Entity, taffy::node::Node>,
|
||||||
window_nodes: HashMap<WindowId, stretch::node::Node>,
|
window_nodes: HashMap<WindowId, taffy::node::Node>,
|
||||||
stretch: Stretch,
|
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
|
// TODO: remove allow on lint - https://github.com/bevyengine/bevy/issues/3666
|
||||||
#[allow(clippy::non_send_fields_in_send_ty)]
|
#[allow(clippy::non_send_fields_in_send_ty)]
|
||||||
unsafe impl Send for FlexSurface {}
|
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_flex_surface_impl_safe() {
|
||||||
fn _assert_send_sync<T: Send + Sync>() {}
|
fn _assert_send_sync<T: Send + Sync>() {}
|
||||||
_assert_send_sync::<HashMap<Entity, stretch::node::Node>>();
|
_assert_send_sync::<HashMap<Entity, taffy::node::Node>>();
|
||||||
_assert_send_sync::<HashMap<WindowId, stretch::node::Node>>();
|
_assert_send_sync::<HashMap<WindowId, taffy::node::Node>>();
|
||||||
// FIXME https://github.com/vislyhq/stretch/issues/69
|
// FIXME https://github.com/DioxusLabs/taffy/issues/146
|
||||||
// _assert_send_sync::<Stretch>();
|
// _assert_send_sync::<Taffy>();
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Debug for FlexSurface {
|
impl fmt::Debug for FlexSurface {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
f.debug_struct("FlexSurface")
|
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)
|
.field("window_nodes", &self.window_nodes)
|
||||||
.finish()
|
.finish()
|
||||||
}
|
}
|
||||||
|
@ -48,9 +48,9 @@ impl fmt::Debug for FlexSurface {
|
||||||
impl Default for FlexSurface {
|
impl Default for FlexSurface {
|
||||||
fn default() -> Self {
|
fn default() -> Self {
|
||||||
Self {
|
Self {
|
||||||
entity_to_stretch: Default::default(),
|
entity_to_taffy: Default::default(),
|
||||||
window_nodes: Default::default(),
|
window_nodes: Default::default(),
|
||||||
stretch: Stretch::new(),
|
taffy: Taffy::new(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -58,17 +58,15 @@ impl Default for FlexSurface {
|
||||||
impl FlexSurface {
|
impl FlexSurface {
|
||||||
pub fn upsert_node(&mut self, entity: Entity, style: &Style, scale_factor: f64) {
|
pub fn upsert_node(&mut self, entity: Entity, style: &Style, scale_factor: f64) {
|
||||||
let mut added = false;
|
let mut added = false;
|
||||||
let stretch = &mut self.stretch;
|
let taffy = &mut self.taffy;
|
||||||
let stretch_style = convert::from_style(scale_factor, style);
|
let taffy_style = convert::from_style(scale_factor, style);
|
||||||
let stretch_node = self.entity_to_stretch.entry(entity).or_insert_with(|| {
|
let taffy_node = self.entity_to_taffy.entry(entity).or_insert_with(|| {
|
||||||
added = true;
|
added = true;
|
||||||
stretch.new_node(stretch_style, Vec::new()).unwrap()
|
taffy.new_node(taffy_style, &Vec::new()).unwrap()
|
||||||
});
|
});
|
||||||
|
|
||||||
if !added {
|
if !added {
|
||||||
self.stretch
|
self.taffy.set_style(*taffy_node, taffy_style).unwrap();
|
||||||
.set_style(*stretch_node, stretch_style)
|
|
||||||
.unwrap();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -79,46 +77,44 @@ impl FlexSurface {
|
||||||
calculated_size: CalculatedSize,
|
calculated_size: CalculatedSize,
|
||||||
scale_factor: f64,
|
scale_factor: f64,
|
||||||
) {
|
) {
|
||||||
let stretch = &mut self.stretch;
|
let taffy = &mut self.taffy;
|
||||||
let stretch_style = convert::from_style(scale_factor, style);
|
let taffy_style = convert::from_style(scale_factor, style);
|
||||||
let measure = Box::new(move |constraints: stretch::geometry::Size<Number>| {
|
let measure = taffy::node::MeasureFunc::Boxed(Box::new(
|
||||||
let mut size = convert::from_f32_size(scale_factor, calculated_size.size);
|
move |constraints: taffy::geometry::Size<Number>| {
|
||||||
match (constraints.width, constraints.height) {
|
let mut size = convert::from_f32_size(scale_factor, calculated_size.size);
|
||||||
(Number::Undefined, Number::Undefined) => {}
|
match (constraints.width, constraints.height) {
|
||||||
(Number::Defined(width), Number::Undefined) => {
|
(Number::Undefined, Number::Undefined) => {}
|
||||||
size.height = width * size.height / size.width;
|
(Number::Defined(width), Number::Undefined) => {
|
||||||
size.width = width;
|
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
|
||||||
size.width = height * size.width / size.height;
|
},
|
||||||
size.height = height;
|
));
|
||||||
}
|
|
||||||
(Number::Defined(width), Number::Defined(height)) => {
|
|
||||||
size.width = width;
|
|
||||||
size.height = height;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Ok(size)
|
|
||||||
});
|
|
||||||
|
|
||||||
if let Some(stretch_node) = self.entity_to_stretch.get(&entity) {
|
if let Some(taffy_node) = self.entity_to_taffy.get(&entity) {
|
||||||
self.stretch
|
self.taffy.set_style(*taffy_node, taffy_style).unwrap();
|
||||||
.set_style(*stretch_node, stretch_style)
|
self.taffy.set_measure(*taffy_node, Some(measure)).unwrap();
|
||||||
.unwrap();
|
|
||||||
self.stretch
|
|
||||||
.set_measure(*stretch_node, Some(measure))
|
|
||||||
.unwrap();
|
|
||||||
} else {
|
} else {
|
||||||
let stretch_node = stretch.new_leaf(stretch_style, measure).unwrap();
|
let taffy_node = taffy.new_leaf(taffy_style, measure).unwrap();
|
||||||
self.entity_to_stretch.insert(entity, stretch_node);
|
self.entity_to_taffy.insert(entity, taffy_node);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update_children(&mut self, entity: Entity, children: &Children) {
|
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() {
|
for child in children.iter() {
|
||||||
if let Some(stretch_node) = self.entity_to_stretch.get(child) {
|
if let Some(taffy_node) = self.entity_to_taffy.get(child) {
|
||||||
stretch_children.push(*stretch_node);
|
taffy_children.push(*taffy_node);
|
||||||
} else {
|
} else {
|
||||||
warn!(
|
warn!(
|
||||||
"Unstyled child in a UI entity hierarchy. You are using an entity \
|
"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();
|
let taffy_node = self.entity_to_taffy.get(&entity).unwrap();
|
||||||
self.stretch
|
self.taffy
|
||||||
.set_children(*stretch_node, stretch_children)
|
.set_children(*taffy_node, &taffy_children)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn update_window(&mut self, window: &Window) {
|
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(|| {
|
let node = self.window_nodes.entry(window.id()).or_insert_with(|| {
|
||||||
stretch
|
taffy
|
||||||
.new_node(stretch::style::Style::default(), Vec::new())
|
.new_node(taffy::style::Style::default(), &Vec::new())
|
||||||
.unwrap()
|
.unwrap()
|
||||||
});
|
});
|
||||||
|
|
||||||
stretch
|
taffy
|
||||||
.set_style(
|
.set_style(
|
||||||
*node,
|
*node,
|
||||||
stretch::style::Style {
|
taffy::style::Style {
|
||||||
size: stretch::geometry::Size {
|
size: taffy::geometry::Size {
|
||||||
width: stretch::style::Dimension::Points(window.physical_width() as f32),
|
width: taffy::style::Dimension::Points(window.physical_width() as f32),
|
||||||
height: stretch::style::Dimension::Points(window.physical_height() as f32),
|
height: taffy::style::Dimension::Points(window.physical_height() as f32),
|
||||||
},
|
},
|
||||||
..Default::default()
|
..Default::default()
|
||||||
},
|
},
|
||||||
|
@ -160,28 +156,26 @@ without UI components as a child of an entity with UI components, results may be
|
||||||
window_id: WindowId,
|
window_id: WindowId,
|
||||||
children: impl Iterator<Item = Entity>,
|
children: impl Iterator<Item = Entity>,
|
||||||
) {
|
) {
|
||||||
let stretch_node = self.window_nodes.get(&window_id).unwrap();
|
let taffy_node = self.window_nodes.get(&window_id).unwrap();
|
||||||
let child_nodes = children
|
let child_nodes = children
|
||||||
.map(|e| *self.entity_to_stretch.get(&e).unwrap())
|
.map(|e| *self.entity_to_taffy.get(&e).unwrap())
|
||||||
.collect::<Vec<stretch::node::Node>>();
|
.collect::<Vec<taffy::node::Node>>();
|
||||||
self.stretch
|
self.taffy.set_children(*taffy_node, &child_nodes).unwrap();
|
||||||
.set_children(*stretch_node, child_nodes)
|
|
||||||
.unwrap();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn compute_window_layouts(&mut self) {
|
pub fn compute_window_layouts(&mut self) {
|
||||||
for window_node in self.window_nodes.values() {
|
for window_node in self.window_nodes.values() {
|
||||||
self.stretch
|
self.taffy
|
||||||
.compute_layout(*window_node, stretch::geometry::Size::undefined())
|
.compute_layout(*window_node, taffy::geometry::Size::undefined())
|
||||||
.unwrap();
|
.unwrap();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_layout(&self, entity: Entity) -> Result<&stretch::result::Layout, FlexError> {
|
pub fn get_layout(&self, entity: Entity) -> Result<&taffy::layout::Layout, FlexError> {
|
||||||
if let Some(stretch_node) = self.entity_to_stretch.get(&entity) {
|
if let Some(taffy_node) = self.entity_to_taffy.get(&entity) {
|
||||||
self.stretch
|
self.taffy
|
||||||
.layout(*stretch_node)
|
.layout(*taffy_node)
|
||||||
.map_err(FlexError::StretchError)
|
.map_err(FlexError::TaffyError)
|
||||||
} else {
|
} else {
|
||||||
warn!(
|
warn!(
|
||||||
"Styled child in a non-UI entity hierarchy. You are using an entity \
|
"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)]
|
#[derive(Debug)]
|
||||||
pub enum FlexError {
|
pub enum FlexError {
|
||||||
InvalidHierarchy,
|
InvalidHierarchy,
|
||||||
StretchError(stretch::Error),
|
TaffyError(taffy::Error),
|
||||||
}
|
}
|
||||||
|
|
||||||
#[allow(clippy::too_many_arguments)]
|
#[allow(clippy::too_many_arguments)]
|
||||||
|
|
|
@ -25,11 +25,6 @@ allow = [
|
||||||
]
|
]
|
||||||
default = "deny"
|
default = "deny"
|
||||||
|
|
||||||
[[licenses.clarify]]
|
|
||||||
name = "stretch"
|
|
||||||
expression = "MIT"
|
|
||||||
license-files = []
|
|
||||||
|
|
||||||
[bans]
|
[bans]
|
||||||
multiple-versions = "deny"
|
multiple-versions = "deny"
|
||||||
wildcards = "deny"
|
wildcards = "deny"
|
||||||
|
|
Loading…
Reference in a new issue