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

Add CSS Grid support to bevy_ui #8026

Merged
merged 76 commits into from
Apr 17, 2023
Merged
Changes from 1 commit
Commits
Show all changes
76 commits
Select commit Hold shift + click to select a range
07aad3b
Enable Taffy grid feature
nicoburns Mar 10, 2023
ff6f5af
CSS Grid WIP (update alignment types)
nicoburns Mar 10, 2023
26e0803
Add GridAutoFlow style
nicoburns Mar 10, 2023
8c2c345
Add Grid variant to Display enum
nicoburns Mar 10, 2023
8f4f276
Implement GridPlacement properties
nicoburns Mar 10, 2023
b4f7b9a
Implement GridTrack type
nicoburns Mar 10, 2023
92cfb4a
Ignore fields that are problematic for reflection
nicoburns Mar 10, 2023
c92d8a7
Fix conversion of grid track lists
nicoburns Mar 10, 2023
ad7b087
cargo fmt
nicoburns Mar 10, 2023
437c55b
Remove const modifier from with_style function
nicoburns Mar 10, 2023
25ed559
Add CSS Grid example
nicoburns Mar 10, 2023
3d5e948
Add metadata to example
nicoburns Mar 10, 2023
d8a9d6b
Fix example metadata (take 2)
nicoburns Mar 10, 2023
c836762
Fix reflection of Style struct
nicoburns Mar 10, 2023
8b569a7
Make default window size of grid example smaller
nicoburns Mar 10, 2023
458abbc
Format example
nicoburns Mar 10, 2023
1b6b271
Update example docs
nicoburns Mar 10, 2023
58d0a18
Fix position:absolute node not displaying
nicoburns Mar 10, 2023
a5c15a2
Apply suggestions from code review
nicoburns Mar 10, 2023
414f154
Update crates/bevy_ui/src/ui_node.rs
nicoburns Mar 10, 2023
5a3c8f0
Rename Normal variants to Default
nicoburns Mar 10, 2023
e189f12
Ensure that grid style values are scaled when converting to taffy styles
nicoburns Mar 11, 2023
0a67774
Add support for repeated track definitions
nicoburns Mar 11, 2023
cfeff47
Use method rather than static function for repeating
nicoburns Mar 11, 2023
49d9424
Add constructors for min-content, max-content, and fit-content grid t…
nicoburns Mar 11, 2023
40eee01
Add constructor for minmax() grid tracks
nicoburns Mar 11, 2023
e57fa7e
Merge branch 'main' into css-grid
nicoburns Mar 13, 2023
726329d
Fix convert test
nicoburns Mar 13, 2023
9beb563
Add documentation to grid example
nicoburns Mar 13, 2023
d2016de
Redo grid example
nicoburns Mar 13, 2023
faee682
Clean up example
nicoburns Mar 13, 2023
e183caf
Document grid example
nicoburns Mar 13, 2023
d38dd22
cargo fmt
nicoburns Mar 13, 2023
8633918
Simplify text spawning
nicoburns Mar 13, 2023
b73c6c0
Remove FlexStart and FlexEnd variants from JustifyItems/JustifySelf a…
nicoburns Mar 13, 2023
e209d59
Improve documentation of Style struct
nicoburns Mar 13, 2023
9533721
Document RepeatedGridTrack
nicoburns Mar 13, 2023
4947f1f
Fix missing backticks in docstring
nicoburns Mar 14, 2023
33dc75d
Update API for repeated grid tracks
nicoburns Mar 14, 2023
7bd9471
Add From<usize> impl for GridTrackRepetition
nicoburns Mar 14, 2023
b75e576
Fixing / in doc comment
nicoburns Mar 14, 2023
3c46510
Update crates/bevy_ui/src/ui_node.rs
nicoburns Mar 14, 2023
3f568e1
Impl From<i32> for GridTrackRepetition + fix test
nicoburns Mar 14, 2023
78931a1
Fix clippy lints
nicoburns Mar 14, 2023
ad9c4e8
More lint fixes
nicoburns Mar 14, 2023
0029667
Fix missing backticks in doc comment
nicoburns Mar 14, 2023
2a4621a
Merge branch 'main' into css-grid
nicoburns Mar 15, 2023
3570782
Flatten MinTrackSizingFunction and MaxTrackSizingFunction enums
nicoburns Mar 15, 2023
8982201
Make it possible to repeat multiple grid tracks
nicoburns Mar 15, 2023
e74f819
Appease clippy
nicoburns Mar 15, 2023
f6402c5
cargo fmt
nicoburns Mar 15, 2023
3b12180
Expose full grid placement model to bevy
nicoburns Mar 15, 2023
887b640
Add links to MDN Flexbox and CSS Grid guides
nicoburns Mar 15, 2023
5468190
Improve docs of GridTrack
nicoburns Mar 16, 2023
4cc047d
Fix link formatting on GridAutoFlow
nicoburns Mar 16, 2023
29654df
Make all squares in example different colours
nicoburns Mar 16, 2023
bf4b2dc
Merge branch 'main' into css-grid
nicoburns Mar 21, 2023
89988b5
Merge branch 'main' into css-grid
nicoburns Mar 25, 2023
6530043
Merge branch 'main' into css-grid
nicoburns Mar 28, 2023
0bdcd68
Merge branch 'main' into css-grid
nicoburns Mar 30, 2023
c6cbaa7
Bump taffy req to 0.3.10
nicoburns Mar 30, 2023
8575045
Merge branch 'main' into css-grid
nicoburns Apr 6, 2023
f14dc98
Apply suggestions from code review
nicoburns Apr 6, 2023
c220b99
Remove unsafe Send/Sync impls for FlexSurface
nicoburns Apr 6, 2023
c9c85d2
Rename flex module and types to layout/Ui as appropriate
nicoburns Apr 6, 2023
204bc2d
Register grid styles with ExtractComponentPlugin
nicoburns Apr 6, 2023
91a6a78
Fix doc references
nicoburns Apr 6, 2023
d798b5b
Merge branch 'main' into css-grid
nicoburns Apr 13, 2023
689cc23
Remove &nbsp; from docs
nicoburns Apr 13, 2023
7341c13
Mark grid and flex_layout examples as working in wasm
nicoburns Apr 13, 2023
a503bb8
Update description of style field in NodeBundle
nicoburns Apr 13, 2023
3f31332
Update description of style field in NodeBundle again
nicoburns Apr 13, 2023
1a59031
Merge branch 'main' into css-grid
nicoburns Apr 14, 2023
6079032
Merge branch 'main' into css-grid
nicoburns Apr 17, 2023
44b06e5
Merge branch 'main' into css-grid
nicoburns Apr 17, 2023
7873b52
Update description of style property for all bundles
nicoburns Apr 17, 2023
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
Prev Previous commit
Next Next commit
Improve documentation of Style struct
  • Loading branch information
nicoburns committed Mar 13, 2023
commit e209d599a677c43db259d2240642e07d1d26d554
331 changes: 278 additions & 53 deletions crates/bevy_ui/src/ui_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,43 +220,202 @@ impl Val {
}
}

/// Describes the style of a UI node
/// Describes the style of a UI container node
///
/// It uses the [Flexbox](https://cssreference.io/flexbox/) system.
/// Node's can be laid out using either Flexbox or CSS Grid Layout.<br />
/// See below for general learning resources and for documentation on the individual style properties.
///
/// ### Flexbox
///
/// - [Flexbox Froggy](https://flexboxfroggy.com/). An interactive tutorial/game that teaches the essential parts of Flebox in a fun engaging way.
/// - [A Complete Guide To Flexbox](https://css-tricks.com/snippets/css/a-guide-to-flexbox/) by CSS Tricks. This is detailed guide with illustrations and comphrehensive written explanation of the different Flexbox properties and how they work.
///
/// ### CSS Grid
///
/// - [CSS Grid Garden](https://cssgridgarden.com/). An interactive tutorial/game that teaches the essential parts of CSS Grid in a fun engaging way.
/// - [A Complete Guide To CSS Grid](https://css-tricks.com/snippets/css/complete-guide-grid/) by CSS Tricks. This is detailed guide with illustrations and comphrehensive written explanation of the different CSS Grid properties and how they work.

#[derive(Component, Clone, PartialEq, Debug, Reflect)]
#[reflect(Component, Default, PartialEq)]
pub struct Style {
/// Whether to arrange this node and its children with flexbox layout
/// 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.
///
/// If this is set to [`Display::None`], this node will be collapsed.
/// <https://developer.mozilla.org/en-US/docs/Web/CSS/display>
///
/// &nbsp;
pub display: Display,
/// Whether to arrange this node relative to other nodes, or positioned absolutely

/// Whether a node should be laid out in-flow with, or independently of it's 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.
///
/// <https://developer.mozilla.org/en-US/docs/Web/CSS/position>
///
/// &nbsp;
pub position_type: PositionType,

/// Whether overflowing content should be displayed or clipped.
///
/// <https://developer.mozilla.org/en-US/docs/Web/CSS/overflow>
///
/// &nbsp;
pub overflow: Overflow,

/// Defines the text direction. For example English is written LTR (left-to-right) while Arabic is written RTL (right-to-left).
///
/// Note: the corresponding CSS property also affects box layout order, but this isn't yet implemented in bevy.
/// <https://developer.mozilla.org/en-US/docs/Web/CSS/direction>
///
/// &nbsp;
pub direction: Direction,
mockersf marked this conversation as resolved.
Show resolved Hide resolved

/// The horizontal position of the left edge of the node.
/// - For relatively positioned nodes, this is relative to the node's position as computed during regular layout.
/// - For absolutely positioned nodes, this is relative to the *parent* node's bounding box.
///
/// <https://developer.mozilla.org/en-US/docs/Web/CSS/left>
///
/// &nbsp;
pub left: Val,

/// The horizontal position of the right edge of the node.
/// - For relatively positioned nodes, this is relative to the node's position as computed during regular layout.
/// - For absolutely positioned nodes, this is relative to the *parent* node's bounding box.
///
/// <https://developer.mozilla.org/en-US/docs/Web/CSS/right>
///
/// &nbsp;
pub right: Val,

/// The vertical position of the top edge of the node.
/// - For relatively positioned nodes, this is relative to the node's position as computed during regular layout.
/// - For absolutely positioned nodes, this is relative to the *parent* node's bounding box.
///
/// <https://developer.mozilla.org/en-US/docs/Web/CSS/top>
///
/// &nbsp;
pub top: Val,

/// The vertical position of the bottom edge of the node.
/// - For relatively positioned nodes, this is relative to the node's position as computed during regular layout.
/// - For absolutely positioned nodes, this is relative to the *parent* node's bounding box.
///
/// <https://developer.mozilla.org/en-US/docs/Web/CSS/bottom>
///
/// &nbsp;
pub bottom: Val,
/// Which direction the content of this node should go
pub direction: Direction,
/// Whether to use column or row layout
pub flex_direction: FlexDirection,
/// How to wrap nodes
pub flex_wrap: FlexWrap,
/// How items are aligned according to the cross axis

/// The ideal size of the node
///
/// `size.width` is used when it is within the bounds defined by `min_size.width` and `max_size.width`.
/// `size.height` is used when it is within the bounds defined by `min_size.height` and `max_size.height`.
///
/// <https://developer.mozilla.org/en-US/docs/Web/CSS/width> <br />
/// <https://developer.mozilla.org/en-US/docs/Web/CSS/height>
///
/// &nbsp;
pub size: Size,

/// The minimum size of the node
///
/// `min_size.width` is used if it is greater than either `size.width` or `max_size.width`, or both.
/// `min_size.height` is used if it is greater than either `size.height` or `max_size.height`, or both.
///
/// <https://developer.mozilla.org/en-US/docs/Web/CSS/min-width> <br />
/// <https://developer.mozilla.org/en-US/docs/Web/CSS/min-height>
///
/// &nbsp;
pub min_size: Size,

/// The maximum size of the node
///
/// `max_size.width` is used if it is within the bounds defined by `min_size.width` and `size.width`.
/// `max_size.height` is used if it is within the bounds defined by `min_size.height` and `size.height.
///
/// <https://developer.mozilla.org/en-US/docs/Web/CSS/max-width> <br />
/// <https://developer.mozilla.org/en-US/docs/Web/CSS/max-height>
///
/// &nbsp;
pub max_size: Size,

/// The aspect ratio of the node (defined as `width / height`)
///
/// <https://developer.mozilla.org/en-US/docs/Web/CSS/aspect-ratio>
///
/// &nbsp;
pub aspect_ratio: Option<f32>,

/// For Flexbox containers:
/// - Sets default cross-axis alignment of the child items.
/// For CSS Grid containers:
/// - Controls block (vertical) axis alignment of children of this grid container within their grid areas
///
/// This value is overriden [`JustifySelf`] on the child node is set.
///
/// <https://developer.mozilla.org/en-US/docs/Web/CSS/align-items>
///
/// &nbsp;
pub align_items: AlignItems,
/// How items are aligned according to the inline axis

/// For Flexbox containers:
/// - This property has no effect. See `justify_content` for main-axis alignment of flex items.
/// For CSS Grid containers:
/// - Sets default inline (horizontal) axis alignment of child items within their grid areas
///
/// This value is overriden [`JustifySelf`] on the child node is set.
///
/// <https://developer.mozilla.org/en-US/docs/Web/CSS/justify-items>
///
/// &nbsp;
pub justify_items: JustifyItems,
/// How this item is aligned according to the cross axis.
/// Overrides [`AlignItems`].

/// For Flexbox items:
/// - Controls cross-axis alignment of the item.
/// For CSS Grid items:
/// - Controls block (vertical) axis alignment of a grid item within it's grid area
///
/// If set to `Auto`, alignment is inherited from the value of [`AlignItems`] set on the parent node.
///
/// <https://developer.mozilla.org/en-US/docs/Web/CSS/align-self>
///
/// &nbsp;
pub align_self: AlignSelf,
/// How this item is aligned according to the inline axis.
/// Overrides [`JustifyItems`].

/// For Flexbox items:
/// - This property has no effect. See `justify_content` for main-axis alignment of flex items.
/// For CSS Grid items:
/// - Controls inline (horizontal) axis alignment of a grid item within it's grid area.
///
/// If set to `Auto`, alignment is inherited from the value of [`JustifyItems`] set on the parent node.
///
/// <https://developer.mozilla.org/en-US/docs/Web/CSS/justify-items>
///
/// &nbsp;
pub justify_self: JustifySelf,
/// How to align each line, only applies if flex_wrap is set to
/// [`FlexWrap::Wrap`] and there are multiple lines of items

/// For Flexbox containers:
/// - Controls alignment of lines if flex_wrap is set to [`FlexWrap::Wrap`] and there are multiple lines of items
/// For CSS Grid container:
/// - Controls alignment of grid rows
///
/// <https://developer.mozilla.org/en-US/docs/Web/CSS/align-content>
///
/// &nbsp;
pub align_content: AlignContent,
/// How items align according to the main axis

/// For Flexbox containers:
/// - Controls alignment of items in the main axis
/// For CSS Grid containers:
/// - Controls alignment of grid columns
///
/// <https://developer.mozilla.org/en-US/docs/Web/CSS/justify-content>
///
/// &nbsp;
pub justify_content: JustifyContent,

/// The amount of space around a node outside its border.
///
/// If a percentage value is used, the percentage is calculated based on the width of the parent node.
Expand All @@ -274,8 +433,13 @@ pub struct Style {
/// ..Default::default()
/// };
/// ```
/// A node with this style and a parent with dimensions of 100px by 300px, will have calculated margins of 10px on both left and right edges, and 15px on both top and bottom egdes.
/// A node with this style and a parent with dimensions of 100px by 300px, will have calculated margins of 10px on both left and right edges, and 15px on both top and bottom edges.
///
/// <https://developer.mozilla.org/en-US/docs/Web/CSS/margin>
///
/// &nbsp;
pub margin: UiRect,

/// The amount of space between the edges of a node and its contents.
///
/// If a percentage value is used, the percentage is calculated based on the width of the parent node.
Expand All @@ -294,62 +458,123 @@ pub struct Style {
/// };
/// ```
/// A node with this style and a parent with dimensions of 300px by 100px, will have calculated padding of 3px on the left, 6px on the right, 9px on the top and 12px on the bottom.
///
/// <https://developer.mozilla.org/en-US/docs/Web/CSS/padding>
///
/// &nbsp;
pub padding: UiRect,

/// The amount of space between the margins of a node and its padding.
///
/// If a percentage value is used, the percentage is calculated based on the width of the parent node.
///
/// The size of the node will be expanded if there are constraints that prevent the layout algorithm from placing the border within the existing node boundary.
///
/// Rendering for borders is not yet implemented.
///
/// <https://developer.mozilla.org/en-US/docs/Web/CSS/border-width>
///
/// &nbsp;
pub border: UiRect,
/// Defines how much a flexbox item should grow if there's space available

/// Whether a Flexbox container should be a row or a column. This property has no effect of Grid nodes.
///
/// <https://developer.mozilla.org/en-US/docs/Web/CSS/flex-direction>
///
/// &nbsp;
pub flex_direction: FlexDirection,

/// Whether a Flexbox container should wrap it's contents onto multiple line wrap if they overflow. This property has no effect of Grid nodes.
///
/// <https://developer.mozilla.org/en-US/docs/Web/CSS/flex-wrap>
///
/// &nbsp;
pub flex_wrap: FlexWrap,

/// Defines how much a flexbox item should grow if there's space available. Defaults to 0 (don't grow at all).
///
/// <https://developer.mozilla.org/en-US/docs/Web/CSS/flex-grow>
///
/// &nbsp;
pub flex_grow: f32,
/// How to shrink if there's not enough space available

/// Defines how much a flexbox item should shrink if there's not enough space available. Defaults to 1.
///
/// <https://developer.mozilla.org/en-US/docs/Web/CSS/flex-shrink>
///
/// &nbsp;
pub flex_shrink: f32,
/// The initial length of the main axis, before other properties are applied.

/// The initial length of a flexbox in the main axis, before flex growing/shrinking properties are applied.
///
/// If both are set, `flex_basis` overrides `size` on the main axis but it obeys the bounds defined by `min_size` and `max_size`.
pub flex_basis: Val,
/// The ideal size of the flexbox
/// `flex_basis` overrides `size` on the main axis if both are set, but it obeys the bounds defined by `min_size` and `max_size`.
///
/// `size.width` is used when it is within the bounds defined by `min_size.width` and `max_size.width`.
/// `size.height` is used when it is within the bounds defined by `min_size.height` and `max_size.height`.
pub size: Size,
/// The minimum size of the flexbox
/// <https://developer.mozilla.org/en-US/docs/Web/CSS/flex-basis>
///
/// `min_size.width` is used if it is greater than either `size.width` or `max_size.width`, or both.
/// `min_size.height` is used if it is greater than either `size.height` or `max_size.height`, or both.
pub min_size: Size,
/// The maximum size of the flexbox
/// &nbsp;
pub flex_basis: Val,
nicoburns marked this conversation as resolved.
Show resolved Hide resolved

/// The size of the gutters between items in flexbox layout or rows/columns in a grid layout
///
/// `max_size.width` is used if it is within the bounds defined by `min_size.width` and `size.width`.
/// `max_size.height` is used if it is within the bounds defined by `min_size.height` and `size.height.
pub max_size: Size,
/// The aspect ratio of the flexbox
pub aspect_ratio: Option<f32>,
/// How to handle overflow
pub overflow: Overflow,
/// The size of the gutters between the rows and columns of the flexbox layout
/// Note: Values of `Val::Auto` are not valid and are treated as zero.
///
/// A value of `Size::AUTO` is treated as zero.
pub gap: Size,
/// Controls whether grid items are placed row-wise or column-wise. And whether the sparse or dense packing algorithm is used.
/// <https://developer.mozilla.org/en-US/docs/Web/CSS/gap>
///
/// &nbsp;
pub gap: Size,

/// Controls whether automatically placed grid items are placed row-wise or column-wise. And whether the sparse or dense packing algorithm is used.
/// Only affect Grid layouts
///
/// <https://developer.mozilla.org/en-US/docs/Web/CSS/grid-auto-flow>
///
/// &nbsp;
pub grid_auto_flow: GridAutoFlow,
/// Defines the track sizing functions (widths) of the grid rows

/// Defines the number of rows a grid has and the sizes of those rows. If grid items are given explicit placements then more rows may
/// be implicitly generated by items that are placed out of bounds. The sizes of those rows are controlled by `grid_auto_rows` property.
///
/// <https://developer.mozilla.org/en-US/docs/Web/CSS/grid-template-rows>
///
/// &nbsp;
pub grid_template_rows: Vec<RepeatedGridTrack>,
/// Defines the track sizing functions (heights) of the grid columns

/// Defines the number of columns a grid has and the sizes of those columns. If grid items are given explicit placements then more columns may
/// be implicitly generated by items that are placed out of bounds. The sizes of those columns are controlled by `grid_auto_columns` property.
///
/// <https://developer.mozilla.org/en-US/docs/Web/CSS/grid-template-columns>
///
/// &nbsp;
pub grid_template_columns: Vec<RepeatedGridTrack>,
/// Defines the size of implicitly created rows

/// Defines the size of implicitly created rows. Rows are created implicitly when grid items are given explicit placements that are out of bounds
/// of the rows explicitly created using `grid_template_rows`.
///
/// <https://developer.mozilla.org/en-US/docs/Web/CSS/grid-auto-rows>
///
/// &nbsp;
pub grid_auto_rows: Vec<GridTrack>,
/// Defined the size of implicitly created columns
/// Defines the size of implicitly created columns. Columns are created implicitly when grid items are given explicit placements that are out of bounds
/// of the columns explicitly created using `grid_template_columms`.
///
/// <https://developer.mozilla.org/en-US/docs/Web/CSS/grid-template-columns>
///
/// &nbsp;
pub grid_auto_columns: Vec<GridTrack>,
/// The column in which a grid item starts and how many columns it span
pub grid_column: GridPlacement,
/// The row in which a grid item starts and how many rows it span

/// The row in which a grid item starts and how many rows it spans.
///
/// <https://developer.mozilla.org/en-US/docs/Web/CSS/grid-row>
///
/// &nbsp;
pub grid_row: GridPlacement,

/// The column in which a grid item starts and how many columns it spans.
///
/// <https://developer.mozilla.org/en-US/docs/Web/CSS/grid-column>
///
/// &nbsp;
pub grid_column: GridPlacement,
}

impl Style {
Expand Down