[2.0.55] LuaGuiElement natural_width/natural_height don't work as described

Bugs that are actually features.
Osmo
Long Handed Inserter
Long Handed Inserter
Posts: 70
Joined: Wed Oct 23, 2024 12:08 pm
Contact:

[2.0.55] LuaGuiElement natural_width/natural_height don't work as described

Post by Osmo »

The documentation of natural_width/natural_height says the following:
Natural width specifies the width of the element tries to have, but it can still be squashed/stretched to have a smaller or bigger size.
However i found that most of the time, and in situations where that would be the most useful, it, in fact, does not try to squash/stretch.


Example 1:

Code: Select all

    local frame = game.players[1].gui.screen.add{
        type = "frame",
        name = "test"
    }
    frame.auto_center = true
    frame.style.horizontally_stretchable = true
    local label = frame.add{
        type = "label",
        caption = "this is a long label dsbajdbkasbdkjabsjkdbakjdbksj"
    }
    label.style.horizontally_stretchable = true
    label.style.natural_width = 300
Frame is stretchable. Label is stretchable. Label has natural width.
Expected result: label stretches to its natural width since the parent element allows it.
Actual result:
изображение.png
изображение.png (4.67 KiB) Viewed 412 times

Example 2:

Code: Select all

    local frame = game.players[1].gui.screen.add{
        type = "frame",
        name = "test"
    }
    frame.auto_center = true
    frame.style.natural_width = 150
    frame.style.horizontally_stretchable = true
    local label = frame.add{
        type = "label",
        caption = "this is a long label dsbajdbkasbdkjabsjkdbakjdbksj"
    }
    label.style.horizontally_stretchable = true
    label.style.natural_width = 300
Frame is stretchable. Label is stretchable. Frame has lower natural_width. Label has higher natural width.

Actual result:
Frame stretches up to its natural_width. This makes sense, it shows that natural_width sometimes works. Whether it should stretch up to label natural_width could be ambiguous, so i assume this works as intended.
изображение.png
изображение.png (6.1 KiB) Viewed 412 times

Example 3:

Code: Select all

   local frame = game.players[1].gui.screen.add{
        type = "frame",
        name = "test"
    }
    frame.auto_center = true
    frame.style.natural_width = 150
    frame.style.horizontally_stretchable = true
    local label = frame.add{
        type = "label",
        caption = "this is a long label"
    }
    label.style.horizontally_stretchable = true
    label.style.horizontally_squashable = true
    label.style.natural_width = 100
    local empty = frame.add{
        type = "empty-widget",
    }
    empty.style.horizontally_stretchable = true
    empty.style.horizontally_squashable = true
    empty.style.natural_width = 20
Frame has natural_width of 150. Label has natural_width of 100. Empty-widget has natural_width of 20
Expected result: Either label or empty-widget stretches to its natural_width, with the other stretching to fill the remaining space.
So the width should either be 100 and 50 or 130 and 20, or at a 100:20 ratio (ignoring padding/margins).
Actual result: even sizes
изображение.png
изображение.png (48.48 KiB) Viewed 412 times
The last example is of a real problem i encountered trying to have a stretchable label, while having an empty widget as padding so that the label is as wide as it can fit in a flow without hardcoding the widths. While this can probably be solved with some hacks, it is extremely unintuitive and the natural size feature turns out to be mostly useless; i rarely see it used even in gui heavy projects, despite its description making it seem like it allows more versatility in layouts.
Rseding91
Factorio Staff
Factorio Staff
Posts: 15724
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: [2.0.55] LuaGuiElement natural_width/natural_height don't work as described

Post by Rseding91 »

Thanks for the report however it is working as described. It will take the size if nothing else requests it stretch or shrink. By having it in a frame that's stretchable, that also means it's shrinkable and so it shrinks. You have to have *some other* elements in the frame that force the size to be larger, the label will then take its natural size if it can.

You can observe how it's used in the load-map GUI. The load button has a natural size of 300.
If you want to get ahold of me I'm almost always on Discord.
Osmo
Long Handed Inserter
Long Handed Inserter
Posts: 70
Joined: Wed Oct 23, 2024 12:08 pm
Contact:

Re: [2.0.55] LuaGuiElement natural_width/natural_height don't work as described

Post by Osmo »

Thanks for the response, it makes sense, however its the last example that was the main concern. Even if the frame is set to not stretchable, and instead of natural_width, just width is set to 150, the elements still don't try to stretch/squash to their natural sizes, despite being able to.
Post Reply

Return to “Not a bug”