Skip to content

Commit

Permalink
fix: the error in text.bounds when there is no root canvas (#2543)
Browse files Browse the repository at this point in the history
  • Loading branch information
cptbtptpbcptdtptp authored Feb 8, 2025
1 parent 5ab9ce4 commit 57a9f48
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 1 deletion.
3 changes: 2 additions & 1 deletion packages/ui/src/component/advanced/Text.ts
Original file line number Diff line number Diff line change
Expand Up @@ -598,7 +598,8 @@ export class Text extends UIRenderer implements ITextRenderer {
this._text === "" ||
this._fontSize === 0 ||
(this.enableWrapping && size.x <= 0) ||
(this.overflowMode === OverflowMode.Truncate && size.y <= 0)
(this.overflowMode === OverflowMode.Truncate && size.y <= 0) ||
!this._getRootCanvas()
);
}

Expand Down
20 changes: 20 additions & 0 deletions tests/src/ui/Text.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,26 @@ describe("Text", async () => {
expect(label.enableWrapping).to.eq(true);
});

it("get bounds", () => {
const textWithoutCanvas = root.addComponent(Text);
textWithoutCanvas.text = "hello world";
const bounds = textWithoutCanvas.bounds;
expect(bounds.min).to.deep.include({ x: -50, y: -50, z: 0 });
expect(bounds.max).to.deep.include({ x: 50, y: 50, z: 0 });

const labelBounds = label.bounds;
expect(labelBounds.min).to.deep.include({ x: -50, y: -50, z: 0 });
expect(labelBounds.max).to.deep.include({ x: 50, y: 50, z: 0 });
label.text = "hello world";
const labelBounds2 = label.bounds;
expect(labelBounds2.min).to.deep.include({ x: -50, y: -50, z: 0 });
expect(labelBounds2.max).to.deep.include({ x: 50, y: 50, z: 0 });
(<UITransform>label.entity.transform).size.x = 200;
const labelBounds3 = label.bounds;
expect(labelBounds3.min).to.deep.include({ x: -100, y: -50, z: 0 });
expect(labelBounds3.max).to.deep.include({ x: 100, y: 50, z: 0 });
});

it("emoji", () => {
const textEntity = canvasEntity.createChild("text");

Expand Down

0 comments on commit 57a9f48

Please sign in to comment.