arcade icon indicating copy to clipboard operation
arcade copied to clipboard

GUI: UILabel lacks call to "fit_contents"

Open alejcas opened this issue 1 year ago • 3 comments

When adding a UILabel with the text "_" the label internally sets its rect size to fit the contents which is 1 character on init. Later on changing this text to something longer will not resize itself to fit the contents resulting in text that is cut...

This happens only on UILabel and is different on what happens with UITextWidget which calls fit_content when the text is changed:

# UI Label:

   @text.setter
    def text(self, value):
        """
        Update text of the label.

        This triggers a full render to ensure that previous text is cleared out.
        """

        if self.label.text != value:
            self.label.text = value
            self._update_layout()
            self._update_size_hint_min()
            self.trigger_full_render()

#--------------------------------------------------------------------------

# UI Text Widget

    @text.setter
    def text(self, value):
        self.ui_label.text = value
        self.ui_label.fit_content()  # <-- CALL TO FIT CONTENT
        self.trigger_render()

Is there a reason for UILabel to not call fit_content when text is changed? Is it possible to add this call internally so user don't have to do manually?

alejcas avatar May 23 '24 09:05 alejcas

Also, if this is not added it will recreate Rects because users should remember to manually check if the content changed:

if self.ui_label.text != new_value:
    self.ui_label.fit_content()

alejcas avatar May 23 '24 09:05 alejcas

fit_content does overwrite width and height. So if you give it a explicit size, that would just be overwritten.

Feedback demanded: Would this fit more your expectation?

eruvanos avatar May 24 '24 20:05 eruvanos

I create the labels without size:

self.label = UILabel(text=“hello”)

if later on I change the text of the label and that text is bigger than the former one, the text is cut out:

self.label.text = “a longer text than before” Here label will display something like “a lon” (maybe half “n”).

This does not happen with UITextWidget because it calls fit_content when the text changes. But UILabel does not call fit_content.

I don’t understand the reason behind not having a call to fit_content when text changes on UILabel.

And this means the user needs to call manually fit_content and also check if the text has change to avoid label rect recreation when it’s unnecessary.

alejcas avatar May 24 '24 20:05 alejcas

Hi, I looked further into it. I changed the default of UILabel.size_hint to (0,0). The size_hint_min is already updated after a text is set.

The behaviour allows for all use cases:

Fix size: UILabel(text="Hello", size_hint=None, width=100, height=20)

Fit content UILabel(text="Hello")

Now it is more consistent, when no width/height is given the default behaviour fits the content size.

eruvanos avatar Jul 03 '24 13:07 eruvanos

Great news! I’ll try this

alejcas avatar Jul 03 '24 13:07 alejcas