Skip to content

Commit

Permalink
Add BoxSizing to Style
Browse files Browse the repository at this point in the history
  • Loading branch information
nicoburns committed Oct 11, 2024
1 parent 4a1fe1b commit 3210895
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
15 changes: 13 additions & 2 deletions 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,7 +70,7 @@ pub fn from_style(
) -> taffy::style::Style {
taffy::style::Style {
display: style.display.into(),
box_sizing: taffy::BoxSizing::BorderBox,
box_sizing: style.box_sizing.into(),
item_is_table: false,
text_align: taffy::TextAlign::Auto,
overflow: taffy::Point {
Expand Down Expand Up @@ -255,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 @@ -453,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 @@ -520,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
42 changes: 42 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,20 @@ pub struct Style {
/// <https://developer.mozilla.org/en-US/docs/Web/CSS/display>
pub display: Display,

/// Which layout algorithm to use when laying out this node's contents:
/// - [`Display::Flex`]: Use the Flexbox layout algorithm
/// - [`Display::Grid`]: Use the CSS Grid layout algorithm
/// - [`Display::None`]: Hide this node and perform layout as if it does not exist.
///
/// 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 +547,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 +881,33 @@ impl Default for Display {
}
}

/// Whether to show or hide overflowing items
#[derive(Copy, Clone, PartialEq, Eq, Debug, Reflect)]
#[reflect(Default, PartialEq)]
#[cfg_attr(
feature = "serialize",
derive(serde::Serialize, serde::Deserialize),
reflect(Serialize, Deserialize)
)]

/// 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>
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

0 comments on commit 3210895

Please sign in to comment.