Don't resize a window below the size of its children

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By darkpenguin

I have a WindowDialog with a few buttons in a VBoxContainer. There are expandable separators between buttons, so the container is resized along with the window.

the problem is, I can resize the window below the minimal size of the container, and the buttons are “hovering in the air”.

I know there is “Min Size” for the window, but that’s a poor solution - what is I want to add more buttons and forget to change it? Or add them in runtime.

Is it possible to set “do not resize the window below the size of its children”? Or at least get the container’s current minimal size - not the specified “Min Size”, which is (0,0) , but the actual current (runtime) minimal size, below which it can not resize because of the buttons on it.

There’s probably a way to do it automatically that I don’t know about, but what you could do is have a script that runs after the game has launched and all buttons have been instanced, that measures the min size the window needs to be by looking at the dimensions of all its children, and sets Min Size based on that

denxi | 2020-02-08 19:58

I have exactly the same issue, have you found a solution? Looking for a clean answer

henkdewit | 2021-08-30 07:26

There is Window.get_contents_minimum_size, although that didn’t work for me.

I’m able to set the correct window size with the following:

get_window().size = size

I run this code in the a toplevel PanelContainer under the root (called “UIMain”) which already “knows” the right size for the UI. Any resizing calculations I need are done here in the scene tree, rather than this weird, cranky “window” object that I don’t understand.

Here’s my scene tree:
sceneTreeForQuestion

I’m resizing this UIMain panel container like this:

# UIMain.gd
func _ready():
    %MainContainer.connect("sort_children", onSortMainContainerChildren)

func onSortMainContainerChildren():
    %MainContainer.reset_size()
    reset_size()
    get_window().size = size

However, that line of code also causes everything in scene to disappear, and I’m not sure if it’ll work better for you. I’m working on Linux with Godot v4.2.1. Likely all the window-handling business is more stable on Windows or even Mac, so there’s a fair chance this will work for others.