feat: derive some common traits on some UI types (#12532)

# 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.
This commit is contained in:
Andrew 2024-03-17 23:41:50 -04:00 committed by GitHub
parent ea6540dc41
commit fc4716f56c
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 8 additions and 17 deletions

View File

@ -7,7 +7,7 @@ use serde::{Deserialize, Serialize};
use crate::Font; use crate::Font;
#[derive(Component, Debug, Clone, Reflect)] #[derive(Component, Debug, Clone, Default, Reflect)]
#[reflect(Component, Default)] #[reflect(Component, Default)]
pub struct Text { pub struct Text {
pub sections: Vec<TextSection>, pub sections: Vec<TextSection>,
@ -18,16 +18,6 @@ pub struct Text {
pub linebreak_behavior: BreakLineOn, pub linebreak_behavior: BreakLineOn,
} }
impl Default for Text {
fn default() -> Self {
Self {
sections: Default::default(),
justify: JustifyText::Left,
linebreak_behavior: BreakLineOn::WordBoundary,
}
}
}
impl Text { impl Text {
/// Constructs a [`Text`] with a single section. /// Constructs a [`Text`] with a single section.
/// ///
@ -219,12 +209,13 @@ impl Default for TextStyle {
} }
/// Determines how lines will be broken when preventing text from running out of bounds. /// 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)] #[reflect(Serialize, Deserialize)]
pub enum BreakLineOn { pub enum BreakLineOn {
/// Uses the [Unicode Line Breaking Algorithm](https://www.unicode.org/reports/tr14/). /// 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. /// 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. /// This behavior suits most cases, as it keeps words intact across linebreaks.
#[default]
WordBoundary, WordBoundary,
/// Lines will be broken without discrimination on any character that would leave bounds. /// 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. /// This is closer to the behavior one might expect from text in a terminal.

View File

@ -23,7 +23,7 @@ use thiserror::Error;
/// - [`RelativeCursorPosition`](crate::RelativeCursorPosition) /// - [`RelativeCursorPosition`](crate::RelativeCursorPosition)
/// to obtain the cursor position relative to this node /// to obtain the cursor position relative to this node
/// - [`Interaction`](crate::Interaction) to obtain the interaction state of 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)] #[reflect(Component, Default)]
pub struct Node { pub struct Node {
/// The order of the node in the UI layout. /// The order of the node in the UI layout.
@ -1590,7 +1590,7 @@ pub enum GridPlacementError {
/// The background color of the node /// The background color of the node
/// ///
/// This serves as the "fill" color. /// This serves as the "fill" color.
#[derive(Component, Copy, Clone, Debug, Reflect)] #[derive(Component, Copy, Clone, Debug, PartialEq, Reflect)]
#[reflect(Component, Default)] #[reflect(Component, Default)]
#[cfg_attr( #[cfg_attr(
feature = "serialize", feature = "serialize",
@ -1616,7 +1616,7 @@ impl<T: Into<Color>> From<T> for BackgroundColor {
} }
/// The border color of the UI node. /// The border color of the UI node.
#[derive(Component, Copy, Clone, Debug, Reflect)] #[derive(Component, Copy, Clone, Debug, PartialEq, Reflect)]
#[reflect(Component, Default)] #[reflect(Component, Default)]
#[cfg_attr( #[cfg_attr(
feature = "serialize", feature = "serialize",
@ -1796,7 +1796,7 @@ pub struct CalculatedClip {
/// `ZIndex::Local(n)` and `ZIndex::Global(n)` for root nodes. /// `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)`. /// 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)] #[reflect(Component, Default)]
pub enum ZIndex { pub enum ZIndex {
/// Indicates the order in which this node should be rendered relative to its siblings. /// Indicates the order in which this node should be rendered relative to its siblings.

View File

@ -4,6 +4,6 @@ use bevy_reflect::std_traits::ReflectDefault;
use bevy_reflect::Reflect; use bevy_reflect::Reflect;
/// Marker struct for buttons /// Marker struct for buttons
#[derive(Component, Debug, Default, Clone, Copy, Reflect)] #[derive(Component, Debug, Default, Clone, Copy, PartialEq, Eq, Reflect)]
#[reflect(Component, Default)] #[reflect(Component, Default)]
pub struct Button; pub struct Button;