Skip to content

Commit 853832a

Browse files
committed
Add BoxSizing to Style
1 parent 5d0a56b commit 853832a

File tree

2 files changed

+48
-2
lines changed

2 files changed

+48
-2
lines changed

crates/bevy_ui/src/layout/convert.rs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use taffy::style_helpers;
22

33
use crate::{
4-
AlignContent, AlignItems, AlignSelf, Display, FlexDirection, FlexWrap, GridAutoFlow,
4+
AlignContent, AlignItems, AlignSelf, BoxSizing, Display, FlexDirection, FlexWrap, GridAutoFlow,
55
GridPlacement, GridTrack, GridTrackRepetition, JustifyContent, JustifyItems, JustifySelf,
66
MaxTrackSizingFunction, MinTrackSizingFunction, OverflowAxis, PositionType, RepeatedGridTrack,
77
Style, UiRect, Val,
@@ -70,7 +70,7 @@ pub fn from_style(
7070
) -> taffy::style::Style {
7171
taffy::style::Style {
7272
display: style.display.into(),
73-
box_sizing: taffy::BoxSizing::BorderBox,
73+
box_sizing: style.box_sizing.into(),
7474
item_is_table: false,
7575
text_align: taffy::TextAlign::Auto,
7676
overflow: taffy::Point {
@@ -255,6 +255,15 @@ impl From<Display> for taffy::style::Display {
255255
}
256256
}
257257

258+
impl From<BoxSizing> for taffy::style::BoxSizing {
259+
fn from(value: BoxSizing) -> Self {
260+
match value {
261+
BoxSizing::BorderBox => taffy::style::BoxSizing::BorderBox,
262+
BoxSizing::ContentBox => taffy::style::BoxSizing::ContentBox,
263+
}
264+
}
265+
}
266+
258267
impl From<OverflowAxis> for taffy::style::Overflow {
259268
fn from(value: OverflowAxis) -> Self {
260269
match value {
@@ -453,6 +462,7 @@ mod tests {
453462

454463
let bevy_style = Style {
455464
display: Display::Flex,
465+
box_sizing: BoxSizing::ContentBox,
456466
position_type: PositionType::Absolute,
457467
left: Val::ZERO,
458468
right: Val::Percent(50.),
@@ -520,6 +530,7 @@ mod tests {
520530
let viewport_values = LayoutContext::new(1.0, bevy_math::Vec2::new(800., 600.));
521531
let taffy_style = from_style(&viewport_values, &bevy_style, false);
522532
assert_eq!(taffy_style.display, taffy::style::Display::Flex);
533+
assert_eq!(taffy_style.box_sizing, taffy::style::BoxSizing::ContentBox);
523534
assert_eq!(taffy_style.position, taffy::style::Position::Absolute);
524535
assert_eq!(
525536
taffy_style.inset.left,

crates/bevy_ui/src/ui_node.rs

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,15 @@ pub struct Style {
271271
/// <https://developer.mozilla.org/en-US/docs/Web/CSS/display>
272272
pub display: Display,
273273

274+
/// Which part of a Node's box length styles like width and height control
275+
/// - [`BoxSizing::BorderBox`]: They refer to the "border box" size (size including padding and border)
276+
/// - [`BoxSizing::ContentBox`]: They refer to the "content box" size (size excluding padding and border)
277+
///
278+
/// `BoxSizing::BorderBox` is generally considered more intuitive and is the default in Bevy even though it is not on the web.
279+
///
280+
/// See: <https://developer.mozilla.org/en-US/docs/Web/CSS/box-sizing>
281+
pub box_sizing: BoxSizing,
282+
274283
/// Whether a node should be laid out in-flow with, or independently of its siblings:
275284
/// - [`PositionType::Relative`]: Layout this node in-flow with other nodes using the usual (flexbox/grid) layout algorithm.
276285
/// - [`PositionType::Absolute`]: Layout this node on top and independently of other nodes.
@@ -533,6 +542,7 @@ pub struct Style {
533542
impl Style {
534543
pub const DEFAULT: Self = Self {
535544
display: Display::DEFAULT,
545+
box_sizing: BoxSizing::DEFAULT,
536546
position_type: PositionType::DEFAULT,
537547
left: Val::Auto,
538548
right: Val::Auto,
@@ -866,6 +876,31 @@ impl Default for Display {
866876
}
867877
}
868878

879+
/// Which part of a Node's box length styles like width and height control
880+
///
881+
/// See: <https://developer.mozilla.org/en-US/docs/Web/CSS/box-sizing>
882+
#[derive(Copy, Clone, PartialEq, Eq, Debug, Reflect)]
883+
#[reflect(Default, PartialEq)]
884+
#[cfg_attr(
885+
feature = "serialize",
886+
derive(serde::Serialize, serde::Deserialize),
887+
reflect(Serialize, Deserialize)
888+
)]
889+
pub enum BoxSizing {
890+
/// Length styles like width and height refer to the "border box" size (size including padding and border)
891+
BorderBox,
892+
/// Length styles like width and height refer to the "content box" size (size excluding padding and border)
893+
ContentBox,
894+
}
895+
impl BoxSizing {
896+
pub const DEFAULT: Self = Self::BorderBox;
897+
}
898+
impl Default for BoxSizing {
899+
fn default() -> Self {
900+
Self::DEFAULT
901+
}
902+
}
903+
869904
/// Defines how flexbox items are ordered within a flexbox
870905
#[derive(Copy, Clone, PartialEq, Eq, Debug, Reflect)]
871906
#[reflect(Default, PartialEq)]

0 commit comments

Comments
 (0)