Skip to content

Commit 572f0c1

Browse files
ickshonpemockersf
authored andcommitted
Text2d scalefactor change detection fix (#16264)
# Objective Text2d doesn't respond to changes to the window scalefactor. Fixes #16223 ## Solution In `update_text2d_layout` store the previous scale factor in a `Local` instead and check against the current scale factor to detect changes. It seems like previously the text wasn't updated because of a bug with the `WindowScaleFactorChanged` event and it isn't emitted after changes to the scale factor. That needs to be looked into, but this will work for now. ## Testing Really simple app that draws a big message in the middle of the window: ``` use bevy::prelude::*; fn main() { App::new() .add_plugins(DefaultPlugins) .add_systems(Startup, setup) .run(); } fn setup(mut commands: Commands) { commands.spawn(Camera2d); commands.spawn(( Text2d::new("Hello"), TextFont { font_size: 400., ..Default::default() }, )); } ``` Looks fine: <img width="500" alt="hello1" src="https://github.com/user-attachments/assets/5320746b-687e-4682-9e4c-bc43ab7ff9d3"> On main, after changing the monitor's scale factor: <img width="500" alt="hello2" src="https://github.com/user-attachments/assets/486cea16-fc44-4d66-9468-6f68905d4196"> With this PR the text maintains the same size and position after the scale factor is changed.
1 parent 76a44cc commit 572f0c1

File tree

1 file changed

+6
-8
lines changed

1 file changed

+6
-8
lines changed

crates/bevy_text/src/text2d.rs

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@ use bevy_ecs::component::Component;
1111
use bevy_ecs::{
1212
change_detection::{DetectChanges, Ref},
1313
entity::Entity,
14-
event::EventReader,
1514
prelude::{ReflectComponent, With},
1615
query::{Changed, Without},
1716
system::{Commands, Local, Query, Res, ResMut},
@@ -30,7 +29,7 @@ use bevy_sprite::{Anchor, ExtractedSprite, ExtractedSprites, SpriteSource, Textu
3029
use bevy_transform::components::Transform;
3130
use bevy_transform::prelude::GlobalTransform;
3231
use bevy_utils::HashSet;
33-
use bevy_window::{PrimaryWindow, Window, WindowScaleFactorChanged};
32+
use bevy_window::{PrimaryWindow, Window};
3433

3534
/// [`Text2dBundle`] was removed in favor of required components.
3635
/// The core component is now [`Text2d`] which can contain a single text segment.
@@ -235,12 +234,12 @@ pub fn extract_text2d_sprite(
235234
/// It does not modify or observe existing ones.
236235
#[allow(clippy::too_many_arguments)]
237236
pub fn update_text2d_layout(
237+
mut last_scale_factor: Local<f32>,
238238
// Text items which should be reprocessed again, generally when the font hasn't loaded yet.
239239
mut queue: Local<HashSet<Entity>>,
240240
mut textures: ResMut<Assets<Image>>,
241241
fonts: Res<Assets<Font>>,
242242
windows: Query<&Window, With<PrimaryWindow>>,
243-
mut scale_factor_changed: EventReader<WindowScaleFactorChanged>,
244243
mut texture_atlases: ResMut<Assets<TextureAtlasLayout>>,
245244
mut font_atlas_sets: ResMut<FontAtlasSets>,
246245
mut text_pipeline: ResMut<TextPipeline>,
@@ -255,9 +254,6 @@ pub fn update_text2d_layout(
255254
mut font_system: ResMut<CosmicFontSystem>,
256255
mut swash_cache: ResMut<SwashCache>,
257256
) {
258-
// We need to consume the entire iterator, hence `last`
259-
let factor_changed = scale_factor_changed.read().last().is_some();
260-
261257
// TODO: Support window-independent scaling: https://github.com/bevyengine/bevy/issues/5621
262258
let scale_factor = windows
263259
.get_single()
@@ -266,6 +262,9 @@ pub fn update_text2d_layout(
266262

267263
let inverse_scale_factor = scale_factor.recip();
268264

265+
let factor_changed = *last_scale_factor != scale_factor;
266+
*last_scale_factor = scale_factor;
267+
269268
for (entity, block, bounds, text_layout_info, mut computed) in &mut text_query {
270269
if factor_changed
271270
|| computed.needs_rerender()
@@ -359,7 +358,7 @@ mod tests {
359358

360359
use bevy_app::{App, Update};
361360
use bevy_asset::{load_internal_binary_asset, Handle};
362-
use bevy_ecs::{event::Events, schedule::IntoSystemConfigs};
361+
use bevy_ecs::schedule::IntoSystemConfigs;
363362

364363
use crate::{detect_text_needs_rerender, TextIterScratch};
365364

@@ -374,7 +373,6 @@ mod tests {
374373
.init_resource::<Assets<Image>>()
375374
.init_resource::<Assets<TextureAtlasLayout>>()
376375
.init_resource::<FontAtlasSets>()
377-
.init_resource::<Events<WindowScaleFactorChanged>>()
378376
.init_resource::<TextPipeline>()
379377
.init_resource::<CosmicFontSystem>()
380378
.init_resource::<SwashCache>()

0 commit comments

Comments
 (0)