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

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

Merged
merged 1 commit into from
Mar 18, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
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,
}
}
}
Comment on lines -21 to -29
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BreakLineOn::WordBoundary is now the Default value, so impl Default for Text can be derived automatically


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)]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ZIndex can impl Eq because its variants are just newtypes around i32s

#[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)]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Marker components can trivially impl many traits

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