Rename JustifyText to Justify (#19522)

# Objective

Rename `JustifyText`:

* The name `JustifyText` is just ugly.
* It's inconsistent since no other `bevy_text` types have a `Text-`
suffix, only prefix.
* It's inconsistent with the other text layout enum `Linebreak` which
doesn't have a prefix or suffix.

Fixes #19521.

## Solution

Rename `JustifyText` to `Justify`.

Without other context, it's natural to assume the name `Justify` refers
to text justification.

---------

Co-authored-by: Alice Cecile <alice.i.cecile@gmail.com>
This commit is contained in:
ickshonpe 2025-06-09 20:59:48 +01:00 committed by GitHub
parent 437c4d5b25
commit 02fa833be1
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
32 changed files with 75 additions and 67 deletions

View File

@ -5,7 +5,7 @@ use bevy_reflect::{std_traits::ReflectDefault, Reflect};
/// The maximum width and height of text. The text will wrap according to the specified size.
///
/// Characters out of the bounds after wrapping will be truncated. Text is aligned according to the
/// specified [`JustifyText`](crate::text::JustifyText).
/// specified [`Justify`](crate::text::Justify).
///
/// Note: only characters that are completely out of the bounds will be truncated, so this is not a
/// reliable limit if it is necessary to contain the text strictly in the bounds. Currently this

View File

@ -61,7 +61,7 @@ pub use text_access::*;
pub mod prelude {
#[doc(hidden)]
pub use crate::{
Font, JustifyText, LineBreak, Text2d, Text2dReader, Text2dWriter, TextColor, TextError,
Font, Justify, LineBreak, Text2d, Text2dReader, Text2dWriter, TextColor, TextError,
TextFont, TextLayout, TextSpan,
};
}

View File

@ -16,8 +16,8 @@ use bevy_reflect::{std_traits::ReflectDefault, Reflect};
use cosmic_text::{Attrs, Buffer, Family, Metrics, Shaping, Wrap};
use crate::{
error::TextError, ComputedTextBlock, Font, FontAtlasSets, FontSmoothing, JustifyText,
LineBreak, PositionedGlyph, TextBounds, TextEntity, TextFont, TextLayout,
error::TextError, ComputedTextBlock, Font, FontAtlasSets, FontSmoothing, Justify, LineBreak,
PositionedGlyph, TextBounds, TextEntity, TextFont, TextLayout,
};
/// A wrapper resource around a [`cosmic_text::FontSystem`]
@ -88,7 +88,7 @@ impl TextPipeline {
fonts: &Assets<Font>,
text_spans: impl Iterator<Item = (Entity, usize, &'a str, &'a TextFont, Color)>,
linebreak: LineBreak,
justify: JustifyText,
justify: Justify,
bounds: TextBounds,
scale_factor: f64,
computed: &mut ComputedTextBlock,
@ -201,7 +201,7 @@ impl TextPipeline {
// Workaround for alignment not working for unbounded text.
// See https://github.com/pop-os/cosmic-text/issues/343
if bounds.width.is_none() && justify != JustifyText::Left {
if bounds.width.is_none() && justify != Justify::Left {
let dimensions = buffer_dimensions(buffer);
// `set_size` causes a re-layout to occur.
buffer.set_size(font_system, Some(dimensions.x), bounds.height);

View File

@ -116,19 +116,19 @@ impl Default for ComputedTextBlock {
pub struct TextLayout {
/// The text's internal alignment.
/// Should not affect its position within a container.
pub justify: JustifyText,
pub justify: Justify,
/// How the text should linebreak when running out of the bounds determined by `max_size`.
pub linebreak: LineBreak,
}
impl TextLayout {
/// Makes a new [`TextLayout`].
pub const fn new(justify: JustifyText, linebreak: LineBreak) -> Self {
pub const fn new(justify: Justify, linebreak: LineBreak) -> Self {
Self { justify, linebreak }
}
/// Makes a new [`TextLayout`] with the specified [`JustifyText`].
pub fn new_with_justify(justify: JustifyText) -> Self {
/// Makes a new [`TextLayout`] with the specified [`Justify`].
pub fn new_with_justify(justify: Justify) -> Self {
Self::default().with_justify(justify)
}
@ -143,8 +143,8 @@ impl TextLayout {
Self::default().with_no_wrap()
}
/// Returns this [`TextLayout`] with the specified [`JustifyText`].
pub const fn with_justify(mut self, justify: JustifyText) -> Self {
/// Returns this [`TextLayout`] with the specified [`Justify`].
pub const fn with_justify(mut self, justify: Justify) -> Self {
self.justify = justify;
self
}
@ -246,7 +246,7 @@ impl From<String> for TextSpan {
/// [`TextBounds`](super::bounds::TextBounds) component with an explicit `width` value.
#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, Hash, Reflect, Serialize, Deserialize)]
#[reflect(Serialize, Deserialize, Clone, PartialEq, Hash)]
pub enum JustifyText {
pub enum Justify {
/// Leftmost character is immediately to the right of the render position.
/// Bounds start from the render position and advance rightwards.
#[default]
@ -263,13 +263,13 @@ pub enum JustifyText {
Justified,
}
impl From<JustifyText> for cosmic_text::Align {
fn from(justify: JustifyText) -> Self {
impl From<Justify> for cosmic_text::Align {
fn from(justify: Justify) -> Self {
match justify {
JustifyText::Left => cosmic_text::Align::Left,
JustifyText::Center => cosmic_text::Align::Center,
JustifyText::Right => cosmic_text::Align::Right,
JustifyText::Justified => cosmic_text::Align::Justified,
Justify::Left => cosmic_text::Align::Left,
Justify::Center => cosmic_text::Align::Center,
Justify::Right => cosmic_text::Align::Right,
Justify::Justified => cosmic_text::Align::Justified,
}
}
}

View File

@ -51,7 +51,7 @@ use bevy_window::{PrimaryWindow, Window};
/// # use bevy_color::Color;
/// # use bevy_color::palettes::basic::BLUE;
/// # use bevy_ecs::world::World;
/// # use bevy_text::{Font, JustifyText, Text2d, TextLayout, TextFont, TextColor, TextSpan};
/// # use bevy_text::{Font, Justify, Text2d, TextLayout, TextFont, TextColor, TextSpan};
/// #
/// # let font_handle: Handle<Font> = Default::default();
/// # let mut world = World::default();
@ -73,7 +73,7 @@ use bevy_window::{PrimaryWindow, Window};
/// // With text justification.
/// world.spawn((
/// Text2d::new("hello world\nand bevy!"),
/// TextLayout::new_with_justify(JustifyText::Center)
/// TextLayout::new_with_justify(Justify::Center)
/// ));
///
/// // With spans

View File

@ -61,7 +61,7 @@ impl Default for TextNodeFlags {
/// # use bevy_color::Color;
/// # use bevy_color::palettes::basic::BLUE;
/// # use bevy_ecs::world::World;
/// # use bevy_text::{Font, JustifyText, TextLayout, TextFont, TextColor, TextSpan};
/// # use bevy_text::{Font, Justify, TextLayout, TextFont, TextColor, TextSpan};
/// # use bevy_ui::prelude::Text;
/// #
/// # let font_handle: Handle<Font> = Default::default();
@ -84,7 +84,7 @@ impl Default for TextNodeFlags {
/// // With text justification.
/// world.spawn((
/// Text::new("hello world\nand bevy!"),
/// TextLayout::new_with_justify(JustifyText::Center)
/// TextLayout::new_with_justify(Justify::Center)
/// ));
///
/// // With spans

View File

@ -129,7 +129,7 @@ fn setup_sprites(mut commands: Commands, asset_server: Res<AssetServer>) {
cmd.with_children(|builder| {
builder.spawn((
Text2d::new(rect.text),
TextLayout::new_with_justify(JustifyText::Center),
TextLayout::new_with_justify(Justify::Center),
TextFont::from_font_size(15.),
Transform::from_xyz(0., -0.5 * rect.size.y - 10., 0.),
bevy::sprite::Anchor::TOP_CENTER,
@ -275,7 +275,7 @@ fn setup_texture_atlas(
cmd.with_children(|builder| {
builder.spawn((
Text2d::new(sprite_sheet.text),
TextLayout::new_with_justify(JustifyText::Center),
TextLayout::new_with_justify(Justify::Center),
TextFont::from_font_size(15.),
Transform::from_xyz(0., -0.5 * sprite_sheet.size.y - 10., 0.),
bevy::sprite::Anchor::TOP_CENTER,

View File

@ -94,7 +94,7 @@ fn spawn_sprites(
children![(
Text2d::new(label),
text_style,
TextLayout::new_with_justify(JustifyText::Center),
TextLayout::new_with_justify(Justify::Center),
Transform::from_xyz(0., -0.5 * size.y - 10., 0.0),
bevy::sprite::Anchor::TOP_CENTER,
)],

View File

@ -40,7 +40,7 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
font_size: 50.0,
..default()
};
let text_justification = JustifyText::Center;
let text_justification = Justify::Center;
commands.spawn(Camera2d);
// Demonstrate changing translation
commands.spawn((
@ -78,7 +78,7 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
children![(
Text2d::new("this text wraps in the box\n(Unicode linebreaks)"),
slightly_smaller_text_font.clone(),
TextLayout::new(JustifyText::Left, LineBreak::WordBoundary),
TextLayout::new(Justify::Left, LineBreak::WordBoundary),
// Wrap text in the rectangle
TextBounds::from(box_size),
// Ensure the text is drawn on top of the box
@ -94,7 +94,7 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
children![(
Text2d::new("this text wraps in the box\n(AnyCharacter linebreaks)"),
slightly_smaller_text_font.clone(),
TextLayout::new(JustifyText::Left, LineBreak::AnyCharacter),
TextLayout::new(Justify::Left, LineBreak::AnyCharacter),
// Wrap text in the rectangle
TextBounds::from(other_box_size),
// Ensure the text is drawn on top of the box
@ -104,11 +104,11 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
// Demonstrate font smoothing off
commands.spawn((
Text2d::new("This text has\nFontSmoothing::None\nAnd JustifyText::Center"),
Text2d::new("This text has\nFontSmoothing::None\nAnd Justify::Center"),
slightly_smaller_text_font
.clone()
.with_font_smoothing(FontSmoothing::None),
TextLayout::new_with_justify(JustifyText::Center),
TextLayout::new_with_justify(Justify::Center),
Transform::from_translation(Vec3::new(-400.0, -250.0, 0.0)),
));

View File

@ -279,7 +279,7 @@ fn create_label(
commands.spawn((
Text2d::new(text),
text_style,
TextLayout::new_with_justify(JustifyText::Center),
TextLayout::new_with_justify(Justify::Center),
Transform {
translation: Vec3::new(translation.0, translation.1, translation.2),
..default()

View File

@ -180,7 +180,7 @@ fn setup_image_viewer_scene(
..default()
},
TextColor(Color::BLACK),
TextLayout::new_with_justify(JustifyText::Center),
TextLayout::new_with_justify(Justify::Center),
Node {
align_self: AlignSelf::Center,
margin: UiRect::all(Val::Auto),

View File

@ -151,7 +151,7 @@ fn setup(
..default()
},
TextColor(Color::Srgba(Srgba::RED)),
TextLayout::new_with_justify(JustifyText::Center),
TextLayout::new_with_justify(Justify::Center),
))
// Mark as an animation target.
.insert(AnimationTarget {

View File

@ -277,7 +277,7 @@ fn setup_node_rects(commands: &mut Commands) {
..default()
},
TextColor(ANTIQUE_WHITE.into()),
TextLayout::new_with_justify(JustifyText::Center),
TextLayout::new_with_justify(Justify::Center),
))
.id();

View File

@ -334,7 +334,7 @@ fn add_mask_group_control(
} else {
selected_button_text_style.clone()
},
TextLayout::new_with_justify(JustifyText::Center),
TextLayout::new_with_justify(Justify::Center),
Node {
flex_grow: 1.0,
margin: UiRect::vertical(Val::Px(3.0)),

View File

@ -54,7 +54,7 @@ fn spawn_text(mut commands: Commands, mut reader: EventReader<StreamEvent>) {
for (per_frame, event) in reader.read().enumerate() {
commands.spawn((
Text2d::new(event.0.to_string()),
TextLayout::new_with_justify(JustifyText::Center),
TextLayout::new_with_justify(Justify::Center),
Transform::from_xyz(per_frame as f32 * 100.0, 300.0, 0.0),
));
}

View File

@ -94,7 +94,7 @@ fn setup_ui(mut commands: Commands) {
commands
.spawn((
Text::default(),
TextLayout::new_with_justify(JustifyText::Center),
TextLayout::new_with_justify(Justify::Center),
Node {
align_self: AlignSelf::Center,
justify_self: JustifySelf::Center,

View File

@ -379,7 +379,7 @@ fn setup_text(mut commands: Commands, cameras: Query<(Entity, &Camera)>) {
children![(
Text::default(),
HeaderText,
TextLayout::new_with_justify(JustifyText::Center),
TextLayout::new_with_justify(Justify::Center),
children![
TextSpan::new("Primitive: "),
TextSpan(format!("{text}", text = PrimitiveSelected::default())),

View File

@ -158,7 +158,7 @@ fn setup_scene(
..default()
},
TextColor::BLACK,
TextLayout::new_with_justify(JustifyText::Center),
TextLayout::new_with_justify(Justify::Center),
));
}

View File

@ -74,7 +74,7 @@ fn setup(mut commands: Commands, args: Res<Args>) {
..Default::default()
};
let text_block = TextLayout {
justify: JustifyText::Left,
justify: Justify::Left,
linebreak: LineBreak::AnyCharacter,
};

View File

@ -47,7 +47,7 @@ struct Args {
#[argh(switch)]
no_frustum_culling: bool,
/// whether the text should use `JustifyText::Center`.
/// whether the text should use `Justify::Center`.
#[argh(switch)]
center: bool,
}
@ -132,9 +132,9 @@ fn setup(mut commands: Commands, font: Res<FontHandle>, args: Res<Args>) {
random_text_font(&mut rng, &args, font.0.clone()),
TextColor(color.into()),
TextLayout::new_with_justify(if args.center {
JustifyText::Center
Justify::Center
} else {
JustifyText::Left
Justify::Left
}),
Transform {
translation,

View File

@ -69,7 +69,7 @@ fn spawn(mut commands: Commands, asset_server: Res<AssetServer>) {
.spawn((
Text2d::default(),
TextLayout {
justify: JustifyText::Center,
justify: Justify::Center,
linebreak: LineBreak::AnyCharacter,
},
TextBounds::default(),

View File

@ -151,10 +151,10 @@ mod text {
commands.spawn((Camera2d, DespawnOnExitState(super::Scene::Text)));
for (i, justify) in [
JustifyText::Left,
JustifyText::Right,
JustifyText::Center,
JustifyText::Justified,
Justify::Left,
Justify::Right,
Justify::Center,
Justify::Justified,
]
.into_iter()
.enumerate()
@ -196,7 +196,7 @@ mod text {
fn spawn_anchored_text(
commands: &mut Commands,
dest: Vec3,
justify: JustifyText,
justify: Justify,
bounds: Option<TextBounds>,
) {
commands.spawn((

View File

@ -372,7 +372,7 @@ mod text_wrap {
for (j, message) in messages.into_iter().enumerate() {
commands.entity(root).with_child((
Text(message.clone()),
TextLayout::new(JustifyText::Left, linebreak),
TextLayout::new(Justify::Left, linebreak),
BackgroundColor(Color::srgb(0.8 - j as f32 * 0.3, 0., 0.)),
));
}

View File

@ -102,7 +102,7 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>, mut time: ResMu
..default()
},
TextColor(Color::srgb(0.85, 0.85, 0.85)),
TextLayout::new_with_justify(JustifyText::Center),
TextLayout::new_with_justify(Justify::Center),
),
(
Text::default(),
@ -111,7 +111,7 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>, mut time: ResMu
..default()
},
TextColor(virtual_color),
TextLayout::new_with_justify(JustifyText::Right),
TextLayout::new_with_justify(Justify::Right),
VirtualTime,
),
],

View File

@ -186,7 +186,7 @@ fn setup_ui(
Text::new(button_name),
// And center the text if it flows onto multiple lines
TextLayout {
justify: JustifyText::Center,
justify: Justify::Center,
..default()
},
))

View File

@ -99,7 +99,7 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
parent.spawn((
Text::new("Use the panel on the right to change the Display and Visibility properties for the respective nodes of the panel on the left"),
text_font.clone(),
TextLayout::new_with_justify(JustifyText::Center),
TextLayout::new_with_justify(Justify::Center),
Node {
margin: UiRect::bottom(Val::Px(10.)),
..Default::default()
@ -153,13 +153,13 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
Text::new("Display::None\nVisibility::Hidden\nVisibility::Inherited"),
text_font.clone(),
TextColor(HIDDEN_COLOR),
TextLayout::new_with_justify(JustifyText::Center),
TextLayout::new_with_justify(Justify::Center),
));
builder.spawn((
Text::new("-\n-\n-"),
text_font.clone(),
TextColor(DARK_GRAY.into()),
TextLayout::new_with_justify(JustifyText::Center),
TextLayout::new_with_justify(Justify::Center),
));
builder.spawn((Text::new("The UI Node and its descendants will not be visible and will not be allotted any space in the UI layout.\nThe UI Node will not be visible but will still occupy space in the UI layout.\nThe UI node will inherit the visibility property of its parent. If it has no parent it will be visible."), text_font));
});
@ -396,7 +396,7 @@ where
builder.spawn((
Text(format!("{}::{:?}", Target::<T>::NAME, T::default())),
text_font,
TextLayout::new_with_justify(JustifyText::Center),
TextLayout::new_with_justify(Justify::Center),
));
});
}

View File

@ -251,7 +251,7 @@ fn spawn_button(
} else {
UNHOVERED_TEXT_COLOR
}),
TextLayout::new_with_justify(JustifyText::Center),
TextLayout::new_with_justify(Justify::Center),
));
});
}

View File

@ -40,7 +40,7 @@ fn setup(mut commands: Commands, asset_server: Res<AssetServer>) {
},
TextShadow::default(),
// Set the justification of the Text
TextLayout::new_with_justify(JustifyText::Center),
TextLayout::new_with_justify(Justify::Center),
// Set the style of the Node itself.
Node {
position_type: PositionType::Absolute,

View File

@ -43,7 +43,7 @@ fn setup(mut commands: Commands) {
.spawn((
Text::default(),
TextLayout {
justify: JustifyText::Center,
justify: Justify::Center,
..Default::default()
},
))

View File

@ -64,7 +64,7 @@ fn infotext_system(mut commands: Commands, asset_server: Res<AssetServer>) {
));
builder.spawn((
Text::new(
"This text is right-justified. The `JustifyText` component controls the horizontal alignment of the lines of multi-line text relative to each other, and does not affect the text node's position in the UI layout.",
"This text is right-justified. The `Justify` component controls the horizontal alignment of the lines of multi-line text relative to each other, and does not affect the text node's position in the UI layout.",
),
TextFont {
font: font.clone(),
@ -72,7 +72,7 @@ fn infotext_system(mut commands: Commands, asset_server: Res<AssetServer>) {
..default()
},
TextColor(YELLOW.into()),
TextLayout::new_with_justify(JustifyText::Right),
TextLayout::new_with_justify(Justify::Right),
Node {
max_width: Val::Px(300.),
..default()
@ -114,7 +114,7 @@ fn infotext_system(mut commands: Commands, asset_server: Res<AssetServer>) {
..default()
},
TextColor(Color::srgb(0.8, 0.2, 0.7)),
TextLayout::new_with_justify(JustifyText::Center),
TextLayout::new_with_justify(Justify::Center),
Node {
max_width: Val::Px(400.),
..default()
@ -130,7 +130,7 @@ fn infotext_system(mut commands: Commands, asset_server: Res<AssetServer>) {
..default()
},
TextColor(YELLOW.into()),
TextLayout::new_with_justify(JustifyText::Left),
TextLayout::new_with_justify(Justify::Left),
Node {
max_width: Val::Px(300.),
..default()
@ -145,7 +145,7 @@ fn infotext_system(mut commands: Commands, asset_server: Res<AssetServer>) {
font_size: 29.0,
..default()
},
TextLayout::new_with_justify(JustifyText::Justified),
TextLayout::new_with_justify(Justify::Justified),
TextColor(GREEN_YELLOW.into()),
Node {
max_width: Val::Px(300.),

View File

@ -117,7 +117,7 @@ fn spawn(mut commands: Commands, asset_server: Res<AssetServer>) {
commands.entity(column_id).with_child((
Text(message.clone()),
text_font.clone(),
TextLayout::new(JustifyText::Left, linebreak),
TextLayout::new(Justify::Left, linebreak),
BackgroundColor(Color::srgb(0.8 - j as f32 * 0.2, 0., 0.)),
));
}

View File

@ -0,0 +1,8 @@
---
title: Renamed `JustifyText` to `Justify`
pull_requests: [19522]
---
`JustifyText` has been renamed to `Justify`.
The `-Text` suffix was inconsistent with the names of other `bevy_text` types and unnecessary since it's natural to assume `Justify` refers to text justification.