Skip to content

Commit a6be9b4

Browse files
authored
Rename TextBlock to TextLayout (#15797)
# Objective - Improve clarity when spawning a text block. See [this discussion](#15591). ## Solution - Rename `TextBlock` to `TextLayout`.
1 parent b4071ca commit a6be9b4

27 files changed

+89
-87
lines changed

crates/bevy_text/src/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ pub use text_access::*;
6565
pub mod prelude {
6666
#[doc(hidden)]
6767
pub use crate::{
68-
Font, JustifyText, LineBreak, Text2d, TextBlock, TextError, TextReader2d, TextSpan,
68+
Font, JustifyText, LineBreak, Text2d, TextError, TextLayout, TextReader2d, TextSpan,
6969
TextStyle, TextWriter2d,
7070
};
7171
}

crates/bevy_text/src/pipeline.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use cosmic_text::{Attrs, Buffer, Family, Metrics, Shaping, Wrap};
1717

1818
use crate::{
1919
error::TextError, ComputedTextBlock, Font, FontAtlasSets, FontSmoothing, JustifyText,
20-
LineBreak, PositionedGlyph, TextBlock, TextBounds, TextEntity, TextStyle, YAxisOrientation,
20+
LineBreak, PositionedGlyph, TextBounds, TextEntity, TextLayout, TextStyle, YAxisOrientation,
2121
};
2222

2323
/// A wrapper resource around a [`cosmic_text::FontSystem`]
@@ -203,7 +203,7 @@ impl TextPipeline {
203203
fonts: &Assets<Font>,
204204
text_spans: impl Iterator<Item = (Entity, usize, &'a str, &'a TextStyle)>,
205205
scale_factor: f64,
206-
block: &TextBlock,
206+
layout: &TextLayout,
207207
bounds: TextBounds,
208208
font_atlas_sets: &mut FontAtlasSets,
209209
texture_atlases: &mut Assets<TextureAtlasLayout>,
@@ -229,8 +229,8 @@ impl TextPipeline {
229229
let update_result = self.update_buffer(
230230
fonts,
231231
text_spans,
232-
block.linebreak,
233-
block.justify,
232+
layout.linebreak,
233+
layout.justify,
234234
bounds,
235235
scale_factor,
236236
computed,
@@ -337,7 +337,7 @@ impl TextPipeline {
337337
fonts: &Assets<Font>,
338338
text_spans: impl Iterator<Item = (Entity, usize, &'a str, &'a TextStyle)>,
339339
scale_factor: f64,
340-
block: &TextBlock,
340+
layout: &TextLayout,
341341
computed: &mut ComputedTextBlock,
342342
font_system: &mut CosmicFontSystem,
343343
) -> Result<TextMeasureInfo, TextError> {
@@ -350,8 +350,8 @@ impl TextPipeline {
350350
self.update_buffer(
351351
fonts,
352352
text_spans,
353-
block.linebreak,
354-
block.justify,
353+
layout.linebreak,
354+
layout.justify,
355355
MIN_WIDTH_CONTENT_BOUNDS,
356356
scale_factor,
357357
computed,
@@ -386,7 +386,7 @@ impl TextPipeline {
386386
/// Render information for a corresponding text block.
387387
///
388388
/// Contains scaled glyphs and their size. Generated via [`TextPipeline::queue_text`] when an entity has
389-
/// [`TextBlock`] and [`ComputedTextBlock`] components.
389+
/// [`TextLayout`] and [`ComputedTextBlock`] components.
390390
#[derive(Component, Clone, Default, Debug, Reflect)]
391391
#[reflect(Component, Default, Debug)]
392392
pub struct TextLayoutInfo {

crates/bevy_text/src/text.rs

+28-26
Original file line numberDiff line numberDiff line change
@@ -25,18 +25,20 @@ impl Default for CosmicBuffer {
2525
}
2626
}
2727

28-
/// A sub-entity of a [`TextBlock`].
28+
/// A sub-entity of a [`ComputedTextBlock`].
2929
///
3030
/// Returned by [`ComputedTextBlock::entities`].
3131
#[derive(Debug, Copy, Clone)]
3232
pub struct TextEntity {
3333
/// The entity.
3434
pub entity: Entity,
35-
/// Records the hierarchy depth of the entity within a `TextBlock`.
35+
/// Records the hierarchy depth of the entity within a `TextLayout`.
3636
pub depth: usize,
3737
}
3838

39-
/// Computed information for a [`TextBlock`].
39+
/// Computed information for a text block.
40+
///
41+
/// See [`TextLayout`].
4042
///
4143
/// Automatically updated by 2d and UI text systems.
4244
#[derive(Component, Debug, Clone)]
@@ -45,7 +47,7 @@ pub struct ComputedTextBlock {
4547
///
4648
/// This is private because buffer contents are always refreshed from ECS state when writing glyphs to
4749
/// `TextLayoutInfo`. If you want to control the buffer contents manually or use the `cosmic-text`
48-
/// editor, then you need to not use `TextBlock` and instead manually implement the conversion to
50+
/// editor, then you need to not use `TextLayout` and instead manually implement the conversion to
4951
/// `TextLayoutInfo`.
5052
pub(crate) buffer: CosmicBuffer,
5153
/// Entities for all text spans in the block, including the root-level text.
@@ -55,12 +57,12 @@ pub struct ComputedTextBlock {
5557
/// Flag set when any change has been made to this block that should cause it to be rerendered.
5658
///
5759
/// Includes:
58-
/// - [`TextBlock`] changes.
60+
/// - [`TextLayout`] changes.
5961
/// - [`TextStyle`] or `Text2d`/`Text`/`TextSpan` changes anywhere in the block's entity hierarchy.
6062
// TODO: This encompasses both structural changes like font size or justification and non-structural
6163
// changes like text color and font smoothing. This field currently causes UI to 'remeasure' text, even if
6264
// the actual changes are non-structural and can be handled by only rerendering and not remeasuring. A full
63-
// solution would probably require splitting TextBlock and TextStyle into structural/non-structural
65+
// solution would probably require splitting TextLayout and TextStyle into structural/non-structural
6466
// components for more granular change detection. A cost/benefit analysis is needed.
6567
pub(crate) needs_rerender: bool,
6668
}
@@ -103,57 +105,57 @@ impl Default for ComputedTextBlock {
103105
#[derive(Component, Debug, Copy, Clone, Default, Reflect)]
104106
#[reflect(Component, Default, Debug)]
105107
#[require(ComputedTextBlock, TextLayoutInfo)]
106-
pub struct TextBlock {
108+
pub struct TextLayout {
107109
/// The text's internal alignment.
108110
/// Should not affect its position within a container.
109111
pub justify: JustifyText,
110112
/// How the text should linebreak when running out of the bounds determined by `max_size`.
111113
pub linebreak: LineBreak,
112114
}
113115

114-
impl TextBlock {
115-
/// Makes a new [`TextBlock`].
116+
impl TextLayout {
117+
/// Makes a new [`TextLayout`].
116118
pub const fn new(justify: JustifyText, linebreak: LineBreak) -> Self {
117119
Self { justify, linebreak }
118120
}
119121

120-
/// Makes a new [`TextBlock`] with the specified [`JustifyText`].
122+
/// Makes a new [`TextLayout`] with the specified [`JustifyText`].
121123
pub fn new_with_justify(justify: JustifyText) -> Self {
122124
Self::default().with_justify(justify)
123125
}
124126

125-
/// Makes a new [`TextBlock`] with the specified [`LineBreak`].
127+
/// Makes a new [`TextLayout`] with the specified [`LineBreak`].
126128
pub fn new_with_linebreak(linebreak: LineBreak) -> Self {
127129
Self::default().with_linebreak(linebreak)
128130
}
129131

130-
/// Makes a new [`TextBlock`] with soft wrapping disabled.
132+
/// Makes a new [`TextLayout`] with soft wrapping disabled.
131133
/// Hard wrapping, where text contains an explicit linebreak such as the escape sequence `\n`, will still occur.
132134
pub fn new_with_no_wrap() -> Self {
133135
Self::default().with_no_wrap()
134136
}
135137

136-
/// Returns this [`TextBlock`] with the specified [`JustifyText`].
138+
/// Returns this [`TextLayout`] with the specified [`JustifyText`].
137139
pub const fn with_justify(mut self, justify: JustifyText) -> Self {
138140
self.justify = justify;
139141
self
140142
}
141143

142-
/// Returns this [`TextBlock`] with the specified [`LineBreak`].
144+
/// Returns this [`TextLayout`] with the specified [`LineBreak`].
143145
pub const fn with_linebreak(mut self, linebreak: LineBreak) -> Self {
144146
self.linebreak = linebreak;
145147
self
146148
}
147149

148-
/// Returns this [`TextBlock`] with soft wrapping disabled.
150+
/// Returns this [`TextLayout`] with soft wrapping disabled.
149151
/// Hard wrapping, where text contains an explicit linebreak such as the escape sequence `\n`, will still occur.
150152
pub const fn with_no_wrap(mut self) -> Self {
151153
self.linebreak = LineBreak::NoWrap;
152154
self
153155
}
154156
}
155157

156-
/// A span of UI text in a tree of spans under an entity with [`TextBlock`], such as `Text` or `Text2d`.
158+
/// A span of UI text in a tree of spans under an entity with [`TextLayout`] and `Text` or `Text2d`.
157159
///
158160
/// Spans are collected in hierarchy traversal order into a [`ComputedTextBlock`] for layout.
159161
///
@@ -163,13 +165,13 @@ impl TextBlock {
163165
# use bevy_color::Color;
164166
# use bevy_color::palettes::basic::{RED, BLUE};
165167
# use bevy_ecs::World;
166-
# use bevy_text::{Font, TextBlock, TextStyle, TextSection};
168+
# use bevy_text::{Font, TextLayout, TextStyle, TextSection};
167169
168170
# let font_handle: Handle<Font> = Default::default();
169171
# let mut world = World::default();
170172
#
171173
world.spawn((
172-
TextBlock::default(),
174+
TextLayout::default(),
173175
TextStyle {
174176
font: font_handle.clone().into(),
175177
font_size: 60.0,
@@ -257,7 +259,7 @@ impl From<JustifyText> for cosmic_text::Align {
257259
}
258260
}
259261

260-
/// `TextStyle` determines the style of a text span within a [`TextBlock`], specifically
262+
/// `TextStyle` determines the style of a text span within a [`ComputedTextBlock`], specifically
261263
/// the font face, the font size, and the color.
262264
#[derive(Component, Clone, Debug, Reflect)]
263265
#[reflect(Component, Default, Debug)]
@@ -285,7 +287,7 @@ pub struct TextStyle {
285287
}
286288

287289
impl TextStyle {
288-
/// Returns this [`TextBlock`] with the specified [`FontSmoothing`].
290+
/// Returns this [`TextStyle`] with the specified [`FontSmoothing`].
289291
pub const fn with_font_smoothing(mut self, font_smoothing: FontSmoothing) -> Self {
290292
self.font_smoothing = font_smoothing;
291293
self
@@ -358,23 +360,23 @@ pub fn detect_text_needs_rerender<Root: Component>(
358360
Or<(
359361
Changed<Root>,
360362
Changed<TextStyle>,
361-
Changed<TextBlock>,
363+
Changed<TextLayout>,
362364
Changed<Children>,
363365
)>,
364366
With<Root>,
365367
With<TextStyle>,
366-
With<TextBlock>,
368+
With<TextLayout>,
367369
),
368370
>,
369371
changed_spans: Query<
370-
(Entity, Option<&Parent>, Has<TextBlock>),
372+
(Entity, Option<&Parent>, Has<TextLayout>),
371373
(
372374
Or<(
373375
Changed<TextSpan>,
374376
Changed<TextStyle>,
375377
Changed<Children>,
376378
Changed<Parent>, // Included to detect broken text block hierarchies.
377-
Added<TextBlock>,
379+
Added<TextLayout>,
378380
)>,
379381
With<TextSpan>,
380382
With<TextStyle>,
@@ -389,7 +391,7 @@ pub fn detect_text_needs_rerender<Root: Component>(
389391
// Root entity:
390392
// - Root component changed.
391393
// - TextStyle on root changed.
392-
// - TextBlock changed.
394+
// - TextLayout changed.
393395
// - Root children changed (can include additions and removals).
394396
for root in changed_roots.iter() {
395397
let Ok((_, Some(mut computed), _)) = computed.get_mut(root) else {
@@ -406,7 +408,7 @@ pub fn detect_text_needs_rerender<Root: Component>(
406408
// - Span children changed (can include additions and removals).
407409
for (entity, maybe_span_parent, has_text_block) in changed_spans.iter() {
408410
if has_text_block {
409-
warn_once!("found entity {:?} with a TextSpan that has a TextBlock, which should only be on root \
411+
warn_once!("found entity {:?} with a TextSpan that has a TextLayout, which should only be on root \
410412
text entities (that have {}); this warning only prints once",
411413
entity, core::any::type_name::<Root>());
412414
}

crates/bevy_text/src/text2d.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::pipeline::CosmicFontSystem;
22
use crate::{
3-
ComputedTextBlock, Font, FontAtlasSets, LineBreak, PositionedGlyph, SwashCache, TextBlock,
4-
TextBounds, TextError, TextLayoutInfo, TextPipeline, TextReader, TextRoot, TextSpanAccess,
3+
ComputedTextBlock, Font, FontAtlasSets, LineBreak, PositionedGlyph, SwashCache, TextBounds,
4+
TextError, TextLayout, TextLayoutInfo, TextPipeline, TextReader, TextRoot, TextSpanAccess,
55
TextStyle, TextWriter, YAxisOrientation,
66
};
77
use bevy_asset::Assets;
@@ -38,9 +38,9 @@ use bevy_window::{PrimaryWindow, Window, WindowScaleFactorChanged};
3838
/// [Example usage.](https://github.com/bevyengine/bevy/blob/latest/examples/2d/text2d.rs)
3939
///
4040
/// The string in this component is the first 'text span' in a hierarchy of text spans that are collected into
41-
/// a [`TextBlock`]. See [`TextSpan`](crate::TextSpan) for the component used by children of entities with [`Text2d`].
41+
/// a [`ComputedTextBlock`]. See [`TextSpan`](crate::TextSpan) for the component used by children of entities with [`Text2d`].
4242
///
43-
/// With `Text2d` the `justify` field of [`TextBlock`] only affects the internal alignment of a block of text and not its
43+
/// With `Text2d` the `justify` field of [`TextLayout`] only affects the internal alignment of a block of text and not its
4444
/// relative position, which is controlled by the [`Anchor`] component.
4545
/// This means that for a block of text consisting of only one line that doesn't wrap, the `justify` field will have no effect.
4646
///
@@ -50,7 +50,7 @@ use bevy_window::{PrimaryWindow, Window, WindowScaleFactorChanged};
5050
# use bevy_color::Color;
5151
# use bevy_color::palettes::basic::BLUE;
5252
# use bevy_ecs::World;
53-
# use bevy_text::{Font, JustifyText, Text2d, TextBlock, TextStyle};
53+
# use bevy_text::{Font, JustifyText, Text2d, TextLayout, TextStyle};
5454
#
5555
# let font_handle: Handle<Font> = Default::default();
5656
# let mut world = World::default();
@@ -71,14 +71,14 @@ world.spawn((
7171
// With text justification.
7272
world.spawn((
7373
Text2d::new("hello world\nand bevy!"),
74-
TextBlock::new_with_justify(JustifyText::Center)
74+
TextLayout::new_with_justify(JustifyText::Center)
7575
));
7676
```
7777
*/
7878
#[derive(Component, Clone, Debug, Default, Deref, DerefMut, Reflect)]
7979
#[reflect(Component, Default, Debug)]
8080
#[require(
81-
TextBlock,
81+
TextLayout,
8282
TextStyle,
8383
TextBounds,
8484
Anchor,
@@ -230,7 +230,7 @@ pub fn update_text2d_layout(
230230
mut text_pipeline: ResMut<TextPipeline>,
231231
mut text_query: Query<(
232232
Entity,
233-
Ref<TextBlock>,
233+
Ref<TextLayout>,
234234
Ref<TextBounds>,
235235
&mut TextLayoutInfo,
236236
&mut ComputedTextBlock,

crates/bevy_text/src/text_access.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ impl TextIterScratch {
4242
}
4343
}
4444

45-
/// System parameter for reading text spans in a [`TextBlock`](crate::TextBlock).
45+
/// System parameter for reading text spans in a text block.
4646
///
4747
/// `R` is the root text component, and `S` is the text span component on children.
4848
#[derive(SystemParam)]
@@ -184,7 +184,7 @@ impl<'a, R: TextRoot> Drop for TextSpanIter<'a, R> {
184184
}
185185
}
186186

187-
/// System parameter for reading and writing text spans in a [`TextBlock`](crate::TextBlock).
187+
/// System parameter for reading and writing text spans in a text block.
188188
///
189189
/// `R` is the root text component, and `S` is the text span component on children.
190190
#[derive(SystemParam)]

0 commit comments

Comments
 (0)