66import com .cleanroommc .modularui .theme .WidgetTheme ;
77import com .cleanroommc .modularui .utils .Alignment ;
88import com .cleanroommc .modularui .widget .Widget ;
9+ import com .cleanroommc .modularui .widget .WidgetTree ;
910import com .cleanroommc .modularui .widget .sizer .Box ;
1011
1112import net .minecraft .util .text .TextFormatting ;
@@ -20,13 +21,21 @@ public class TextWidget extends Widget<TextWidget> {
2021 private Boolean shadow = null ;
2122 private float scale = 1f ;
2223
24+ private String lastText = null ;
25+
2326 public TextWidget (IKey key ) {
2427 this .key = key ;
2528 }
2629
2730 @ Override
2831 public void draw (ModularGuiContext context , WidgetTheme widgetTheme ) {
2932 TextRenderer renderer = TextRenderer .SHARED ;
33+ String text = this .key .getFormatted ();
34+ if (this .lastText != null && !this .lastText .equals (text )) {
35+ // scheduling it would resize it on next frame, but we need it now
36+ WidgetTree .resizeInternal (this , false );
37+ }
38+ this .lastText = text ;
3039 renderer .setColor (this .color != null ? this .color : widgetTheme .getTextColor ());
3140 renderer .setAlignment (this .alignment , getArea ().w () + this .scale , getArea ().h ());
3241 renderer .setShadow (this .shadow != null ? this .shadow : widgetTheme .getTextShadow ());
@@ -44,6 +53,7 @@ private TextRenderer simulate(float maxWidth) {
4453 renderer .setScale (this .scale );
4554 renderer .setSimulate (true );
4655 renderer .draw (this .key .getFormatted ());
56+ renderer .setSimulate (false );
4757 return renderer ;
4858 }
4959
@@ -58,8 +68,7 @@ public int getDefaultHeight() {
5868 maxWidth = getScreen ().getScreenArea ().width ;
5969 }
6070 TextRenderer renderer = simulate (maxWidth );
61- Box padding = getArea ().getPadding ();
62- return Math .max (1 , (int ) (renderer .getLastActualHeight () + padding .vertical ()));
71+ return getWidgetHeight (renderer .getLastActualHeight ());
6372 }
6473
6574 @ Override
@@ -69,8 +78,17 @@ public int getDefaultWidth() {
6978 maxWidth = getParent ().getArea ().width ;
7079 }
7180 TextRenderer renderer = simulate (maxWidth );
81+ return getWidgetWidth (renderer .getLastActualWidth ());
82+ }
83+
84+ protected int getWidgetWidth (float actualTextWidth ) {
85+ Box padding = getArea ().getPadding ();
86+ return Math .max (1 , (int ) Math .ceil (actualTextWidth + padding .horizontal ()));
87+ }
88+
89+ protected int getWidgetHeight (float actualTextHeight ) {
7290 Box padding = getArea ().getPadding ();
73- return Math .max (1 , (int ) Math .ceil (renderer . getLastActualWidth () + padding .horizontal ()));
91+ return Math .max (1 , (int ) Math .ceil (actualTextHeight + padding .vertical ()));
7492 }
7593
7694 public IKey getKey () {
0 commit comments