Skip to content

Commit

Permalink
feat: derive some common traits on some UI types (#12532)
Browse files Browse the repository at this point in the history
# Objective

- working with UI components in Bevy, I found myself wanting some of
these common traits, like `PartialEq` for comparing simple types

## Solution

- I added only (hopefully) uncontroversial `derive`s for some common UI
types

Note that many types, unfortunately, can't have `PartialEq` `derive`d
for them, because they contain `f32`s and / or `Vec`s.
  • Loading branch information
awwsmm authored Mar 18, 2024
1 parent ea6540d commit fc4716f
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 17 deletions.
15 changes: 3 additions & 12 deletions crates/bevy_text/src/text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use serde::{Deserialize, Serialize};

use crate::Font;

#[derive(Component, Debug, Clone, Reflect)]
#[derive(Component, Debug, Clone, Default, Reflect)]
#[reflect(Component, Default)]
pub struct Text {
pub sections: Vec<TextSection>,
Expand All @@ -18,16 +18,6 @@ pub struct Text {
pub linebreak_behavior: BreakLineOn,
}

impl Default for Text {
fn default() -> Self {
Self {
sections: Default::default(),
justify: JustifyText::Left,
linebreak_behavior: BreakLineOn::WordBoundary,
}
}
}

impl Text {
/// Constructs a [`Text`] with a single section.
///
Expand Down Expand Up @@ -219,12 +209,13 @@ impl Default for TextStyle {
}

/// Determines how lines will be broken when preventing text from running out of bounds.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Reflect, Serialize, Deserialize)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, Reflect, Serialize, Deserialize)]
#[reflect(Serialize, Deserialize)]
pub enum BreakLineOn {
/// Uses the [Unicode Line Breaking Algorithm](https://www.unicode.org/reports/tr14/).
/// Lines will be broken up at the nearest suitable word boundary, usually a space.
/// This behavior suits most cases, as it keeps words intact across linebreaks.
#[default]
WordBoundary,
/// Lines will be broken without discrimination on any character that would leave bounds.
/// This is closer to the behavior one might expect from text in a terminal.
Expand Down
8 changes: 4 additions & 4 deletions crates/bevy_ui/src/ui_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ use thiserror::Error;
/// - [`RelativeCursorPosition`](crate::RelativeCursorPosition)
/// to obtain the cursor position relative to this node
/// - [`Interaction`](crate::Interaction) to obtain the interaction state of this node
#[derive(Component, Debug, Copy, Clone, Reflect)]
#[derive(Component, Debug, Copy, Clone, PartialEq, Reflect)]
#[reflect(Component, Default)]
pub struct Node {
/// The order of the node in the UI layout.
Expand Down Expand Up @@ -1590,7 +1590,7 @@ pub enum GridPlacementError {
/// The background color of the node
///
/// This serves as the "fill" color.
#[derive(Component, Copy, Clone, Debug, Reflect)]
#[derive(Component, Copy, Clone, Debug, PartialEq, Reflect)]
#[reflect(Component, Default)]
#[cfg_attr(
feature = "serialize",
Expand All @@ -1616,7 +1616,7 @@ impl<T: Into<Color>> From<T> for BackgroundColor {
}

/// The border color of the UI node.
#[derive(Component, Copy, Clone, Debug, Reflect)]
#[derive(Component, Copy, Clone, Debug, PartialEq, Reflect)]
#[reflect(Component, Default)]
#[cfg_attr(
feature = "serialize",
Expand Down Expand Up @@ -1796,7 +1796,7 @@ pub struct CalculatedClip {
/// `ZIndex::Local(n)` and `ZIndex::Global(n)` for root nodes.
///
/// Nodes without this component will be treated as if they had a value of `ZIndex::Local(0)`.
#[derive(Component, Copy, Clone, Debug, Reflect)]
#[derive(Component, Copy, Clone, Debug, PartialEq, Eq, Reflect)]
#[reflect(Component, Default)]
pub enum ZIndex {
/// Indicates the order in which this node should be rendered relative to its siblings.
Expand Down
2 changes: 1 addition & 1 deletion crates/bevy_ui/src/widget/button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ use bevy_reflect::std_traits::ReflectDefault;
use bevy_reflect::Reflect;

/// Marker struct for buttons
#[derive(Component, Debug, Default, Clone, Copy, Reflect)]
#[derive(Component, Debug, Default, Clone, Copy, PartialEq, Eq, Reflect)]
#[reflect(Component, Default)]
pub struct Button;

0 comments on commit fc4716f

Please sign in to comment.