Skip to content

Better TextStyle construct function parameters using Borrow<T> #9765

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

Closed
wants to merge 3 commits into from
Closed
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
9 changes: 6 additions & 3 deletions crates/bevy_text/src/text.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::borrow::Borrow;

use bevy_asset::Handle;
use bevy_ecs::{prelude::Component, reflect::ReflectComponent};
use bevy_reflect::prelude::*;
Expand Down Expand Up @@ -59,7 +61,7 @@ impl Text {
/// ) // You can still add an alignment.
/// .with_alignment(TextAlignment::Center);
/// ```
pub fn from_section(value: impl Into<String>, style: TextStyle) -> Self {
pub fn from_section(value: impl Into<String>, style: impl Borrow<TextStyle>) -> Self {
Self {
sections: vec![TextSection::new(value, style)],
..default()
Expand Down Expand Up @@ -123,10 +125,10 @@ pub struct TextSection {

impl TextSection {
/// Create a new [`TextSection`].
pub fn new(value: impl Into<String>, style: TextStyle) -> Self {
pub fn new(value: impl Into<String>, style: impl Borrow<TextStyle>) -> Self {
Self {
value: value.into(),
style,
style: style.borrow().to_owned(),
}
}

Expand Down Expand Up @@ -189,6 +191,7 @@ 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)]
#[reflect(Serialize, Deserialize)]
Expand Down
8 changes: 4 additions & 4 deletions examples/2d/text2d.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
// Demonstrate changing translation
commands.spawn((
Text2dBundle {
text: Text::from_section("translation", text_style.clone())
text: Text::from_section("translation", &text_style)
.with_alignment(text_alignment),
..default()
},
Expand All @@ -53,7 +53,7 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
// Demonstrate changing rotation
commands.spawn((
Text2dBundle {
text: Text::from_section("rotation", text_style.clone()).with_alignment(text_alignment),
text: Text::from_section("rotation", &text_style).with_alignment(text_alignment),
..default()
},
AnimateRotation,
Expand Down Expand Up @@ -89,7 +89,7 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
text: Text {
sections: vec![TextSection::new(
"this text wraps in the box\n(Unicode linebreaks)",
slightly_smaller_text_style.clone(),
&slightly_smaller_text_style,
)],
alignment: TextAlignment::Left,
linebreak_behavior: BreakLineOn::WordBoundary,
Expand Down Expand Up @@ -121,7 +121,7 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
text: Text {
sections: vec![TextSection::new(
"this text wraps in the box\n(AnyCharacter linebreaks)",
slightly_smaller_text_style.clone(),
&slightly_smaller_text_style,
)],
alignment: TextAlignment::Left,
linebreak_behavior: BreakLineOn::AnyCharacter,
Expand Down