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

[Merged by Bors] - Add const to methods and const defaults to bevy_ui #5542

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 4 commits
Commits
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
31 changes: 23 additions & 8 deletions crates/bevy_ui/src/focus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,32 +30,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
34 changes: 29 additions & 5 deletions crates/bevy_ui/src/geometry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ use std::ops::{Add, AddAssign, Div, DivAssign, Mul, MulAssign, Sub, SubAssign};
/// 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 @@ -134,6 +134,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 @@ -153,7 +160,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 @@ -176,7 +183,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 All @@ -186,10 +193,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 @@ -199,6 +212,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 All @@ -211,11 +229,17 @@ impl Size {
/// assert_eq!(size.width, Val::Px(100.0));
/// assert_eq!(size.height, Val::Px(200.0));
/// ```
pub fn new(width: Val, height: Val) -> Self {
pub const fn new(width: Val, height: Val) -> Self {
Size { width, height }
}
}

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

impl Add<Vec2> for Size {
type Output = Size;

Expand Down
Loading