Skip to content

Commit

Permalink
Add const to methods and const defaults to bevy_ui (bevyengine#5542)
Browse files Browse the repository at this point in the history
# Objective
- Fixes bevyengine#5529 

## Solution
- Add assosciated constants named DEFAULT to as many types as possible
- Add const to as many methods in bevy_ui as possible

I have not applied the same treatment to the bundles in bevy_ui as it would require going into other bevy crates to implement const defaults for structs in bevy_text or relies on UiImage which calls HandleUntyped.typed() which isn't const safe.

Alternatively the defaults could relatively easily be turned into a macro to regain some of the readability and conciseness at the cost of explicitness.
Such a macro that partially implements this exists as a crate here: [const-default](https://docs.rs/const-default/latest/const_default/derive.ConstDefault.html) but does not support enums.

Let me know if there's anything I've missed or if I should push further into other crates.

Co-authored-by: Carter Anderson <[email protected]>
  • Loading branch information
2 people authored and alradish committed Jan 22, 2023
1 parent 411c1de commit eb4094b
Show file tree
Hide file tree
Showing 3 changed files with 238 additions and 61 deletions.
31 changes: 23 additions & 8 deletions crates/bevy_ui/src/focus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,32 +31,47 @@ use smallvec::SmallVec;
///
/// Note that you can also control the visibility of a node using the [`Display`](crate::ui_node::Display) property,
/// which fully collapses it during layout calculations.
#[derive(
Component, Copy, Clone, Default, Eq, PartialEq, Debug, Reflect, Serialize, Deserialize,
)]
#[derive(Component, Copy, Clone, Eq, PartialEq, Debug, Reflect, Serialize, Deserialize)]
#[reflect(Component, Serialize, Deserialize, PartialEq)]
pub enum Interaction {
/// The node has been clicked
Clicked,
/// The node has been hovered over
Hovered,
/// Nothing has happened
#[default]
None,
}

impl Interaction {
const DEFAULT: Self = Self::None;
}

impl Default for Interaction {
fn default() -> Self {
Self::DEFAULT
}
}

/// Describes whether the node should block interactions with lower nodes
#[derive(
Component, Copy, Clone, Default, Eq, PartialEq, Debug, Reflect, Serialize, Deserialize,
)]
#[derive(Component, Copy, Clone, Eq, PartialEq, Debug, Reflect, Serialize, Deserialize)]
#[reflect(Component, Serialize, Deserialize, PartialEq)]
pub enum FocusPolicy {
/// Blocks interaction
#[default]
Block,
/// Lets interaction pass through
Pass,
}

impl FocusPolicy {
const DEFAULT: Self = Self::Block;
}

impl Default for FocusPolicy {
fn default() -> Self {
Self::DEFAULT
}
}

/// Contains entities whose Interaction should be set to None
#[derive(Default)]
pub struct State {
Expand Down
32 changes: 28 additions & 4 deletions crates/bevy_ui/src/geometry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ use std::ops::{Div, DivAssign, Mul, MulAssign};
/// bottom: Val::Px(40.0),
/// };
/// ```
#[derive(Copy, Clone, PartialEq, Debug, Default, Reflect)]
#[derive(Copy, Clone, PartialEq, Debug, Reflect)]
#[reflect(PartialEq)]
pub struct UiRect {
/// The value corresponding to the left side of the UI rect.
Expand All @@ -133,6 +133,13 @@ pub struct UiRect {
}

impl UiRect {
pub const DEFAULT: Self = Self {
left: Val::DEFAULT,
right: Val::DEFAULT,
top: Val::DEFAULT,
bottom: Val::DEFAULT,
};

/// Creates a new [`UiRect`] from the values specified.
///
/// # Example
Expand All @@ -152,7 +159,7 @@ impl UiRect {
/// assert_eq!(ui_rect.top, Val::Px(30.0));
/// assert_eq!(ui_rect.bottom, Val::Px(40.0));
/// ```
pub fn new(left: Val, right: Val, top: Val, bottom: Val) -> Self {
pub const fn new(left: Val, right: Val, top: Val, bottom: Val) -> Self {
UiRect {
left,
right,
Expand All @@ -175,7 +182,7 @@ impl UiRect {
/// assert_eq!(ui_rect.top, Val::Px(10.0));
/// assert_eq!(ui_rect.bottom, Val::Px(10.0));
/// ```
pub fn all(value: Val) -> Self {
pub const fn all(value: Val) -> Self {
UiRect {
left: value,
right: value,
Expand Down Expand Up @@ -313,10 +320,16 @@ impl UiRect {
}
}

impl Default for UiRect {
fn default() -> Self {
Self::DEFAULT
}
}

/// A 2-dimensional area defined by a width and height.
///
/// It is commonly used to define the size of a text or UI element.
#[derive(Copy, Clone, PartialEq, Debug, Default, Reflect)]
#[derive(Copy, Clone, PartialEq, Debug, Reflect)]
#[reflect(PartialEq)]
pub struct Size {
/// The width of the 2-dimensional area.
Expand All @@ -326,6 +339,11 @@ pub struct Size {
}

impl Size {
pub const DEFAULT: Self = Self {
width: Val::DEFAULT,
height: Val::DEFAULT,
};

/// Creates a new [`Size`] from a width and a height.
///
/// # Example
Expand Down Expand Up @@ -355,6 +373,12 @@ impl Size {
};
}

impl Default for Size {
fn default() -> Self {
Self::DEFAULT
}
}

impl From<(Val, Val)> for Size {
fn from(vals: (Val, Val)) -> Self {
Self {
Expand Down
Loading

0 comments on commit eb4094b

Please sign in to comment.