Skip to content

Commit 3210895

Browse files
committed
Add BoxSizing to Style
1 parent 4a1fe1b commit 3210895

File tree

2 files changed

+55
-2
lines changed

2 files changed

+55
-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: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -271,6 +271,20 @@ pub struct Style {
271271
/// <https://developer.mozilla.org/en-US/docs/Web/CSS/display>
272272
pub display: Display,
273273

274+
/// Which layout algorithm to use when laying out this node's contents:
275+
/// - [`Display::Flex`]: Use the Flexbox layout algorithm
276+
/// - [`Display::Grid`]: Use the CSS Grid layout algorithm
277+
/// - [`Display::None`]: Hide this node and perform layout as if it does not exist.
278+
///
279+
/// Which part of a Node's box length styles like width and height control
280+
/// - [`BoxSizing::BorderBox`]: They refer to the "border box" size (size including padding and border)
281+
/// - [`BoxSizing::ContentBox`]: They refer to the "content box" size (size excluding padding and border)
282+
///
283+
/// `BoxSizing::BorderBox` is generally considered more intuitive and is the default in Bevy even though it is not on the web.
284+
///
285+
/// See: <https://developer.mozilla.org/en-US/docs/Web/CSS/box-sizing>
286+
pub box_sizing: BoxSizing,
287+
274288
/// Whether a node should be laid out in-flow with, or independently of its siblings:
275289
/// - [`PositionType::Relative`]: Layout this node in-flow with other nodes using the usual (flexbox/grid) layout algorithm.
276290
/// - [`PositionType::Absolute`]: Layout this node on top and independently of other nodes.
@@ -533,6 +547,7 @@ pub struct Style {
533547
impl Style {
534548
pub const DEFAULT: Self = Self {
535549
display: Display::DEFAULT,
550+
box_sizing: BoxSizing::DEFAULT,
536551
position_type: PositionType::DEFAULT,
537552
left: Val::Auto,
538553
right: Val::Auto,
@@ -866,6 +881,33 @@ impl Default for Display {
866881
}
867882
}
868883

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

0 commit comments

Comments
 (0)