Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade to Taffy 0.6 #15844

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion crates/bevy_ui/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ bevy_window = { path = "../bevy_window", version = "0.15.0-dev" }
bevy_utils = { path = "../bevy_utils", version = "0.15.0-dev" }

# other
taffy = { version = "0.5" }
taffy = { version = "0.6" }
serde = { version = "1", features = ["derive"], optional = true }
bytemuck = { version = "1.5", features = ["derive"] }
derive_more = { version = "1", default-features = false, features = [
Expand Down
16 changes: 15 additions & 1 deletion crates/bevy_ui/src/layout/convert.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use taffy::style_helpers;

use crate::{
AlignContent, AlignItems, AlignSelf, Display, FlexDirection, FlexWrap, GridAutoFlow,
AlignContent, AlignItems, AlignSelf, BoxSizing, Display, FlexDirection, FlexWrap, GridAutoFlow,
GridPlacement, GridTrack, GridTrackRepetition, JustifyContent, JustifyItems, JustifySelf,
MaxTrackSizingFunction, MinTrackSizingFunction, OverflowAxis, PositionType, RepeatedGridTrack,
Style, UiRect, Val,
Expand Down Expand Up @@ -70,6 +70,9 @@ pub fn from_style(
) -> taffy::style::Style {
taffy::style::Style {
display: style.display.into(),
box_sizing: style.box_sizing.into(),
item_is_table: false,
text_align: taffy::TextAlign::Auto,
overflow: taffy::Point {
x: style.overflow.x.into(),
y: style.overflow.y.into(),
Expand Down Expand Up @@ -252,6 +255,15 @@ impl From<Display> for taffy::style::Display {
}
}

impl From<BoxSizing> for taffy::style::BoxSizing {
fn from(value: BoxSizing) -> Self {
match value {
BoxSizing::BorderBox => taffy::style::BoxSizing::BorderBox,
BoxSizing::ContentBox => taffy::style::BoxSizing::ContentBox,
}
}
}

impl From<OverflowAxis> for taffy::style::Overflow {
fn from(value: OverflowAxis) -> Self {
match value {
Expand Down Expand Up @@ -450,6 +462,7 @@ mod tests {

let bevy_style = Style {
display: Display::Flex,
box_sizing: BoxSizing::ContentBox,
position_type: PositionType::Absolute,
left: Val::ZERO,
right: Val::Percent(50.),
Expand Down Expand Up @@ -517,6 +530,7 @@ mod tests {
let viewport_values = LayoutContext::new(1.0, bevy_math::Vec2::new(800., 600.));
let taffy_style = from_style(&viewport_values, &bevy_style, false);
assert_eq!(taffy_style.display, taffy::style::Display::Flex);
assert_eq!(taffy_style.box_sizing, taffy::style::BoxSizing::ContentBox);
assert_eq!(taffy_style.position, taffy::style::Position::Absolute);
assert_eq!(
taffy_style.inset.left,
Expand Down
35 changes: 35 additions & 0 deletions crates/bevy_ui/src/ui_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,15 @@ pub struct Style {
/// <https://developer.mozilla.org/en-US/docs/Web/CSS/display>
pub display: Display,

/// Which part of a Node's box length styles like width and height control
/// - [`BoxSizing::BorderBox`]: They refer to the "border box" size (size including padding and border)
/// - [`BoxSizing::ContentBox`]: They refer to the "content box" size (size excluding padding and border)
///
/// `BoxSizing::BorderBox` is generally considered more intuitive and is the default in Bevy even though it is not on the web.
///
/// See: <https://developer.mozilla.org/en-US/docs/Web/CSS/box-sizing>
pub box_sizing: BoxSizing,

/// Whether a node should be laid out in-flow with, or independently of its siblings:
/// - [`PositionType::Relative`]: Layout this node in-flow with other nodes using the usual (flexbox/grid) layout algorithm.
/// - [`PositionType::Absolute`]: Layout this node on top and independently of other nodes.
Expand Down Expand Up @@ -533,6 +542,7 @@ pub struct Style {
impl Style {
pub const DEFAULT: Self = Self {
display: Display::DEFAULT,
box_sizing: BoxSizing::DEFAULT,
position_type: PositionType::DEFAULT,
left: Val::Auto,
right: Val::Auto,
Expand Down Expand Up @@ -866,6 +876,31 @@ impl Default for Display {
}
}

/// Which part of a Node's box length styles like width and height control
///
/// See: <https://developer.mozilla.org/en-US/docs/Web/CSS/box-sizing>
#[derive(Copy, Clone, PartialEq, Eq, Debug, Reflect)]
#[reflect(Default, PartialEq)]
#[cfg_attr(
feature = "serialize",
derive(serde::Serialize, serde::Deserialize),
reflect(Serialize, Deserialize)
)]
pub enum BoxSizing {
/// Length styles like width and height refer to the "border box" size (size including padding and border)
BorderBox,
/// Length styles like width and height refer to the "content box" size (size excluding padding and border)
ContentBox,
}
impl BoxSizing {
pub const DEFAULT: Self = Self::BorderBox;
}
impl Default for BoxSizing {
fn default() -> Self {
Self::DEFAULT
}
}

/// Defines how flexbox items are ordered within a flexbox
#[derive(Copy, Clone, PartialEq, Eq, Debug, Reflect)]
#[reflect(Default, PartialEq)]
Expand Down