Repository: bevyengine/bevy
File: crates/bevy_ui/src/render/mod.rs (Rust)

Code before:
            &ComputedNode,
            &GlobalTransform,
            &InheritedVisibility,
            Option<&CalculatedClip>,
            &ComputedNodeTarget,
            &TextLayoutInfo,
        )>,
    >,
    text_background_colors_query: Extract<Query<&TextBackgroundColor>>,
    camera_map: Extract<UiCameraMap>,
) {
    let mut camera_mapper = camera_map.get_mapper();
    for (entity, uinode, global_transform, inherited_visibility, clip, camera, text_layout_info) in
        &uinode_query
    {
        // Skip if not visible or if size is set to zero (e.g. when a parent is set to `Display::None`)
        if !inherited_visibility.get() || uinode.is_empty() {
            continue;
        }

        let Some(extracted_camera_entity) = camera_mapper.map(camera) else {
            continue;
        };

        let size = -0.5 * uinode.size().extend(0.);
        let transform = global_transform.affine() * bevy_math::Affine3A::from_translation(size);

        for &(section_entity, rect) in text_layout_info.section_rects.iter() {
            let Ok(text_background_color) = text_background_colors_query.get(section_entity) else {
                continue;
            };

            extracted_uinodes.uinodes.push(ExtractedUiNode {
                render_entity: commands.spawn(TemporaryRenderEntity).id(),
                stack_index: uinode.stack_index,
                color: text_background_color.0.to_linear(),
                rect: Rect {
                    min: Vec2::ZERO,
                    max: rect.size(),
                },
                clip: clip.map(|clip| clip.clip),
                image: AssetId::default(),
                extracted_camera_entity,
                item: ExtractedUiItem::Node {
                    atlas_scaling: None,
                    transform: transform * Mat4::from_translation(rect.center().extend(0.)),
                    flip_x: false,
                    flip_y: false,
                    border: uinode.border(),
                    border_radius: uinode.border_radius(),
                    node_type: NodeType::Rect,

Review comment (refactor):
`size` is a bit unclear for me as it's used as a translation. I would remove the temporary
```suggestion
        let transform = global_transform.affine() * bevy_math::Affine3A::from_translation(-0.5 * uinode.size().extend(0.));
```
or rename it
```suggestion
        let size_offset = -0.5 * uinode.size().extend(0.);
        let transform = global_transform.affine() * bevy_math::Affine3A::from_translation(size_offset);
```
(Even better would be to have a comment or helper to compute this specific offset, because it seems to be used at least 3 times in the module)

Code after:
            &ComputedNode,
            &GlobalTransform,
            &InheritedVisibility,
            Option<&CalculatedClip>,
            &ComputedNodeTarget,
            &TextLayoutInfo,
        )>,
    >,
    text_background_colors_query: Extract<Query<&TextBackgroundColor>>,
    camera_map: Extract<UiCameraMap>,
) {
    let mut camera_mapper = camera_map.get_mapper();
    for (entity, uinode, global_transform, inherited_visibility, clip, camera, text_layout_info) in
        &uinode_query
    {
        // Skip if not visible or if size is set to zero (e.g. when a parent is set to `Display::None`)
        if !inherited_visibility.get() || uinode.is_empty() {
            continue;
        }

        let Some(extracted_camera_entity) = camera_mapper.map(camera) else {
            continue;
        };

        let transform = global_transform.affine()
            * bevy_math::Affine3A::from_translation(-0.5 * uinode.size().extend(0.));

        for &(section_entity, rect) in text_layout_info.section_rects.iter() {
            let Ok(text_background_color) = text_background_colors_query.get(section_entity) else {
                continue;
            };

            extracted_uinodes.uinodes.push(ExtractedUiNode {
                render_entity: commands.spawn(TemporaryRenderEntity).id(),
                stack_index: uinode.stack_index,
                color: text_background_color.0.to_linear(),
                rect: Rect {
                    min: Vec2::ZERO,
                    max: rect.size(),
                },
                clip: clip.map(|clip| clip.clip),
                image: AssetId::default(),
                extracted_camera_entity,
                item: ExtractedUiItem::Node {
                    atlas_scaling: None,
                    transform: transform * Mat4::from_translation(rect.center().extend(0.)),
                    flip_x: false,
                    flip_y: false,
                    border: uinode.border(),
                    border_radius: uinode.border_radius(),
                    node_type: NodeType::Rect,